blob: b4534d918e4234f3a89f958159dd69649a236a34 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Jeff Mahoney143bede2012-03-01 14:56:26 +010040static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +020041 struct btrfs_path *path, int level, int slot,
42 int tree_mod_log);
Jan Schmidtf2304752012-05-26 11:43:17 +020043static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
44 struct extent_buffer *eb);
45struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
46 u32 blocksize, u64 parent_transid,
47 u64 time_seq);
48struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
49 u64 bytenr, u32 blocksize,
50 u64 time_seq);
Chris Masond97e63b2007-02-20 16:40:44 -050051
Chris Mason2c90e5d2007-04-02 10:50:19 -040052struct btrfs_path *btrfs_alloc_path(void)
53{
Chris Masondf24a2b2007-04-04 09:36:31 -040054 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050055 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040056 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040057}
58
Chris Masonb4ce94d2009-02-04 09:25:08 -050059/*
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
62 */
63noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64{
65 int i;
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040067 if (!p->nodes[i] || !p->locks[i])
68 continue;
69 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
70 if (p->locks[i] == BTRFS_READ_LOCK)
71 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
72 else if (p->locks[i] == BTRFS_WRITE_LOCK)
73 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 }
75}
76
77/*
78 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050079 *
80 * held is used to keep lockdep happy, when lockdep is enabled
81 * we set held to a blocking lock before we go around and
82 * retake all the spinlocks in the path. You can safely use NULL
83 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050084 */
Chris Mason4008c042009-02-12 14:09:45 -050085noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040086 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050087{
88 int i;
Chris Mason4008c042009-02-12 14:09:45 -050089
90#ifdef CONFIG_DEBUG_LOCK_ALLOC
91 /* lockdep really cares that we take all of these spinlocks
92 * in the right order. If any of the locks in the path are not
93 * currently blocking, it is going to complain. So, make really
94 * really sure by forcing the path to blocking before we clear
95 * the path blocking.
96 */
Chris Masonbd681512011-07-16 15:23:14 -040097 if (held) {
98 btrfs_set_lock_blocking_rw(held, held_rw);
99 if (held_rw == BTRFS_WRITE_LOCK)
100 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
101 else if (held_rw == BTRFS_READ_LOCK)
102 held_rw = BTRFS_READ_LOCK_BLOCKING;
103 }
Chris Mason4008c042009-02-12 14:09:45 -0500104 btrfs_set_path_blocking(p);
105#endif
106
107 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400108 if (p->nodes[i] && p->locks[i]) {
109 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
110 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
111 p->locks[i] = BTRFS_WRITE_LOCK;
112 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
113 p->locks[i] = BTRFS_READ_LOCK;
114 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115 }
Chris Mason4008c042009-02-12 14:09:45 -0500116
117#ifdef CONFIG_DEBUG_LOCK_ALLOC
118 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400119 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500120#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400124void btrfs_free_path(struct btrfs_path *p)
125{
Jesper Juhlff175d52010-12-25 21:22:30 +0000126 if (!p)
127 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200128 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400129 kmem_cache_free(btrfs_path_cachep, p);
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
133 * path release drops references on the extent buffers in the path
134 * and it drops any locks held by this path
135 *
136 * It is safe to call this on paths that no locks or extent buffers held.
137 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200138noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500139{
140 int i;
Chris Masona2135012008-06-25 16:01:30 -0400141
Chris Mason234b63a2007-03-13 10:46:10 -0400142 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400143 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400145 continue;
146 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400147 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400148 p->locks[i] = 0;
149 }
Chris Mason5f39d392007-10-15 16:14:19 -0400150 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400151 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500152 }
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/*
156 * safely gets a reference on the root node of a tree. A lock
157 * is not taken, so a concurrent writer may put a different node
158 * at the root of the tree. See btrfs_lock_root_node for the
159 * looping required.
160 *
161 * The extent buffer returned by this has a reference taken, so
162 * it won't disappear. It may stop being the root of the tree
163 * at any time because there are no locks held.
164 */
Chris Mason925baed2008-06-25 16:01:30 -0400165struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
166{
167 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400168
Josef Bacik3083ee22012-03-09 16:01:49 -0500169 while (1) {
170 rcu_read_lock();
171 eb = rcu_dereference(root->node);
172
173 /*
174 * RCU really hurts here, we could free up the root node because
175 * it was cow'ed but we may not get the new root node yet so do
176 * the inc_not_zero dance and if it doesn't work then
177 * synchronize_rcu and try again.
178 */
179 if (atomic_inc_not_zero(&eb->refs)) {
180 rcu_read_unlock();
181 break;
182 }
183 rcu_read_unlock();
184 synchronize_rcu();
185 }
Chris Mason925baed2008-06-25 16:01:30 -0400186 return eb;
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/* loop around taking references on and locking the root node of the
190 * tree until you end up with a lock on the root. A locked buffer
191 * is returned, with a reference held.
192 */
Chris Mason925baed2008-06-25 16:01:30 -0400193struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
194{
195 struct extent_buffer *eb;
196
Chris Masond3977122009-01-05 21:25:51 -0500197 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400198 eb = btrfs_root_node(root);
199 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400200 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400201 break;
Chris Mason925baed2008-06-25 16:01:30 -0400202 btrfs_tree_unlock(eb);
203 free_extent_buffer(eb);
204 }
205 return eb;
206}
207
Chris Masonbd681512011-07-16 15:23:14 -0400208/* loop around taking references on and locking the root node of the
209 * tree until you end up with a lock on the root. A locked buffer
210 * is returned, with a reference held.
211 */
212struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
213{
214 struct extent_buffer *eb;
215
216 while (1) {
217 eb = btrfs_root_node(root);
218 btrfs_tree_read_lock(eb);
219 if (eb == root->node)
220 break;
221 btrfs_tree_read_unlock(eb);
222 free_extent_buffer(eb);
223 }
224 return eb;
225}
226
Chris Masond352ac62008-09-29 15:18:18 -0400227/* cowonly root (everything not a reference counted cow subvolume), just get
228 * put onto a simple dirty list. transaction.c walks this to make sure they
229 * get properly updated on disk.
230 */
Chris Mason0b86a832008-03-24 15:01:56 -0400231static void add_root_to_dirty_list(struct btrfs_root *root)
232{
Chris Masone5846fc2012-05-03 12:08:48 -0400233 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400234 if (root->track_dirty && list_empty(&root->dirty_list)) {
235 list_add(&root->dirty_list,
236 &root->fs_info->dirty_cowonly_roots);
237 }
Chris Masone5846fc2012-05-03 12:08:48 -0400238 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400239}
240
Chris Masond352ac62008-09-29 15:18:18 -0400241/*
242 * used by snapshot creation to make a copy of a root for a tree with
243 * a given objectid. The buffer with the new root node is returned in
244 * cow_ret, and this func returns zero on success or a negative error code.
245 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500246int btrfs_copy_root(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct extent_buffer *buf,
249 struct extent_buffer **cow_ret, u64 new_root_objectid)
250{
251 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 int ret = 0;
253 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255
256 WARN_ON(root->ref_cows && trans->transid !=
257 root->fs_info->running_transaction->transid);
258 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
259
260 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 if (level == 0)
262 btrfs_item_key(buf, &disk_key, 0);
263 else
264 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400265
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
267 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200268 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500270 return PTR_ERR(cow);
271
272 copy_extent_buffer(cow, buf, 0, 0, cow->len);
273 btrfs_set_header_bytenr(cow, cow->start);
274 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
276 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
277 BTRFS_HEADER_FLAG_RELOC);
278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
279 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
280 else
281 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500282
Yan Zheng2b820322008-11-17 21:11:30 -0500283 write_extent_buffer(cow, root->fs_info->fsid,
284 (unsigned long)btrfs_header_fsid(cow),
285 BTRFS_FSID_SIZE);
286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400288 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200289 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400290 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200291 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500292
Chris Masonbe20aa92007-12-17 20:14:01 -0500293 if (ret)
294 return ret;
295
296 btrfs_mark_buffer_dirty(cow);
297 *cow_ret = cow;
298 return 0;
299}
300
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200301enum mod_log_op {
302 MOD_LOG_KEY_REPLACE,
303 MOD_LOG_KEY_ADD,
304 MOD_LOG_KEY_REMOVE,
305 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
306 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
307 MOD_LOG_MOVE_KEYS,
308 MOD_LOG_ROOT_REPLACE,
309};
310
311struct tree_mod_move {
312 int dst_slot;
313 int nr_items;
314};
315
316struct tree_mod_root {
317 u64 logical;
318 u8 level;
319};
320
321struct tree_mod_elem {
322 struct rb_node node;
323 u64 index; /* shifted logical */
324 struct seq_list elem;
325 enum mod_log_op op;
326
327 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
328 int slot;
329
330 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
331 u64 generation;
332
333 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
334 struct btrfs_disk_key key;
335 u64 blockptr;
336
337 /* this is used for op == MOD_LOG_MOVE_KEYS */
338 struct tree_mod_move move;
339
340 /* this is used for op == MOD_LOG_ROOT_REPLACE */
341 struct tree_mod_root old_root;
342};
343
344static inline void
345__get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
346{
347 elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
348 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
349}
350
351void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
352 struct seq_list *elem)
353{
354 elem->flags = 1;
355 spin_lock(&fs_info->tree_mod_seq_lock);
356 __get_tree_mod_seq(fs_info, elem);
357 spin_unlock(&fs_info->tree_mod_seq_lock);
358}
359
360void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
361 struct seq_list *elem)
362{
363 struct rb_root *tm_root;
364 struct rb_node *node;
365 struct rb_node *next;
366 struct seq_list *cur_elem;
367 struct tree_mod_elem *tm;
368 u64 min_seq = (u64)-1;
369 u64 seq_putting = elem->seq;
370
371 if (!seq_putting)
372 return;
373
374 BUG_ON(!(elem->flags & 1));
375 spin_lock(&fs_info->tree_mod_seq_lock);
376 list_del(&elem->list);
377
378 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
379 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
380 if (seq_putting > cur_elem->seq) {
381 /*
382 * blocker with lower sequence number exists, we
383 * cannot remove anything from the log
384 */
385 goto out;
386 }
387 min_seq = cur_elem->seq;
388 }
389 }
390
391 /*
392 * anything that's lower than the lowest existing (read: blocked)
393 * sequence number can be removed from the tree.
394 */
395 write_lock(&fs_info->tree_mod_log_lock);
396 tm_root = &fs_info->tree_mod_log;
397 for (node = rb_first(tm_root); node; node = next) {
398 next = rb_next(node);
399 tm = container_of(node, struct tree_mod_elem, node);
400 if (tm->elem.seq > min_seq)
401 continue;
402 rb_erase(node, tm_root);
403 list_del(&tm->elem.list);
404 kfree(tm);
405 }
406 write_unlock(&fs_info->tree_mod_log_lock);
407out:
408 spin_unlock(&fs_info->tree_mod_seq_lock);
409}
410
411/*
412 * key order of the log:
413 * index -> sequence
414 *
415 * the index is the shifted logical of the *new* root node for root replace
416 * operations, or the shifted logical of the affected block for all other
417 * operations.
418 */
419static noinline int
420__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
421{
422 struct rb_root *tm_root;
423 struct rb_node **new;
424 struct rb_node *parent = NULL;
425 struct tree_mod_elem *cur;
426 int ret = 0;
427
428 BUG_ON(!tm || !tm->elem.seq);
429
430 write_lock(&fs_info->tree_mod_log_lock);
431 tm_root = &fs_info->tree_mod_log;
432 new = &tm_root->rb_node;
433 while (*new) {
434 cur = container_of(*new, struct tree_mod_elem, node);
435 parent = *new;
436 if (cur->index < tm->index)
437 new = &((*new)->rb_left);
438 else if (cur->index > tm->index)
439 new = &((*new)->rb_right);
440 else if (cur->elem.seq < tm->elem.seq)
441 new = &((*new)->rb_left);
442 else if (cur->elem.seq > tm->elem.seq)
443 new = &((*new)->rb_right);
444 else {
445 kfree(tm);
446 ret = -EEXIST;
447 goto unlock;
448 }
449 }
450
451 rb_link_node(&tm->node, parent, new);
452 rb_insert_color(&tm->node, tm_root);
453unlock:
454 write_unlock(&fs_info->tree_mod_log_lock);
455 return ret;
456}
457
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200458static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
459 struct extent_buffer *eb) {
460 smp_mb();
461 if (list_empty(&(fs_info)->tree_mod_seq_list))
462 return 1;
463 if (!eb)
464 return 0;
465 if (btrfs_header_level(eb) == 0)
466 return 1;
467 return 0;
468}
469
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200470static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
471 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200472{
473 struct tree_mod_elem *tm;
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200474 int seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200475
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200476 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200477 return 0;
478
479 tm = *tm_ret = kzalloc(sizeof(*tm), flags);
480 if (!tm)
481 return -ENOMEM;
482
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200483 tm->elem.flags = 0;
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200484 spin_lock(&fs_info->tree_mod_seq_lock);
485 if (list_empty(&fs_info->tree_mod_seq_list)) {
486 /*
487 * someone emptied the list while we were waiting for the lock.
488 * we must not add to the list, because no blocker exists. items
489 * are removed from the list only when the existing blocker is
490 * removed from the list.
491 */
492 kfree(tm);
493 seq = 0;
494 } else {
495 __get_tree_mod_seq(fs_info, &tm->elem);
496 seq = tm->elem.seq;
497 }
498 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200499
500 return seq;
501}
502
503static noinline int
504tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
505 struct extent_buffer *eb, int slot,
506 enum mod_log_op op, gfp_t flags)
507{
508 struct tree_mod_elem *tm;
509 int ret;
510
511 ret = tree_mod_alloc(fs_info, flags, &tm);
512 if (ret <= 0)
513 return ret;
514
515 tm->index = eb->start >> PAGE_CACHE_SHIFT;
516 if (op != MOD_LOG_KEY_ADD) {
517 btrfs_node_key(eb, &tm->key, slot);
518 tm->blockptr = btrfs_node_blockptr(eb, slot);
519 }
520 tm->op = op;
521 tm->slot = slot;
522 tm->generation = btrfs_node_ptr_generation(eb, slot);
523
524 return __tree_mod_log_insert(fs_info, tm);
525}
526
527static noinline int
528tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
529 int slot, enum mod_log_op op)
530{
531 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
532}
533
534static noinline int
535tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
536 struct extent_buffer *eb, int dst_slot, int src_slot,
537 int nr_items, gfp_t flags)
538{
539 struct tree_mod_elem *tm;
540 int ret;
541 int i;
542
Jan Schmidtf3956942012-05-31 15:02:32 +0200543 if (tree_mod_dont_log(fs_info, eb))
544 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200545
546 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
547 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
548 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
549 BUG_ON(ret < 0);
550 }
551
Jan Schmidtf3956942012-05-31 15:02:32 +0200552 ret = tree_mod_alloc(fs_info, flags, &tm);
553 if (ret <= 0)
554 return ret;
555
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200556 tm->index = eb->start >> PAGE_CACHE_SHIFT;
557 tm->slot = src_slot;
558 tm->move.dst_slot = dst_slot;
559 tm->move.nr_items = nr_items;
560 tm->op = MOD_LOG_MOVE_KEYS;
561
562 return __tree_mod_log_insert(fs_info, tm);
563}
564
565static noinline int
566tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
567 struct extent_buffer *old_root,
568 struct extent_buffer *new_root, gfp_t flags)
569{
570 struct tree_mod_elem *tm;
571 int ret;
572
573 ret = tree_mod_alloc(fs_info, flags, &tm);
574 if (ret <= 0)
575 return ret;
576
577 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
578 tm->old_root.logical = old_root->start;
579 tm->old_root.level = btrfs_header_level(old_root);
580 tm->generation = btrfs_header_generation(old_root);
581 tm->op = MOD_LOG_ROOT_REPLACE;
582
583 return __tree_mod_log_insert(fs_info, tm);
584}
585
586static struct tree_mod_elem *
587__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
588 int smallest)
589{
590 struct rb_root *tm_root;
591 struct rb_node *node;
592 struct tree_mod_elem *cur = NULL;
593 struct tree_mod_elem *found = NULL;
594 u64 index = start >> PAGE_CACHE_SHIFT;
595
596 read_lock(&fs_info->tree_mod_log_lock);
597 tm_root = &fs_info->tree_mod_log;
598 node = tm_root->rb_node;
599 while (node) {
600 cur = container_of(node, struct tree_mod_elem, node);
601 if (cur->index < index) {
602 node = node->rb_left;
603 } else if (cur->index > index) {
604 node = node->rb_right;
605 } else if (cur->elem.seq < min_seq) {
606 node = node->rb_left;
607 } else if (!smallest) {
608 /* we want the node with the highest seq */
609 if (found)
610 BUG_ON(found->elem.seq > cur->elem.seq);
611 found = cur;
612 node = node->rb_left;
613 } else if (cur->elem.seq > min_seq) {
614 /* we want the node with the smallest seq */
615 if (found)
616 BUG_ON(found->elem.seq < cur->elem.seq);
617 found = cur;
618 node = node->rb_right;
619 } else {
620 found = cur;
621 break;
622 }
623 }
624 read_unlock(&fs_info->tree_mod_log_lock);
625
626 return found;
627}
628
629/*
630 * this returns the element from the log with the smallest time sequence
631 * value that's in the log (the oldest log item). any element with a time
632 * sequence lower than min_seq will be ignored.
633 */
634static struct tree_mod_elem *
635tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
636 u64 min_seq)
637{
638 return __tree_mod_log_search(fs_info, start, min_seq, 1);
639}
640
641/*
642 * this returns the element from the log with the largest time sequence
643 * value that's in the log (the most recent log item). any element with
644 * a time sequence lower than min_seq will be ignored.
645 */
646static struct tree_mod_elem *
647tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
648{
649 return __tree_mod_log_search(fs_info, start, min_seq, 0);
650}
651
652static inline void
653tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
654 struct extent_buffer *src, unsigned long dst_offset,
655 unsigned long src_offset, int nr_items)
656{
657 int ret;
658 int i;
659
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200660 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200661 return;
662
663 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
664 return;
665
666 /* speed this up by single seq for all operations? */
667 for (i = 0; i < nr_items; i++) {
668 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
669 MOD_LOG_KEY_REMOVE);
670 BUG_ON(ret < 0);
671 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
672 MOD_LOG_KEY_ADD);
673 BUG_ON(ret < 0);
674 }
675}
676
677static inline void
678tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
679 int dst_offset, int src_offset, int nr_items)
680{
681 int ret;
682 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
683 nr_items, GFP_NOFS);
684 BUG_ON(ret < 0);
685}
686
687static inline void
688tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
689 struct extent_buffer *eb,
690 struct btrfs_disk_key *disk_key, int slot, int atomic)
691{
692 int ret;
693
694 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
695 MOD_LOG_KEY_REPLACE,
696 atomic ? GFP_ATOMIC : GFP_NOFS);
697 BUG_ON(ret < 0);
698}
699
700static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
701 struct extent_buffer *eb)
702{
703 int i;
704 int ret;
705 u32 nritems;
706
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200707 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200708 return;
709
710 nritems = btrfs_header_nritems(eb);
711 for (i = nritems - 1; i >= 0; i--) {
712 ret = tree_mod_log_insert_key(fs_info, eb, i,
713 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
714 BUG_ON(ret < 0);
715 }
716}
717
718static inline void
719tree_mod_log_set_root_pointer(struct btrfs_root *root,
720 struct extent_buffer *new_root_node)
721{
722 int ret;
723 tree_mod_log_free_eb(root->fs_info, root->node);
724 ret = tree_mod_log_insert_root(root->fs_info, root->node,
725 new_root_node, GFP_NOFS);
726 BUG_ON(ret < 0);
727}
728
Chris Masond352ac62008-09-29 15:18:18 -0400729/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400730 * check if the tree block can be shared by multiple trees
731 */
732int btrfs_block_can_be_shared(struct btrfs_root *root,
733 struct extent_buffer *buf)
734{
735 /*
736 * Tree blocks not in refernece counted trees and tree roots
737 * are never shared. If a block was allocated after the last
738 * snapshot and the block was not allocated by tree relocation,
739 * we know the block is not shared.
740 */
741 if (root->ref_cows &&
742 buf != root->node && buf != root->commit_root &&
743 (btrfs_header_generation(buf) <=
744 btrfs_root_last_snapshot(&root->root_item) ||
745 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
746 return 1;
747#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
748 if (root->ref_cows &&
749 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
750 return 1;
751#endif
752 return 0;
753}
754
755static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
756 struct btrfs_root *root,
757 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400758 struct extent_buffer *cow,
759 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400760{
761 u64 refs;
762 u64 owner;
763 u64 flags;
764 u64 new_flags = 0;
765 int ret;
766
767 /*
768 * Backrefs update rules:
769 *
770 * Always use full backrefs for extent pointers in tree block
771 * allocated by tree relocation.
772 *
773 * If a shared tree block is no longer referenced by its owner
774 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
775 * use full backrefs for extent pointers in tree block.
776 *
777 * If a tree block is been relocating
778 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
779 * use full backrefs for extent pointers in tree block.
780 * The reason for this is some operations (such as drop tree)
781 * are only allowed for blocks use full backrefs.
782 */
783
784 if (btrfs_block_can_be_shared(root, buf)) {
785 ret = btrfs_lookup_extent_info(trans, root, buf->start,
786 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700787 if (ret)
788 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700789 if (refs == 0) {
790 ret = -EROFS;
791 btrfs_std_error(root->fs_info, ret);
792 return ret;
793 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400794 } else {
795 refs = 1;
796 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
797 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
798 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
799 else
800 flags = 0;
801 }
802
803 owner = btrfs_header_owner(buf);
804 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
805 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
806
807 if (refs > 1) {
808 if ((owner == root->root_key.objectid ||
809 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
810 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200811 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100812 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400813
814 if (root->root_key.objectid ==
815 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200816 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100817 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200818 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100819 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 }
821 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
822 } else {
823
824 if (root->root_key.objectid ==
825 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200826 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400827 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200828 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100829 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830 }
831 if (new_flags != 0) {
832 ret = btrfs_set_disk_extent_flags(trans, root,
833 buf->start,
834 buf->len,
835 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700836 if (ret)
837 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838 }
839 } else {
840 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
841 if (root->root_key.objectid ==
842 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200843 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400844 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200845 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100846 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200847 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100848 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400849 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200850 /*
851 * don't log freeing in case we're freeing the root node, this
852 * is done by tree_mod_log_set_root_pointer later
853 */
854 if (buf != root->node && btrfs_header_level(buf) != 0)
855 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400857 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858 }
859 return 0;
860}
861
862/*
Chris Masond3977122009-01-05 21:25:51 -0500863 * does the dirty work in cow of a single block. The parent block (if
864 * supplied) is updated to point to the new cow copy. The new buffer is marked
865 * dirty and returned locked. If you modify the block it needs to be marked
866 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400867 *
868 * search_start -- an allocation hint for the new block
869 *
Chris Masond3977122009-01-05 21:25:51 -0500870 * empty_size -- a hint that you plan on doing more cow. This is the size in
871 * bytes the allocator should try to find free next to the block it returns.
872 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400873 */
Chris Masond3977122009-01-05 21:25:51 -0500874static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400875 struct btrfs_root *root,
876 struct extent_buffer *buf,
877 struct extent_buffer *parent, int parent_slot,
878 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400879 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400880{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700883 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400884 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400885 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400887
Chris Mason925baed2008-06-25 16:01:30 -0400888 if (*cow_ret == buf)
889 unlock_orig = 1;
890
Chris Masonb9447ef2009-03-09 11:45:38 -0400891 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400892
Chris Mason7bb86312007-12-11 09:25:06 -0500893 WARN_ON(root->ref_cows && trans->transid !=
894 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400895 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400896
Chris Mason7bb86312007-12-11 09:25:06 -0500897 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400898
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 if (level == 0)
900 btrfs_item_key(buf, &disk_key, 0);
901 else
902 btrfs_node_key(buf, &disk_key, 0);
903
904 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
905 if (parent)
906 parent_start = parent->start;
907 else
908 parent_start = 0;
909 } else
910 parent_start = 0;
911
912 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
913 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200914 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400915 if (IS_ERR(cow))
916 return PTR_ERR(cow);
917
Chris Masonb4ce94d2009-02-04 09:25:08 -0500918 /* cow is set to blocking by btrfs_init_new_buffer */
919
Chris Mason5f39d392007-10-15 16:14:19 -0400920 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400921 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400922 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
924 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
925 BTRFS_HEADER_FLAG_RELOC);
926 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
927 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
928 else
929 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400930
Yan Zheng2b820322008-11-17 21:11:30 -0500931 write_extent_buffer(cow, root->fs_info->fsid,
932 (unsigned long)btrfs_header_fsid(cow),
933 BTRFS_FSID_SIZE);
934
Mark Fashehbe1a5562011-08-08 13:20:18 -0700935 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700936 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100937 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700938 return ret;
939 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400940
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400941 if (root->ref_cows)
942 btrfs_reloc_cow_block(trans, root, buf, cow);
943
Chris Mason6702ed42007-08-07 16:15:09 -0400944 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400945 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
947 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
948 parent_start = buf->start;
949 else
950 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400951
Chris Mason5f39d392007-10-15 16:14:19 -0400952 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +0200953 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400954 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400955
Yan, Zhengf0486c62010-05-16 10:46:25 -0400956 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200957 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400958 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400959 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400960 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
962 parent_start = parent->start;
963 else
964 parent_start = 0;
965
966 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +0200967 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
968 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -0400969 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400970 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500971 btrfs_set_node_ptr_generation(parent, parent_slot,
972 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400973 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400974 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200975 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400976 }
Chris Mason925baed2008-06-25 16:01:30 -0400977 if (unlock_orig)
978 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500979 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400980 btrfs_mark_buffer_dirty(cow);
981 *cow_ret = cow;
982 return 0;
983}
984
Jan Schmidt5d9e75c42012-05-16 18:25:47 +0200985/*
986 * returns the logical address of the oldest predecessor of the given root.
987 * entries older than time_seq are ignored.
988 */
989static struct tree_mod_elem *
990__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
991 struct btrfs_root *root, u64 time_seq)
992{
993 struct tree_mod_elem *tm;
994 struct tree_mod_elem *found = NULL;
995 u64 root_logical = root->node->start;
996 int looped = 0;
997
998 if (!time_seq)
999 return 0;
1000
1001 /*
1002 * the very last operation that's logged for a root is the replacement
1003 * operation (if it is replaced at all). this has the index of the *new*
1004 * root, making it the very first operation that's logged for this root.
1005 */
1006 while (1) {
1007 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1008 time_seq);
1009 if (!looped && !tm)
1010 return 0;
1011 /*
1012 * we must have key remove operations in the log before the
1013 * replace operation.
1014 */
1015 BUG_ON(!tm);
1016
1017 if (tm->op != MOD_LOG_ROOT_REPLACE)
1018 break;
1019
1020 found = tm;
1021 root_logical = tm->old_root.logical;
1022 BUG_ON(root_logical == root->node->start);
1023 looped = 1;
1024 }
1025
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:
Jan Schmidtc3193102012-05-31 19:24:36 +02001079 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1080 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1081 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001082 tm->move.nr_items * p_size);
1083 break;
1084 case MOD_LOG_ROOT_REPLACE:
1085 /*
1086 * this operation is special. for roots, this must be
1087 * handled explicitly before rewinding.
1088 * for non-roots, this operation may exist if the node
1089 * was a root: root A -> child B; then A gets empty and
1090 * B is promoted to the new root. in the mod log, we'll
1091 * have a root-replace operation for B, a tree block
1092 * that is no root. we simply ignore that operation.
1093 */
1094 break;
1095 }
1096 next = rb_next(&tm->node);
1097 if (!next)
1098 break;
1099 tm = container_of(next, struct tree_mod_elem, node);
1100 if (tm->index != first_tm->index)
1101 break;
1102 }
1103 btrfs_set_header_nritems(eb, n);
1104}
1105
1106static struct extent_buffer *
1107tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1108 u64 time_seq)
1109{
1110 struct extent_buffer *eb_rewin;
1111 struct tree_mod_elem *tm;
1112
1113 if (!time_seq)
1114 return eb;
1115
1116 if (btrfs_header_level(eb) == 0)
1117 return eb;
1118
1119 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1120 if (!tm)
1121 return eb;
1122
1123 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1124 BUG_ON(tm->slot != 0);
1125 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1126 fs_info->tree_root->nodesize);
1127 BUG_ON(!eb_rewin);
1128 btrfs_set_header_bytenr(eb_rewin, eb->start);
1129 btrfs_set_header_backref_rev(eb_rewin,
1130 btrfs_header_backref_rev(eb));
1131 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001132 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001133 } else {
1134 eb_rewin = btrfs_clone_extent_buffer(eb);
1135 BUG_ON(!eb_rewin);
1136 }
1137
1138 extent_buffer_get(eb_rewin);
1139 free_extent_buffer(eb);
1140
1141 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1142
1143 return eb_rewin;
1144}
1145
1146static inline struct extent_buffer *
1147get_old_root(struct btrfs_root *root, u64 time_seq)
1148{
1149 struct tree_mod_elem *tm;
1150 struct extent_buffer *eb;
1151 struct tree_mod_root *old_root;
1152 u64 old_generation;
1153
1154 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1155 if (!tm)
1156 return root->node;
1157
1158 old_root = &tm->old_root;
1159 old_generation = tm->generation;
1160
1161 tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq);
1162 /*
1163 * there was an item in the log when __tree_mod_log_oldest_root
1164 * returned. this one must not go away, because the time_seq passed to
1165 * us must be blocking its removal.
1166 */
1167 BUG_ON(!tm);
1168
1169 if (old_root->logical == root->node->start) {
1170 /* there are logged operations for the current root */
1171 eb = btrfs_clone_extent_buffer(root->node);
1172 } else {
1173 /* there's a root replace operation for the current root */
1174 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1175 root->nodesize);
1176 btrfs_set_header_bytenr(eb, eb->start);
1177 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1178 btrfs_set_header_owner(eb, root->root_key.objectid);
1179 }
1180 if (!eb)
1181 return NULL;
1182 btrfs_set_header_level(eb, old_root->level);
1183 btrfs_set_header_generation(eb, old_generation);
1184 __tree_mod_log_rewind(eb, time_seq, tm);
1185
1186 return eb;
1187}
1188
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001189static inline int should_cow_block(struct btrfs_trans_handle *trans,
1190 struct btrfs_root *root,
1191 struct extent_buffer *buf)
1192{
Liu Bof1ebcc72011-11-14 20:48:06 -05001193 /* ensure we can see the force_cow */
1194 smp_rmb();
1195
1196 /*
1197 * We do not need to cow a block if
1198 * 1) this block is not created or changed in this transaction;
1199 * 2) this block does not belong to TREE_RELOC tree;
1200 * 3) the root is not forced COW.
1201 *
1202 * What is forced COW:
1203 * when we create snapshot during commiting the transaction,
1204 * after we've finished coping src root, we must COW the shared
1205 * block to ensure the metadata consistency.
1206 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001207 if (btrfs_header_generation(buf) == trans->transid &&
1208 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1209 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001210 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1211 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001212 return 0;
1213 return 1;
1214}
1215
Chris Masond352ac62008-09-29 15:18:18 -04001216/*
1217 * cows a single block, see __btrfs_cow_block for the real work.
1218 * This version of it has extra checks so that a block isn't cow'd more than
1219 * once per transaction, as long as it hasn't been written yet
1220 */
Chris Masond3977122009-01-05 21:25:51 -05001221noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001222 struct btrfs_root *root, struct extent_buffer *buf,
1223 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001224 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001225{
Chris Mason6702ed42007-08-07 16:15:09 -04001226 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001227 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001228
Chris Masonccd467d2007-06-28 15:57:36 -04001229 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001230 printk(KERN_CRIT "trans %llu running %llu\n",
1231 (unsigned long long)trans->transid,
1232 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001233 root->fs_info->running_transaction->transid);
1234 WARN_ON(1);
1235 }
1236 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001237 printk(KERN_CRIT "trans %llu running %llu\n",
1238 (unsigned long long)trans->transid,
1239 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001240 WARN_ON(1);
1241 }
Chris Masondc17ff82008-01-08 15:46:30 -05001242
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001243 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001244 *cow_ret = buf;
1245 return 0;
1246 }
Chris Masonc4876852009-02-04 09:24:25 -05001247
Chris Mason0b86a832008-03-24 15:01:56 -04001248 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001249
1250 if (parent)
1251 btrfs_set_lock_blocking(parent);
1252 btrfs_set_lock_blocking(buf);
1253
Chris Masonf510cfe2007-10-15 16:14:48 -04001254 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001255 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001256
1257 trace_btrfs_cow_block(root, buf, *cow_ret);
1258
Chris Masonf510cfe2007-10-15 16:14:48 -04001259 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001260}
1261
Chris Masond352ac62008-09-29 15:18:18 -04001262/*
1263 * helper function for defrag to decide if two blocks pointed to by a
1264 * node are actually close by
1265 */
Chris Mason6b800532007-10-15 16:17:34 -04001266static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001267{
Chris Mason6b800532007-10-15 16:17:34 -04001268 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001269 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001270 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001271 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001272 return 0;
1273}
1274
Chris Mason081e9572007-11-06 10:26:24 -05001275/*
1276 * compare two keys in a memcmp fashion
1277 */
1278static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1279{
1280 struct btrfs_key k1;
1281
1282 btrfs_disk_key_to_cpu(&k1, disk);
1283
Diego Calleja20736ab2009-07-24 11:06:52 -04001284 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001285}
1286
Josef Bacikf3465ca2008-11-12 14:19:50 -05001287/*
1288 * same as comp_keys only with two btrfs_key's
1289 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001290int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001291{
1292 if (k1->objectid > k2->objectid)
1293 return 1;
1294 if (k1->objectid < k2->objectid)
1295 return -1;
1296 if (k1->type > k2->type)
1297 return 1;
1298 if (k1->type < k2->type)
1299 return -1;
1300 if (k1->offset > k2->offset)
1301 return 1;
1302 if (k1->offset < k2->offset)
1303 return -1;
1304 return 0;
1305}
Chris Mason081e9572007-11-06 10:26:24 -05001306
Chris Masond352ac62008-09-29 15:18:18 -04001307/*
1308 * this is used by the defrag code to go through all the
1309 * leaves pointed to by a node and reallocate them so that
1310 * disk order is close to key order
1311 */
Chris Mason6702ed42007-08-07 16:15:09 -04001312int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001313 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001314 int start_slot, int cache_only, u64 *last_ret,
1315 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001316{
Chris Mason6b800532007-10-15 16:17:34 -04001317 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001318 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001319 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001320 u64 search_start = *last_ret;
1321 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001322 u64 other;
1323 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001324 int end_slot;
1325 int i;
1326 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001327 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001328 int uptodate;
1329 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001330 int progress_passed = 0;
1331 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001332
Chris Mason5708b952007-10-25 15:43:18 -04001333 parent_level = btrfs_header_level(parent);
1334 if (cache_only && parent_level != 1)
1335 return 0;
1336
Chris Masond3977122009-01-05 21:25:51 -05001337 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001338 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001339 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001340 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001341
Chris Mason6b800532007-10-15 16:17:34 -04001342 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001343 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001344 end_slot = parent_nritems;
1345
1346 if (parent_nritems == 1)
1347 return 0;
1348
Chris Masonb4ce94d2009-02-04 09:25:08 -05001349 btrfs_set_lock_blocking(parent);
1350
Chris Mason6702ed42007-08-07 16:15:09 -04001351 for (i = start_slot; i < end_slot; i++) {
1352 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001353
Chris Mason081e9572007-11-06 10:26:24 -05001354 btrfs_node_key(parent, &disk_key, i);
1355 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1356 continue;
1357
1358 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001359 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001360 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001361 if (last_block == 0)
1362 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001363
Chris Mason6702ed42007-08-07 16:15:09 -04001364 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001365 other = btrfs_node_blockptr(parent, i - 1);
1366 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001367 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001368 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001369 other = btrfs_node_blockptr(parent, i + 1);
1370 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001371 }
Chris Masone9d0b132007-08-10 14:06:19 -04001372 if (close) {
1373 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001374 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001375 }
Chris Mason6702ed42007-08-07 16:15:09 -04001376
Chris Mason6b800532007-10-15 16:17:34 -04001377 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1378 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001379 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001380 else
1381 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001382 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001383 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001384 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001385 continue;
1386 }
Chris Mason6b800532007-10-15 16:17:34 -04001387 if (!cur) {
1388 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001389 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001390 if (!cur)
1391 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001392 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001393 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -04001394 }
Chris Mason6702ed42007-08-07 16:15:09 -04001395 }
Chris Masone9d0b132007-08-10 14:06:19 -04001396 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001397 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001398
Chris Masone7a84562008-06-25 16:01:31 -04001399 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001400 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001401 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001402 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001403 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001404 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001405 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001406 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001407 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001408 break;
Yan252c38f2007-08-29 09:11:44 -04001409 }
Chris Masone7a84562008-06-25 16:01:31 -04001410 search_start = cur->start;
1411 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001412 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001413 btrfs_tree_unlock(cur);
1414 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001415 }
1416 return err;
1417}
1418
Chris Mason74123bd2007-02-02 11:05:29 -05001419/*
1420 * The leaf data grows from end-to-front in the node.
1421 * this returns the address of the start of the last item,
1422 * which is the stop of the leaf data stack
1423 */
Chris Mason123abc82007-03-14 14:14:43 -04001424static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001425 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001426{
Chris Mason5f39d392007-10-15 16:14:19 -04001427 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001428 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001429 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001430 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001431}
1432
Chris Masonaa5d6be2007-02-28 16:35:06 -05001433
Chris Mason74123bd2007-02-02 11:05:29 -05001434/*
Chris Mason5f39d392007-10-15 16:14:19 -04001435 * search for key in the extent_buffer. The items start at offset p,
1436 * and they are item_size apart. There are 'max' items in p.
1437 *
Chris Mason74123bd2007-02-02 11:05:29 -05001438 * the slot in the array is returned via slot, and it points to
1439 * the place where you would insert key if it is not found in
1440 * the array.
1441 *
1442 * slot may point to max if the key is bigger than all of the keys
1443 */
Chris Masone02119d2008-09-05 16:13:11 -04001444static noinline int generic_bin_search(struct extent_buffer *eb,
1445 unsigned long p,
1446 int item_size, struct btrfs_key *key,
1447 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001448{
1449 int low = 0;
1450 int high = max;
1451 int mid;
1452 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001453 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001454 struct btrfs_disk_key unaligned;
1455 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001456 char *kaddr = NULL;
1457 unsigned long map_start = 0;
1458 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001459 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001460
Chris Masond3977122009-01-05 21:25:51 -05001461 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001462 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001463 offset = p + mid * item_size;
1464
Chris Masona6591712011-07-19 12:04:14 -04001465 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001466 (offset + sizeof(struct btrfs_disk_key)) >
1467 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001468
1469 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001470 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001471 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001472
Chris Mason479965d2007-10-15 16:14:27 -04001473 if (!err) {
1474 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1475 map_start);
1476 } else {
1477 read_extent_buffer(eb, &unaligned,
1478 offset, sizeof(unaligned));
1479 tmp = &unaligned;
1480 }
1481
Chris Mason5f39d392007-10-15 16:14:19 -04001482 } else {
1483 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1484 map_start);
1485 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001486 ret = comp_keys(tmp, key);
1487
1488 if (ret < 0)
1489 low = mid + 1;
1490 else if (ret > 0)
1491 high = mid;
1492 else {
1493 *slot = mid;
1494 return 0;
1495 }
1496 }
1497 *slot = low;
1498 return 1;
1499}
1500
Chris Mason97571fd2007-02-24 13:39:08 -05001501/*
1502 * simple bin_search frontend that does the right thing for
1503 * leaves vs nodes
1504 */
Chris Mason5f39d392007-10-15 16:14:19 -04001505static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1506 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001507{
Chris Mason5f39d392007-10-15 16:14:19 -04001508 if (level == 0) {
1509 return generic_bin_search(eb,
1510 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001511 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001512 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001513 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001514 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001515 return generic_bin_search(eb,
1516 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001517 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001518 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001519 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001520 }
1521 return -1;
1522}
1523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1525 int level, int *slot)
1526{
1527 return bin_search(eb, key, level, slot);
1528}
1529
Yan, Zhengf0486c62010-05-16 10:46:25 -04001530static void root_add_used(struct btrfs_root *root, u32 size)
1531{
1532 spin_lock(&root->accounting_lock);
1533 btrfs_set_root_used(&root->root_item,
1534 btrfs_root_used(&root->root_item) + size);
1535 spin_unlock(&root->accounting_lock);
1536}
1537
1538static void root_sub_used(struct btrfs_root *root, u32 size)
1539{
1540 spin_lock(&root->accounting_lock);
1541 btrfs_set_root_used(&root->root_item,
1542 btrfs_root_used(&root->root_item) - size);
1543 spin_unlock(&root->accounting_lock);
1544}
1545
Chris Masond352ac62008-09-29 15:18:18 -04001546/* given a node and slot number, this reads the blocks it points to. The
1547 * extent buffer is returned with a reference taken (but unlocked).
1548 * NULL is returned on error.
1549 */
Chris Masone02119d2008-09-05 16:13:11 -04001550static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001551 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001552{
Chris Masonca7a79a2008-05-12 12:59:19 -04001553 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001554 if (slot < 0)
1555 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001556 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001557 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001558
1559 BUG_ON(level == 0);
1560
Chris Masondb945352007-10-15 16:15:53 -04001561 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001562 btrfs_level_size(root, level - 1),
1563 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001564}
1565
Chris Masond352ac62008-09-29 15:18:18 -04001566/*
1567 * node level balancing, used to make sure nodes are in proper order for
1568 * item deletion. We balance from the top down, so we have to make sure
1569 * that a deletion won't leave an node completely empty later on.
1570 */
Chris Masone02119d2008-09-05 16:13:11 -04001571static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001572 struct btrfs_root *root,
1573 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001574{
Chris Mason5f39d392007-10-15 16:14:19 -04001575 struct extent_buffer *right = NULL;
1576 struct extent_buffer *mid;
1577 struct extent_buffer *left = NULL;
1578 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001579 int ret = 0;
1580 int wret;
1581 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001582 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001583 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001584
1585 if (level == 0)
1586 return 0;
1587
Chris Mason5f39d392007-10-15 16:14:19 -04001588 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001589
Chris Masonbd681512011-07-16 15:23:14 -04001590 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1591 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001592 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1593
Chris Mason1d4f8a02007-03-13 09:28:32 -04001594 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001595
Li Zefana05a9bb2011-09-06 16:55:34 +08001596 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001597 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001598 pslot = path->slots[level + 1];
1599 }
Chris Masonbb803952007-03-01 12:04:21 -05001600
Chris Mason40689472007-03-17 14:29:23 -04001601 /*
1602 * deal with the case where there is only one pointer in the root
1603 * by promoting the node below to a root
1604 */
Chris Mason5f39d392007-10-15 16:14:19 -04001605 if (!parent) {
1606 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001607
Chris Mason5f39d392007-10-15 16:14:19 -04001608 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001609 return 0;
1610
1611 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001612 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001613 if (!child) {
1614 ret = -EROFS;
1615 btrfs_std_error(root->fs_info, ret);
1616 goto enospc;
1617 }
1618
Chris Mason925baed2008-06-25 16:01:30 -04001619 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001620 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001621 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001622 if (ret) {
1623 btrfs_tree_unlock(child);
1624 free_extent_buffer(child);
1625 goto enospc;
1626 }
Yan2f375ab2008-02-01 14:58:07 -05001627
Jan Schmidtf2304752012-05-26 11:43:17 +02001628 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001629 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001630
Chris Mason0b86a832008-03-24 15:01:56 -04001631 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001632 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001633
Chris Mason925baed2008-06-25 16:01:30 -04001634 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001635 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001636 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001637 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001638 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001639 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001640
1641 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001642 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001643 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001644 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001645 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001646 }
Chris Mason5f39d392007-10-15 16:14:19 -04001647 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001648 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001649 return 0;
1650
Andi Kleen559af822010-10-29 15:14:37 -04001651 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001652
Chris Mason5f39d392007-10-15 16:14:19 -04001653 left = read_node_slot(root, parent, pslot - 1);
1654 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001655 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001656 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001657 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001658 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001659 if (wret) {
1660 ret = wret;
1661 goto enospc;
1662 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001663 }
Chris Mason5f39d392007-10-15 16:14:19 -04001664 right = read_node_slot(root, parent, pslot + 1);
1665 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001666 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001667 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001668 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001669 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001670 if (wret) {
1671 ret = wret;
1672 goto enospc;
1673 }
1674 }
1675
1676 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001677 if (left) {
1678 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001679 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001680 if (wret < 0)
1681 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001682 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001683 }
Chris Mason79f95c82007-03-01 15:16:26 -05001684
1685 /*
1686 * then try to empty the right most buffer into the middle
1687 */
Chris Mason5f39d392007-10-15 16:14:19 -04001688 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001689 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001690 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001691 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001692 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001693 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001694 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001695 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001696 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001697 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001698 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001699 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001700 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001701 struct btrfs_disk_key right_key;
1702 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001703 tree_mod_log_set_node_key(root->fs_info, parent,
1704 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001705 btrfs_set_node_key(parent, &right_key, pslot + 1);
1706 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001707 }
1708 }
Chris Mason5f39d392007-10-15 16:14:19 -04001709 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001710 /*
1711 * we're not allowed to leave a node with one item in the
1712 * tree during a delete. A deletion from lower in the tree
1713 * could try to delete the only pointer in this node.
1714 * So, pull some keys from the left.
1715 * There has to be a left pointer at this point because
1716 * otherwise we would have pulled some pointers from the
1717 * right
1718 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001719 if (!left) {
1720 ret = -EROFS;
1721 btrfs_std_error(root->fs_info, ret);
1722 goto enospc;
1723 }
Chris Mason5f39d392007-10-15 16:14:19 -04001724 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001725 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001726 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001727 goto enospc;
1728 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001729 if (wret == 1) {
1730 wret = push_node_left(trans, root, left, mid, 1);
1731 if (wret < 0)
1732 ret = wret;
1733 }
Chris Mason79f95c82007-03-01 15:16:26 -05001734 BUG_ON(wret == 1);
1735 }
Chris Mason5f39d392007-10-15 16:14:19 -04001736 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001737 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001738 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001739 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001740 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001741 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001742 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001743 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001744 } else {
1745 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001746 struct btrfs_disk_key mid_key;
1747 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001748 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1749 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001750 btrfs_set_node_key(parent, &mid_key, pslot);
1751 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001752 }
Chris Masonbb803952007-03-01 12:04:21 -05001753
Chris Mason79f95c82007-03-01 15:16:26 -05001754 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001755 if (left) {
1756 if (btrfs_header_nritems(left) > orig_slot) {
1757 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001758 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001759 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001760 path->slots[level + 1] -= 1;
1761 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001762 if (mid) {
1763 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001764 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001765 }
Chris Masonbb803952007-03-01 12:04:21 -05001766 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001767 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001768 path->slots[level] = orig_slot;
1769 }
1770 }
Chris Mason79f95c82007-03-01 15:16:26 -05001771 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001772 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001773 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001774 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001775enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001776 if (right) {
1777 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001778 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001779 }
1780 if (left) {
1781 if (path->nodes[level] != left)
1782 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001783 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001784 }
Chris Masonbb803952007-03-01 12:04:21 -05001785 return ret;
1786}
1787
Chris Masond352ac62008-09-29 15:18:18 -04001788/* Node balancing for insertion. Here we only split or push nodes around
1789 * when they are completely full. This is also done top down, so we
1790 * have to be pessimistic.
1791 */
Chris Masond3977122009-01-05 21:25:51 -05001792static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001793 struct btrfs_root *root,
1794 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001795{
Chris Mason5f39d392007-10-15 16:14:19 -04001796 struct extent_buffer *right = NULL;
1797 struct extent_buffer *mid;
1798 struct extent_buffer *left = NULL;
1799 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001800 int ret = 0;
1801 int wret;
1802 int pslot;
1803 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001804
1805 if (level == 0)
1806 return 1;
1807
Chris Mason5f39d392007-10-15 16:14:19 -04001808 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001809 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001810
Li Zefana05a9bb2011-09-06 16:55:34 +08001811 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001812 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001813 pslot = path->slots[level + 1];
1814 }
Chris Masone66f7092007-04-20 13:16:02 -04001815
Chris Mason5f39d392007-10-15 16:14:19 -04001816 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001817 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001818
Chris Mason5f39d392007-10-15 16:14:19 -04001819 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001820
1821 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001822 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001823 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001824
1825 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001826 btrfs_set_lock_blocking(left);
1827
Chris Mason5f39d392007-10-15 16:14:19 -04001828 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001829 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1830 wret = 1;
1831 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001832 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001833 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001834 if (ret)
1835 wret = 1;
1836 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001837 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001838 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001839 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001840 }
Chris Masone66f7092007-04-20 13:16:02 -04001841 if (wret < 0)
1842 ret = wret;
1843 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001844 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001845 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001846 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001847 tree_mod_log_set_node_key(root->fs_info, parent,
1848 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001849 btrfs_set_node_key(parent, &disk_key, pslot);
1850 btrfs_mark_buffer_dirty(parent);
1851 if (btrfs_header_nritems(left) > orig_slot) {
1852 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001853 path->slots[level + 1] -= 1;
1854 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001855 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001856 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001857 } else {
1858 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001859 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001860 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001861 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001862 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001863 }
Chris Masone66f7092007-04-20 13:16:02 -04001864 return 0;
1865 }
Chris Mason925baed2008-06-25 16:01:30 -04001866 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001867 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001868 }
Chris Mason925baed2008-06-25 16:01:30 -04001869 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001870
1871 /*
1872 * then try to empty the right most buffer into the middle
1873 */
Chris Mason5f39d392007-10-15 16:14:19 -04001874 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001875 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001876
Chris Mason925baed2008-06-25 16:01:30 -04001877 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001878 btrfs_set_lock_blocking(right);
1879
Chris Mason5f39d392007-10-15 16:14:19 -04001880 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001881 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1882 wret = 1;
1883 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001884 ret = btrfs_cow_block(trans, root, right,
1885 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001886 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001887 if (ret)
1888 wret = 1;
1889 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001890 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001891 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001892 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001893 }
Chris Masone66f7092007-04-20 13:16:02 -04001894 if (wret < 0)
1895 ret = wret;
1896 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001897 struct btrfs_disk_key disk_key;
1898
1899 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001900 tree_mod_log_set_node_key(root->fs_info, parent,
1901 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001902 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1903 btrfs_mark_buffer_dirty(parent);
1904
1905 if (btrfs_header_nritems(mid) <= orig_slot) {
1906 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001907 path->slots[level + 1] += 1;
1908 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001909 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001910 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001911 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001912 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001913 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001914 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001915 }
Chris Masone66f7092007-04-20 13:16:02 -04001916 return 0;
1917 }
Chris Mason925baed2008-06-25 16:01:30 -04001918 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001919 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001920 }
Chris Masone66f7092007-04-20 13:16:02 -04001921 return 1;
1922}
1923
Chris Mason74123bd2007-02-02 11:05:29 -05001924/*
Chris Masond352ac62008-09-29 15:18:18 -04001925 * readahead one full node of leaves, finding things that are close
1926 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001927 */
Chris Masonc8c42862009-04-03 10:14:18 -04001928static void reada_for_search(struct btrfs_root *root,
1929 struct btrfs_path *path,
1930 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001931{
Chris Mason5f39d392007-10-15 16:14:19 -04001932 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001933 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001934 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001935 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001936 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001937 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001938 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001939 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001940 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001941 u32 nr;
1942 u32 blocksize;
1943 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001944
Chris Masona6b6e752007-10-15 16:22:39 -04001945 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001946 return;
1947
Chris Mason6702ed42007-08-07 16:15:09 -04001948 if (!path->nodes[level])
1949 return;
1950
Chris Mason5f39d392007-10-15 16:14:19 -04001951 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001952
Chris Mason3c69fae2007-08-07 15:52:22 -04001953 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001954 blocksize = btrfs_level_size(root, level - 1);
1955 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001956 if (eb) {
1957 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001958 return;
1959 }
1960
Chris Masona7175312009-01-22 09:23:10 -05001961 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001962
Chris Mason5f39d392007-10-15 16:14:19 -04001963 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001964 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001965
Chris Masond3977122009-01-05 21:25:51 -05001966 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001967 if (direction < 0) {
1968 if (nr == 0)
1969 break;
1970 nr--;
1971 } else if (direction > 0) {
1972 nr++;
1973 if (nr >= nritems)
1974 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001975 }
Chris Mason01f46652007-12-21 16:24:26 -05001976 if (path->reada < 0 && objectid) {
1977 btrfs_node_key(node, &disk_key, nr);
1978 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1979 break;
1980 }
Chris Mason6b800532007-10-15 16:17:34 -04001981 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001982 if ((search <= target && target - search <= 65536) ||
1983 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001984 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001985 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001986 nread += blocksize;
1987 }
1988 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001989 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001990 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001991 }
1992}
Chris Mason925baed2008-06-25 16:01:30 -04001993
Chris Masond352ac62008-09-29 15:18:18 -04001994/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001995 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1996 * cache
1997 */
1998static noinline int reada_for_balance(struct btrfs_root *root,
1999 struct btrfs_path *path, int level)
2000{
2001 int slot;
2002 int nritems;
2003 struct extent_buffer *parent;
2004 struct extent_buffer *eb;
2005 u64 gen;
2006 u64 block1 = 0;
2007 u64 block2 = 0;
2008 int ret = 0;
2009 int blocksize;
2010
Chris Mason8c594ea2009-04-20 15:50:10 -04002011 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002012 if (!parent)
2013 return 0;
2014
2015 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002016 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002017 blocksize = btrfs_level_size(root, level);
2018
2019 if (slot > 0) {
2020 block1 = btrfs_node_blockptr(parent, slot - 1);
2021 gen = btrfs_node_ptr_generation(parent, slot - 1);
2022 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002023 /*
2024 * if we get -eagain from btrfs_buffer_uptodate, we
2025 * don't want to return eagain here. That will loop
2026 * forever
2027 */
2028 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002029 block1 = 0;
2030 free_extent_buffer(eb);
2031 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002032 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002033 block2 = btrfs_node_blockptr(parent, slot + 1);
2034 gen = btrfs_node_ptr_generation(parent, slot + 1);
2035 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002036 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002037 block2 = 0;
2038 free_extent_buffer(eb);
2039 }
2040 if (block1 || block2) {
2041 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002042
2043 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002044 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002045
2046 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002047 if (block1)
2048 readahead_tree_block(root, block1, blocksize, 0);
2049 if (block2)
2050 readahead_tree_block(root, block2, blocksize, 0);
2051
2052 if (block1) {
2053 eb = read_tree_block(root, block1, blocksize, 0);
2054 free_extent_buffer(eb);
2055 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002056 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002057 eb = read_tree_block(root, block2, blocksize, 0);
2058 free_extent_buffer(eb);
2059 }
2060 }
2061 return ret;
2062}
2063
2064
2065/*
Chris Masond3977122009-01-05 21:25:51 -05002066 * when we walk down the tree, it is usually safe to unlock the higher layers
2067 * in the tree. The exceptions are when our path goes through slot 0, because
2068 * operations on the tree might require changing key pointers higher up in the
2069 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002070 *
Chris Masond3977122009-01-05 21:25:51 -05002071 * callers might also have set path->keep_locks, which tells this code to keep
2072 * the lock if the path points to the last slot in the block. This is part of
2073 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002074 *
Chris Masond3977122009-01-05 21:25:51 -05002075 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2076 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002077 */
Chris Masone02119d2008-09-05 16:13:11 -04002078static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002079 int lowest_unlock, int min_write_lock_level,
2080 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002081{
2082 int i;
2083 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002084 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002085 struct extent_buffer *t;
2086
2087 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2088 if (!path->nodes[i])
2089 break;
2090 if (!path->locks[i])
2091 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002092 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002093 skip_level = i + 1;
2094 continue;
2095 }
Chris Mason051e1b92008-06-25 16:01:30 -04002096 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002097 u32 nritems;
2098 t = path->nodes[i];
2099 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002100 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002101 skip_level = i + 1;
2102 continue;
2103 }
2104 }
Chris Mason051e1b92008-06-25 16:01:30 -04002105 if (skip_level < i && i >= lowest_unlock)
2106 no_skips = 1;
2107
Chris Mason925baed2008-06-25 16:01:30 -04002108 t = path->nodes[i];
2109 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002110 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002111 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002112 if (write_lock_level &&
2113 i > min_write_lock_level &&
2114 i <= *write_lock_level) {
2115 *write_lock_level = i - 1;
2116 }
Chris Mason925baed2008-06-25 16:01:30 -04002117 }
2118 }
2119}
2120
Chris Mason3c69fae2007-08-07 15:52:22 -04002121/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002122 * This releases any locks held in the path starting at level and
2123 * going all the way up to the root.
2124 *
2125 * btrfs_search_slot will keep the lock held on higher nodes in a few
2126 * corner cases, such as COW of the block at slot zero in the node. This
2127 * ignores those rules, and it should only be called when there are no
2128 * more updates to be done higher up in the tree.
2129 */
2130noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2131{
2132 int i;
2133
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002134 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002135 return;
2136
2137 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2138 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002139 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002140 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002141 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002142 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002143 path->locks[i] = 0;
2144 }
2145}
2146
2147/*
Chris Masonc8c42862009-04-03 10:14:18 -04002148 * helper function for btrfs_search_slot. The goal is to find a block
2149 * in cache without setting the path to blocking. If we find the block
2150 * we return zero and the path is unchanged.
2151 *
2152 * If we can't find the block, we set the path blocking and do some
2153 * reada. -EAGAIN is returned and the search must be repeated.
2154 */
2155static int
2156read_block_for_search(struct btrfs_trans_handle *trans,
2157 struct btrfs_root *root, struct btrfs_path *p,
2158 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002159 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002160{
2161 u64 blocknr;
2162 u64 gen;
2163 u32 blocksize;
2164 struct extent_buffer *b = *eb_ret;
2165 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002166 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002167
2168 blocknr = btrfs_node_blockptr(b, slot);
2169 gen = btrfs_node_ptr_generation(b, slot);
2170 blocksize = btrfs_level_size(root, level - 1);
2171
2172 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002173 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002174 /* first we do an atomic uptodate check */
2175 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2176 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002177 /*
2178 * we found an up to date block without
2179 * sleeping, return
2180 * right away
2181 */
2182 *eb_ret = tmp;
2183 return 0;
2184 }
2185 /* the pages were up to date, but we failed
2186 * the generation number check. Do a full
2187 * read for the generation number that is correct.
2188 * We must do this without dropping locks so
2189 * we can trust our generation number
2190 */
2191 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002192 btrfs_set_path_blocking(p);
2193
Chris Masonb9fab912012-05-06 07:23:47 -04002194 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002195 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002196 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002197 *eb_ret = tmp;
2198 return 0;
2199 }
2200 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002201 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002202 return -EIO;
2203 }
Chris Masonc8c42862009-04-03 10:14:18 -04002204 }
2205
2206 /*
2207 * reduce lock contention at high levels
2208 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002209 * we read. Don't release the lock on the current
2210 * level because we need to walk this node to figure
2211 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002212 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002213 btrfs_unlock_up_safe(p, level + 1);
2214 btrfs_set_path_blocking(p);
2215
Chris Masoncb449212010-10-24 11:01:27 -04002216 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002217 if (p->reada)
2218 reada_for_search(root, p, level, slot, key->objectid);
2219
David Sterbab3b4aa72011-04-21 01:20:15 +02002220 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002221
2222 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002223 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002224 if (tmp) {
2225 /*
2226 * If the read above didn't mark this buffer up to date,
2227 * it will never end up being up to date. Set ret to EIO now
2228 * and give up so that our caller doesn't loop forever
2229 * on our EAGAINs.
2230 */
Chris Masonb9fab912012-05-06 07:23:47 -04002231 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002232 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002233 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002234 }
2235 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002236}
2237
2238/*
2239 * helper function for btrfs_search_slot. This does all of the checks
2240 * for node-level blocks and does any balancing required based on
2241 * the ins_len.
2242 *
2243 * If no extra work was required, zero is returned. If we had to
2244 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2245 * start over
2246 */
2247static int
2248setup_nodes_for_search(struct btrfs_trans_handle *trans,
2249 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002250 struct extent_buffer *b, int level, int ins_len,
2251 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002252{
2253 int ret;
2254 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2255 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2256 int sret;
2257
Chris Masonbd681512011-07-16 15:23:14 -04002258 if (*write_lock_level < level + 1) {
2259 *write_lock_level = level + 1;
2260 btrfs_release_path(p);
2261 goto again;
2262 }
2263
Chris Masonc8c42862009-04-03 10:14:18 -04002264 sret = reada_for_balance(root, p, level);
2265 if (sret)
2266 goto again;
2267
2268 btrfs_set_path_blocking(p);
2269 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002270 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002271
2272 BUG_ON(sret > 0);
2273 if (sret) {
2274 ret = sret;
2275 goto done;
2276 }
2277 b = p->nodes[level];
2278 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002279 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002280 int sret;
2281
Chris Masonbd681512011-07-16 15:23:14 -04002282 if (*write_lock_level < level + 1) {
2283 *write_lock_level = level + 1;
2284 btrfs_release_path(p);
2285 goto again;
2286 }
2287
Chris Masonc8c42862009-04-03 10:14:18 -04002288 sret = reada_for_balance(root, p, level);
2289 if (sret)
2290 goto again;
2291
2292 btrfs_set_path_blocking(p);
2293 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002294 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002295
2296 if (sret) {
2297 ret = sret;
2298 goto done;
2299 }
2300 b = p->nodes[level];
2301 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002302 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002303 goto again;
2304 }
2305 BUG_ON(btrfs_header_nritems(b) == 1);
2306 }
2307 return 0;
2308
2309again:
2310 ret = -EAGAIN;
2311done:
2312 return ret;
2313}
2314
2315/*
Chris Mason74123bd2007-02-02 11:05:29 -05002316 * look for key in the tree. path is filled in with nodes along the way
2317 * if key is found, we return zero and you can find the item in the leaf
2318 * level of the path (level 0)
2319 *
2320 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002321 * be inserted, and 1 is returned. If there are other errors during the
2322 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002323 *
2324 * if ins_len > 0, nodes and leaves will be split as we walk down the
2325 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2326 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002327 */
Chris Masone089f052007-03-16 16:20:31 -04002328int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2329 *root, struct btrfs_key *key, struct btrfs_path *p, int
2330 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002331{
Chris Mason5f39d392007-10-15 16:14:19 -04002332 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002333 int slot;
2334 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002335 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002336 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002337 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002338 int root_lock;
2339 /* everything at write_lock_level or lower must be write locked */
2340 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002341 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002342 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002343
Chris Mason6702ed42007-08-07 16:15:09 -04002344 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002345 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002346 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002347
Chris Masonbd681512011-07-16 15:23:14 -04002348 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002349 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002350
Chris Masonbd681512011-07-16 15:23:14 -04002351 /* when we are removing items, we might have to go up to level
2352 * two as we update tree pointers Make sure we keep write
2353 * for those levels as well
2354 */
2355 write_lock_level = 2;
2356 } else if (ins_len > 0) {
2357 /*
2358 * for inserting items, make sure we have a write lock on
2359 * level 1 so we can update keys
2360 */
2361 write_lock_level = 1;
2362 }
2363
2364 if (!cow)
2365 write_lock_level = -1;
2366
2367 if (cow && (p->keep_locks || p->lowest_level))
2368 write_lock_level = BTRFS_MAX_LEVEL;
2369
Chris Masonf7c79f32012-03-19 15:54:38 -04002370 min_write_lock_level = write_lock_level;
2371
Chris Masonbb803952007-03-01 12:04:21 -05002372again:
Chris Masonbd681512011-07-16 15:23:14 -04002373 /*
2374 * we try very hard to do read locks on the root
2375 */
2376 root_lock = BTRFS_READ_LOCK;
2377 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002378 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002379 /*
2380 * the commit roots are read only
2381 * so we always do read locks
2382 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002383 b = root->commit_root;
2384 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002385 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002386 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002387 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002388 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002389 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002390 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002391 level = btrfs_header_level(b);
2392 } else {
2393 /* we don't know the level of the root node
2394 * until we actually have it read locked
2395 */
2396 b = btrfs_read_lock_root_node(root);
2397 level = btrfs_header_level(b);
2398 if (level <= write_lock_level) {
2399 /* whoops, must trade for write lock */
2400 btrfs_tree_read_unlock(b);
2401 free_extent_buffer(b);
2402 b = btrfs_lock_root_node(root);
2403 root_lock = BTRFS_WRITE_LOCK;
2404
2405 /* the level might have changed, check again */
2406 level = btrfs_header_level(b);
2407 }
2408 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002409 }
Chris Masonbd681512011-07-16 15:23:14 -04002410 p->nodes[level] = b;
2411 if (!p->skip_locking)
2412 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002413
Chris Masoneb60cea2007-02-02 09:18:22 -05002414 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002415 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002416
2417 /*
2418 * setup the path here so we can release it under lock
2419 * contention with the cow code
2420 */
Chris Mason02217ed2007-03-02 16:08:05 -05002421 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002422 /*
2423 * if we don't really need to cow this block
2424 * then we don't want to set the path blocking,
2425 * so we test it here
2426 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002427 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002428 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002429
Chris Masonb4ce94d2009-02-04 09:25:08 -05002430 btrfs_set_path_blocking(p);
2431
Chris Masonbd681512011-07-16 15:23:14 -04002432 /*
2433 * must have write locks on this node and the
2434 * parent
2435 */
2436 if (level + 1 > write_lock_level) {
2437 write_lock_level = level + 1;
2438 btrfs_release_path(p);
2439 goto again;
2440 }
2441
Yan Zheng33c66f42009-07-22 09:59:00 -04002442 err = btrfs_cow_block(trans, root, b,
2443 p->nodes[level + 1],
2444 p->slots[level + 1], &b);
2445 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002446 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002447 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002448 }
Chris Mason02217ed2007-03-02 16:08:05 -05002449 }
Chris Mason65b51a02008-08-01 15:11:20 -04002450cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002451 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002452
Chris Masoneb60cea2007-02-02 09:18:22 -05002453 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002454 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002455
2456 /*
2457 * we have a lock on b and as long as we aren't changing
2458 * the tree, there is no way to for the items in b to change.
2459 * It is safe to drop the lock on our parent before we
2460 * go through the expensive btree search on b.
2461 *
2462 * If cow is true, then we might be changing slot zero,
2463 * which may require changing the parent. So, we can't
2464 * drop the lock until after we know which slot we're
2465 * operating on.
2466 */
2467 if (!cow)
2468 btrfs_unlock_up_safe(p, level + 1);
2469
Chris Mason5f39d392007-10-15 16:14:19 -04002470 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002471
Chris Mason5f39d392007-10-15 16:14:19 -04002472 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002473 int dec = 0;
2474 if (ret && slot > 0) {
2475 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002476 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002477 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002478 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002479 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002480 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002481 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002482 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002483 if (err) {
2484 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002485 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002486 }
Chris Masonc8c42862009-04-03 10:14:18 -04002487 b = p->nodes[level];
2488 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002489
Chris Masonbd681512011-07-16 15:23:14 -04002490 /*
2491 * slot 0 is special, if we change the key
2492 * we have to update the parent pointer
2493 * which means we must have a write lock
2494 * on the parent
2495 */
2496 if (slot == 0 && cow &&
2497 write_lock_level < level + 1) {
2498 write_lock_level = level + 1;
2499 btrfs_release_path(p);
2500 goto again;
2501 }
2502
Chris Masonf7c79f32012-03-19 15:54:38 -04002503 unlock_up(p, level, lowest_unlock,
2504 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002505
Chris Mason925baed2008-06-25 16:01:30 -04002506 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002507 if (dec)
2508 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002509 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002510 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002511
Yan Zheng33c66f42009-07-22 09:59:00 -04002512 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002513 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002514 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002515 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002516 if (err) {
2517 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002518 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002519 }
Chris Mason76a05b32009-05-14 13:24:30 -04002520
Chris Masonb4ce94d2009-02-04 09:25:08 -05002521 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002522 level = btrfs_header_level(b);
2523 if (level <= write_lock_level) {
2524 err = btrfs_try_tree_write_lock(b);
2525 if (!err) {
2526 btrfs_set_path_blocking(p);
2527 btrfs_tree_lock(b);
2528 btrfs_clear_path_blocking(p, b,
2529 BTRFS_WRITE_LOCK);
2530 }
2531 p->locks[level] = BTRFS_WRITE_LOCK;
2532 } else {
2533 err = btrfs_try_tree_read_lock(b);
2534 if (!err) {
2535 btrfs_set_path_blocking(p);
2536 btrfs_tree_read_lock(b);
2537 btrfs_clear_path_blocking(p, b,
2538 BTRFS_READ_LOCK);
2539 }
2540 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002541 }
Chris Masonbd681512011-07-16 15:23:14 -04002542 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002543 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002544 } else {
2545 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002546 if (ins_len > 0 &&
2547 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002548 if (write_lock_level < 1) {
2549 write_lock_level = 1;
2550 btrfs_release_path(p);
2551 goto again;
2552 }
2553
Chris Masonb4ce94d2009-02-04 09:25:08 -05002554 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002555 err = split_leaf(trans, root, key,
2556 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002557 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002558
Yan Zheng33c66f42009-07-22 09:59:00 -04002559 BUG_ON(err > 0);
2560 if (err) {
2561 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002562 goto done;
2563 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002564 }
Chris Mason459931e2008-12-10 09:10:46 -05002565 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002566 unlock_up(p, level, lowest_unlock,
2567 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002568 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002569 }
2570 }
Chris Mason65b51a02008-08-01 15:11:20 -04002571 ret = 1;
2572done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002573 /*
2574 * we don't really know what they plan on doing with the path
2575 * from here on, so for now just mark it as blocking
2576 */
Chris Masonb9473432009-03-13 11:00:37 -04002577 if (!p->leave_spinning)
2578 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002579 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002580 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002581 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002582}
2583
Chris Mason74123bd2007-02-02 11:05:29 -05002584/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002585 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2586 * current state of the tree together with the operations recorded in the tree
2587 * modification log to search for the key in a previous version of this tree, as
2588 * denoted by the time_seq parameter.
2589 *
2590 * Naturally, there is no support for insert, delete or cow operations.
2591 *
2592 * The resulting path and return value will be set up as if we called
2593 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2594 */
2595int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2596 struct btrfs_path *p, u64 time_seq)
2597{
2598 struct extent_buffer *b;
2599 int slot;
2600 int ret;
2601 int err;
2602 int level;
2603 int lowest_unlock = 1;
2604 u8 lowest_level = 0;
2605
2606 lowest_level = p->lowest_level;
2607 WARN_ON(p->nodes[0] != NULL);
2608
2609 if (p->search_commit_root) {
2610 BUG_ON(time_seq);
2611 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2612 }
2613
2614again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002615 b = get_old_root(root, time_seq);
2616 extent_buffer_get(b);
2617 level = btrfs_header_level(b);
2618 btrfs_tree_read_lock(b);
2619 p->locks[level] = BTRFS_READ_LOCK;
2620
2621 while (b) {
2622 level = btrfs_header_level(b);
2623 p->nodes[level] = b;
2624 btrfs_clear_path_blocking(p, NULL, 0);
2625
2626 /*
2627 * we have a lock on b and as long as we aren't changing
2628 * the tree, there is no way to for the items in b to change.
2629 * It is safe to drop the lock on our parent before we
2630 * go through the expensive btree search on b.
2631 */
2632 btrfs_unlock_up_safe(p, level + 1);
2633
2634 ret = bin_search(b, key, level, &slot);
2635
2636 if (level != 0) {
2637 int dec = 0;
2638 if (ret && slot > 0) {
2639 dec = 1;
2640 slot -= 1;
2641 }
2642 p->slots[level] = slot;
2643 unlock_up(p, level, lowest_unlock, 0, NULL);
2644
2645 if (level == lowest_level) {
2646 if (dec)
2647 p->slots[level]++;
2648 goto done;
2649 }
2650
2651 err = read_block_for_search(NULL, root, p, &b, level,
2652 slot, key, time_seq);
2653 if (err == -EAGAIN)
2654 goto again;
2655 if (err) {
2656 ret = err;
2657 goto done;
2658 }
2659
2660 level = btrfs_header_level(b);
2661 err = btrfs_try_tree_read_lock(b);
2662 if (!err) {
2663 btrfs_set_path_blocking(p);
2664 btrfs_tree_read_lock(b);
2665 btrfs_clear_path_blocking(p, b,
2666 BTRFS_READ_LOCK);
2667 }
2668 p->locks[level] = BTRFS_READ_LOCK;
2669 p->nodes[level] = b;
2670 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2671 if (b != p->nodes[level]) {
2672 btrfs_tree_unlock_rw(p->nodes[level],
2673 p->locks[level]);
2674 p->locks[level] = 0;
2675 p->nodes[level] = b;
2676 }
2677 } else {
2678 p->slots[level] = slot;
2679 unlock_up(p, level, lowest_unlock, 0, NULL);
2680 goto done;
2681 }
2682 }
2683 ret = 1;
2684done:
2685 if (!p->leave_spinning)
2686 btrfs_set_path_blocking(p);
2687 if (ret < 0)
2688 btrfs_release_path(p);
2689
2690 return ret;
2691}
2692
2693/*
Chris Mason74123bd2007-02-02 11:05:29 -05002694 * adjust the pointers going up the tree, starting at level
2695 * making sure the right key of each node is points to 'key'.
2696 * This is used after shifting pointers to the left, so it stops
2697 * fixing up pointers when a given leaf/node is not in slot 0 of the
2698 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002699 *
Chris Mason74123bd2007-02-02 11:05:29 -05002700 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002701static void fixup_low_keys(struct btrfs_trans_handle *trans,
2702 struct btrfs_root *root, struct btrfs_path *path,
2703 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704{
2705 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002706 struct extent_buffer *t;
2707
Chris Mason234b63a2007-03-13 10:46:10 -04002708 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002709 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002710 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002711 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002712 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002713 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002714 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002715 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002716 if (tslot != 0)
2717 break;
2718 }
2719}
2720
Chris Mason74123bd2007-02-02 11:05:29 -05002721/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002722 * update item key.
2723 *
2724 * This function isn't completely safe. It's the caller's responsibility
2725 * that the new key won't break the order
2726 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002727void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2728 struct btrfs_root *root, struct btrfs_path *path,
2729 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002730{
2731 struct btrfs_disk_key disk_key;
2732 struct extent_buffer *eb;
2733 int slot;
2734
2735 eb = path->nodes[0];
2736 slot = path->slots[0];
2737 if (slot > 0) {
2738 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002739 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002740 }
2741 if (slot < btrfs_header_nritems(eb) - 1) {
2742 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002743 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002744 }
2745
2746 btrfs_cpu_key_to_disk(&disk_key, new_key);
2747 btrfs_set_item_key(eb, &disk_key, slot);
2748 btrfs_mark_buffer_dirty(eb);
2749 if (slot == 0)
2750 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002751}
2752
2753/*
Chris Mason74123bd2007-02-02 11:05:29 -05002754 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002755 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002756 *
2757 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2758 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002759 */
Chris Mason98ed5172008-01-03 10:01:48 -05002760static int push_node_left(struct btrfs_trans_handle *trans,
2761 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002762 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002763{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002764 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002765 int src_nritems;
2766 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002767 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002768
Chris Mason5f39d392007-10-15 16:14:19 -04002769 src_nritems = btrfs_header_nritems(src);
2770 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002771 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002772 WARN_ON(btrfs_header_generation(src) != trans->transid);
2773 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002774
Chris Masonbce4eae2008-04-24 14:42:46 -04002775 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002776 return 1;
2777
Chris Masond3977122009-01-05 21:25:51 -05002778 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002779 return 1;
2780
Chris Masonbce4eae2008-04-24 14:42:46 -04002781 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002782 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002783 if (push_items < src_nritems) {
2784 /* leave at least 8 pointers in the node if
2785 * we aren't going to empty it
2786 */
2787 if (src_nritems - push_items < 8) {
2788 if (push_items <= 8)
2789 return 1;
2790 push_items -= 8;
2791 }
2792 }
2793 } else
2794 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002795
Jan Schmidtf2304752012-05-26 11:43:17 +02002796 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2797 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002798 copy_extent_buffer(dst, src,
2799 btrfs_node_key_ptr_offset(dst_nritems),
2800 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002801 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002802
Chris Masonbb803952007-03-01 12:04:21 -05002803 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002804 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2805 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002806 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2807 btrfs_node_key_ptr_offset(push_items),
2808 (src_nritems - push_items) *
2809 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002810 }
Chris Mason5f39d392007-10-15 16:14:19 -04002811 btrfs_set_header_nritems(src, src_nritems - push_items);
2812 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2813 btrfs_mark_buffer_dirty(src);
2814 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002815
Chris Masonbb803952007-03-01 12:04:21 -05002816 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002817}
2818
Chris Mason97571fd2007-02-24 13:39:08 -05002819/*
Chris Mason79f95c82007-03-01 15:16:26 -05002820 * try to push data from one node into the next node right in the
2821 * tree.
2822 *
2823 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2824 * error, and > 0 if there was no room in the right hand block.
2825 *
2826 * this will only push up to 1/2 the contents of the left node over
2827 */
Chris Mason5f39d392007-10-15 16:14:19 -04002828static int balance_node_right(struct btrfs_trans_handle *trans,
2829 struct btrfs_root *root,
2830 struct extent_buffer *dst,
2831 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002832{
Chris Mason79f95c82007-03-01 15:16:26 -05002833 int push_items = 0;
2834 int max_push;
2835 int src_nritems;
2836 int dst_nritems;
2837 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002838
Chris Mason7bb86312007-12-11 09:25:06 -05002839 WARN_ON(btrfs_header_generation(src) != trans->transid);
2840 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2841
Chris Mason5f39d392007-10-15 16:14:19 -04002842 src_nritems = btrfs_header_nritems(src);
2843 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002844 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002845 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002846 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002847
Chris Masond3977122009-01-05 21:25:51 -05002848 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002849 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002850
2851 max_push = src_nritems / 2 + 1;
2852 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002853 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002854 return 1;
Yan252c38f2007-08-29 09:11:44 -04002855
Chris Mason79f95c82007-03-01 15:16:26 -05002856 if (max_push < push_items)
2857 push_items = max_push;
2858
Jan Schmidtf2304752012-05-26 11:43:17 +02002859 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002860 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2861 btrfs_node_key_ptr_offset(0),
2862 (dst_nritems) *
2863 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002864
Jan Schmidtf2304752012-05-26 11:43:17 +02002865 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2866 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002867 copy_extent_buffer(dst, src,
2868 btrfs_node_key_ptr_offset(0),
2869 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002870 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002871
Chris Mason5f39d392007-10-15 16:14:19 -04002872 btrfs_set_header_nritems(src, src_nritems - push_items);
2873 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002874
Chris Mason5f39d392007-10-15 16:14:19 -04002875 btrfs_mark_buffer_dirty(src);
2876 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002877
Chris Mason79f95c82007-03-01 15:16:26 -05002878 return ret;
2879}
2880
2881/*
Chris Mason97571fd2007-02-24 13:39:08 -05002882 * helper function to insert a new root level in the tree.
2883 * A new node is allocated, and a single item is inserted to
2884 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002885 *
2886 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002887 */
Chris Masond3977122009-01-05 21:25:51 -05002888static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002889 struct btrfs_root *root,
2890 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002891{
Chris Mason7bb86312007-12-11 09:25:06 -05002892 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002893 struct extent_buffer *lower;
2894 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002895 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002896 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002897
2898 BUG_ON(path->nodes[level]);
2899 BUG_ON(path->nodes[level-1] != root->node);
2900
Chris Mason7bb86312007-12-11 09:25:06 -05002901 lower = path->nodes[level-1];
2902 if (level == 1)
2903 btrfs_item_key(lower, &lower_key, 0);
2904 else
2905 btrfs_node_key(lower, &lower_key, 0);
2906
Zheng Yan31840ae2008-09-23 13:14:14 -04002907 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002908 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002909 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002910 if (IS_ERR(c))
2911 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002912
Yan, Zhengf0486c62010-05-16 10:46:25 -04002913 root_add_used(root, root->nodesize);
2914
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002915 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002916 btrfs_set_header_nritems(c, 1);
2917 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002918 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002919 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002920 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002921 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002922
Chris Mason5f39d392007-10-15 16:14:19 -04002923 write_extent_buffer(c, root->fs_info->fsid,
2924 (unsigned long)btrfs_header_fsid(c),
2925 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002926
2927 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2928 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2929 BTRFS_UUID_SIZE);
2930
Chris Mason5f39d392007-10-15 16:14:19 -04002931 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002932 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002933 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002934 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002935
2936 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002937
2938 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002939
Chris Mason925baed2008-06-25 16:01:30 -04002940 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02002941 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04002942 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002943
2944 /* the super has an extra ref to root->node */
2945 free_extent_buffer(old);
2946
Chris Mason0b86a832008-03-24 15:01:56 -04002947 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002948 extent_buffer_get(c);
2949 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002950 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002951 path->slots[level] = 0;
2952 return 0;
2953}
2954
Chris Mason74123bd2007-02-02 11:05:29 -05002955/*
2956 * worker function to insert a single pointer in a node.
2957 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002958 *
Chris Mason74123bd2007-02-02 11:05:29 -05002959 * slot and level indicate where you want the key to go, and
2960 * blocknr is the block the key points to.
2961 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002962static void insert_ptr(struct btrfs_trans_handle *trans,
2963 struct btrfs_root *root, struct btrfs_path *path,
2964 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002965 int slot, int level, int tree_mod_log)
Chris Mason74123bd2007-02-02 11:05:29 -05002966{
Chris Mason5f39d392007-10-15 16:14:19 -04002967 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002968 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002969 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002970
2971 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002972 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002973 lower = path->nodes[level];
2974 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002975 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002976 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002977 if (slot != nritems) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002978 if (tree_mod_log && level)
2979 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2980 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002981 memmove_extent_buffer(lower,
2982 btrfs_node_key_ptr_offset(slot + 1),
2983 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002984 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002985 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002986 if (tree_mod_log && level) {
2987 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
2988 MOD_LOG_KEY_ADD);
2989 BUG_ON(ret < 0);
2990 }
Chris Mason5f39d392007-10-15 16:14:19 -04002991 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002992 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002993 WARN_ON(trans->transid == 0);
2994 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002995 btrfs_set_header_nritems(lower, nritems + 1);
2996 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002997}
2998
Chris Mason97571fd2007-02-24 13:39:08 -05002999/*
3000 * split the node at the specified level in path in two.
3001 * The path is corrected to point to the appropriate node after the split
3002 *
3003 * Before splitting this tries to make some room in the node by pushing
3004 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003005 *
3006 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003007 */
Chris Masone02119d2008-09-05 16:13:11 -04003008static noinline int split_node(struct btrfs_trans_handle *trans,
3009 struct btrfs_root *root,
3010 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003011{
Chris Mason5f39d392007-10-15 16:14:19 -04003012 struct extent_buffer *c;
3013 struct extent_buffer *split;
3014 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003015 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003016 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003017 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003018
Chris Mason5f39d392007-10-15 16:14:19 -04003019 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003020 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003021 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003022 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003023 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003024 if (ret)
3025 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003026 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003027 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003028 c = path->nodes[level];
3029 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003030 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003031 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003032 if (ret < 0)
3033 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003034 }
Chris Masone66f7092007-04-20 13:16:02 -04003035
Chris Mason5f39d392007-10-15 16:14:19 -04003036 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037 mid = (c_nritems + 1) / 2;
3038 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003039
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003040 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003041 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003042 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003043 if (IS_ERR(split))
3044 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003045
Yan, Zhengf0486c62010-05-16 10:46:25 -04003046 root_add_used(root, root->nodesize);
3047
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003048 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003049 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003050 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003051 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003052 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003053 btrfs_set_header_owner(split, root->root_key.objectid);
3054 write_extent_buffer(split, root->fs_info->fsid,
3055 (unsigned long)btrfs_header_fsid(split),
3056 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003057 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3058 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3059 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003060
Jan Schmidtf2304752012-05-26 11:43:17 +02003061 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003062 copy_extent_buffer(split, c,
3063 btrfs_node_key_ptr_offset(0),
3064 btrfs_node_key_ptr_offset(mid),
3065 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3066 btrfs_set_header_nritems(split, c_nritems - mid);
3067 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003068 ret = 0;
3069
Chris Mason5f39d392007-10-15 16:14:19 -04003070 btrfs_mark_buffer_dirty(c);
3071 btrfs_mark_buffer_dirty(split);
3072
Jeff Mahoney143bede2012-03-01 14:56:26 +01003073 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003074 path->slots[level + 1] + 1, level + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003075
Chris Mason5de08d72007-02-24 06:24:44 -05003076 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003077 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003078 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003079 free_extent_buffer(c);
3080 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003081 path->slots[level + 1] += 1;
3082 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003083 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003084 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003085 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003086 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003087}
3088
Chris Mason74123bd2007-02-02 11:05:29 -05003089/*
3090 * how many bytes are required to store the items in a leaf. start
3091 * and nr indicate which items in the leaf to check. This totals up the
3092 * space used both by the item structs and the item data
3093 */
Chris Mason5f39d392007-10-15 16:14:19 -04003094static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003095{
3096 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003097 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003098 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003099
3100 if (!nr)
3101 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003102 data_len = btrfs_item_end_nr(l, start);
3103 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003104 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003105 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003106 return data_len;
3107}
3108
Chris Mason74123bd2007-02-02 11:05:29 -05003109/*
Chris Masond4dbff92007-04-04 14:08:15 -04003110 * The space between the end of the leaf items and
3111 * the start of the leaf data. IOW, how much room
3112 * the leaf has left for both items and data
3113 */
Chris Masond3977122009-01-05 21:25:51 -05003114noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003115 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003116{
Chris Mason5f39d392007-10-15 16:14:19 -04003117 int nritems = btrfs_header_nritems(leaf);
3118 int ret;
3119 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3120 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003121 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3122 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003123 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003124 leaf_space_used(leaf, 0, nritems), nritems);
3125 }
3126 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003127}
3128
Chris Mason99d8f832010-07-07 10:51:48 -04003129/*
3130 * min slot controls the lowest index we're willing to push to the
3131 * right. We'll push up to and including min_slot, but no lower
3132 */
Chris Mason44871b12009-03-13 10:04:31 -04003133static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3134 struct btrfs_root *root,
3135 struct btrfs_path *path,
3136 int data_size, int empty,
3137 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003138 int free_space, u32 left_nritems,
3139 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003140{
Chris Mason5f39d392007-10-15 16:14:19 -04003141 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003142 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003143 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003144 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003145 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003146 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003147 int push_space = 0;
3148 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003149 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003150 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003151 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003152 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003153 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003154
Chris Masoncfed81a2012-03-03 07:40:03 -05003155 btrfs_init_map_token(&token);
3156
Chris Mason34a38212007-11-07 13:31:03 -05003157 if (empty)
3158 nr = 0;
3159 else
Chris Mason99d8f832010-07-07 10:51:48 -04003160 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003161
Zheng Yan31840ae2008-09-23 13:14:14 -04003162 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003163 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003164
Chris Mason44871b12009-03-13 10:04:31 -04003165 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003166 i = left_nritems - 1;
3167 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003168 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003169
Zheng Yan31840ae2008-09-23 13:14:14 -04003170 if (!empty && push_items > 0) {
3171 if (path->slots[0] > i)
3172 break;
3173 if (path->slots[0] == i) {
3174 int space = btrfs_leaf_free_space(root, left);
3175 if (space + push_space * 2 > free_space)
3176 break;
3177 }
3178 }
3179
Chris Mason00ec4c52007-02-24 12:47:20 -05003180 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003181 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003182
Chris Masondb945352007-10-15 16:15:53 -04003183 this_item_size = btrfs_item_size(left, item);
3184 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003185 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003186
Chris Mason00ec4c52007-02-24 12:47:20 -05003187 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003188 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003189 if (i == 0)
3190 break;
3191 i--;
Chris Masondb945352007-10-15 16:15:53 -04003192 }
Chris Mason5f39d392007-10-15 16:14:19 -04003193
Chris Mason925baed2008-06-25 16:01:30 -04003194 if (push_items == 0)
3195 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003196
Chris Mason34a38212007-11-07 13:31:03 -05003197 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003198 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003199
Chris Mason00ec4c52007-02-24 12:47:20 -05003200 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003201 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003202
Chris Mason5f39d392007-10-15 16:14:19 -04003203 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003204 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003205
Chris Mason00ec4c52007-02-24 12:47:20 -05003206 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003207 data_end = leaf_data_end(root, right);
3208 memmove_extent_buffer(right,
3209 btrfs_leaf_data(right) + data_end - push_space,
3210 btrfs_leaf_data(right) + data_end,
3211 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3212
Chris Mason00ec4c52007-02-24 12:47:20 -05003213 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003214 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003215 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3216 btrfs_leaf_data(left) + leaf_data_end(root, left),
3217 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003218
3219 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3220 btrfs_item_nr_offset(0),
3221 right_nritems * sizeof(struct btrfs_item));
3222
Chris Mason00ec4c52007-02-24 12:47:20 -05003223 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003224 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3225 btrfs_item_nr_offset(left_nritems - push_items),
3226 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003227
3228 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003229 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003230 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003231 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003232 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003233 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003234 push_space -= btrfs_token_item_size(right, item, &token);
3235 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003236 }
3237
Chris Mason7518a232007-03-12 12:01:18 -04003238 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003239 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003240
Chris Mason34a38212007-11-07 13:31:03 -05003241 if (left_nritems)
3242 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003243 else
3244 clean_tree_block(trans, root, left);
3245
Chris Mason5f39d392007-10-15 16:14:19 -04003246 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003247
Chris Mason5f39d392007-10-15 16:14:19 -04003248 btrfs_item_key(right, &disk_key, 0);
3249 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003250 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003251
Chris Mason00ec4c52007-02-24 12:47:20 -05003252 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003253 if (path->slots[0] >= left_nritems) {
3254 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003255 if (btrfs_header_nritems(path->nodes[0]) == 0)
3256 clean_tree_block(trans, root, path->nodes[0]);
3257 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003258 free_extent_buffer(path->nodes[0]);
3259 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003260 path->slots[1] += 1;
3261 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003262 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003263 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003264 }
3265 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003266
3267out_unlock:
3268 btrfs_tree_unlock(right);
3269 free_extent_buffer(right);
3270 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003271}
Chris Mason925baed2008-06-25 16:01:30 -04003272
Chris Mason00ec4c52007-02-24 12:47:20 -05003273/*
Chris Mason44871b12009-03-13 10:04:31 -04003274 * push some data in the path leaf to the right, trying to free up at
3275 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3276 *
3277 * returns 1 if the push failed because the other node didn't have enough
3278 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003279 *
3280 * this will push starting from min_slot to the end of the leaf. It won't
3281 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003282 */
3283static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003284 *root, struct btrfs_path *path,
3285 int min_data_size, int data_size,
3286 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003287{
3288 struct extent_buffer *left = path->nodes[0];
3289 struct extent_buffer *right;
3290 struct extent_buffer *upper;
3291 int slot;
3292 int free_space;
3293 u32 left_nritems;
3294 int ret;
3295
3296 if (!path->nodes[1])
3297 return 1;
3298
3299 slot = path->slots[1];
3300 upper = path->nodes[1];
3301 if (slot >= btrfs_header_nritems(upper) - 1)
3302 return 1;
3303
3304 btrfs_assert_tree_locked(path->nodes[1]);
3305
3306 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003307 if (right == NULL)
3308 return 1;
3309
Chris Mason44871b12009-03-13 10:04:31 -04003310 btrfs_tree_lock(right);
3311 btrfs_set_lock_blocking(right);
3312
3313 free_space = btrfs_leaf_free_space(root, right);
3314 if (free_space < data_size)
3315 goto out_unlock;
3316
3317 /* cow and double check */
3318 ret = btrfs_cow_block(trans, root, right, upper,
3319 slot + 1, &right);
3320 if (ret)
3321 goto out_unlock;
3322
3323 free_space = btrfs_leaf_free_space(root, right);
3324 if (free_space < data_size)
3325 goto out_unlock;
3326
3327 left_nritems = btrfs_header_nritems(left);
3328 if (left_nritems == 0)
3329 goto out_unlock;
3330
Chris Mason99d8f832010-07-07 10:51:48 -04003331 return __push_leaf_right(trans, root, path, min_data_size, empty,
3332 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003333out_unlock:
3334 btrfs_tree_unlock(right);
3335 free_extent_buffer(right);
3336 return 1;
3337}
3338
3339/*
Chris Mason74123bd2007-02-02 11:05:29 -05003340 * push some data in the path leaf to the left, trying to free up at
3341 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003342 *
3343 * max_slot can put a limit on how far into the leaf we'll push items. The
3344 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3345 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003346 */
Chris Mason44871b12009-03-13 10:04:31 -04003347static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3348 struct btrfs_root *root,
3349 struct btrfs_path *path, int data_size,
3350 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003351 int free_space, u32 right_nritems,
3352 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003353{
Chris Mason5f39d392007-10-15 16:14:19 -04003354 struct btrfs_disk_key disk_key;
3355 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003356 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003357 int push_space = 0;
3358 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003359 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003360 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003361 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003362 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003363 u32 this_item_size;
3364 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003365 struct btrfs_map_token token;
3366
3367 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003368
Chris Mason34a38212007-11-07 13:31:03 -05003369 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003370 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003371 else
Chris Mason99d8f832010-07-07 10:51:48 -04003372 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003373
3374 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003375 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003376
Zheng Yan31840ae2008-09-23 13:14:14 -04003377 if (!empty && push_items > 0) {
3378 if (path->slots[0] < i)
3379 break;
3380 if (path->slots[0] == i) {
3381 int space = btrfs_leaf_free_space(root, right);
3382 if (space + push_space * 2 > free_space)
3383 break;
3384 }
3385 }
3386
Chris Masonbe0e5c02007-01-26 15:51:26 -05003387 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003388 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003389
3390 this_item_size = btrfs_item_size(right, item);
3391 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003392 break;
Chris Masondb945352007-10-15 16:15:53 -04003393
Chris Masonbe0e5c02007-01-26 15:51:26 -05003394 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003395 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003396 }
Chris Masondb945352007-10-15 16:15:53 -04003397
Chris Masonbe0e5c02007-01-26 15:51:26 -05003398 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003399 ret = 1;
3400 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003401 }
Chris Mason34a38212007-11-07 13:31:03 -05003402 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003403 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003404
Chris Masonbe0e5c02007-01-26 15:51:26 -05003405 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003406 copy_extent_buffer(left, right,
3407 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3408 btrfs_item_nr_offset(0),
3409 push_items * sizeof(struct btrfs_item));
3410
Chris Mason123abc82007-03-14 14:14:43 -04003411 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003412 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003413
3414 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003415 leaf_data_end(root, left) - push_space,
3416 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003417 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003418 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003419 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003420 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003421
Chris Masondb945352007-10-15 16:15:53 -04003422 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003423 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003424 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003425
Chris Mason5f39d392007-10-15 16:14:19 -04003426 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003427
Chris Masoncfed81a2012-03-03 07:40:03 -05003428 ioff = btrfs_token_item_offset(left, item, &token);
3429 btrfs_set_token_item_offset(left, item,
3430 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3431 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003432 }
Chris Mason5f39d392007-10-15 16:14:19 -04003433 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003434
3435 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003436 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003437 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3438 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003439 WARN_ON(1);
3440 }
Chris Mason5f39d392007-10-15 16:14:19 -04003441
Chris Mason34a38212007-11-07 13:31:03 -05003442 if (push_items < right_nritems) {
3443 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3444 leaf_data_end(root, right);
3445 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3446 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3447 btrfs_leaf_data(right) +
3448 leaf_data_end(root, right), push_space);
3449
3450 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003451 btrfs_item_nr_offset(push_items),
3452 (btrfs_header_nritems(right) - push_items) *
3453 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003454 }
Yaneef1c492007-11-26 10:58:13 -05003455 right_nritems -= push_items;
3456 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003457 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003458 for (i = 0; i < right_nritems; i++) {
3459 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003460
Chris Masoncfed81a2012-03-03 07:40:03 -05003461 push_space = push_space - btrfs_token_item_size(right,
3462 item, &token);
3463 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003464 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003465
Chris Mason5f39d392007-10-15 16:14:19 -04003466 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003467 if (right_nritems)
3468 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003469 else
3470 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003471
Chris Mason5f39d392007-10-15 16:14:19 -04003472 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003473 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003474
3475 /* then fixup the leaf pointer in the path */
3476 if (path->slots[0] < push_items) {
3477 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003478 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003479 free_extent_buffer(path->nodes[0]);
3480 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003481 path->slots[1] -= 1;
3482 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003483 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003484 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003485 path->slots[0] -= push_items;
3486 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003487 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003488 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003489out:
3490 btrfs_tree_unlock(left);
3491 free_extent_buffer(left);
3492 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003493}
3494
Chris Mason74123bd2007-02-02 11:05:29 -05003495/*
Chris Mason44871b12009-03-13 10:04:31 -04003496 * push some data in the path leaf to the left, trying to free up at
3497 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003498 *
3499 * max_slot can put a limit on how far into the leaf we'll push items. The
3500 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3501 * items
Chris Mason44871b12009-03-13 10:04:31 -04003502 */
3503static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003504 *root, struct btrfs_path *path, int min_data_size,
3505 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003506{
3507 struct extent_buffer *right = path->nodes[0];
3508 struct extent_buffer *left;
3509 int slot;
3510 int free_space;
3511 u32 right_nritems;
3512 int ret = 0;
3513
3514 slot = path->slots[1];
3515 if (slot == 0)
3516 return 1;
3517 if (!path->nodes[1])
3518 return 1;
3519
3520 right_nritems = btrfs_header_nritems(right);
3521 if (right_nritems == 0)
3522 return 1;
3523
3524 btrfs_assert_tree_locked(path->nodes[1]);
3525
3526 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003527 if (left == NULL)
3528 return 1;
3529
Chris Mason44871b12009-03-13 10:04:31 -04003530 btrfs_tree_lock(left);
3531 btrfs_set_lock_blocking(left);
3532
3533 free_space = btrfs_leaf_free_space(root, left);
3534 if (free_space < data_size) {
3535 ret = 1;
3536 goto out;
3537 }
3538
3539 /* cow and double check */
3540 ret = btrfs_cow_block(trans, root, left,
3541 path->nodes[1], slot - 1, &left);
3542 if (ret) {
3543 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003544 if (ret == -ENOSPC)
3545 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003546 goto out;
3547 }
3548
3549 free_space = btrfs_leaf_free_space(root, left);
3550 if (free_space < data_size) {
3551 ret = 1;
3552 goto out;
3553 }
3554
Chris Mason99d8f832010-07-07 10:51:48 -04003555 return __push_leaf_left(trans, root, path, min_data_size,
3556 empty, left, free_space, right_nritems,
3557 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003558out:
3559 btrfs_tree_unlock(left);
3560 free_extent_buffer(left);
3561 return ret;
3562}
3563
3564/*
Chris Mason74123bd2007-02-02 11:05:29 -05003565 * split the path's leaf in two, making sure there is at least data_size
3566 * available for the resulting leaf level of the path.
3567 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003568static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3569 struct btrfs_root *root,
3570 struct btrfs_path *path,
3571 struct extent_buffer *l,
3572 struct extent_buffer *right,
3573 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003574{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003575 int data_copy_size;
3576 int rt_data_off;
3577 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003578 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003579 struct btrfs_map_token token;
3580
3581 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003582
Chris Mason5f39d392007-10-15 16:14:19 -04003583 nritems = nritems - mid;
3584 btrfs_set_header_nritems(right, nritems);
3585 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3586
3587 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3588 btrfs_item_nr_offset(mid),
3589 nritems * sizeof(struct btrfs_item));
3590
3591 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003592 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3593 data_copy_size, btrfs_leaf_data(l) +
3594 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003595
Chris Mason5f39d392007-10-15 16:14:19 -04003596 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3597 btrfs_item_end_nr(l, mid);
3598
3599 for (i = 0; i < nritems; i++) {
3600 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003601 u32 ioff;
3602
Chris Masoncfed81a2012-03-03 07:40:03 -05003603 ioff = btrfs_token_item_offset(right, item, &token);
3604 btrfs_set_token_item_offset(right, item,
3605 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003606 }
Chris Mason74123bd2007-02-02 11:05:29 -05003607
Chris Mason5f39d392007-10-15 16:14:19 -04003608 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003609 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003610 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003611 path->slots[1] + 1, 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003612
3613 btrfs_mark_buffer_dirty(right);
3614 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003615 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003616
Chris Masonbe0e5c02007-01-26 15:51:26 -05003617 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003618 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003619 free_extent_buffer(path->nodes[0]);
3620 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003621 path->slots[0] -= mid;
3622 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003623 } else {
3624 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003625 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003626 }
Chris Mason5f39d392007-10-15 16:14:19 -04003627
Chris Masoneb60cea2007-02-02 09:18:22 -05003628 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003629}
3630
3631/*
Chris Mason99d8f832010-07-07 10:51:48 -04003632 * double splits happen when we need to insert a big item in the middle
3633 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3634 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3635 * A B C
3636 *
3637 * We avoid this by trying to push the items on either side of our target
3638 * into the adjacent leaves. If all goes well we can avoid the double split
3639 * completely.
3640 */
3641static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3642 struct btrfs_root *root,
3643 struct btrfs_path *path,
3644 int data_size)
3645{
3646 int ret;
3647 int progress = 0;
3648 int slot;
3649 u32 nritems;
3650
3651 slot = path->slots[0];
3652
3653 /*
3654 * try to push all the items after our slot into the
3655 * right leaf
3656 */
3657 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3658 if (ret < 0)
3659 return ret;
3660
3661 if (ret == 0)
3662 progress++;
3663
3664 nritems = btrfs_header_nritems(path->nodes[0]);
3665 /*
3666 * our goal is to get our slot at the start or end of a leaf. If
3667 * we've done so we're done
3668 */
3669 if (path->slots[0] == 0 || path->slots[0] == nritems)
3670 return 0;
3671
3672 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3673 return 0;
3674
3675 /* try to push all the items before our slot into the next leaf */
3676 slot = path->slots[0];
3677 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3678 if (ret < 0)
3679 return ret;
3680
3681 if (ret == 0)
3682 progress++;
3683
3684 if (progress)
3685 return 0;
3686 return 1;
3687}
3688
3689/*
Chris Mason44871b12009-03-13 10:04:31 -04003690 * split the path's leaf in two, making sure there is at least data_size
3691 * available for the resulting leaf level of the path.
3692 *
3693 * returns 0 if all went well and < 0 on failure.
3694 */
3695static noinline int split_leaf(struct btrfs_trans_handle *trans,
3696 struct btrfs_root *root,
3697 struct btrfs_key *ins_key,
3698 struct btrfs_path *path, int data_size,
3699 int extend)
3700{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003701 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003702 struct extent_buffer *l;
3703 u32 nritems;
3704 int mid;
3705 int slot;
3706 struct extent_buffer *right;
3707 int ret = 0;
3708 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003709 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003710 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003711 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003712
Yan, Zhenga5719522009-09-24 09:17:31 -04003713 l = path->nodes[0];
3714 slot = path->slots[0];
3715 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3716 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3717 return -EOVERFLOW;
3718
Chris Mason44871b12009-03-13 10:04:31 -04003719 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003720 if (data_size) {
3721 wret = push_leaf_right(trans, root, path, data_size,
3722 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003723 if (wret < 0)
3724 return wret;
3725 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003726 wret = push_leaf_left(trans, root, path, data_size,
3727 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003728 if (wret < 0)
3729 return wret;
3730 }
3731 l = path->nodes[0];
3732
3733 /* did the pushes work? */
3734 if (btrfs_leaf_free_space(root, l) >= data_size)
3735 return 0;
3736 }
3737
3738 if (!path->nodes[1]) {
3739 ret = insert_new_root(trans, root, path, 1);
3740 if (ret)
3741 return ret;
3742 }
3743again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003744 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003745 l = path->nodes[0];
3746 slot = path->slots[0];
3747 nritems = btrfs_header_nritems(l);
3748 mid = (nritems + 1) / 2;
3749
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003750 if (mid <= slot) {
3751 if (nritems == 1 ||
3752 leaf_space_used(l, mid, nritems - mid) + data_size >
3753 BTRFS_LEAF_DATA_SIZE(root)) {
3754 if (slot >= nritems) {
3755 split = 0;
3756 } else {
3757 mid = slot;
3758 if (mid != nritems &&
3759 leaf_space_used(l, mid, nritems - mid) +
3760 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003761 if (data_size && !tried_avoid_double)
3762 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003763 split = 2;
3764 }
3765 }
3766 }
3767 } else {
3768 if (leaf_space_used(l, 0, mid) + data_size >
3769 BTRFS_LEAF_DATA_SIZE(root)) {
3770 if (!extend && data_size && slot == 0) {
3771 split = 0;
3772 } else if ((extend || !data_size) && slot == 0) {
3773 mid = 1;
3774 } else {
3775 mid = slot;
3776 if (mid != nritems &&
3777 leaf_space_used(l, mid, nritems - mid) +
3778 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003779 if (data_size && !tried_avoid_double)
3780 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003781 split = 2 ;
3782 }
3783 }
3784 }
3785 }
3786
3787 if (split == 0)
3788 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3789 else
3790 btrfs_item_key(l, &disk_key, mid);
3791
3792 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003793 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003794 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003795 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003796 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003797
3798 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003799
3800 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3801 btrfs_set_header_bytenr(right, right->start);
3802 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003803 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003804 btrfs_set_header_owner(right, root->root_key.objectid);
3805 btrfs_set_header_level(right, 0);
3806 write_extent_buffer(right, root->fs_info->fsid,
3807 (unsigned long)btrfs_header_fsid(right),
3808 BTRFS_FSID_SIZE);
3809
3810 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3811 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3812 BTRFS_UUID_SIZE);
3813
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003814 if (split == 0) {
3815 if (mid <= slot) {
3816 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003817 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003818 path->slots[1] + 1, 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003819 btrfs_tree_unlock(path->nodes[0]);
3820 free_extent_buffer(path->nodes[0]);
3821 path->nodes[0] = right;
3822 path->slots[0] = 0;
3823 path->slots[1] += 1;
3824 } else {
3825 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003826 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003827 path->slots[1], 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003828 btrfs_tree_unlock(path->nodes[0]);
3829 free_extent_buffer(path->nodes[0]);
3830 path->nodes[0] = right;
3831 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003832 if (path->slots[1] == 0)
3833 fixup_low_keys(trans, root, path,
3834 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003835 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003836 btrfs_mark_buffer_dirty(right);
3837 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003838 }
3839
Jeff Mahoney143bede2012-03-01 14:56:26 +01003840 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003841
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003842 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003843 BUG_ON(num_doubles != 0);
3844 num_doubles++;
3845 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003846 }
Chris Mason44871b12009-03-13 10:04:31 -04003847
Jeff Mahoney143bede2012-03-01 14:56:26 +01003848 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003849
3850push_for_double:
3851 push_for_double_split(trans, root, path, data_size);
3852 tried_avoid_double = 1;
3853 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3854 return 0;
3855 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003856}
3857
Yan, Zhengad48fd752009-11-12 09:33:58 +00003858static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3859 struct btrfs_root *root,
3860 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003861{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003862 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003863 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003864 struct btrfs_file_extent_item *fi;
3865 u64 extent_len = 0;
3866 u32 item_size;
3867 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003868
3869 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003870 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3871
3872 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3873 key.type != BTRFS_EXTENT_CSUM_KEY);
3874
3875 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3876 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003877
3878 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003879 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3880 fi = btrfs_item_ptr(leaf, path->slots[0],
3881 struct btrfs_file_extent_item);
3882 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3883 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003884 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003885
Chris Mason459931e2008-12-10 09:10:46 -05003886 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003887 path->search_for_split = 1;
3888 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003889 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003890 if (ret < 0)
3891 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003892
Yan, Zhengad48fd752009-11-12 09:33:58 +00003893 ret = -EAGAIN;
3894 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003895 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003896 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3897 goto err;
3898
Chris Mason109f6ae2010-04-02 09:20:18 -04003899 /* the leaf has changed, it now has room. return now */
3900 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3901 goto err;
3902
Yan, Zhengad48fd752009-11-12 09:33:58 +00003903 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3904 fi = btrfs_item_ptr(leaf, path->slots[0],
3905 struct btrfs_file_extent_item);
3906 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3907 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003908 }
3909
Chris Masonb9473432009-03-13 11:00:37 -04003910 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003911 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003912 if (ret)
3913 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003914
Yan, Zhengad48fd752009-11-12 09:33:58 +00003915 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003916 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003917 return 0;
3918err:
3919 path->keep_locks = 0;
3920 return ret;
3921}
3922
3923static noinline int split_item(struct btrfs_trans_handle *trans,
3924 struct btrfs_root *root,
3925 struct btrfs_path *path,
3926 struct btrfs_key *new_key,
3927 unsigned long split_offset)
3928{
3929 struct extent_buffer *leaf;
3930 struct btrfs_item *item;
3931 struct btrfs_item *new_item;
3932 int slot;
3933 char *buf;
3934 u32 nritems;
3935 u32 item_size;
3936 u32 orig_offset;
3937 struct btrfs_disk_key disk_key;
3938
Chris Masonb9473432009-03-13 11:00:37 -04003939 leaf = path->nodes[0];
3940 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3941
Chris Masonb4ce94d2009-02-04 09:25:08 -05003942 btrfs_set_path_blocking(path);
3943
Chris Mason459931e2008-12-10 09:10:46 -05003944 item = btrfs_item_nr(leaf, path->slots[0]);
3945 orig_offset = btrfs_item_offset(leaf, item);
3946 item_size = btrfs_item_size(leaf, item);
3947
Chris Mason459931e2008-12-10 09:10:46 -05003948 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003949 if (!buf)
3950 return -ENOMEM;
3951
Chris Mason459931e2008-12-10 09:10:46 -05003952 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3953 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003954
Chris Mason459931e2008-12-10 09:10:46 -05003955 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003956 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003957 if (slot != nritems) {
3958 /* shift the items */
3959 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003960 btrfs_item_nr_offset(slot),
3961 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003962 }
3963
3964 btrfs_cpu_key_to_disk(&disk_key, new_key);
3965 btrfs_set_item_key(leaf, &disk_key, slot);
3966
3967 new_item = btrfs_item_nr(leaf, slot);
3968
3969 btrfs_set_item_offset(leaf, new_item, orig_offset);
3970 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3971
3972 btrfs_set_item_offset(leaf, item,
3973 orig_offset + item_size - split_offset);
3974 btrfs_set_item_size(leaf, item, split_offset);
3975
3976 btrfs_set_header_nritems(leaf, nritems + 1);
3977
3978 /* write the data for the start of the original item */
3979 write_extent_buffer(leaf, buf,
3980 btrfs_item_ptr_offset(leaf, path->slots[0]),
3981 split_offset);
3982
3983 /* write the data for the new item */
3984 write_extent_buffer(leaf, buf + split_offset,
3985 btrfs_item_ptr_offset(leaf, slot),
3986 item_size - split_offset);
3987 btrfs_mark_buffer_dirty(leaf);
3988
Yan, Zhengad48fd752009-11-12 09:33:58 +00003989 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003990 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003991 return 0;
3992}
3993
3994/*
3995 * This function splits a single item into two items,
3996 * giving 'new_key' to the new item and splitting the
3997 * old one at split_offset (from the start of the item).
3998 *
3999 * The path may be released by this operation. After
4000 * the split, the path is pointing to the old item. The
4001 * new item is going to be in the same node as the old one.
4002 *
4003 * Note, the item being split must be smaller enough to live alone on
4004 * a tree block with room for one extra struct btrfs_item
4005 *
4006 * This allows us to split the item in place, keeping a lock on the
4007 * leaf the entire time.
4008 */
4009int btrfs_split_item(struct btrfs_trans_handle *trans,
4010 struct btrfs_root *root,
4011 struct btrfs_path *path,
4012 struct btrfs_key *new_key,
4013 unsigned long split_offset)
4014{
4015 int ret;
4016 ret = setup_leaf_for_split(trans, root, path,
4017 sizeof(struct btrfs_item));
4018 if (ret)
4019 return ret;
4020
4021 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004022 return ret;
4023}
4024
4025/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004026 * This function duplicate a item, giving 'new_key' to the new item.
4027 * It guarantees both items live in the same tree leaf and the new item
4028 * is contiguous with the original item.
4029 *
4030 * This allows us to split file extent in place, keeping a lock on the
4031 * leaf the entire time.
4032 */
4033int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4034 struct btrfs_root *root,
4035 struct btrfs_path *path,
4036 struct btrfs_key *new_key)
4037{
4038 struct extent_buffer *leaf;
4039 int ret;
4040 u32 item_size;
4041
4042 leaf = path->nodes[0];
4043 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4044 ret = setup_leaf_for_split(trans, root, path,
4045 item_size + sizeof(struct btrfs_item));
4046 if (ret)
4047 return ret;
4048
4049 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004050 setup_items_for_insert(trans, root, path, new_key, &item_size,
4051 item_size, item_size +
4052 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004053 leaf = path->nodes[0];
4054 memcpy_extent_buffer(leaf,
4055 btrfs_item_ptr_offset(leaf, path->slots[0]),
4056 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4057 item_size);
4058 return 0;
4059}
4060
4061/*
Chris Masond352ac62008-09-29 15:18:18 -04004062 * make the item pointed to by the path smaller. new_size indicates
4063 * how small to make it, and from_end tells us if we just chop bytes
4064 * off the end of the item or if we shift the item to chop bytes off
4065 * the front.
4066 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004067void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4068 struct btrfs_root *root,
4069 struct btrfs_path *path,
4070 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004071{
Chris Masonb18c6682007-04-17 13:26:50 -04004072 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004073 struct extent_buffer *leaf;
4074 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004075 u32 nritems;
4076 unsigned int data_end;
4077 unsigned int old_data_start;
4078 unsigned int old_size;
4079 unsigned int size_diff;
4080 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004081 struct btrfs_map_token token;
4082
4083 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004084
Chris Mason5f39d392007-10-15 16:14:19 -04004085 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004086 slot = path->slots[0];
4087
4088 old_size = btrfs_item_size_nr(leaf, slot);
4089 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004090 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004091
Chris Mason5f39d392007-10-15 16:14:19 -04004092 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004093 data_end = leaf_data_end(root, leaf);
4094
Chris Mason5f39d392007-10-15 16:14:19 -04004095 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004096
Chris Masonb18c6682007-04-17 13:26:50 -04004097 size_diff = old_size - new_size;
4098
4099 BUG_ON(slot < 0);
4100 BUG_ON(slot >= nritems);
4101
4102 /*
4103 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4104 */
4105 /* first correct the data pointers */
4106 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004107 u32 ioff;
4108 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004109
Chris Masoncfed81a2012-03-03 07:40:03 -05004110 ioff = btrfs_token_item_offset(leaf, item, &token);
4111 btrfs_set_token_item_offset(leaf, item,
4112 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004113 }
Chris Masondb945352007-10-15 16:15:53 -04004114
Chris Masonb18c6682007-04-17 13:26:50 -04004115 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004116 if (from_end) {
4117 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4118 data_end + size_diff, btrfs_leaf_data(leaf) +
4119 data_end, old_data_start + new_size - data_end);
4120 } else {
4121 struct btrfs_disk_key disk_key;
4122 u64 offset;
4123
4124 btrfs_item_key(leaf, &disk_key, slot);
4125
4126 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4127 unsigned long ptr;
4128 struct btrfs_file_extent_item *fi;
4129
4130 fi = btrfs_item_ptr(leaf, slot,
4131 struct btrfs_file_extent_item);
4132 fi = (struct btrfs_file_extent_item *)(
4133 (unsigned long)fi - size_diff);
4134
4135 if (btrfs_file_extent_type(leaf, fi) ==
4136 BTRFS_FILE_EXTENT_INLINE) {
4137 ptr = btrfs_item_ptr_offset(leaf, slot);
4138 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004139 (unsigned long)fi,
4140 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004141 disk_bytenr));
4142 }
4143 }
4144
4145 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4146 data_end + size_diff, btrfs_leaf_data(leaf) +
4147 data_end, old_data_start - data_end);
4148
4149 offset = btrfs_disk_key_offset(&disk_key);
4150 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4151 btrfs_set_item_key(leaf, &disk_key, slot);
4152 if (slot == 0)
4153 fixup_low_keys(trans, root, path, &disk_key, 1);
4154 }
Chris Mason5f39d392007-10-15 16:14:19 -04004155
4156 item = btrfs_item_nr(leaf, slot);
4157 btrfs_set_item_size(leaf, item, new_size);
4158 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004159
Chris Mason5f39d392007-10-15 16:14:19 -04004160 if (btrfs_leaf_free_space(root, leaf) < 0) {
4161 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004162 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004163 }
Chris Masonb18c6682007-04-17 13:26:50 -04004164}
4165
Chris Masond352ac62008-09-29 15:18:18 -04004166/*
4167 * make the item pointed to by the path bigger, data_size is the new size.
4168 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004169void btrfs_extend_item(struct btrfs_trans_handle *trans,
4170 struct btrfs_root *root, struct btrfs_path *path,
4171 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004172{
Chris Mason6567e832007-04-16 09:22:45 -04004173 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004174 struct extent_buffer *leaf;
4175 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004176 u32 nritems;
4177 unsigned int data_end;
4178 unsigned int old_data;
4179 unsigned int old_size;
4180 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004181 struct btrfs_map_token token;
4182
4183 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004184
Chris Mason5f39d392007-10-15 16:14:19 -04004185 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004186
Chris Mason5f39d392007-10-15 16:14:19 -04004187 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004188 data_end = leaf_data_end(root, leaf);
4189
Chris Mason5f39d392007-10-15 16:14:19 -04004190 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4191 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004192 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004193 }
Chris Mason6567e832007-04-16 09:22:45 -04004194 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004195 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004196
4197 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004198 if (slot >= nritems) {
4199 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004200 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4201 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004202 BUG_ON(1);
4203 }
Chris Mason6567e832007-04-16 09:22:45 -04004204
4205 /*
4206 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4207 */
4208 /* first correct the data pointers */
4209 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004210 u32 ioff;
4211 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004212
Chris Masoncfed81a2012-03-03 07:40:03 -05004213 ioff = btrfs_token_item_offset(leaf, item, &token);
4214 btrfs_set_token_item_offset(leaf, item,
4215 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004216 }
Chris Mason5f39d392007-10-15 16:14:19 -04004217
Chris Mason6567e832007-04-16 09:22:45 -04004218 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004219 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004220 data_end - data_size, btrfs_leaf_data(leaf) +
4221 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004222
Chris Mason6567e832007-04-16 09:22:45 -04004223 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004224 old_size = btrfs_item_size_nr(leaf, slot);
4225 item = btrfs_item_nr(leaf, slot);
4226 btrfs_set_item_size(leaf, item, old_size + data_size);
4227 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004228
Chris Mason5f39d392007-10-15 16:14:19 -04004229 if (btrfs_leaf_free_space(root, leaf) < 0) {
4230 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004231 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004232 }
Chris Mason6567e832007-04-16 09:22:45 -04004233}
4234
Chris Mason74123bd2007-02-02 11:05:29 -05004235/*
Chris Masond352ac62008-09-29 15:18:18 -04004236 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05004237 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05004238 * Returns the number of keys that were inserted.
4239 */
4240int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4241 struct btrfs_root *root,
4242 struct btrfs_path *path,
4243 struct btrfs_key *cpu_key, u32 *data_size,
4244 int nr)
4245{
4246 struct extent_buffer *leaf;
4247 struct btrfs_item *item;
4248 int ret = 0;
4249 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05004250 int i;
4251 u32 nritems;
4252 u32 total_data = 0;
4253 u32 total_size = 0;
4254 unsigned int data_end;
4255 struct btrfs_disk_key disk_key;
4256 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004257 struct btrfs_map_token token;
4258
4259 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004260
Yan Zheng87b29b22008-12-17 10:21:48 -05004261 for (i = 0; i < nr; i++) {
4262 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4263 BTRFS_LEAF_DATA_SIZE(root)) {
4264 break;
4265 nr = i;
4266 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004267 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05004268 total_size += data_size[i] + sizeof(struct btrfs_item);
4269 }
4270 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004271
Josef Bacikf3465ca2008-11-12 14:19:50 -05004272 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4273 if (ret == 0)
4274 return -EEXIST;
4275 if (ret < 0)
4276 goto out;
4277
Josef Bacikf3465ca2008-11-12 14:19:50 -05004278 leaf = path->nodes[0];
4279
4280 nritems = btrfs_header_nritems(leaf);
4281 data_end = leaf_data_end(root, leaf);
4282
4283 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4284 for (i = nr; i >= 0; i--) {
4285 total_data -= data_size[i];
4286 total_size -= data_size[i] + sizeof(struct btrfs_item);
4287 if (total_size < btrfs_leaf_free_space(root, leaf))
4288 break;
4289 }
4290 nr = i;
4291 }
4292
4293 slot = path->slots[0];
4294 BUG_ON(slot < 0);
4295
4296 if (slot != nritems) {
4297 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4298
4299 item = btrfs_item_nr(leaf, slot);
4300 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4301
4302 /* figure out how many keys we can insert in here */
4303 total_data = data_size[0];
4304 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004305 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05004306 break;
4307 total_data += data_size[i];
4308 }
4309 nr = i;
4310
4311 if (old_data < data_end) {
4312 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004313 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05004314 slot, old_data, data_end);
4315 BUG_ON(1);
4316 }
4317 /*
4318 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4319 */
4320 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05004321 for (i = slot; i < nritems; i++) {
4322 u32 ioff;
4323
4324 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004325 ioff = btrfs_token_item_offset(leaf, item, &token);
4326 btrfs_set_token_item_offset(leaf, item,
4327 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004328 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004329 /* shift the items */
4330 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4331 btrfs_item_nr_offset(slot),
4332 (nritems - slot) * sizeof(struct btrfs_item));
4333
4334 /* shift the data */
4335 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4336 data_end - total_data, btrfs_leaf_data(leaf) +
4337 data_end, old_data - data_end);
4338 data_end = old_data;
4339 } else {
4340 /*
4341 * this sucks but it has to be done, if we are inserting at
4342 * the end of the leaf only insert 1 of the items, since we
4343 * have no way of knowing whats on the next leaf and we'd have
4344 * to drop our current locks to figure it out
4345 */
4346 nr = 1;
4347 }
4348
4349 /* setup the item for the new data */
4350 for (i = 0; i < nr; i++) {
4351 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4352 btrfs_set_item_key(leaf, &disk_key, slot + i);
4353 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004354 btrfs_set_token_item_offset(leaf, item,
4355 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004356 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004357 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004358 }
4359 btrfs_set_header_nritems(leaf, nritems + nr);
4360 btrfs_mark_buffer_dirty(leaf);
4361
4362 ret = 0;
4363 if (slot == 0) {
4364 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004365 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004366 }
4367
4368 if (btrfs_leaf_free_space(root, leaf) < 0) {
4369 btrfs_print_leaf(root, leaf);
4370 BUG();
4371 }
4372out:
4373 if (!ret)
4374 ret = nr;
4375 return ret;
4376}
4377
4378/*
Chris Mason44871b12009-03-13 10:04:31 -04004379 * this is a helper for btrfs_insert_empty_items, the main goal here is
4380 * to save stack depth by doing the bulk of the work in a function
4381 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004382 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004383void setup_items_for_insert(struct btrfs_trans_handle *trans,
4384 struct btrfs_root *root, struct btrfs_path *path,
4385 struct btrfs_key *cpu_key, u32 *data_size,
4386 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004387{
Chris Mason5f39d392007-10-15 16:14:19 -04004388 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004389 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004390 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004391 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004392 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004393 struct extent_buffer *leaf;
4394 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004395 struct btrfs_map_token token;
4396
4397 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004398
Chris Mason5f39d392007-10-15 16:14:19 -04004399 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004400 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004401
Chris Mason5f39d392007-10-15 16:14:19 -04004402 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004403 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004404
Chris Masonf25956c2008-09-12 15:32:53 -04004405 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004406 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004407 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004408 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004409 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004410 }
Chris Mason5f39d392007-10-15 16:14:19 -04004411
Chris Masonbe0e5c02007-01-26 15:51:26 -05004412 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004413 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004414
Chris Mason5f39d392007-10-15 16:14:19 -04004415 if (old_data < data_end) {
4416 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004417 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004418 slot, old_data, data_end);
4419 BUG_ON(1);
4420 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004421 /*
4422 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4423 */
4424 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004425 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004426 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004427
Chris Mason5f39d392007-10-15 16:14:19 -04004428 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004429 ioff = btrfs_token_item_offset(leaf, item, &token);
4430 btrfs_set_token_item_offset(leaf, item,
4431 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004432 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004433 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004434 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004435 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004436 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004437
4438 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004439 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004440 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004441 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004442 data_end = old_data;
4443 }
Chris Mason5f39d392007-10-15 16:14:19 -04004444
Chris Mason62e27492007-03-15 12:56:47 -04004445 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004446 for (i = 0; i < nr; i++) {
4447 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4448 btrfs_set_item_key(leaf, &disk_key, slot + i);
4449 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004450 btrfs_set_token_item_offset(leaf, item,
4451 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004452 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004453 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004454 }
Chris Mason44871b12009-03-13 10:04:31 -04004455
Chris Mason9c583092008-01-29 15:15:18 -05004456 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004457
Chris Mason5a01a2e2008-01-30 11:43:54 -05004458 if (slot == 0) {
4459 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004460 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004461 }
Chris Masonb9473432009-03-13 11:00:37 -04004462 btrfs_unlock_up_safe(path, 1);
4463 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004464
Chris Mason5f39d392007-10-15 16:14:19 -04004465 if (btrfs_leaf_free_space(root, leaf) < 0) {
4466 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004467 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004468 }
Chris Mason44871b12009-03-13 10:04:31 -04004469}
4470
4471/*
4472 * Given a key and some data, insert items into the tree.
4473 * This does all the path init required, making room in the tree if needed.
4474 */
4475int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4476 struct btrfs_root *root,
4477 struct btrfs_path *path,
4478 struct btrfs_key *cpu_key, u32 *data_size,
4479 int nr)
4480{
Chris Mason44871b12009-03-13 10:04:31 -04004481 int ret = 0;
4482 int slot;
4483 int i;
4484 u32 total_size = 0;
4485 u32 total_data = 0;
4486
4487 for (i = 0; i < nr; i++)
4488 total_data += data_size[i];
4489
4490 total_size = total_data + (nr * sizeof(struct btrfs_item));
4491 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4492 if (ret == 0)
4493 return -EEXIST;
4494 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004495 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004496
Chris Mason44871b12009-03-13 10:04:31 -04004497 slot = path->slots[0];
4498 BUG_ON(slot < 0);
4499
Jeff Mahoney143bede2012-03-01 14:56:26 +01004500 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004501 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004502 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004503}
4504
4505/*
4506 * Given a key and some data, insert an item into the tree.
4507 * This does all the path init required, making room in the tree if needed.
4508 */
Chris Masone089f052007-03-16 16:20:31 -04004509int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4510 *root, struct btrfs_key *cpu_key, void *data, u32
4511 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004512{
4513 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004514 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004515 struct extent_buffer *leaf;
4516 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004517
Chris Mason2c90e5d2007-04-02 10:50:19 -04004518 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004519 if (!path)
4520 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004521 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004522 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004523 leaf = path->nodes[0];
4524 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4525 write_extent_buffer(leaf, data, ptr, data_size);
4526 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004527 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004528 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004529 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004530}
4531
Chris Mason74123bd2007-02-02 11:05:29 -05004532/*
Chris Mason5de08d72007-02-24 06:24:44 -05004533 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004534 *
Chris Masond352ac62008-09-29 15:18:18 -04004535 * the tree should have been previously balanced so the deletion does not
4536 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004537 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004538static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004539 struct btrfs_path *path, int level, int slot,
4540 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004541{
Chris Mason5f39d392007-10-15 16:14:19 -04004542 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004543 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004544 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004545
Chris Mason5f39d392007-10-15 16:14:19 -04004546 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004547 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004548 if (tree_mod_log && level)
4549 tree_mod_log_eb_move(root->fs_info, parent, slot,
4550 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004551 memmove_extent_buffer(parent,
4552 btrfs_node_key_ptr_offset(slot),
4553 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004554 sizeof(struct btrfs_key_ptr) *
4555 (nritems - slot - 1));
Jan Schmidtf3956942012-05-31 15:02:32 +02004556 } else if (tree_mod_log && level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004557 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4558 MOD_LOG_KEY_REMOVE);
4559 BUG_ON(ret < 0);
4560 }
4561
Chris Mason7518a232007-03-12 12:01:18 -04004562 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004563 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004564 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004565 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004566 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004567 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004568 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004569 struct btrfs_disk_key disk_key;
4570
4571 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004572 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004573 }
Chris Masond6025572007-03-30 14:27:56 -04004574 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004575}
4576
Chris Mason74123bd2007-02-02 11:05:29 -05004577/*
Chris Mason323ac952008-10-01 19:05:46 -04004578 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004579 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004580 *
4581 * This deletes the pointer in path->nodes[1] and frees the leaf
4582 * block extent. zero is returned if it all worked out, < 0 otherwise.
4583 *
4584 * The path must have already been setup for deleting the leaf, including
4585 * all the proper balancing. path->nodes[1] must be locked.
4586 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004587static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4588 struct btrfs_root *root,
4589 struct btrfs_path *path,
4590 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004591{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004592 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004593 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004594
Chris Mason4d081c42009-02-04 09:31:28 -05004595 /*
4596 * btrfs_free_extent is expensive, we want to make sure we
4597 * aren't holding any locks when we call it
4598 */
4599 btrfs_unlock_up_safe(path, 0);
4600
Yan, Zhengf0486c62010-05-16 10:46:25 -04004601 root_sub_used(root, leaf->len);
4602
Josef Bacik3083ee22012-03-09 16:01:49 -05004603 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004604 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004605 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004606}
4607/*
Chris Mason74123bd2007-02-02 11:05:29 -05004608 * delete the item at the leaf level in path. If that empties
4609 * the leaf, remove it from the tree
4610 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004611int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4612 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004613{
Chris Mason5f39d392007-10-15 16:14:19 -04004614 struct extent_buffer *leaf;
4615 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004616 int last_off;
4617 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004618 int ret = 0;
4619 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004620 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004621 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004622 struct btrfs_map_token token;
4623
4624 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004625
Chris Mason5f39d392007-10-15 16:14:19 -04004626 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004627 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4628
4629 for (i = 0; i < nr; i++)
4630 dsize += btrfs_item_size_nr(leaf, slot + i);
4631
Chris Mason5f39d392007-10-15 16:14:19 -04004632 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004633
Chris Mason85e21ba2008-01-29 15:11:36 -05004634 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004635 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004636
4637 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004638 data_end + dsize,
4639 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004640 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004641
Chris Mason85e21ba2008-01-29 15:11:36 -05004642 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004643 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004644
Chris Mason5f39d392007-10-15 16:14:19 -04004645 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004646 ioff = btrfs_token_item_offset(leaf, item, &token);
4647 btrfs_set_token_item_offset(leaf, item,
4648 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004649 }
Chris Masondb945352007-10-15 16:15:53 -04004650
Chris Mason5f39d392007-10-15 16:14:19 -04004651 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004652 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004653 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004654 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004655 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004656 btrfs_set_header_nritems(leaf, nritems - nr);
4657 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004658
Chris Mason74123bd2007-02-02 11:05:29 -05004659 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004660 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004661 if (leaf == root->node) {
4662 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004663 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004664 btrfs_set_path_blocking(path);
4665 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004666 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004667 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004668 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004669 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004670 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004671 struct btrfs_disk_key disk_key;
4672
4673 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004674 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004675 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004676
Chris Mason74123bd2007-02-02 11:05:29 -05004677 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004678 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004679 /* push_leaf_left fixes the path.
4680 * make sure the path still points to our leaf
4681 * for possible call to del_ptr below
4682 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004683 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004684 extent_buffer_get(leaf);
4685
Chris Masonb9473432009-03-13 11:00:37 -04004686 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004687 wret = push_leaf_left(trans, root, path, 1, 1,
4688 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004689 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004690 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004691
4692 if (path->nodes[0] == leaf &&
4693 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004694 wret = push_leaf_right(trans, root, path, 1,
4695 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004696 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004697 ret = wret;
4698 }
Chris Mason5f39d392007-10-15 16:14:19 -04004699
4700 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004701 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004702 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004703 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004704 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004705 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004706 /* if we're still in the path, make sure
4707 * we're dirty. Otherwise, one of the
4708 * push_leaf functions must have already
4709 * dirtied this buffer
4710 */
4711 if (path->nodes[0] == leaf)
4712 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004713 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004714 }
Chris Masond5719762007-03-23 10:01:08 -04004715 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004716 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004717 }
4718 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004719 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004720}
4721
Chris Mason97571fd2007-02-24 13:39:08 -05004722/*
Chris Mason925baed2008-06-25 16:01:30 -04004723 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004724 * returns 0 if it found something or 1 if there are no lesser leaves.
4725 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004726 *
4727 * This may release the path, and so you may lose any locks held at the
4728 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004729 */
4730int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4731{
Chris Mason925baed2008-06-25 16:01:30 -04004732 struct btrfs_key key;
4733 struct btrfs_disk_key found_key;
4734 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004735
Chris Mason925baed2008-06-25 16:01:30 -04004736 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004737
Chris Mason925baed2008-06-25 16:01:30 -04004738 if (key.offset > 0)
4739 key.offset--;
4740 else if (key.type > 0)
4741 key.type--;
4742 else if (key.objectid > 0)
4743 key.objectid--;
4744 else
4745 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004746
David Sterbab3b4aa72011-04-21 01:20:15 +02004747 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004748 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4749 if (ret < 0)
4750 return ret;
4751 btrfs_item_key(path->nodes[0], &found_key, 0);
4752 ret = comp_keys(&found_key, &key);
4753 if (ret < 0)
4754 return 0;
4755 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004756}
4757
Chris Mason3f157a22008-06-25 16:01:31 -04004758/*
4759 * A helper function to walk down the tree starting at min_key, and looking
4760 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004761 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004762 *
4763 * This does not cow, but it does stuff the starting key it finds back
4764 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4765 * key and get a writable path.
4766 *
4767 * This does lock as it descends, and path->keep_locks should be set
4768 * to 1 by the caller.
4769 *
4770 * This honors path->lowest_level to prevent descent past a given level
4771 * of the tree.
4772 *
Chris Masond352ac62008-09-29 15:18:18 -04004773 * min_trans indicates the oldest transaction that you are interested
4774 * in walking through. Any nodes or leaves older than min_trans are
4775 * skipped over (without reading them).
4776 *
Chris Mason3f157a22008-06-25 16:01:31 -04004777 * returns zero if something useful was found, < 0 on error and 1 if there
4778 * was nothing in the tree that matched the search criteria.
4779 */
4780int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004781 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004782 struct btrfs_path *path, int cache_only,
4783 u64 min_trans)
4784{
4785 struct extent_buffer *cur;
4786 struct btrfs_key found_key;
4787 int slot;
Yan96524802008-07-24 12:19:49 -04004788 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004789 u32 nritems;
4790 int level;
4791 int ret = 1;
4792
Chris Mason934d3752008-12-08 16:43:10 -05004793 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004794again:
Chris Masonbd681512011-07-16 15:23:14 -04004795 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004796 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004797 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004798 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004799 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004800
4801 if (btrfs_header_generation(cur) < min_trans) {
4802 ret = 1;
4803 goto out;
4804 }
Chris Masond3977122009-01-05 21:25:51 -05004805 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004806 nritems = btrfs_header_nritems(cur);
4807 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004808 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004809
Chris Mason323ac952008-10-01 19:05:46 -04004810 /* at the lowest level, we're done, setup the path and exit */
4811 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004812 if (slot >= nritems)
4813 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004814 ret = 0;
4815 path->slots[level] = slot;
4816 btrfs_item_key_to_cpu(cur, &found_key, slot);
4817 goto out;
4818 }
Yan96524802008-07-24 12:19:49 -04004819 if (sret && slot > 0)
4820 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004821 /*
4822 * check this node pointer against the cache_only and
4823 * min_trans parameters. If it isn't in cache or is too
4824 * old, skip to the next one.
4825 */
Chris Masond3977122009-01-05 21:25:51 -05004826 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004827 u64 blockptr;
4828 u64 gen;
4829 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004830 struct btrfs_disk_key disk_key;
4831
Chris Mason3f157a22008-06-25 16:01:31 -04004832 blockptr = btrfs_node_blockptr(cur, slot);
4833 gen = btrfs_node_ptr_generation(cur, slot);
4834 if (gen < min_trans) {
4835 slot++;
4836 continue;
4837 }
4838 if (!cache_only)
4839 break;
4840
Chris Masone02119d2008-09-05 16:13:11 -04004841 if (max_key) {
4842 btrfs_node_key(cur, &disk_key, slot);
4843 if (comp_keys(&disk_key, max_key) >= 0) {
4844 ret = 1;
4845 goto out;
4846 }
4847 }
4848
Chris Mason3f157a22008-06-25 16:01:31 -04004849 tmp = btrfs_find_tree_block(root, blockptr,
4850 btrfs_level_size(root, level - 1));
4851
Chris Masonb9fab912012-05-06 07:23:47 -04004852 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004853 free_extent_buffer(tmp);
4854 break;
4855 }
4856 if (tmp)
4857 free_extent_buffer(tmp);
4858 slot++;
4859 }
Chris Masone02119d2008-09-05 16:13:11 -04004860find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004861 /*
4862 * we didn't find a candidate key in this node, walk forward
4863 * and find another one
4864 */
4865 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004866 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004867 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004868 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004869 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004870 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004871 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004872 goto again;
4873 } else {
4874 goto out;
4875 }
4876 }
4877 /* save our key for returning back */
4878 btrfs_node_key_to_cpu(cur, &found_key, slot);
4879 path->slots[level] = slot;
4880 if (level == path->lowest_level) {
4881 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004882 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004883 goto out;
4884 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004885 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004886 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004887 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004888
Chris Masonbd681512011-07-16 15:23:14 -04004889 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004890
Chris Masonbd681512011-07-16 15:23:14 -04004891 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004892 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004893 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004894 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004895 }
4896out:
4897 if (ret == 0)
4898 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004899 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004900 return ret;
4901}
4902
4903/*
4904 * this is similar to btrfs_next_leaf, but does not try to preserve
4905 * and fixup the path. It looks for and returns the next key in the
4906 * tree based on the current path and the cache_only and min_trans
4907 * parameters.
4908 *
4909 * 0 is returned if another key is found, < 0 if there are any errors
4910 * and 1 is returned if there are no higher keys in the tree
4911 *
4912 * path->keep_locks should be set to 1 on the search made before
4913 * calling this function.
4914 */
Chris Masone7a84562008-06-25 16:01:31 -04004915int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004916 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004917 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004918{
Chris Masone7a84562008-06-25 16:01:31 -04004919 int slot;
4920 struct extent_buffer *c;
4921
Chris Mason934d3752008-12-08 16:43:10 -05004922 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004923 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004924 if (!path->nodes[level])
4925 return 1;
4926
4927 slot = path->slots[level] + 1;
4928 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004929next:
Chris Masone7a84562008-06-25 16:01:31 -04004930 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004931 int ret;
4932 int orig_lowest;
4933 struct btrfs_key cur_key;
4934 if (level + 1 >= BTRFS_MAX_LEVEL ||
4935 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004936 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004937
4938 if (path->locks[level + 1]) {
4939 level++;
4940 continue;
4941 }
4942
4943 slot = btrfs_header_nritems(c) - 1;
4944 if (level == 0)
4945 btrfs_item_key_to_cpu(c, &cur_key, slot);
4946 else
4947 btrfs_node_key_to_cpu(c, &cur_key, slot);
4948
4949 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004950 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004951 path->lowest_level = level;
4952 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4953 0, 0);
4954 path->lowest_level = orig_lowest;
4955 if (ret < 0)
4956 return ret;
4957
4958 c = path->nodes[level];
4959 slot = path->slots[level];
4960 if (ret == 0)
4961 slot++;
4962 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004963 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004964
Chris Masone7a84562008-06-25 16:01:31 -04004965 if (level == 0)
4966 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004967 else {
4968 u64 blockptr = btrfs_node_blockptr(c, slot);
4969 u64 gen = btrfs_node_ptr_generation(c, slot);
4970
4971 if (cache_only) {
4972 struct extent_buffer *cur;
4973 cur = btrfs_find_tree_block(root, blockptr,
4974 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004975 if (!cur ||
4976 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004977 slot++;
4978 if (cur)
4979 free_extent_buffer(cur);
4980 goto next;
4981 }
4982 free_extent_buffer(cur);
4983 }
4984 if (gen < min_trans) {
4985 slot++;
4986 goto next;
4987 }
Chris Masone7a84562008-06-25 16:01:31 -04004988 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004989 }
Chris Masone7a84562008-06-25 16:01:31 -04004990 return 0;
4991 }
4992 return 1;
4993}
4994
Chris Mason7bb86312007-12-11 09:25:06 -05004995/*
Chris Mason925baed2008-06-25 16:01:30 -04004996 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004997 * returns 0 if it found something or 1 if there are no greater leaves.
4998 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004999 */
Chris Mason234b63a2007-03-13 10:46:10 -04005000int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005001{
5002 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005003 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005004 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005005 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005006 struct btrfs_key key;
5007 u32 nritems;
5008 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005009 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005010 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005011
5012 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005013 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005014 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005015
Chris Mason8e73f272009-04-03 10:14:18 -04005016 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5017again:
5018 level = 1;
5019 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005020 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005021 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005022
Chris Masona2135012008-06-25 16:01:30 -04005023 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005024 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005025
Chris Mason925baed2008-06-25 16:01:30 -04005026 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5027 path->keep_locks = 0;
5028
5029 if (ret < 0)
5030 return ret;
5031
Chris Masona2135012008-06-25 16:01:30 -04005032 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005033 /*
5034 * by releasing the path above we dropped all our locks. A balance
5035 * could have added more items next to the key that used to be
5036 * at the very end of the block. So, check again here and
5037 * advance the path if there are now more items available.
5038 */
Chris Masona2135012008-06-25 16:01:30 -04005039 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005040 if (ret == 0)
5041 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005042 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005043 goto done;
5044 }
Chris Masond97e63b2007-02-20 16:40:44 -05005045
Chris Masond3977122009-01-05 21:25:51 -05005046 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005047 if (!path->nodes[level]) {
5048 ret = 1;
5049 goto done;
5050 }
Chris Mason5f39d392007-10-15 16:14:19 -04005051
Chris Masond97e63b2007-02-20 16:40:44 -05005052 slot = path->slots[level] + 1;
5053 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005054 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005055 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005056 if (level == BTRFS_MAX_LEVEL) {
5057 ret = 1;
5058 goto done;
5059 }
Chris Masond97e63b2007-02-20 16:40:44 -05005060 continue;
5061 }
Chris Mason5f39d392007-10-15 16:14:19 -04005062
Chris Mason925baed2008-06-25 16:01:30 -04005063 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005064 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005065 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005066 }
Chris Mason5f39d392007-10-15 16:14:19 -04005067
Chris Mason8e73f272009-04-03 10:14:18 -04005068 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005069 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005070 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005071 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005072 if (ret == -EAGAIN)
5073 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005074
Chris Mason76a05b32009-05-14 13:24:30 -04005075 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005076 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005077 goto done;
5078 }
5079
Chris Mason5cd57b22008-06-25 16:01:30 -04005080 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005081 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005082 if (!ret) {
5083 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005084 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005085 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005086 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005087 }
Chris Mason31533fb2011-07-26 16:01:59 -04005088 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005089 }
Chris Masond97e63b2007-02-20 16:40:44 -05005090 break;
5091 }
5092 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005093 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005094 level--;
5095 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005096 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005097 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005098
Chris Mason5f39d392007-10-15 16:14:19 -04005099 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005100 path->nodes[level] = next;
5101 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005102 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005103 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005104 if (!level)
5105 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005106
Chris Mason8e73f272009-04-03 10:14:18 -04005107 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005108 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005109 if (ret == -EAGAIN)
5110 goto again;
5111
Chris Mason76a05b32009-05-14 13:24:30 -04005112 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005113 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005114 goto done;
5115 }
5116
Chris Mason5cd57b22008-06-25 16:01:30 -04005117 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005118 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005119 if (!ret) {
5120 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005121 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005122 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005123 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005124 }
Chris Mason31533fb2011-07-26 16:01:59 -04005125 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005126 }
Chris Masond97e63b2007-02-20 16:40:44 -05005127 }
Chris Mason8e73f272009-04-03 10:14:18 -04005128 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005129done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005130 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005131 path->leave_spinning = old_spinning;
5132 if (!old_spinning)
5133 btrfs_set_path_blocking(path);
5134
5135 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005136}
Chris Mason0b86a832008-03-24 15:01:56 -04005137
Chris Mason3f157a22008-06-25 16:01:31 -04005138/*
5139 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5140 * searching until it gets past min_objectid or finds an item of 'type'
5141 *
5142 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5143 */
Chris Mason0b86a832008-03-24 15:01:56 -04005144int btrfs_previous_item(struct btrfs_root *root,
5145 struct btrfs_path *path, u64 min_objectid,
5146 int type)
5147{
5148 struct btrfs_key found_key;
5149 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005150 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005151 int ret;
5152
Chris Masond3977122009-01-05 21:25:51 -05005153 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005154 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005155 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005156 ret = btrfs_prev_leaf(root, path);
5157 if (ret != 0)
5158 return ret;
5159 } else {
5160 path->slots[0]--;
5161 }
5162 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005163 nritems = btrfs_header_nritems(leaf);
5164 if (nritems == 0)
5165 return 1;
5166 if (path->slots[0] == nritems)
5167 path->slots[0]--;
5168
Chris Mason0b86a832008-03-24 15:01:56 -04005169 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005170 if (found_key.objectid < min_objectid)
5171 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005172 if (found_key.type == type)
5173 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005174 if (found_key.objectid == min_objectid &&
5175 found_key.type < type)
5176 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005177 }
5178 return 1;
5179}