blob: fad8f23d70f099d47e5b5fa9ca34f9ec08cbb713 [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>
Chris Masoneb60cea2007-02-02 09:18:22 -050021#include "ctree.h"
22#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040023#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040024#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040030 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040031 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040032static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040034 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040035static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040039static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40 struct btrfs_path *path, int level, int slot);
Yan, Zhengad48fd752009-11-12 09:33:58 +000041static int setup_items_for_insert(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root, struct btrfs_path *path,
43 struct btrfs_key *cpu_key, u32 *data_size,
44 u32 total_data, u32 total_size, int nr);
45
Chris Masond97e63b2007-02-20 16:40:44 -050046
Chris Mason2c90e5d2007-04-02 10:50:19 -040047struct btrfs_path *btrfs_alloc_path(void)
48{
Chris Masondf24a2b2007-04-04 09:36:31 -040049 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050050 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
51 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040053 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040054}
55
Chris Masonb4ce94d2009-02-04 09:25:08 -050056/*
57 * set all locked nodes in the path to blocking locks. This should
58 * be done before scheduling
59 */
60noinline void btrfs_set_path_blocking(struct btrfs_path *p)
61{
62 int i;
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
64 if (p->nodes[i] && p->locks[i])
65 btrfs_set_lock_blocking(p->nodes[i]);
66 }
67}
68
69/*
70 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050071 *
72 * held is used to keep lockdep happy, when lockdep is enabled
73 * we set held to a blocking lock before we go around and
74 * retake all the spinlocks in the path. You can safely use NULL
75 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050076 */
David Sterba62a45b62011-04-20 15:52:26 +020077static noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
78 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050079{
80 int i;
Chris Mason4008c042009-02-12 14:09:45 -050081
82#ifdef CONFIG_DEBUG_LOCK_ALLOC
83 /* lockdep really cares that we take all of these spinlocks
84 * in the right order. If any of the locks in the path are not
85 * currently blocking, it is going to complain. So, make really
86 * really sure by forcing the path to blocking before we clear
87 * the path blocking.
88 */
89 if (held)
90 btrfs_set_lock_blocking(held);
91 btrfs_set_path_blocking(p);
92#endif
93
94 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050095 if (p->nodes[i] && p->locks[i])
96 btrfs_clear_lock_blocking(p->nodes[i]);
97 }
Chris Mason4008c042009-02-12 14:09:45 -050098
99#ifdef CONFIG_DEBUG_LOCK_ALLOC
100 if (held)
101 btrfs_clear_lock_blocking(held);
102#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500103}
104
Chris Masond352ac62008-09-29 15:18:18 -0400105/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400106void btrfs_free_path(struct btrfs_path *p)
107{
Jesper Juhlff175d52010-12-25 21:22:30 +0000108 if (!p)
109 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200110 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400111 kmem_cache_free(btrfs_path_cachep, p);
112}
113
Chris Masond352ac62008-09-29 15:18:18 -0400114/*
115 * path release drops references on the extent buffers in the path
116 * and it drops any locks held by this path
117 *
118 * It is safe to call this on paths that no locks or extent buffers held.
119 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200120noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500121{
122 int i;
Chris Masona2135012008-06-25 16:01:30 -0400123
Chris Mason234b63a2007-03-13 10:46:10 -0400124 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400125 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500126 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400127 continue;
128 if (p->locks[i]) {
129 btrfs_tree_unlock(p->nodes[i]);
130 p->locks[i] = 0;
131 }
Chris Mason5f39d392007-10-15 16:14:19 -0400132 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400133 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 }
135}
136
Chris Masond352ac62008-09-29 15:18:18 -0400137/*
138 * safely gets a reference on the root node of a tree. A lock
139 * is not taken, so a concurrent writer may put a different node
140 * at the root of the tree. See btrfs_lock_root_node for the
141 * looping required.
142 *
143 * The extent buffer returned by this has a reference taken, so
144 * it won't disappear. It may stop being the root of the tree
145 * at any time because there are no locks held.
146 */
Chris Mason925baed2008-06-25 16:01:30 -0400147struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
148{
149 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400150
151 rcu_read_lock();
152 eb = rcu_dereference(root->node);
Chris Mason925baed2008-06-25 16:01:30 -0400153 extent_buffer_get(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400154 rcu_read_unlock();
Chris Mason925baed2008-06-25 16:01:30 -0400155 return eb;
156}
157
Chris Masond352ac62008-09-29 15:18:18 -0400158/* loop around taking references on and locking the root node of the
159 * tree until you end up with a lock on the root. A locked buffer
160 * is returned, with a reference held.
161 */
Chris Mason925baed2008-06-25 16:01:30 -0400162struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
163{
164 struct extent_buffer *eb;
165
Chris Masond3977122009-01-05 21:25:51 -0500166 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400167 eb = btrfs_root_node(root);
168 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400169 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400170 break;
Chris Mason925baed2008-06-25 16:01:30 -0400171 btrfs_tree_unlock(eb);
172 free_extent_buffer(eb);
173 }
174 return eb;
175}
176
Chris Masond352ac62008-09-29 15:18:18 -0400177/* cowonly root (everything not a reference counted cow subvolume), just get
178 * put onto a simple dirty list. transaction.c walks this to make sure they
179 * get properly updated on disk.
180 */
Chris Mason0b86a832008-03-24 15:01:56 -0400181static void add_root_to_dirty_list(struct btrfs_root *root)
182{
183 if (root->track_dirty && list_empty(&root->dirty_list)) {
184 list_add(&root->dirty_list,
185 &root->fs_info->dirty_cowonly_roots);
186 }
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/*
190 * used by snapshot creation to make a copy of a root for a tree with
191 * a given objectid. The buffer with the new root node is returned in
192 * cow_ret, and this func returns zero on success or a negative error code.
193 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500194int btrfs_copy_root(struct btrfs_trans_handle *trans,
195 struct btrfs_root *root,
196 struct extent_buffer *buf,
197 struct extent_buffer **cow_ret, u64 new_root_objectid)
198{
199 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500200 int ret = 0;
201 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400202 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500203
204 WARN_ON(root->ref_cows && trans->transid !=
205 root->fs_info->running_transaction->transid);
206 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
207
208 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400209 if (level == 0)
210 btrfs_item_key(buf, &disk_key, 0);
211 else
212 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400213
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400214 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
215 new_root_objectid, &disk_key, level,
216 buf->start, 0);
217 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500218 return PTR_ERR(cow);
219
220 copy_extent_buffer(cow, buf, 0, 0, cow->len);
221 btrfs_set_header_bytenr(cow, cow->start);
222 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400223 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
224 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
225 BTRFS_HEADER_FLAG_RELOC);
226 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
227 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
228 else
229 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500230
Yan Zheng2b820322008-11-17 21:11:30 -0500231 write_extent_buffer(cow, root->fs_info->fsid,
232 (unsigned long)btrfs_header_fsid(cow),
233 BTRFS_FSID_SIZE);
234
Chris Masonbe20aa92007-12-17 20:14:01 -0500235 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400236 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
237 ret = btrfs_inc_ref(trans, root, cow, 1);
238 else
239 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500240
Chris Masonbe20aa92007-12-17 20:14:01 -0500241 if (ret)
242 return ret;
243
244 btrfs_mark_buffer_dirty(cow);
245 *cow_ret = cow;
246 return 0;
247}
248
Chris Masond352ac62008-09-29 15:18:18 -0400249/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400250 * check if the tree block can be shared by multiple trees
251 */
252int btrfs_block_can_be_shared(struct btrfs_root *root,
253 struct extent_buffer *buf)
254{
255 /*
256 * Tree blocks not in refernece counted trees and tree roots
257 * are never shared. If a block was allocated after the last
258 * snapshot and the block was not allocated by tree relocation,
259 * we know the block is not shared.
260 */
261 if (root->ref_cows &&
262 buf != root->node && buf != root->commit_root &&
263 (btrfs_header_generation(buf) <=
264 btrfs_root_last_snapshot(&root->root_item) ||
265 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
266 return 1;
267#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
268 if (root->ref_cows &&
269 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
270 return 1;
271#endif
272 return 0;
273}
274
275static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
276 struct btrfs_root *root,
277 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400278 struct extent_buffer *cow,
279 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400280{
281 u64 refs;
282 u64 owner;
283 u64 flags;
284 u64 new_flags = 0;
285 int ret;
286
287 /*
288 * Backrefs update rules:
289 *
290 * Always use full backrefs for extent pointers in tree block
291 * allocated by tree relocation.
292 *
293 * If a shared tree block is no longer referenced by its owner
294 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
295 * use full backrefs for extent pointers in tree block.
296 *
297 * If a tree block is been relocating
298 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
299 * use full backrefs for extent pointers in tree block.
300 * The reason for this is some operations (such as drop tree)
301 * are only allowed for blocks use full backrefs.
302 */
303
304 if (btrfs_block_can_be_shared(root, buf)) {
305 ret = btrfs_lookup_extent_info(trans, root, buf->start,
306 buf->len, &refs, &flags);
307 BUG_ON(ret);
308 BUG_ON(refs == 0);
309 } else {
310 refs = 1;
311 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
312 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
313 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
314 else
315 flags = 0;
316 }
317
318 owner = btrfs_header_owner(buf);
319 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
320 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
321
322 if (refs > 1) {
323 if ((owner == root->root_key.objectid ||
324 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
325 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
326 ret = btrfs_inc_ref(trans, root, buf, 1);
327 BUG_ON(ret);
328
329 if (root->root_key.objectid ==
330 BTRFS_TREE_RELOC_OBJECTID) {
331 ret = btrfs_dec_ref(trans, root, buf, 0);
332 BUG_ON(ret);
333 ret = btrfs_inc_ref(trans, root, cow, 1);
334 BUG_ON(ret);
335 }
336 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
337 } else {
338
339 if (root->root_key.objectid ==
340 BTRFS_TREE_RELOC_OBJECTID)
341 ret = btrfs_inc_ref(trans, root, cow, 1);
342 else
343 ret = btrfs_inc_ref(trans, root, cow, 0);
344 BUG_ON(ret);
345 }
346 if (new_flags != 0) {
347 ret = btrfs_set_disk_extent_flags(trans, root,
348 buf->start,
349 buf->len,
350 new_flags, 0);
351 BUG_ON(ret);
352 }
353 } else {
354 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
355 if (root->root_key.objectid ==
356 BTRFS_TREE_RELOC_OBJECTID)
357 ret = btrfs_inc_ref(trans, root, cow, 1);
358 else
359 ret = btrfs_inc_ref(trans, root, cow, 0);
360 BUG_ON(ret);
361 ret = btrfs_dec_ref(trans, root, buf, 1);
362 BUG_ON(ret);
363 }
364 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400365 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400366 }
367 return 0;
368}
369
370/*
Chris Masond3977122009-01-05 21:25:51 -0500371 * does the dirty work in cow of a single block. The parent block (if
372 * supplied) is updated to point to the new cow copy. The new buffer is marked
373 * dirty and returned locked. If you modify the block it needs to be marked
374 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400375 *
376 * search_start -- an allocation hint for the new block
377 *
Chris Masond3977122009-01-05 21:25:51 -0500378 * empty_size -- a hint that you plan on doing more cow. This is the size in
379 * bytes the allocator should try to find free next to the block it returns.
380 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400381 */
Chris Masond3977122009-01-05 21:25:51 -0500382static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400383 struct btrfs_root *root,
384 struct extent_buffer *buf,
385 struct extent_buffer *parent, int parent_slot,
386 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400387 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400388{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400389 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400390 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500391 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400392 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400393 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400394 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400395
Chris Mason925baed2008-06-25 16:01:30 -0400396 if (*cow_ret == buf)
397 unlock_orig = 1;
398
Chris Masonb9447ef2009-03-09 11:45:38 -0400399 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400400
Chris Mason7bb86312007-12-11 09:25:06 -0500401 WARN_ON(root->ref_cows && trans->transid !=
402 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400403 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400404
Chris Mason7bb86312007-12-11 09:25:06 -0500405 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400406
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400407 if (level == 0)
408 btrfs_item_key(buf, &disk_key, 0);
409 else
410 btrfs_node_key(buf, &disk_key, 0);
411
412 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
413 if (parent)
414 parent_start = parent->start;
415 else
416 parent_start = 0;
417 } else
418 parent_start = 0;
419
420 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
421 root->root_key.objectid, &disk_key,
422 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400423 if (IS_ERR(cow))
424 return PTR_ERR(cow);
425
Chris Masonb4ce94d2009-02-04 09:25:08 -0500426 /* cow is set to blocking by btrfs_init_new_buffer */
427
Chris Mason5f39d392007-10-15 16:14:19 -0400428 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400429 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400430 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400431 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
432 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
433 BTRFS_HEADER_FLAG_RELOC);
434 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
435 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
436 else
437 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400438
Yan Zheng2b820322008-11-17 21:11:30 -0500439 write_extent_buffer(cow, root->fs_info->fsid,
440 (unsigned long)btrfs_header_fsid(cow),
441 BTRFS_FSID_SIZE);
442
Yan, Zhengf0486c62010-05-16 10:46:25 -0400443 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400444
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400445 if (root->ref_cows)
446 btrfs_reloc_cow_block(trans, root, buf, cow);
447
Chris Mason6702ed42007-08-07 16:15:09 -0400448 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400449 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
451 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
452 parent_start = buf->start;
453 else
454 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400455
Chris Mason5f39d392007-10-15 16:14:19 -0400456 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400457 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400458
Yan, Zhengf0486c62010-05-16 10:46:25 -0400459 btrfs_free_tree_block(trans, root, buf, parent_start,
460 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400461 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400462 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400463 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400464 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
465 parent_start = parent->start;
466 else
467 parent_start = 0;
468
469 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400470 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400471 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500472 btrfs_set_node_ptr_generation(parent, parent_slot,
473 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400474 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400475 btrfs_free_tree_block(trans, root, buf, parent_start,
476 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400477 }
Chris Mason925baed2008-06-25 16:01:30 -0400478 if (unlock_orig)
479 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400480 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400481 btrfs_mark_buffer_dirty(cow);
482 *cow_ret = cow;
483 return 0;
484}
485
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400486static inline int should_cow_block(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root,
488 struct extent_buffer *buf)
489{
490 if (btrfs_header_generation(buf) == trans->transid &&
491 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
492 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
493 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
494 return 0;
495 return 1;
496}
497
Chris Masond352ac62008-09-29 15:18:18 -0400498/*
499 * cows a single block, see __btrfs_cow_block for the real work.
500 * This version of it has extra checks so that a block isn't cow'd more than
501 * once per transaction, as long as it hasn't been written yet
502 */
Chris Masond3977122009-01-05 21:25:51 -0500503noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400504 struct btrfs_root *root, struct extent_buffer *buf,
505 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400506 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500507{
Chris Mason6702ed42007-08-07 16:15:09 -0400508 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400509 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500510
Chris Masonccd467d2007-06-28 15:57:36 -0400511 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500512 printk(KERN_CRIT "trans %llu running %llu\n",
513 (unsigned long long)trans->transid,
514 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400515 root->fs_info->running_transaction->transid);
516 WARN_ON(1);
517 }
518 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500519 printk(KERN_CRIT "trans %llu running %llu\n",
520 (unsigned long long)trans->transid,
521 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400522 WARN_ON(1);
523 }
Chris Masondc17ff82008-01-08 15:46:30 -0500524
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400525 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500526 *cow_ret = buf;
527 return 0;
528 }
Chris Masonc4876852009-02-04 09:24:25 -0500529
Chris Mason0b86a832008-03-24 15:01:56 -0400530 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500531
532 if (parent)
533 btrfs_set_lock_blocking(parent);
534 btrfs_set_lock_blocking(buf);
535
Chris Masonf510cfe2007-10-15 16:14:48 -0400536 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400537 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000538
539 trace_btrfs_cow_block(root, buf, *cow_ret);
540
Chris Masonf510cfe2007-10-15 16:14:48 -0400541 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400542}
543
Chris Masond352ac62008-09-29 15:18:18 -0400544/*
545 * helper function for defrag to decide if two blocks pointed to by a
546 * node are actually close by
547 */
Chris Mason6b800532007-10-15 16:17:34 -0400548static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400549{
Chris Mason6b800532007-10-15 16:17:34 -0400550 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400551 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400552 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400553 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500554 return 0;
555}
556
Chris Mason081e9572007-11-06 10:26:24 -0500557/*
558 * compare two keys in a memcmp fashion
559 */
560static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
561{
562 struct btrfs_key k1;
563
564 btrfs_disk_key_to_cpu(&k1, disk);
565
Diego Calleja20736ab2009-07-24 11:06:52 -0400566 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500567}
568
Josef Bacikf3465ca2008-11-12 14:19:50 -0500569/*
570 * same as comp_keys only with two btrfs_key's
571 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400572int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500573{
574 if (k1->objectid > k2->objectid)
575 return 1;
576 if (k1->objectid < k2->objectid)
577 return -1;
578 if (k1->type > k2->type)
579 return 1;
580 if (k1->type < k2->type)
581 return -1;
582 if (k1->offset > k2->offset)
583 return 1;
584 if (k1->offset < k2->offset)
585 return -1;
586 return 0;
587}
Chris Mason081e9572007-11-06 10:26:24 -0500588
Chris Masond352ac62008-09-29 15:18:18 -0400589/*
590 * this is used by the defrag code to go through all the
591 * leaves pointed to by a node and reallocate them so that
592 * disk order is close to key order
593 */
Chris Mason6702ed42007-08-07 16:15:09 -0400594int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400595 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400596 int start_slot, int cache_only, u64 *last_ret,
597 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400598{
Chris Mason6b800532007-10-15 16:17:34 -0400599 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400600 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400601 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400602 u64 search_start = *last_ret;
603 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400604 u64 other;
605 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400606 int end_slot;
607 int i;
608 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400609 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400610 int uptodate;
611 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500612 int progress_passed = 0;
613 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400614
Chris Mason5708b952007-10-25 15:43:18 -0400615 parent_level = btrfs_header_level(parent);
616 if (cache_only && parent_level != 1)
617 return 0;
618
Chris Masond3977122009-01-05 21:25:51 -0500619 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400620 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500621 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400622 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400623
Chris Mason6b800532007-10-15 16:17:34 -0400624 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400625 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400626 end_slot = parent_nritems;
627
628 if (parent_nritems == 1)
629 return 0;
630
Chris Masonb4ce94d2009-02-04 09:25:08 -0500631 btrfs_set_lock_blocking(parent);
632
Chris Mason6702ed42007-08-07 16:15:09 -0400633 for (i = start_slot; i < end_slot; i++) {
634 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400635
Chris Mason5708b952007-10-25 15:43:18 -0400636 if (!parent->map_token) {
637 map_extent_buffer(parent,
638 btrfs_node_key_ptr_offset(i),
639 sizeof(struct btrfs_key_ptr),
640 &parent->map_token, &parent->kaddr,
641 &parent->map_start, &parent->map_len,
642 KM_USER1);
643 }
Chris Mason081e9572007-11-06 10:26:24 -0500644 btrfs_node_key(parent, &disk_key, i);
645 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
646 continue;
647
648 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400649 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400650 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400651 if (last_block == 0)
652 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400653
Chris Mason6702ed42007-08-07 16:15:09 -0400654 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400655 other = btrfs_node_blockptr(parent, i - 1);
656 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400657 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400658 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400659 other = btrfs_node_blockptr(parent, i + 1);
660 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400661 }
Chris Masone9d0b132007-08-10 14:06:19 -0400662 if (close) {
663 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400664 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400665 }
Chris Mason5708b952007-10-25 15:43:18 -0400666 if (parent->map_token) {
667 unmap_extent_buffer(parent, parent->map_token,
668 KM_USER1);
669 parent->map_token = NULL;
670 }
Chris Mason6702ed42007-08-07 16:15:09 -0400671
Chris Mason6b800532007-10-15 16:17:34 -0400672 cur = btrfs_find_tree_block(root, blocknr, blocksize);
673 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400674 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400675 else
676 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400677 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400678 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400679 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400680 continue;
681 }
Chris Mason6b800532007-10-15 16:17:34 -0400682 if (!cur) {
683 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400684 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000685 if (!cur)
686 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400687 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400688 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400689 }
Chris Mason6702ed42007-08-07 16:15:09 -0400690 }
Chris Masone9d0b132007-08-10 14:06:19 -0400691 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400692 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400693
Chris Masone7a84562008-06-25 16:01:31 -0400694 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500695 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400696 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400697 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400698 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400699 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400700 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400701 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400702 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400703 break;
Yan252c38f2007-08-29 09:11:44 -0400704 }
Chris Masone7a84562008-06-25 16:01:31 -0400705 search_start = cur->start;
706 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400707 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400708 btrfs_tree_unlock(cur);
709 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400710 }
Chris Mason5708b952007-10-25 15:43:18 -0400711 if (parent->map_token) {
712 unmap_extent_buffer(parent, parent->map_token,
713 KM_USER1);
714 parent->map_token = NULL;
715 }
Chris Mason6702ed42007-08-07 16:15:09 -0400716 return err;
717}
718
Chris Mason74123bd2007-02-02 11:05:29 -0500719/*
720 * The leaf data grows from end-to-front in the node.
721 * this returns the address of the start of the last item,
722 * which is the stop of the leaf data stack
723 */
Chris Mason123abc82007-03-14 14:14:43 -0400724static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400725 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726{
Chris Mason5f39d392007-10-15 16:14:19 -0400727 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400729 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400730 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500731}
732
Chris Masonaa5d6be2007-02-28 16:35:06 -0500733
Chris Mason74123bd2007-02-02 11:05:29 -0500734/*
Chris Mason5f39d392007-10-15 16:14:19 -0400735 * search for key in the extent_buffer. The items start at offset p,
736 * and they are item_size apart. There are 'max' items in p.
737 *
Chris Mason74123bd2007-02-02 11:05:29 -0500738 * the slot in the array is returned via slot, and it points to
739 * the place where you would insert key if it is not found in
740 * the array.
741 *
742 * slot may point to max if the key is bigger than all of the keys
743 */
Chris Masone02119d2008-09-05 16:13:11 -0400744static noinline int generic_bin_search(struct extent_buffer *eb,
745 unsigned long p,
746 int item_size, struct btrfs_key *key,
747 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500748{
749 int low = 0;
750 int high = max;
751 int mid;
752 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400753 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400754 struct btrfs_disk_key unaligned;
755 unsigned long offset;
756 char *map_token = NULL;
757 char *kaddr = NULL;
758 unsigned long map_start = 0;
759 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400760 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500761
Chris Masond3977122009-01-05 21:25:51 -0500762 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500763 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400764 offset = p + mid * item_size;
765
766 if (!map_token || offset < map_start ||
767 (offset + sizeof(struct btrfs_disk_key)) >
768 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400769 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400770 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400771 map_token = NULL;
772 }
Chris Mason934d3752008-12-08 16:43:10 -0500773
774 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400775 sizeof(struct btrfs_disk_key),
776 &map_token, &kaddr,
777 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400778
Chris Mason479965d2007-10-15 16:14:27 -0400779 if (!err) {
780 tmp = (struct btrfs_disk_key *)(kaddr + offset -
781 map_start);
782 } else {
783 read_extent_buffer(eb, &unaligned,
784 offset, sizeof(unaligned));
785 tmp = &unaligned;
786 }
787
Chris Mason5f39d392007-10-15 16:14:19 -0400788 } else {
789 tmp = (struct btrfs_disk_key *)(kaddr + offset -
790 map_start);
791 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500792 ret = comp_keys(tmp, key);
793
794 if (ret < 0)
795 low = mid + 1;
796 else if (ret > 0)
797 high = mid;
798 else {
799 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400800 if (map_token)
801 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500802 return 0;
803 }
804 }
805 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400806 if (map_token)
807 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500808 return 1;
809}
810
Chris Mason97571fd2007-02-24 13:39:08 -0500811/*
812 * simple bin_search frontend that does the right thing for
813 * leaves vs nodes
814 */
Chris Mason5f39d392007-10-15 16:14:19 -0400815static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
816 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500817{
Chris Mason5f39d392007-10-15 16:14:19 -0400818 if (level == 0) {
819 return generic_bin_search(eb,
820 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400821 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400822 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400823 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500824 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400825 return generic_bin_search(eb,
826 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400827 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400828 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400829 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500830 }
831 return -1;
832}
833
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400834int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
835 int level, int *slot)
836{
837 return bin_search(eb, key, level, slot);
838}
839
Yan, Zhengf0486c62010-05-16 10:46:25 -0400840static void root_add_used(struct btrfs_root *root, u32 size)
841{
842 spin_lock(&root->accounting_lock);
843 btrfs_set_root_used(&root->root_item,
844 btrfs_root_used(&root->root_item) + size);
845 spin_unlock(&root->accounting_lock);
846}
847
848static void root_sub_used(struct btrfs_root *root, u32 size)
849{
850 spin_lock(&root->accounting_lock);
851 btrfs_set_root_used(&root->root_item,
852 btrfs_root_used(&root->root_item) - size);
853 spin_unlock(&root->accounting_lock);
854}
855
Chris Masond352ac62008-09-29 15:18:18 -0400856/* given a node and slot number, this reads the blocks it points to. The
857 * extent buffer is returned with a reference taken (but unlocked).
858 * NULL is returned on error.
859 */
Chris Masone02119d2008-09-05 16:13:11 -0400860static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400861 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500862{
Chris Masonca7a79a2008-05-12 12:59:19 -0400863 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500864 if (slot < 0)
865 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400866 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500867 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400868
869 BUG_ON(level == 0);
870
Chris Masondb945352007-10-15 16:15:53 -0400871 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400872 btrfs_level_size(root, level - 1),
873 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500874}
875
Chris Masond352ac62008-09-29 15:18:18 -0400876/*
877 * node level balancing, used to make sure nodes are in proper order for
878 * item deletion. We balance from the top down, so we have to make sure
879 * that a deletion won't leave an node completely empty later on.
880 */
Chris Masone02119d2008-09-05 16:13:11 -0400881static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500882 struct btrfs_root *root,
883 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500884{
Chris Mason5f39d392007-10-15 16:14:19 -0400885 struct extent_buffer *right = NULL;
886 struct extent_buffer *mid;
887 struct extent_buffer *left = NULL;
888 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500889 int ret = 0;
890 int wret;
891 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500892 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500893 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500894
895 if (level == 0)
896 return 0;
897
Chris Mason5f39d392007-10-15 16:14:19 -0400898 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500899
Chris Mason925baed2008-06-25 16:01:30 -0400900 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500901 WARN_ON(btrfs_header_generation(mid) != trans->transid);
902
Chris Mason1d4f8a02007-03-13 09:28:32 -0400903 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500904
Chris Mason234b63a2007-03-13 10:46:10 -0400905 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400906 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500907 pslot = path->slots[level + 1];
908
Chris Mason40689472007-03-17 14:29:23 -0400909 /*
910 * deal with the case where there is only one pointer in the root
911 * by promoting the node below to a root
912 */
Chris Mason5f39d392007-10-15 16:14:19 -0400913 if (!parent) {
914 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500915
Chris Mason5f39d392007-10-15 16:14:19 -0400916 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500917 return 0;
918
919 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400920 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500921 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400922 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500923 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400924 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400925 if (ret) {
926 btrfs_tree_unlock(child);
927 free_extent_buffer(child);
928 goto enospc;
929 }
Yan2f375ab2008-02-01 14:58:07 -0500930
Chris Mason240f62c2011-03-23 14:54:42 -0400931 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400932
Chris Mason0b86a832008-03-24 15:01:56 -0400933 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400934 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500935
Chris Mason925baed2008-06-25 16:01:30 -0400936 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500937 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400938 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400939 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500940 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400941 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400942
943 root_sub_used(root, mid->len);
944 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500945 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400946 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400947 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500948 }
Chris Mason5f39d392007-10-15 16:14:19 -0400949 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400950 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500951 return 0;
952
Andi Kleen559af822010-10-29 15:14:37 -0400953 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400954
Chris Mason5f39d392007-10-15 16:14:19 -0400955 left = read_node_slot(root, parent, pslot - 1);
956 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400957 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500958 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400959 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400960 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400961 if (wret) {
962 ret = wret;
963 goto enospc;
964 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400965 }
Chris Mason5f39d392007-10-15 16:14:19 -0400966 right = read_node_slot(root, parent, pslot + 1);
967 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400968 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500969 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400970 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400971 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400972 if (wret) {
973 ret = wret;
974 goto enospc;
975 }
976 }
977
978 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400979 if (left) {
980 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400981 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500982 if (wret < 0)
983 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -0400984 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500985 }
Chris Mason79f95c82007-03-01 15:16:26 -0500986
987 /*
988 * then try to empty the right most buffer into the middle
989 */
Chris Mason5f39d392007-10-15 16:14:19 -0400990 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400991 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400992 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500993 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400994 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400995 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400996 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -0400997 wret = del_ptr(trans, root, path, level + 1, pslot +
998 1);
Chris Masonbb803952007-03-01 12:04:21 -0500999 if (wret)
1000 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001001 root_sub_used(root, right->len);
1002 btrfs_free_tree_block(trans, root, right, 0, 1);
1003 free_extent_buffer(right);
1004 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001005 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001006 struct btrfs_disk_key right_key;
1007 btrfs_node_key(right, &right_key, 0);
1008 btrfs_set_node_key(parent, &right_key, pslot + 1);
1009 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001010 }
1011 }
Chris Mason5f39d392007-10-15 16:14:19 -04001012 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001013 /*
1014 * we're not allowed to leave a node with one item in the
1015 * tree during a delete. A deletion from lower in the tree
1016 * could try to delete the only pointer in this node.
1017 * So, pull some keys from the left.
1018 * There has to be a left pointer at this point because
1019 * otherwise we would have pulled some pointers from the
1020 * right
1021 */
Chris Mason5f39d392007-10-15 16:14:19 -04001022 BUG_ON(!left);
1023 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001024 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001025 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001026 goto enospc;
1027 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001028 if (wret == 1) {
1029 wret = push_node_left(trans, root, left, mid, 1);
1030 if (wret < 0)
1031 ret = wret;
1032 }
Chris Mason79f95c82007-03-01 15:16:26 -05001033 BUG_ON(wret == 1);
1034 }
Chris Mason5f39d392007-10-15 16:14:19 -04001035 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001036 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001037 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001038 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001039 if (wret)
1040 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001041 root_sub_used(root, mid->len);
1042 btrfs_free_tree_block(trans, root, mid, 0, 1);
1043 free_extent_buffer(mid);
1044 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001045 } else {
1046 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001047 struct btrfs_disk_key mid_key;
1048 btrfs_node_key(mid, &mid_key, 0);
1049 btrfs_set_node_key(parent, &mid_key, pslot);
1050 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001051 }
Chris Masonbb803952007-03-01 12:04:21 -05001052
Chris Mason79f95c82007-03-01 15:16:26 -05001053 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001054 if (left) {
1055 if (btrfs_header_nritems(left) > orig_slot) {
1056 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001057 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001058 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001059 path->slots[level + 1] -= 1;
1060 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001061 if (mid) {
1062 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001063 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001064 }
Chris Masonbb803952007-03-01 12:04:21 -05001065 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001066 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001067 path->slots[level] = orig_slot;
1068 }
1069 }
Chris Mason79f95c82007-03-01 15:16:26 -05001070 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001071 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001072 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001073 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001074enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001075 if (right) {
1076 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001077 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001078 }
1079 if (left) {
1080 if (path->nodes[level] != left)
1081 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001082 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001083 }
Chris Masonbb803952007-03-01 12:04:21 -05001084 return ret;
1085}
1086
Chris Masond352ac62008-09-29 15:18:18 -04001087/* Node balancing for insertion. Here we only split or push nodes around
1088 * when they are completely full. This is also done top down, so we
1089 * have to be pessimistic.
1090 */
Chris Masond3977122009-01-05 21:25:51 -05001091static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001092 struct btrfs_root *root,
1093 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001094{
Chris Mason5f39d392007-10-15 16:14:19 -04001095 struct extent_buffer *right = NULL;
1096 struct extent_buffer *mid;
1097 struct extent_buffer *left = NULL;
1098 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001099 int ret = 0;
1100 int wret;
1101 int pslot;
1102 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001103
1104 if (level == 0)
1105 return 1;
1106
Chris Mason5f39d392007-10-15 16:14:19 -04001107 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001108 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001109
1110 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001111 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001112 pslot = path->slots[level + 1];
1113
Chris Mason5f39d392007-10-15 16:14:19 -04001114 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001115 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001116
Chris Mason5f39d392007-10-15 16:14:19 -04001117 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001118
1119 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001120 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001121 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001122
1123 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001124 btrfs_set_lock_blocking(left);
1125
Chris Mason5f39d392007-10-15 16:14:19 -04001126 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001127 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1128 wret = 1;
1129 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001130 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001131 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001132 if (ret)
1133 wret = 1;
1134 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001135 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001136 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001137 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001138 }
Chris Masone66f7092007-04-20 13:16:02 -04001139 if (wret < 0)
1140 ret = wret;
1141 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001142 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001143 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001144 btrfs_node_key(mid, &disk_key, 0);
1145 btrfs_set_node_key(parent, &disk_key, pslot);
1146 btrfs_mark_buffer_dirty(parent);
1147 if (btrfs_header_nritems(left) > orig_slot) {
1148 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001149 path->slots[level + 1] -= 1;
1150 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001151 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001152 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001153 } else {
1154 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001155 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001156 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001157 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001158 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001159 }
Chris Masone66f7092007-04-20 13:16:02 -04001160 return 0;
1161 }
Chris Mason925baed2008-06-25 16:01:30 -04001162 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001163 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001164 }
Chris Mason925baed2008-06-25 16:01:30 -04001165 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001166
1167 /*
1168 * then try to empty the right most buffer into the middle
1169 */
Chris Mason5f39d392007-10-15 16:14:19 -04001170 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001171 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001172
Chris Mason925baed2008-06-25 16:01:30 -04001173 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001174 btrfs_set_lock_blocking(right);
1175
Chris Mason5f39d392007-10-15 16:14:19 -04001176 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001177 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1178 wret = 1;
1179 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001180 ret = btrfs_cow_block(trans, root, right,
1181 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001182 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001183 if (ret)
1184 wret = 1;
1185 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001186 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001187 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001188 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001189 }
Chris Masone66f7092007-04-20 13:16:02 -04001190 if (wret < 0)
1191 ret = wret;
1192 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001193 struct btrfs_disk_key disk_key;
1194
1195 btrfs_node_key(right, &disk_key, 0);
1196 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1197 btrfs_mark_buffer_dirty(parent);
1198
1199 if (btrfs_header_nritems(mid) <= orig_slot) {
1200 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001201 path->slots[level + 1] += 1;
1202 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001203 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001204 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001205 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001206 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001207 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001208 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001209 }
Chris Masone66f7092007-04-20 13:16:02 -04001210 return 0;
1211 }
Chris Mason925baed2008-06-25 16:01:30 -04001212 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001213 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001214 }
Chris Masone66f7092007-04-20 13:16:02 -04001215 return 1;
1216}
1217
Chris Mason74123bd2007-02-02 11:05:29 -05001218/*
Chris Masond352ac62008-09-29 15:18:18 -04001219 * readahead one full node of leaves, finding things that are close
1220 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001221 */
Chris Masonc8c42862009-04-03 10:14:18 -04001222static void reada_for_search(struct btrfs_root *root,
1223 struct btrfs_path *path,
1224 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001225{
Chris Mason5f39d392007-10-15 16:14:19 -04001226 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001227 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001228 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001229 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001230 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001231 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001232 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001233 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001234 u32 nr;
1235 u32 blocksize;
1236 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001237
Chris Masona6b6e752007-10-15 16:22:39 -04001238 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001239 return;
1240
Chris Mason6702ed42007-08-07 16:15:09 -04001241 if (!path->nodes[level])
1242 return;
1243
Chris Mason5f39d392007-10-15 16:14:19 -04001244 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001245
Chris Mason3c69fae2007-08-07 15:52:22 -04001246 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001247 blocksize = btrfs_level_size(root, level - 1);
1248 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001249 if (eb) {
1250 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001251 return;
1252 }
1253
Chris Masona7175312009-01-22 09:23:10 -05001254 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001255
Chris Mason5f39d392007-10-15 16:14:19 -04001256 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001257 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001258 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001259 if (direction < 0) {
1260 if (nr == 0)
1261 break;
1262 nr--;
1263 } else if (direction > 0) {
1264 nr++;
1265 if (nr >= nritems)
1266 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001267 }
Chris Mason01f46652007-12-21 16:24:26 -05001268 if (path->reada < 0 && objectid) {
1269 btrfs_node_key(node, &disk_key, nr);
1270 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1271 break;
1272 }
Chris Mason6b800532007-10-15 16:17:34 -04001273 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001274 if ((search <= target && target - search <= 65536) ||
1275 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001276 readahead_tree_block(root, search, blocksize,
1277 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001278 nread += blocksize;
1279 }
1280 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001281 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001282 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001283 }
1284}
Chris Mason925baed2008-06-25 16:01:30 -04001285
Chris Masond352ac62008-09-29 15:18:18 -04001286/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001287 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1288 * cache
1289 */
1290static noinline int reada_for_balance(struct btrfs_root *root,
1291 struct btrfs_path *path, int level)
1292{
1293 int slot;
1294 int nritems;
1295 struct extent_buffer *parent;
1296 struct extent_buffer *eb;
1297 u64 gen;
1298 u64 block1 = 0;
1299 u64 block2 = 0;
1300 int ret = 0;
1301 int blocksize;
1302
Chris Mason8c594ea2009-04-20 15:50:10 -04001303 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001304 if (!parent)
1305 return 0;
1306
1307 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001308 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001309 blocksize = btrfs_level_size(root, level);
1310
1311 if (slot > 0) {
1312 block1 = btrfs_node_blockptr(parent, slot - 1);
1313 gen = btrfs_node_ptr_generation(parent, slot - 1);
1314 eb = btrfs_find_tree_block(root, block1, blocksize);
1315 if (eb && btrfs_buffer_uptodate(eb, gen))
1316 block1 = 0;
1317 free_extent_buffer(eb);
1318 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001319 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001320 block2 = btrfs_node_blockptr(parent, slot + 1);
1321 gen = btrfs_node_ptr_generation(parent, slot + 1);
1322 eb = btrfs_find_tree_block(root, block2, blocksize);
1323 if (eb && btrfs_buffer_uptodate(eb, gen))
1324 block2 = 0;
1325 free_extent_buffer(eb);
1326 }
1327 if (block1 || block2) {
1328 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001329
1330 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001331 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001332
1333 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001334 if (block1)
1335 readahead_tree_block(root, block1, blocksize, 0);
1336 if (block2)
1337 readahead_tree_block(root, block2, blocksize, 0);
1338
1339 if (block1) {
1340 eb = read_tree_block(root, block1, blocksize, 0);
1341 free_extent_buffer(eb);
1342 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001343 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001344 eb = read_tree_block(root, block2, blocksize, 0);
1345 free_extent_buffer(eb);
1346 }
1347 }
1348 return ret;
1349}
1350
1351
1352/*
Chris Masond3977122009-01-05 21:25:51 -05001353 * when we walk down the tree, it is usually safe to unlock the higher layers
1354 * in the tree. The exceptions are when our path goes through slot 0, because
1355 * operations on the tree might require changing key pointers higher up in the
1356 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001357 *
Chris Masond3977122009-01-05 21:25:51 -05001358 * callers might also have set path->keep_locks, which tells this code to keep
1359 * the lock if the path points to the last slot in the block. This is part of
1360 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001361 *
Chris Masond3977122009-01-05 21:25:51 -05001362 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1363 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001364 */
Chris Masone02119d2008-09-05 16:13:11 -04001365static noinline void unlock_up(struct btrfs_path *path, int level,
1366 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001367{
1368 int i;
1369 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001370 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001371 struct extent_buffer *t;
1372
1373 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1374 if (!path->nodes[i])
1375 break;
1376 if (!path->locks[i])
1377 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001378 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001379 skip_level = i + 1;
1380 continue;
1381 }
Chris Mason051e1b92008-06-25 16:01:30 -04001382 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001383 u32 nritems;
1384 t = path->nodes[i];
1385 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001386 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001387 skip_level = i + 1;
1388 continue;
1389 }
1390 }
Chris Mason051e1b92008-06-25 16:01:30 -04001391 if (skip_level < i && i >= lowest_unlock)
1392 no_skips = 1;
1393
Chris Mason925baed2008-06-25 16:01:30 -04001394 t = path->nodes[i];
1395 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1396 btrfs_tree_unlock(t);
1397 path->locks[i] = 0;
1398 }
1399 }
1400}
1401
Chris Mason3c69fae2007-08-07 15:52:22 -04001402/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001403 * This releases any locks held in the path starting at level and
1404 * going all the way up to the root.
1405 *
1406 * btrfs_search_slot will keep the lock held on higher nodes in a few
1407 * corner cases, such as COW of the block at slot zero in the node. This
1408 * ignores those rules, and it should only be called when there are no
1409 * more updates to be done higher up in the tree.
1410 */
1411noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1412{
1413 int i;
1414
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001415 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001416 return;
1417
1418 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1419 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001420 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001421 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001422 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001423 btrfs_tree_unlock(path->nodes[i]);
1424 path->locks[i] = 0;
1425 }
1426}
1427
1428/*
Chris Masonc8c42862009-04-03 10:14:18 -04001429 * helper function for btrfs_search_slot. The goal is to find a block
1430 * in cache without setting the path to blocking. If we find the block
1431 * we return zero and the path is unchanged.
1432 *
1433 * If we can't find the block, we set the path blocking and do some
1434 * reada. -EAGAIN is returned and the search must be repeated.
1435 */
1436static int
1437read_block_for_search(struct btrfs_trans_handle *trans,
1438 struct btrfs_root *root, struct btrfs_path *p,
1439 struct extent_buffer **eb_ret, int level, int slot,
1440 struct btrfs_key *key)
1441{
1442 u64 blocknr;
1443 u64 gen;
1444 u32 blocksize;
1445 struct extent_buffer *b = *eb_ret;
1446 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001447 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001448
1449 blocknr = btrfs_node_blockptr(b, slot);
1450 gen = btrfs_node_ptr_generation(b, slot);
1451 blocksize = btrfs_level_size(root, level - 1);
1452
1453 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001454 if (tmp) {
1455 if (btrfs_buffer_uptodate(tmp, 0)) {
1456 if (btrfs_buffer_uptodate(tmp, gen)) {
1457 /*
1458 * we found an up to date block without
1459 * sleeping, return
1460 * right away
1461 */
1462 *eb_ret = tmp;
1463 return 0;
1464 }
1465 /* the pages were up to date, but we failed
1466 * the generation number check. Do a full
1467 * read for the generation number that is correct.
1468 * We must do this without dropping locks so
1469 * we can trust our generation number
1470 */
1471 free_extent_buffer(tmp);
1472 tmp = read_tree_block(root, blocknr, blocksize, gen);
1473 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1474 *eb_ret = tmp;
1475 return 0;
1476 }
1477 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001478 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001479 return -EIO;
1480 }
Chris Masonc8c42862009-04-03 10:14:18 -04001481 }
1482
1483 /*
1484 * reduce lock contention at high levels
1485 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001486 * we read. Don't release the lock on the current
1487 * level because we need to walk this node to figure
1488 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001489 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001490 btrfs_unlock_up_safe(p, level + 1);
1491 btrfs_set_path_blocking(p);
1492
Chris Masoncb449212010-10-24 11:01:27 -04001493 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001494 if (p->reada)
1495 reada_for_search(root, p, level, slot, key->objectid);
1496
David Sterbab3b4aa72011-04-21 01:20:15 +02001497 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001498
1499 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001500 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001501 if (tmp) {
1502 /*
1503 * If the read above didn't mark this buffer up to date,
1504 * it will never end up being up to date. Set ret to EIO now
1505 * and give up so that our caller doesn't loop forever
1506 * on our EAGAINs.
1507 */
1508 if (!btrfs_buffer_uptodate(tmp, 0))
1509 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001510 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001511 }
1512 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001513}
1514
1515/*
1516 * helper function for btrfs_search_slot. This does all of the checks
1517 * for node-level blocks and does any balancing required based on
1518 * the ins_len.
1519 *
1520 * If no extra work was required, zero is returned. If we had to
1521 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1522 * start over
1523 */
1524static int
1525setup_nodes_for_search(struct btrfs_trans_handle *trans,
1526 struct btrfs_root *root, struct btrfs_path *p,
1527 struct extent_buffer *b, int level, int ins_len)
1528{
1529 int ret;
1530 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1531 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1532 int sret;
1533
1534 sret = reada_for_balance(root, p, level);
1535 if (sret)
1536 goto again;
1537
1538 btrfs_set_path_blocking(p);
1539 sret = split_node(trans, root, p, level);
1540 btrfs_clear_path_blocking(p, NULL);
1541
1542 BUG_ON(sret > 0);
1543 if (sret) {
1544 ret = sret;
1545 goto done;
1546 }
1547 b = p->nodes[level];
1548 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001549 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001550 int sret;
1551
1552 sret = reada_for_balance(root, p, level);
1553 if (sret)
1554 goto again;
1555
1556 btrfs_set_path_blocking(p);
1557 sret = balance_level(trans, root, p, level);
1558 btrfs_clear_path_blocking(p, NULL);
1559
1560 if (sret) {
1561 ret = sret;
1562 goto done;
1563 }
1564 b = p->nodes[level];
1565 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001566 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001567 goto again;
1568 }
1569 BUG_ON(btrfs_header_nritems(b) == 1);
1570 }
1571 return 0;
1572
1573again:
1574 ret = -EAGAIN;
1575done:
1576 return ret;
1577}
1578
1579/*
Chris Mason74123bd2007-02-02 11:05:29 -05001580 * look for key in the tree. path is filled in with nodes along the way
1581 * if key is found, we return zero and you can find the item in the leaf
1582 * level of the path (level 0)
1583 *
1584 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001585 * be inserted, and 1 is returned. If there are other errors during the
1586 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001587 *
1588 * if ins_len > 0, nodes and leaves will be split as we walk down the
1589 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1590 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001591 */
Chris Masone089f052007-03-16 16:20:31 -04001592int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1593 *root, struct btrfs_key *key, struct btrfs_path *p, int
1594 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001595{
Chris Mason5f39d392007-10-15 16:14:19 -04001596 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001597 int slot;
1598 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001599 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001600 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001601 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001602 u8 lowest_level = 0;
1603
Chris Mason6702ed42007-08-07 16:15:09 -04001604 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001605 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001606 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001607
Chris Mason925baed2008-06-25 16:01:30 -04001608 if (ins_len < 0)
1609 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001610
Chris Masonbb803952007-03-01 12:04:21 -05001611again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001612 if (p->search_commit_root) {
1613 b = root->commit_root;
1614 extent_buffer_get(b);
1615 if (!p->skip_locking)
1616 btrfs_tree_lock(b);
1617 } else {
1618 if (p->skip_locking)
1619 b = btrfs_root_node(root);
1620 else
1621 b = btrfs_lock_root_node(root);
1622 }
Chris Mason925baed2008-06-25 16:01:30 -04001623
Chris Masoneb60cea2007-02-02 09:18:22 -05001624 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001625 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001626
1627 /*
1628 * setup the path here so we can release it under lock
1629 * contention with the cow code
1630 */
1631 p->nodes[level] = b;
1632 if (!p->skip_locking)
1633 p->locks[level] = 1;
1634
Chris Mason02217ed2007-03-02 16:08:05 -05001635 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001636 /*
1637 * if we don't really need to cow this block
1638 * then we don't want to set the path blocking,
1639 * so we test it here
1640 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001641 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001642 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001643
Chris Masonb4ce94d2009-02-04 09:25:08 -05001644 btrfs_set_path_blocking(p);
1645
Yan Zheng33c66f42009-07-22 09:59:00 -04001646 err = btrfs_cow_block(trans, root, b,
1647 p->nodes[level + 1],
1648 p->slots[level + 1], &b);
1649 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001650 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001651 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001652 }
Chris Mason02217ed2007-03-02 16:08:05 -05001653 }
Chris Mason65b51a02008-08-01 15:11:20 -04001654cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001655 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001656 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001657 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001658 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001659
Chris Masoneb60cea2007-02-02 09:18:22 -05001660 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001661 if (!p->skip_locking)
1662 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001663
Chris Mason4008c042009-02-12 14:09:45 -05001664 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001665
1666 /*
1667 * we have a lock on b and as long as we aren't changing
1668 * the tree, there is no way to for the items in b to change.
1669 * It is safe to drop the lock on our parent before we
1670 * go through the expensive btree search on b.
1671 *
1672 * If cow is true, then we might be changing slot zero,
1673 * which may require changing the parent. So, we can't
1674 * drop the lock until after we know which slot we're
1675 * operating on.
1676 */
1677 if (!cow)
1678 btrfs_unlock_up_safe(p, level + 1);
1679
Chris Mason5f39d392007-10-15 16:14:19 -04001680 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001681
Chris Mason5f39d392007-10-15 16:14:19 -04001682 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001683 int dec = 0;
1684 if (ret && slot > 0) {
1685 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001686 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001687 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001688 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001689 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001690 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001691 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001692 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001693 if (err) {
1694 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001695 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001696 }
Chris Masonc8c42862009-04-03 10:14:18 -04001697 b = p->nodes[level];
1698 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001699
Chris Masonf9efa9c2008-06-25 16:14:04 -04001700 unlock_up(p, level, lowest_unlock);
1701
Chris Mason925baed2008-06-25 16:01:30 -04001702 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001703 if (dec)
1704 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001705 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001706 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001707
Yan Zheng33c66f42009-07-22 09:59:00 -04001708 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001709 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001710 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001711 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001712 if (err) {
1713 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001714 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001715 }
Chris Mason76a05b32009-05-14 13:24:30 -04001716
Chris Masonb4ce94d2009-02-04 09:25:08 -05001717 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001718 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001719 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001720
Yan Zheng33c66f42009-07-22 09:59:00 -04001721 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001722 btrfs_set_path_blocking(p);
1723 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001724 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001725 }
1726 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001727 } else {
1728 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001729 if (ins_len > 0 &&
1730 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001731 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001732 err = split_leaf(trans, root, key,
1733 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001734 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001735
Yan Zheng33c66f42009-07-22 09:59:00 -04001736 BUG_ON(err > 0);
1737 if (err) {
1738 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001739 goto done;
1740 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001741 }
Chris Mason459931e2008-12-10 09:10:46 -05001742 if (!p->search_for_split)
1743 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001744 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001745 }
1746 }
Chris Mason65b51a02008-08-01 15:11:20 -04001747 ret = 1;
1748done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001749 /*
1750 * we don't really know what they plan on doing with the path
1751 * from here on, so for now just mark it as blocking
1752 */
Chris Masonb9473432009-03-13 11:00:37 -04001753 if (!p->leave_spinning)
1754 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001755 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001756 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001757 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001758}
1759
Chris Mason74123bd2007-02-02 11:05:29 -05001760/*
1761 * adjust the pointers going up the tree, starting at level
1762 * making sure the right key of each node is points to 'key'.
1763 * This is used after shifting pointers to the left, so it stops
1764 * fixing up pointers when a given leaf/node is not in slot 0 of the
1765 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001766 *
1767 * If this fails to write a tree block, it returns -1, but continues
1768 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001769 */
Chris Mason5f39d392007-10-15 16:14:19 -04001770static int fixup_low_keys(struct btrfs_trans_handle *trans,
1771 struct btrfs_root *root, struct btrfs_path *path,
1772 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773{
1774 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001775 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001776 struct extent_buffer *t;
1777
Chris Mason234b63a2007-03-13 10:46:10 -04001778 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001779 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001780 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001781 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001782 t = path->nodes[i];
1783 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001784 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001785 if (tslot != 0)
1786 break;
1787 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001788 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001789}
1790
Chris Mason74123bd2007-02-02 11:05:29 -05001791/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001792 * update item key.
1793 *
1794 * This function isn't completely safe. It's the caller's responsibility
1795 * that the new key won't break the order
1796 */
1797int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1798 struct btrfs_root *root, struct btrfs_path *path,
1799 struct btrfs_key *new_key)
1800{
1801 struct btrfs_disk_key disk_key;
1802 struct extent_buffer *eb;
1803 int slot;
1804
1805 eb = path->nodes[0];
1806 slot = path->slots[0];
1807 if (slot > 0) {
1808 btrfs_item_key(eb, &disk_key, slot - 1);
1809 if (comp_keys(&disk_key, new_key) >= 0)
1810 return -1;
1811 }
1812 if (slot < btrfs_header_nritems(eb) - 1) {
1813 btrfs_item_key(eb, &disk_key, slot + 1);
1814 if (comp_keys(&disk_key, new_key) <= 0)
1815 return -1;
1816 }
1817
1818 btrfs_cpu_key_to_disk(&disk_key, new_key);
1819 btrfs_set_item_key(eb, &disk_key, slot);
1820 btrfs_mark_buffer_dirty(eb);
1821 if (slot == 0)
1822 fixup_low_keys(trans, root, path, &disk_key, 1);
1823 return 0;
1824}
1825
1826/*
Chris Mason74123bd2007-02-02 11:05:29 -05001827 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001828 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001829 *
1830 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1831 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001832 */
Chris Mason98ed5172008-01-03 10:01:48 -05001833static int push_node_left(struct btrfs_trans_handle *trans,
1834 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001835 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001836{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001837 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001838 int src_nritems;
1839 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001840 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001841
Chris Mason5f39d392007-10-15 16:14:19 -04001842 src_nritems = btrfs_header_nritems(src);
1843 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001844 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001845 WARN_ON(btrfs_header_generation(src) != trans->transid);
1846 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001847
Chris Masonbce4eae2008-04-24 14:42:46 -04001848 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001849 return 1;
1850
Chris Masond3977122009-01-05 21:25:51 -05001851 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001852 return 1;
1853
Chris Masonbce4eae2008-04-24 14:42:46 -04001854 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001855 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001856 if (push_items < src_nritems) {
1857 /* leave at least 8 pointers in the node if
1858 * we aren't going to empty it
1859 */
1860 if (src_nritems - push_items < 8) {
1861 if (push_items <= 8)
1862 return 1;
1863 push_items -= 8;
1864 }
1865 }
1866 } else
1867 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001868
Chris Mason5f39d392007-10-15 16:14:19 -04001869 copy_extent_buffer(dst, src,
1870 btrfs_node_key_ptr_offset(dst_nritems),
1871 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001872 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001873
Chris Masonbb803952007-03-01 12:04:21 -05001874 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001875 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1876 btrfs_node_key_ptr_offset(push_items),
1877 (src_nritems - push_items) *
1878 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001879 }
Chris Mason5f39d392007-10-15 16:14:19 -04001880 btrfs_set_header_nritems(src, src_nritems - push_items);
1881 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1882 btrfs_mark_buffer_dirty(src);
1883 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001884
Chris Masonbb803952007-03-01 12:04:21 -05001885 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001886}
1887
Chris Mason97571fd2007-02-24 13:39:08 -05001888/*
Chris Mason79f95c82007-03-01 15:16:26 -05001889 * try to push data from one node into the next node right in the
1890 * tree.
1891 *
1892 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1893 * error, and > 0 if there was no room in the right hand block.
1894 *
1895 * this will only push up to 1/2 the contents of the left node over
1896 */
Chris Mason5f39d392007-10-15 16:14:19 -04001897static int balance_node_right(struct btrfs_trans_handle *trans,
1898 struct btrfs_root *root,
1899 struct extent_buffer *dst,
1900 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001901{
Chris Mason79f95c82007-03-01 15:16:26 -05001902 int push_items = 0;
1903 int max_push;
1904 int src_nritems;
1905 int dst_nritems;
1906 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001907
Chris Mason7bb86312007-12-11 09:25:06 -05001908 WARN_ON(btrfs_header_generation(src) != trans->transid);
1909 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1910
Chris Mason5f39d392007-10-15 16:14:19 -04001911 src_nritems = btrfs_header_nritems(src);
1912 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001913 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001914 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001915 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001916
Chris Masond3977122009-01-05 21:25:51 -05001917 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001918 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001919
1920 max_push = src_nritems / 2 + 1;
1921 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001922 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001923 return 1;
Yan252c38f2007-08-29 09:11:44 -04001924
Chris Mason79f95c82007-03-01 15:16:26 -05001925 if (max_push < push_items)
1926 push_items = max_push;
1927
Chris Mason5f39d392007-10-15 16:14:19 -04001928 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1929 btrfs_node_key_ptr_offset(0),
1930 (dst_nritems) *
1931 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001932
Chris Mason5f39d392007-10-15 16:14:19 -04001933 copy_extent_buffer(dst, src,
1934 btrfs_node_key_ptr_offset(0),
1935 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001936 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001937
Chris Mason5f39d392007-10-15 16:14:19 -04001938 btrfs_set_header_nritems(src, src_nritems - push_items);
1939 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001940
Chris Mason5f39d392007-10-15 16:14:19 -04001941 btrfs_mark_buffer_dirty(src);
1942 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001943
Chris Mason79f95c82007-03-01 15:16:26 -05001944 return ret;
1945}
1946
1947/*
Chris Mason97571fd2007-02-24 13:39:08 -05001948 * helper function to insert a new root level in the tree.
1949 * A new node is allocated, and a single item is inserted to
1950 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001951 *
1952 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001953 */
Chris Masond3977122009-01-05 21:25:51 -05001954static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001955 struct btrfs_root *root,
1956 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001957{
Chris Mason7bb86312007-12-11 09:25:06 -05001958 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001959 struct extent_buffer *lower;
1960 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001961 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001962 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001963
1964 BUG_ON(path->nodes[level]);
1965 BUG_ON(path->nodes[level-1] != root->node);
1966
Chris Mason7bb86312007-12-11 09:25:06 -05001967 lower = path->nodes[level-1];
1968 if (level == 1)
1969 btrfs_item_key(lower, &lower_key, 0);
1970 else
1971 btrfs_node_key(lower, &lower_key, 0);
1972
Zheng Yan31840ae2008-09-23 13:14:14 -04001973 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001974 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001975 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001976 if (IS_ERR(c))
1977 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001978
Yan, Zhengf0486c62010-05-16 10:46:25 -04001979 root_add_used(root, root->nodesize);
1980
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001981 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04001982 btrfs_set_header_nritems(c, 1);
1983 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001984 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001985 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001986 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04001987 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001988
Chris Mason5f39d392007-10-15 16:14:19 -04001989 write_extent_buffer(c, root->fs_info->fsid,
1990 (unsigned long)btrfs_header_fsid(c),
1991 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001992
1993 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1994 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1995 BTRFS_UUID_SIZE);
1996
Chris Mason5f39d392007-10-15 16:14:19 -04001997 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001998 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001999 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002000 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002001
2002 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002003
2004 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002005
Chris Mason925baed2008-06-25 16:01:30 -04002006 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002007 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002008
2009 /* the super has an extra ref to root->node */
2010 free_extent_buffer(old);
2011
Chris Mason0b86a832008-03-24 15:01:56 -04002012 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002013 extent_buffer_get(c);
2014 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002015 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002016 path->slots[level] = 0;
2017 return 0;
2018}
2019
Chris Mason74123bd2007-02-02 11:05:29 -05002020/*
2021 * worker function to insert a single pointer in a node.
2022 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002023 *
Chris Mason74123bd2007-02-02 11:05:29 -05002024 * slot and level indicate where you want the key to go, and
2025 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002026 *
2027 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002028 */
Chris Masone089f052007-03-16 16:20:31 -04002029static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2030 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002031 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002032{
Chris Mason5f39d392007-10-15 16:14:19 -04002033 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002034 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002035
2036 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002037 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002038 lower = path->nodes[level];
2039 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002040 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002041 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002042 BUG();
2043 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002044 memmove_extent_buffer(lower,
2045 btrfs_node_key_ptr_offset(slot + 1),
2046 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002047 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002048 }
Chris Mason5f39d392007-10-15 16:14:19 -04002049 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002050 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002051 WARN_ON(trans->transid == 0);
2052 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002053 btrfs_set_header_nritems(lower, nritems + 1);
2054 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002055 return 0;
2056}
2057
Chris Mason97571fd2007-02-24 13:39:08 -05002058/*
2059 * split the node at the specified level in path in two.
2060 * The path is corrected to point to the appropriate node after the split
2061 *
2062 * Before splitting this tries to make some room in the node by pushing
2063 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002064 *
2065 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002066 */
Chris Masone02119d2008-09-05 16:13:11 -04002067static noinline int split_node(struct btrfs_trans_handle *trans,
2068 struct btrfs_root *root,
2069 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002070{
Chris Mason5f39d392007-10-15 16:14:19 -04002071 struct extent_buffer *c;
2072 struct extent_buffer *split;
2073 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002074 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002075 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002076 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002077 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002078
Chris Mason5f39d392007-10-15 16:14:19 -04002079 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002080 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002081 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002082 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002083 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002084 if (ret)
2085 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002086 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002087 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002088 c = path->nodes[level];
2089 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002090 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002091 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002092 if (ret < 0)
2093 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002094 }
Chris Masone66f7092007-04-20 13:16:02 -04002095
Chris Mason5f39d392007-10-15 16:14:19 -04002096 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 mid = (c_nritems + 1) / 2;
2098 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002099
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002101 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002102 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002103 if (IS_ERR(split))
2104 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002105
Yan, Zhengf0486c62010-05-16 10:46:25 -04002106 root_add_used(root, root->nodesize);
2107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002108 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002109 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002110 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002111 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002112 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002113 btrfs_set_header_owner(split, root->root_key.objectid);
2114 write_extent_buffer(split, root->fs_info->fsid,
2115 (unsigned long)btrfs_header_fsid(split),
2116 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002117 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2118 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2119 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002120
Chris Mason5f39d392007-10-15 16:14:19 -04002121
2122 copy_extent_buffer(split, c,
2123 btrfs_node_key_ptr_offset(0),
2124 btrfs_node_key_ptr_offset(mid),
2125 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2126 btrfs_set_header_nritems(split, c_nritems - mid);
2127 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002128 ret = 0;
2129
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_mark_buffer_dirty(c);
2131 btrfs_mark_buffer_dirty(split);
2132
Chris Masondb945352007-10-15 16:15:53 -04002133 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002134 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002135 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002136 if (wret)
2137 ret = wret;
2138
Chris Mason5de08d72007-02-24 06:24:44 -05002139 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002140 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002141 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 free_extent_buffer(c);
2143 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002144 path->slots[level + 1] += 1;
2145 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002146 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002147 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002148 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002149 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002150}
2151
Chris Mason74123bd2007-02-02 11:05:29 -05002152/*
2153 * how many bytes are required to store the items in a leaf. start
2154 * and nr indicate which items in the leaf to check. This totals up the
2155 * space used both by the item structs and the item data
2156 */
Chris Mason5f39d392007-10-15 16:14:19 -04002157static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002158{
2159 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002160 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002161 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002162
2163 if (!nr)
2164 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002165 data_len = btrfs_item_end_nr(l, start);
2166 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002167 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002168 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169 return data_len;
2170}
2171
Chris Mason74123bd2007-02-02 11:05:29 -05002172/*
Chris Masond4dbff92007-04-04 14:08:15 -04002173 * The space between the end of the leaf items and
2174 * the start of the leaf data. IOW, how much room
2175 * the leaf has left for both items and data
2176 */
Chris Masond3977122009-01-05 21:25:51 -05002177noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002178 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002179{
Chris Mason5f39d392007-10-15 16:14:19 -04002180 int nritems = btrfs_header_nritems(leaf);
2181 int ret;
2182 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2183 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002184 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2185 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002186 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002187 leaf_space_used(leaf, 0, nritems), nritems);
2188 }
2189 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002190}
2191
Chris Mason99d8f832010-07-07 10:51:48 -04002192/*
2193 * min slot controls the lowest index we're willing to push to the
2194 * right. We'll push up to and including min_slot, but no lower
2195 */
Chris Mason44871b12009-03-13 10:04:31 -04002196static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2197 struct btrfs_root *root,
2198 struct btrfs_path *path,
2199 int data_size, int empty,
2200 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002201 int free_space, u32 left_nritems,
2202 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002203{
Chris Mason5f39d392007-10-15 16:14:19 -04002204 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002205 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002206 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002207 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002208 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002209 int push_space = 0;
2210 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002211 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002212 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002213 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002214 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002215 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002216
Chris Mason34a38212007-11-07 13:31:03 -05002217 if (empty)
2218 nr = 0;
2219 else
Chris Mason99d8f832010-07-07 10:51:48 -04002220 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002221
Zheng Yan31840ae2008-09-23 13:14:14 -04002222 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002223 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002224
Chris Mason44871b12009-03-13 10:04:31 -04002225 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002226 i = left_nritems - 1;
2227 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002228 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002229
Zheng Yan31840ae2008-09-23 13:14:14 -04002230 if (!empty && push_items > 0) {
2231 if (path->slots[0] > i)
2232 break;
2233 if (path->slots[0] == i) {
2234 int space = btrfs_leaf_free_space(root, left);
2235 if (space + push_space * 2 > free_space)
2236 break;
2237 }
2238 }
2239
Chris Mason00ec4c52007-02-24 12:47:20 -05002240 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002241 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002242
2243 if (!left->map_token) {
2244 map_extent_buffer(left, (unsigned long)item,
2245 sizeof(struct btrfs_item),
2246 &left->map_token, &left->kaddr,
2247 &left->map_start, &left->map_len,
2248 KM_USER1);
2249 }
2250
2251 this_item_size = btrfs_item_size(left, item);
2252 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002253 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002254
Chris Mason00ec4c52007-02-24 12:47:20 -05002255 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002256 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002257 if (i == 0)
2258 break;
2259 i--;
Chris Masondb945352007-10-15 16:15:53 -04002260 }
2261 if (left->map_token) {
2262 unmap_extent_buffer(left, left->map_token, KM_USER1);
2263 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002264 }
Chris Mason5f39d392007-10-15 16:14:19 -04002265
Chris Mason925baed2008-06-25 16:01:30 -04002266 if (push_items == 0)
2267 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002268
Chris Mason34a38212007-11-07 13:31:03 -05002269 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002270 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002271
Chris Mason00ec4c52007-02-24 12:47:20 -05002272 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002273 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002274
Chris Mason5f39d392007-10-15 16:14:19 -04002275 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002276 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002277
Chris Mason00ec4c52007-02-24 12:47:20 -05002278 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002279 data_end = leaf_data_end(root, right);
2280 memmove_extent_buffer(right,
2281 btrfs_leaf_data(right) + data_end - push_space,
2282 btrfs_leaf_data(right) + data_end,
2283 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2284
Chris Mason00ec4c52007-02-24 12:47:20 -05002285 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002286 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002287 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2288 btrfs_leaf_data(left) + leaf_data_end(root, left),
2289 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002290
2291 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2292 btrfs_item_nr_offset(0),
2293 right_nritems * sizeof(struct btrfs_item));
2294
Chris Mason00ec4c52007-02-24 12:47:20 -05002295 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002296 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2297 btrfs_item_nr_offset(left_nritems - push_items),
2298 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002299
2300 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002301 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002302 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002303 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002304 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002305 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002306 if (!right->map_token) {
2307 map_extent_buffer(right, (unsigned long)item,
2308 sizeof(struct btrfs_item),
2309 &right->map_token, &right->kaddr,
2310 &right->map_start, &right->map_len,
2311 KM_USER1);
2312 }
2313 push_space -= btrfs_item_size(right, item);
2314 btrfs_set_item_offset(right, item, push_space);
2315 }
2316
2317 if (right->map_token) {
2318 unmap_extent_buffer(right, right->map_token, KM_USER1);
2319 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002320 }
Chris Mason7518a232007-03-12 12:01:18 -04002321 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002322 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002323
Chris Mason34a38212007-11-07 13:31:03 -05002324 if (left_nritems)
2325 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002326 else
2327 clean_tree_block(trans, root, left);
2328
Chris Mason5f39d392007-10-15 16:14:19 -04002329 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002330
Chris Mason5f39d392007-10-15 16:14:19 -04002331 btrfs_item_key(right, &disk_key, 0);
2332 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002333 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002334
Chris Mason00ec4c52007-02-24 12:47:20 -05002335 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002336 if (path->slots[0] >= left_nritems) {
2337 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002338 if (btrfs_header_nritems(path->nodes[0]) == 0)
2339 clean_tree_block(trans, root, path->nodes[0]);
2340 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002341 free_extent_buffer(path->nodes[0]);
2342 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002343 path->slots[1] += 1;
2344 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002345 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002346 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002347 }
2348 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002349
2350out_unlock:
2351 btrfs_tree_unlock(right);
2352 free_extent_buffer(right);
2353 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002354}
Chris Mason925baed2008-06-25 16:01:30 -04002355
Chris Mason00ec4c52007-02-24 12:47:20 -05002356/*
Chris Mason44871b12009-03-13 10:04:31 -04002357 * push some data in the path leaf to the right, trying to free up at
2358 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2359 *
2360 * returns 1 if the push failed because the other node didn't have enough
2361 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002362 *
2363 * this will push starting from min_slot to the end of the leaf. It won't
2364 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002365 */
2366static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002367 *root, struct btrfs_path *path,
2368 int min_data_size, int data_size,
2369 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002370{
2371 struct extent_buffer *left = path->nodes[0];
2372 struct extent_buffer *right;
2373 struct extent_buffer *upper;
2374 int slot;
2375 int free_space;
2376 u32 left_nritems;
2377 int ret;
2378
2379 if (!path->nodes[1])
2380 return 1;
2381
2382 slot = path->slots[1];
2383 upper = path->nodes[1];
2384 if (slot >= btrfs_header_nritems(upper) - 1)
2385 return 1;
2386
2387 btrfs_assert_tree_locked(path->nodes[1]);
2388
2389 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002390 if (right == NULL)
2391 return 1;
2392
Chris Mason44871b12009-03-13 10:04:31 -04002393 btrfs_tree_lock(right);
2394 btrfs_set_lock_blocking(right);
2395
2396 free_space = btrfs_leaf_free_space(root, right);
2397 if (free_space < data_size)
2398 goto out_unlock;
2399
2400 /* cow and double check */
2401 ret = btrfs_cow_block(trans, root, right, upper,
2402 slot + 1, &right);
2403 if (ret)
2404 goto out_unlock;
2405
2406 free_space = btrfs_leaf_free_space(root, right);
2407 if (free_space < data_size)
2408 goto out_unlock;
2409
2410 left_nritems = btrfs_header_nritems(left);
2411 if (left_nritems == 0)
2412 goto out_unlock;
2413
Chris Mason99d8f832010-07-07 10:51:48 -04002414 return __push_leaf_right(trans, root, path, min_data_size, empty,
2415 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002416out_unlock:
2417 btrfs_tree_unlock(right);
2418 free_extent_buffer(right);
2419 return 1;
2420}
2421
2422/*
Chris Mason74123bd2007-02-02 11:05:29 -05002423 * push some data in the path leaf to the left, trying to free up at
2424 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002425 *
2426 * max_slot can put a limit on how far into the leaf we'll push items. The
2427 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2428 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002429 */
Chris Mason44871b12009-03-13 10:04:31 -04002430static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2431 struct btrfs_root *root,
2432 struct btrfs_path *path, int data_size,
2433 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002434 int free_space, u32 right_nritems,
2435 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002436{
Chris Mason5f39d392007-10-15 16:14:19 -04002437 struct btrfs_disk_key disk_key;
2438 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002439 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002440 int push_space = 0;
2441 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002442 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002443 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002444 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002445 int ret = 0;
2446 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002447 u32 this_item_size;
2448 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002449
Chris Mason34a38212007-11-07 13:31:03 -05002450 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002451 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002452 else
Chris Mason99d8f832010-07-07 10:51:48 -04002453 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002454
2455 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002456 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002457 if (!right->map_token) {
2458 map_extent_buffer(right, (unsigned long)item,
2459 sizeof(struct btrfs_item),
2460 &right->map_token, &right->kaddr,
2461 &right->map_start, &right->map_len,
2462 KM_USER1);
2463 }
2464
Zheng Yan31840ae2008-09-23 13:14:14 -04002465 if (!empty && push_items > 0) {
2466 if (path->slots[0] < i)
2467 break;
2468 if (path->slots[0] == i) {
2469 int space = btrfs_leaf_free_space(root, right);
2470 if (space + push_space * 2 > free_space)
2471 break;
2472 }
2473 }
2474
Chris Masonbe0e5c02007-01-26 15:51:26 -05002475 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002476 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002477
2478 this_item_size = btrfs_item_size(right, item);
2479 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002480 break;
Chris Masondb945352007-10-15 16:15:53 -04002481
Chris Masonbe0e5c02007-01-26 15:51:26 -05002482 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002483 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002484 }
Chris Masondb945352007-10-15 16:15:53 -04002485
2486 if (right->map_token) {
2487 unmap_extent_buffer(right, right->map_token, KM_USER1);
2488 right->map_token = NULL;
2489 }
2490
Chris Masonbe0e5c02007-01-26 15:51:26 -05002491 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002492 ret = 1;
2493 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002494 }
Chris Mason34a38212007-11-07 13:31:03 -05002495 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002496 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002497
Chris Masonbe0e5c02007-01-26 15:51:26 -05002498 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002499 copy_extent_buffer(left, right,
2500 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2501 btrfs_item_nr_offset(0),
2502 push_items * sizeof(struct btrfs_item));
2503
Chris Mason123abc82007-03-14 14:14:43 -04002504 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002505 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002506
2507 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002508 leaf_data_end(root, left) - push_space,
2509 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002510 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002511 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002512 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002513 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002514
Chris Masondb945352007-10-15 16:15:53 -04002515 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002516 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002517 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002518
Chris Mason5f39d392007-10-15 16:14:19 -04002519 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002520 if (!left->map_token) {
2521 map_extent_buffer(left, (unsigned long)item,
2522 sizeof(struct btrfs_item),
2523 &left->map_token, &left->kaddr,
2524 &left->map_start, &left->map_len,
2525 KM_USER1);
2526 }
2527
Chris Mason5f39d392007-10-15 16:14:19 -04002528 ioff = btrfs_item_offset(left, item);
2529 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002530 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002531 }
Chris Mason5f39d392007-10-15 16:14:19 -04002532 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002533 if (left->map_token) {
2534 unmap_extent_buffer(left, left->map_token, KM_USER1);
2535 left->map_token = NULL;
2536 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002537
2538 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002539 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002540 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2541 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002542 WARN_ON(1);
2543 }
Chris Mason5f39d392007-10-15 16:14:19 -04002544
Chris Mason34a38212007-11-07 13:31:03 -05002545 if (push_items < right_nritems) {
2546 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2547 leaf_data_end(root, right);
2548 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2549 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2550 btrfs_leaf_data(right) +
2551 leaf_data_end(root, right), push_space);
2552
2553 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002554 btrfs_item_nr_offset(push_items),
2555 (btrfs_header_nritems(right) - push_items) *
2556 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002557 }
Yaneef1c492007-11-26 10:58:13 -05002558 right_nritems -= push_items;
2559 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002560 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002561 for (i = 0; i < right_nritems; i++) {
2562 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002563
2564 if (!right->map_token) {
2565 map_extent_buffer(right, (unsigned long)item,
2566 sizeof(struct btrfs_item),
2567 &right->map_token, &right->kaddr,
2568 &right->map_start, &right->map_len,
2569 KM_USER1);
2570 }
2571
2572 push_space = push_space - btrfs_item_size(right, item);
2573 btrfs_set_item_offset(right, item, push_space);
2574 }
2575 if (right->map_token) {
2576 unmap_extent_buffer(right, right->map_token, KM_USER1);
2577 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002578 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002579
Chris Mason5f39d392007-10-15 16:14:19 -04002580 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002581 if (right_nritems)
2582 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002583 else
2584 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002585
Chris Mason5f39d392007-10-15 16:14:19 -04002586 btrfs_item_key(right, &disk_key, 0);
2587 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002588 if (wret)
2589 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002590
2591 /* then fixup the leaf pointer in the path */
2592 if (path->slots[0] < push_items) {
2593 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002594 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002595 free_extent_buffer(path->nodes[0]);
2596 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002597 path->slots[1] -= 1;
2598 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002599 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002600 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002601 path->slots[0] -= push_items;
2602 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002603 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002604 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002605out:
2606 btrfs_tree_unlock(left);
2607 free_extent_buffer(left);
2608 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609}
2610
Chris Mason74123bd2007-02-02 11:05:29 -05002611/*
Chris Mason44871b12009-03-13 10:04:31 -04002612 * push some data in the path leaf to the left, trying to free up at
2613 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002614 *
2615 * max_slot can put a limit on how far into the leaf we'll push items. The
2616 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2617 * items
Chris Mason44871b12009-03-13 10:04:31 -04002618 */
2619static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002620 *root, struct btrfs_path *path, int min_data_size,
2621 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002622{
2623 struct extent_buffer *right = path->nodes[0];
2624 struct extent_buffer *left;
2625 int slot;
2626 int free_space;
2627 u32 right_nritems;
2628 int ret = 0;
2629
2630 slot = path->slots[1];
2631 if (slot == 0)
2632 return 1;
2633 if (!path->nodes[1])
2634 return 1;
2635
2636 right_nritems = btrfs_header_nritems(right);
2637 if (right_nritems == 0)
2638 return 1;
2639
2640 btrfs_assert_tree_locked(path->nodes[1]);
2641
2642 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002643 if (left == NULL)
2644 return 1;
2645
Chris Mason44871b12009-03-13 10:04:31 -04002646 btrfs_tree_lock(left);
2647 btrfs_set_lock_blocking(left);
2648
2649 free_space = btrfs_leaf_free_space(root, left);
2650 if (free_space < data_size) {
2651 ret = 1;
2652 goto out;
2653 }
2654
2655 /* cow and double check */
2656 ret = btrfs_cow_block(trans, root, left,
2657 path->nodes[1], slot - 1, &left);
2658 if (ret) {
2659 /* we hit -ENOSPC, but it isn't fatal here */
2660 ret = 1;
2661 goto out;
2662 }
2663
2664 free_space = btrfs_leaf_free_space(root, left);
2665 if (free_space < data_size) {
2666 ret = 1;
2667 goto out;
2668 }
2669
Chris Mason99d8f832010-07-07 10:51:48 -04002670 return __push_leaf_left(trans, root, path, min_data_size,
2671 empty, left, free_space, right_nritems,
2672 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002673out:
2674 btrfs_tree_unlock(left);
2675 free_extent_buffer(left);
2676 return ret;
2677}
2678
2679/*
Chris Mason74123bd2007-02-02 11:05:29 -05002680 * split the path's leaf in two, making sure there is at least data_size
2681 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002682 *
2683 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002684 */
Chris Mason44871b12009-03-13 10:04:31 -04002685static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002686 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002687 struct btrfs_path *path,
2688 struct extent_buffer *l,
2689 struct extent_buffer *right,
2690 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002691{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002692 int data_copy_size;
2693 int rt_data_off;
2694 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002695 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002696 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002697 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002698
Chris Mason5f39d392007-10-15 16:14:19 -04002699 nritems = nritems - mid;
2700 btrfs_set_header_nritems(right, nritems);
2701 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2702
2703 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2704 btrfs_item_nr_offset(mid),
2705 nritems * sizeof(struct btrfs_item));
2706
2707 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002708 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2709 data_copy_size, btrfs_leaf_data(l) +
2710 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002711
Chris Mason5f39d392007-10-15 16:14:19 -04002712 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2713 btrfs_item_end_nr(l, mid);
2714
2715 for (i = 0; i < nritems; i++) {
2716 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002717 u32 ioff;
2718
2719 if (!right->map_token) {
2720 map_extent_buffer(right, (unsigned long)item,
2721 sizeof(struct btrfs_item),
2722 &right->map_token, &right->kaddr,
2723 &right->map_start, &right->map_len,
2724 KM_USER1);
2725 }
2726
2727 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002728 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002729 }
Chris Mason74123bd2007-02-02 11:05:29 -05002730
Chris Masondb945352007-10-15 16:15:53 -04002731 if (right->map_token) {
2732 unmap_extent_buffer(right, right->map_token, KM_USER1);
2733 right->map_token = NULL;
2734 }
2735
Chris Mason5f39d392007-10-15 16:14:19 -04002736 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002737 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002738 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002739 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2740 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002741 if (wret)
2742 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002743
2744 btrfs_mark_buffer_dirty(right);
2745 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002746 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002747
Chris Masonbe0e5c02007-01-26 15:51:26 -05002748 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002749 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002750 free_extent_buffer(path->nodes[0]);
2751 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002752 path->slots[0] -= mid;
2753 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002754 } else {
2755 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002756 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002757 }
Chris Mason5f39d392007-10-15 16:14:19 -04002758
Chris Masoneb60cea2007-02-02 09:18:22 -05002759 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002760
Chris Mason44871b12009-03-13 10:04:31 -04002761 return ret;
2762}
2763
2764/*
Chris Mason99d8f832010-07-07 10:51:48 -04002765 * double splits happen when we need to insert a big item in the middle
2766 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2767 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2768 * A B C
2769 *
2770 * We avoid this by trying to push the items on either side of our target
2771 * into the adjacent leaves. If all goes well we can avoid the double split
2772 * completely.
2773 */
2774static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2775 struct btrfs_root *root,
2776 struct btrfs_path *path,
2777 int data_size)
2778{
2779 int ret;
2780 int progress = 0;
2781 int slot;
2782 u32 nritems;
2783
2784 slot = path->slots[0];
2785
2786 /*
2787 * try to push all the items after our slot into the
2788 * right leaf
2789 */
2790 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2791 if (ret < 0)
2792 return ret;
2793
2794 if (ret == 0)
2795 progress++;
2796
2797 nritems = btrfs_header_nritems(path->nodes[0]);
2798 /*
2799 * our goal is to get our slot at the start or end of a leaf. If
2800 * we've done so we're done
2801 */
2802 if (path->slots[0] == 0 || path->slots[0] == nritems)
2803 return 0;
2804
2805 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2806 return 0;
2807
2808 /* try to push all the items before our slot into the next leaf */
2809 slot = path->slots[0];
2810 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2811 if (ret < 0)
2812 return ret;
2813
2814 if (ret == 0)
2815 progress++;
2816
2817 if (progress)
2818 return 0;
2819 return 1;
2820}
2821
2822/*
Chris Mason44871b12009-03-13 10:04:31 -04002823 * split the path's leaf in two, making sure there is at least data_size
2824 * available for the resulting leaf level of the path.
2825 *
2826 * returns 0 if all went well and < 0 on failure.
2827 */
2828static noinline int split_leaf(struct btrfs_trans_handle *trans,
2829 struct btrfs_root *root,
2830 struct btrfs_key *ins_key,
2831 struct btrfs_path *path, int data_size,
2832 int extend)
2833{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002834 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002835 struct extent_buffer *l;
2836 u32 nritems;
2837 int mid;
2838 int slot;
2839 struct extent_buffer *right;
2840 int ret = 0;
2841 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002842 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002843 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002844 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002845
Yan, Zhenga5719522009-09-24 09:17:31 -04002846 l = path->nodes[0];
2847 slot = path->slots[0];
2848 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2849 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2850 return -EOVERFLOW;
2851
Chris Mason44871b12009-03-13 10:04:31 -04002852 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002853 if (data_size) {
2854 wret = push_leaf_right(trans, root, path, data_size,
2855 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002856 if (wret < 0)
2857 return wret;
2858 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002859 wret = push_leaf_left(trans, root, path, data_size,
2860 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002861 if (wret < 0)
2862 return wret;
2863 }
2864 l = path->nodes[0];
2865
2866 /* did the pushes work? */
2867 if (btrfs_leaf_free_space(root, l) >= data_size)
2868 return 0;
2869 }
2870
2871 if (!path->nodes[1]) {
2872 ret = insert_new_root(trans, root, path, 1);
2873 if (ret)
2874 return ret;
2875 }
2876again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002877 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002878 l = path->nodes[0];
2879 slot = path->slots[0];
2880 nritems = btrfs_header_nritems(l);
2881 mid = (nritems + 1) / 2;
2882
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002883 if (mid <= slot) {
2884 if (nritems == 1 ||
2885 leaf_space_used(l, mid, nritems - mid) + data_size >
2886 BTRFS_LEAF_DATA_SIZE(root)) {
2887 if (slot >= nritems) {
2888 split = 0;
2889 } else {
2890 mid = slot;
2891 if (mid != nritems &&
2892 leaf_space_used(l, mid, nritems - mid) +
2893 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002894 if (data_size && !tried_avoid_double)
2895 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002896 split = 2;
2897 }
2898 }
2899 }
2900 } else {
2901 if (leaf_space_used(l, 0, mid) + data_size >
2902 BTRFS_LEAF_DATA_SIZE(root)) {
2903 if (!extend && data_size && slot == 0) {
2904 split = 0;
2905 } else if ((extend || !data_size) && slot == 0) {
2906 mid = 1;
2907 } else {
2908 mid = slot;
2909 if (mid != nritems &&
2910 leaf_space_used(l, mid, nritems - mid) +
2911 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002912 if (data_size && !tried_avoid_double)
2913 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002914 split = 2 ;
2915 }
2916 }
2917 }
2918 }
2919
2920 if (split == 0)
2921 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2922 else
2923 btrfs_item_key(l, &disk_key, mid);
2924
2925 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002926 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002927 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002928 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002929 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002930
2931 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002932
2933 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2934 btrfs_set_header_bytenr(right, right->start);
2935 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002936 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002937 btrfs_set_header_owner(right, root->root_key.objectid);
2938 btrfs_set_header_level(right, 0);
2939 write_extent_buffer(right, root->fs_info->fsid,
2940 (unsigned long)btrfs_header_fsid(right),
2941 BTRFS_FSID_SIZE);
2942
2943 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2944 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2945 BTRFS_UUID_SIZE);
2946
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002947 if (split == 0) {
2948 if (mid <= slot) {
2949 btrfs_set_header_nritems(right, 0);
2950 wret = insert_ptr(trans, root, path,
2951 &disk_key, right->start,
2952 path->slots[1] + 1, 1);
2953 if (wret)
2954 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002955
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002956 btrfs_tree_unlock(path->nodes[0]);
2957 free_extent_buffer(path->nodes[0]);
2958 path->nodes[0] = right;
2959 path->slots[0] = 0;
2960 path->slots[1] += 1;
2961 } else {
2962 btrfs_set_header_nritems(right, 0);
2963 wret = insert_ptr(trans, root, path,
2964 &disk_key,
2965 right->start,
2966 path->slots[1], 1);
2967 if (wret)
2968 ret = wret;
2969 btrfs_tree_unlock(path->nodes[0]);
2970 free_extent_buffer(path->nodes[0]);
2971 path->nodes[0] = right;
2972 path->slots[0] = 0;
2973 if (path->slots[1] == 0) {
2974 wret = fixup_low_keys(trans, root,
2975 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002976 if (wret)
2977 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002978 }
2979 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002980 btrfs_mark_buffer_dirty(right);
2981 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002982 }
2983
2984 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2985 BUG_ON(ret);
2986
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002987 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002988 BUG_ON(num_doubles != 0);
2989 num_doubles++;
2990 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002991 }
Chris Mason44871b12009-03-13 10:04:31 -04002992
Chris Masonbe0e5c02007-01-26 15:51:26 -05002993 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04002994
2995push_for_double:
2996 push_for_double_split(trans, root, path, data_size);
2997 tried_avoid_double = 1;
2998 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2999 return 0;
3000 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003001}
3002
Yan, Zhengad48fd752009-11-12 09:33:58 +00003003static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3004 struct btrfs_root *root,
3005 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003006{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003007 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003008 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003009 struct btrfs_file_extent_item *fi;
3010 u64 extent_len = 0;
3011 u32 item_size;
3012 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003013
3014 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003015 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3016
3017 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3018 key.type != BTRFS_EXTENT_CSUM_KEY);
3019
3020 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3021 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003022
3023 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003024 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3025 fi = btrfs_item_ptr(leaf, path->slots[0],
3026 struct btrfs_file_extent_item);
3027 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3028 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003029 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003030
Chris Mason459931e2008-12-10 09:10:46 -05003031 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003032 path->search_for_split = 1;
3033 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003034 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003035 if (ret < 0)
3036 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003037
Yan, Zhengad48fd752009-11-12 09:33:58 +00003038 ret = -EAGAIN;
3039 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003040 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003041 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3042 goto err;
3043
Chris Mason109f6ae2010-04-02 09:20:18 -04003044 /* the leaf has changed, it now has room. return now */
3045 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3046 goto err;
3047
Yan, Zhengad48fd752009-11-12 09:33:58 +00003048 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3049 fi = btrfs_item_ptr(leaf, path->slots[0],
3050 struct btrfs_file_extent_item);
3051 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3052 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003053 }
3054
Chris Masonb9473432009-03-13 11:00:37 -04003055 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003056 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003057 if (ret)
3058 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003059
Yan, Zhengad48fd752009-11-12 09:33:58 +00003060 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003061 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003062 return 0;
3063err:
3064 path->keep_locks = 0;
3065 return ret;
3066}
3067
3068static noinline int split_item(struct btrfs_trans_handle *trans,
3069 struct btrfs_root *root,
3070 struct btrfs_path *path,
3071 struct btrfs_key *new_key,
3072 unsigned long split_offset)
3073{
3074 struct extent_buffer *leaf;
3075 struct btrfs_item *item;
3076 struct btrfs_item *new_item;
3077 int slot;
3078 char *buf;
3079 u32 nritems;
3080 u32 item_size;
3081 u32 orig_offset;
3082 struct btrfs_disk_key disk_key;
3083
Chris Masonb9473432009-03-13 11:00:37 -04003084 leaf = path->nodes[0];
3085 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3086
Chris Masonb4ce94d2009-02-04 09:25:08 -05003087 btrfs_set_path_blocking(path);
3088
Chris Mason459931e2008-12-10 09:10:46 -05003089 item = btrfs_item_nr(leaf, path->slots[0]);
3090 orig_offset = btrfs_item_offset(leaf, item);
3091 item_size = btrfs_item_size(leaf, item);
3092
Chris Mason459931e2008-12-10 09:10:46 -05003093 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003094 if (!buf)
3095 return -ENOMEM;
3096
Chris Mason459931e2008-12-10 09:10:46 -05003097 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3098 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003099
Chris Mason459931e2008-12-10 09:10:46 -05003100 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003101 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003102 if (slot != nritems) {
3103 /* shift the items */
3104 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003105 btrfs_item_nr_offset(slot),
3106 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003107 }
3108
3109 btrfs_cpu_key_to_disk(&disk_key, new_key);
3110 btrfs_set_item_key(leaf, &disk_key, slot);
3111
3112 new_item = btrfs_item_nr(leaf, slot);
3113
3114 btrfs_set_item_offset(leaf, new_item, orig_offset);
3115 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3116
3117 btrfs_set_item_offset(leaf, item,
3118 orig_offset + item_size - split_offset);
3119 btrfs_set_item_size(leaf, item, split_offset);
3120
3121 btrfs_set_header_nritems(leaf, nritems + 1);
3122
3123 /* write the data for the start of the original item */
3124 write_extent_buffer(leaf, buf,
3125 btrfs_item_ptr_offset(leaf, path->slots[0]),
3126 split_offset);
3127
3128 /* write the data for the new item */
3129 write_extent_buffer(leaf, buf + split_offset,
3130 btrfs_item_ptr_offset(leaf, slot),
3131 item_size - split_offset);
3132 btrfs_mark_buffer_dirty(leaf);
3133
Yan, Zhengad48fd752009-11-12 09:33:58 +00003134 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003135 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003136 return 0;
3137}
3138
3139/*
3140 * This function splits a single item into two items,
3141 * giving 'new_key' to the new item and splitting the
3142 * old one at split_offset (from the start of the item).
3143 *
3144 * The path may be released by this operation. After
3145 * the split, the path is pointing to the old item. The
3146 * new item is going to be in the same node as the old one.
3147 *
3148 * Note, the item being split must be smaller enough to live alone on
3149 * a tree block with room for one extra struct btrfs_item
3150 *
3151 * This allows us to split the item in place, keeping a lock on the
3152 * leaf the entire time.
3153 */
3154int btrfs_split_item(struct btrfs_trans_handle *trans,
3155 struct btrfs_root *root,
3156 struct btrfs_path *path,
3157 struct btrfs_key *new_key,
3158 unsigned long split_offset)
3159{
3160 int ret;
3161 ret = setup_leaf_for_split(trans, root, path,
3162 sizeof(struct btrfs_item));
3163 if (ret)
3164 return ret;
3165
3166 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003167 return ret;
3168}
3169
3170/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003171 * This function duplicate a item, giving 'new_key' to the new item.
3172 * It guarantees both items live in the same tree leaf and the new item
3173 * is contiguous with the original item.
3174 *
3175 * This allows us to split file extent in place, keeping a lock on the
3176 * leaf the entire time.
3177 */
3178int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3179 struct btrfs_root *root,
3180 struct btrfs_path *path,
3181 struct btrfs_key *new_key)
3182{
3183 struct extent_buffer *leaf;
3184 int ret;
3185 u32 item_size;
3186
3187 leaf = path->nodes[0];
3188 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3189 ret = setup_leaf_for_split(trans, root, path,
3190 item_size + sizeof(struct btrfs_item));
3191 if (ret)
3192 return ret;
3193
3194 path->slots[0]++;
3195 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3196 item_size, item_size +
3197 sizeof(struct btrfs_item), 1);
3198 BUG_ON(ret);
3199
3200 leaf = path->nodes[0];
3201 memcpy_extent_buffer(leaf,
3202 btrfs_item_ptr_offset(leaf, path->slots[0]),
3203 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3204 item_size);
3205 return 0;
3206}
3207
3208/*
Chris Masond352ac62008-09-29 15:18:18 -04003209 * make the item pointed to by the path smaller. new_size indicates
3210 * how small to make it, and from_end tells us if we just chop bytes
3211 * off the end of the item or if we shift the item to chop bytes off
3212 * the front.
3213 */
Chris Masonb18c6682007-04-17 13:26:50 -04003214int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3215 struct btrfs_root *root,
3216 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003217 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003218{
3219 int ret = 0;
3220 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003221 struct extent_buffer *leaf;
3222 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003223 u32 nritems;
3224 unsigned int data_end;
3225 unsigned int old_data_start;
3226 unsigned int old_size;
3227 unsigned int size_diff;
3228 int i;
3229
Chris Mason5f39d392007-10-15 16:14:19 -04003230 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003231 slot = path->slots[0];
3232
3233 old_size = btrfs_item_size_nr(leaf, slot);
3234 if (old_size == new_size)
3235 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003236
Chris Mason5f39d392007-10-15 16:14:19 -04003237 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003238 data_end = leaf_data_end(root, leaf);
3239
Chris Mason5f39d392007-10-15 16:14:19 -04003240 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003241
Chris Masonb18c6682007-04-17 13:26:50 -04003242 size_diff = old_size - new_size;
3243
3244 BUG_ON(slot < 0);
3245 BUG_ON(slot >= nritems);
3246
3247 /*
3248 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3249 */
3250 /* first correct the data pointers */
3251 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003252 u32 ioff;
3253 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003254
3255 if (!leaf->map_token) {
3256 map_extent_buffer(leaf, (unsigned long)item,
3257 sizeof(struct btrfs_item),
3258 &leaf->map_token, &leaf->kaddr,
3259 &leaf->map_start, &leaf->map_len,
3260 KM_USER1);
3261 }
3262
Chris Mason5f39d392007-10-15 16:14:19 -04003263 ioff = btrfs_item_offset(leaf, item);
3264 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003265 }
Chris Masondb945352007-10-15 16:15:53 -04003266
3267 if (leaf->map_token) {
3268 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3269 leaf->map_token = NULL;
3270 }
3271
Chris Masonb18c6682007-04-17 13:26:50 -04003272 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003273 if (from_end) {
3274 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3275 data_end + size_diff, btrfs_leaf_data(leaf) +
3276 data_end, old_data_start + new_size - data_end);
3277 } else {
3278 struct btrfs_disk_key disk_key;
3279 u64 offset;
3280
3281 btrfs_item_key(leaf, &disk_key, slot);
3282
3283 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3284 unsigned long ptr;
3285 struct btrfs_file_extent_item *fi;
3286
3287 fi = btrfs_item_ptr(leaf, slot,
3288 struct btrfs_file_extent_item);
3289 fi = (struct btrfs_file_extent_item *)(
3290 (unsigned long)fi - size_diff);
3291
3292 if (btrfs_file_extent_type(leaf, fi) ==
3293 BTRFS_FILE_EXTENT_INLINE) {
3294 ptr = btrfs_item_ptr_offset(leaf, slot);
3295 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003296 (unsigned long)fi,
3297 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003298 disk_bytenr));
3299 }
3300 }
3301
3302 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3303 data_end + size_diff, btrfs_leaf_data(leaf) +
3304 data_end, old_data_start - data_end);
3305
3306 offset = btrfs_disk_key_offset(&disk_key);
3307 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3308 btrfs_set_item_key(leaf, &disk_key, slot);
3309 if (slot == 0)
3310 fixup_low_keys(trans, root, path, &disk_key, 1);
3311 }
Chris Mason5f39d392007-10-15 16:14:19 -04003312
3313 item = btrfs_item_nr(leaf, slot);
3314 btrfs_set_item_size(leaf, item, new_size);
3315 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003316
3317 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003318 if (btrfs_leaf_free_space(root, leaf) < 0) {
3319 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003320 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003321 }
Chris Masonb18c6682007-04-17 13:26:50 -04003322 return ret;
3323}
3324
Chris Masond352ac62008-09-29 15:18:18 -04003325/*
3326 * make the item pointed to by the path bigger, data_size is the new size.
3327 */
Chris Mason5f39d392007-10-15 16:14:19 -04003328int btrfs_extend_item(struct btrfs_trans_handle *trans,
3329 struct btrfs_root *root, struct btrfs_path *path,
3330 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003331{
3332 int ret = 0;
3333 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003334 struct extent_buffer *leaf;
3335 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003336 u32 nritems;
3337 unsigned int data_end;
3338 unsigned int old_data;
3339 unsigned int old_size;
3340 int i;
3341
Chris Mason5f39d392007-10-15 16:14:19 -04003342 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003343
Chris Mason5f39d392007-10-15 16:14:19 -04003344 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003345 data_end = leaf_data_end(root, leaf);
3346
Chris Mason5f39d392007-10-15 16:14:19 -04003347 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3348 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003349 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003350 }
Chris Mason6567e832007-04-16 09:22:45 -04003351 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003352 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003353
3354 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003355 if (slot >= nritems) {
3356 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003357 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3358 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003359 BUG_ON(1);
3360 }
Chris Mason6567e832007-04-16 09:22:45 -04003361
3362 /*
3363 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3364 */
3365 /* first correct the data pointers */
3366 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003367 u32 ioff;
3368 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003369
3370 if (!leaf->map_token) {
3371 map_extent_buffer(leaf, (unsigned long)item,
3372 sizeof(struct btrfs_item),
3373 &leaf->map_token, &leaf->kaddr,
3374 &leaf->map_start, &leaf->map_len,
3375 KM_USER1);
3376 }
Chris Mason5f39d392007-10-15 16:14:19 -04003377 ioff = btrfs_item_offset(leaf, item);
3378 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003379 }
Chris Mason5f39d392007-10-15 16:14:19 -04003380
Chris Masondb945352007-10-15 16:15:53 -04003381 if (leaf->map_token) {
3382 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3383 leaf->map_token = NULL;
3384 }
3385
Chris Mason6567e832007-04-16 09:22:45 -04003386 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003387 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003388 data_end - data_size, btrfs_leaf_data(leaf) +
3389 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003390
Chris Mason6567e832007-04-16 09:22:45 -04003391 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003392 old_size = btrfs_item_size_nr(leaf, slot);
3393 item = btrfs_item_nr(leaf, slot);
3394 btrfs_set_item_size(leaf, item, old_size + data_size);
3395 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003396
3397 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003398 if (btrfs_leaf_free_space(root, leaf) < 0) {
3399 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003400 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003401 }
Chris Mason6567e832007-04-16 09:22:45 -04003402 return ret;
3403}
3404
Chris Mason74123bd2007-02-02 11:05:29 -05003405/*
Chris Masond352ac62008-09-29 15:18:18 -04003406 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003407 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003408 * Returns the number of keys that were inserted.
3409 */
3410int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3411 struct btrfs_root *root,
3412 struct btrfs_path *path,
3413 struct btrfs_key *cpu_key, u32 *data_size,
3414 int nr)
3415{
3416 struct extent_buffer *leaf;
3417 struct btrfs_item *item;
3418 int ret = 0;
3419 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003420 int i;
3421 u32 nritems;
3422 u32 total_data = 0;
3423 u32 total_size = 0;
3424 unsigned int data_end;
3425 struct btrfs_disk_key disk_key;
3426 struct btrfs_key found_key;
3427
Yan Zheng87b29b22008-12-17 10:21:48 -05003428 for (i = 0; i < nr; i++) {
3429 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3430 BTRFS_LEAF_DATA_SIZE(root)) {
3431 break;
3432 nr = i;
3433 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003434 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003435 total_size += data_size[i] + sizeof(struct btrfs_item);
3436 }
3437 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003438
Josef Bacikf3465ca2008-11-12 14:19:50 -05003439 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3440 if (ret == 0)
3441 return -EEXIST;
3442 if (ret < 0)
3443 goto out;
3444
Josef Bacikf3465ca2008-11-12 14:19:50 -05003445 leaf = path->nodes[0];
3446
3447 nritems = btrfs_header_nritems(leaf);
3448 data_end = leaf_data_end(root, leaf);
3449
3450 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3451 for (i = nr; i >= 0; i--) {
3452 total_data -= data_size[i];
3453 total_size -= data_size[i] + sizeof(struct btrfs_item);
3454 if (total_size < btrfs_leaf_free_space(root, leaf))
3455 break;
3456 }
3457 nr = i;
3458 }
3459
3460 slot = path->slots[0];
3461 BUG_ON(slot < 0);
3462
3463 if (slot != nritems) {
3464 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3465
3466 item = btrfs_item_nr(leaf, slot);
3467 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3468
3469 /* figure out how many keys we can insert in here */
3470 total_data = data_size[0];
3471 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003472 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003473 break;
3474 total_data += data_size[i];
3475 }
3476 nr = i;
3477
3478 if (old_data < data_end) {
3479 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003480 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003481 slot, old_data, data_end);
3482 BUG_ON(1);
3483 }
3484 /*
3485 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3486 */
3487 /* first correct the data pointers */
3488 WARN_ON(leaf->map_token);
3489 for (i = slot; i < nritems; i++) {
3490 u32 ioff;
3491
3492 item = btrfs_item_nr(leaf, i);
3493 if (!leaf->map_token) {
3494 map_extent_buffer(leaf, (unsigned long)item,
3495 sizeof(struct btrfs_item),
3496 &leaf->map_token, &leaf->kaddr,
3497 &leaf->map_start, &leaf->map_len,
3498 KM_USER1);
3499 }
3500
3501 ioff = btrfs_item_offset(leaf, item);
3502 btrfs_set_item_offset(leaf, item, ioff - total_data);
3503 }
3504 if (leaf->map_token) {
3505 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3506 leaf->map_token = NULL;
3507 }
3508
3509 /* shift the items */
3510 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3511 btrfs_item_nr_offset(slot),
3512 (nritems - slot) * sizeof(struct btrfs_item));
3513
3514 /* shift the data */
3515 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3516 data_end - total_data, btrfs_leaf_data(leaf) +
3517 data_end, old_data - data_end);
3518 data_end = old_data;
3519 } else {
3520 /*
3521 * this sucks but it has to be done, if we are inserting at
3522 * the end of the leaf only insert 1 of the items, since we
3523 * have no way of knowing whats on the next leaf and we'd have
3524 * to drop our current locks to figure it out
3525 */
3526 nr = 1;
3527 }
3528
3529 /* setup the item for the new data */
3530 for (i = 0; i < nr; i++) {
3531 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3532 btrfs_set_item_key(leaf, &disk_key, slot + i);
3533 item = btrfs_item_nr(leaf, slot + i);
3534 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3535 data_end -= data_size[i];
3536 btrfs_set_item_size(leaf, item, data_size[i]);
3537 }
3538 btrfs_set_header_nritems(leaf, nritems + nr);
3539 btrfs_mark_buffer_dirty(leaf);
3540
3541 ret = 0;
3542 if (slot == 0) {
3543 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3544 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3545 }
3546
3547 if (btrfs_leaf_free_space(root, leaf) < 0) {
3548 btrfs_print_leaf(root, leaf);
3549 BUG();
3550 }
3551out:
3552 if (!ret)
3553 ret = nr;
3554 return ret;
3555}
3556
3557/*
Chris Mason44871b12009-03-13 10:04:31 -04003558 * this is a helper for btrfs_insert_empty_items, the main goal here is
3559 * to save stack depth by doing the bulk of the work in a function
3560 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003561 */
Chris Mason44871b12009-03-13 10:04:31 -04003562static noinline_for_stack int
3563setup_items_for_insert(struct btrfs_trans_handle *trans,
3564 struct btrfs_root *root, struct btrfs_path *path,
3565 struct btrfs_key *cpu_key, u32 *data_size,
3566 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003567{
Chris Mason5f39d392007-10-15 16:14:19 -04003568 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003569 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003570 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003571 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003572 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003573 int ret;
3574 struct extent_buffer *leaf;
3575 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003576
Chris Mason5f39d392007-10-15 16:14:19 -04003577 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003578 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003579
Chris Mason5f39d392007-10-15 16:14:19 -04003580 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003581 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003582
Chris Masonf25956c2008-09-12 15:32:53 -04003583 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003584 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003585 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003586 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003587 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003588 }
Chris Mason5f39d392007-10-15 16:14:19 -04003589
Chris Masonbe0e5c02007-01-26 15:51:26 -05003590 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003591 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003592
Chris Mason5f39d392007-10-15 16:14:19 -04003593 if (old_data < data_end) {
3594 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003595 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003596 slot, old_data, data_end);
3597 BUG_ON(1);
3598 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003599 /*
3600 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3601 */
3602 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003603 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003604 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003605 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003606
Chris Mason5f39d392007-10-15 16:14:19 -04003607 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003608 if (!leaf->map_token) {
3609 map_extent_buffer(leaf, (unsigned long)item,
3610 sizeof(struct btrfs_item),
3611 &leaf->map_token, &leaf->kaddr,
3612 &leaf->map_start, &leaf->map_len,
3613 KM_USER1);
3614 }
3615
Chris Mason5f39d392007-10-15 16:14:19 -04003616 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003617 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003618 }
Chris Masondb945352007-10-15 16:15:53 -04003619 if (leaf->map_token) {
3620 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3621 leaf->map_token = NULL;
3622 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003623
3624 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003625 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003626 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003627 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003628
3629 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003630 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003631 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003632 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003633 data_end = old_data;
3634 }
Chris Mason5f39d392007-10-15 16:14:19 -04003635
Chris Mason62e27492007-03-15 12:56:47 -04003636 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003637 for (i = 0; i < nr; i++) {
3638 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3639 btrfs_set_item_key(leaf, &disk_key, slot + i);
3640 item = btrfs_item_nr(leaf, slot + i);
3641 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3642 data_end -= data_size[i];
3643 btrfs_set_item_size(leaf, item, data_size[i]);
3644 }
Chris Mason44871b12009-03-13 10:04:31 -04003645
Chris Mason9c583092008-01-29 15:15:18 -05003646 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003647
3648 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003649 if (slot == 0) {
3650 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003651 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003652 }
Chris Masonb9473432009-03-13 11:00:37 -04003653 btrfs_unlock_up_safe(path, 1);
3654 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003655
Chris Mason5f39d392007-10-15 16:14:19 -04003656 if (btrfs_leaf_free_space(root, leaf) < 0) {
3657 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003658 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003659 }
Chris Mason44871b12009-03-13 10:04:31 -04003660 return ret;
3661}
3662
3663/*
3664 * Given a key and some data, insert items into the tree.
3665 * This does all the path init required, making room in the tree if needed.
3666 */
3667int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3668 struct btrfs_root *root,
3669 struct btrfs_path *path,
3670 struct btrfs_key *cpu_key, u32 *data_size,
3671 int nr)
3672{
Chris Mason44871b12009-03-13 10:04:31 -04003673 int ret = 0;
3674 int slot;
3675 int i;
3676 u32 total_size = 0;
3677 u32 total_data = 0;
3678
3679 for (i = 0; i < nr; i++)
3680 total_data += data_size[i];
3681
3682 total_size = total_data + (nr * sizeof(struct btrfs_item));
3683 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3684 if (ret == 0)
3685 return -EEXIST;
3686 if (ret < 0)
3687 goto out;
3688
Chris Mason44871b12009-03-13 10:04:31 -04003689 slot = path->slots[0];
3690 BUG_ON(slot < 0);
3691
3692 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3693 total_data, total_size, nr);
3694
Chris Masoned2ff2c2007-03-01 18:59:40 -05003695out:
Chris Mason62e27492007-03-15 12:56:47 -04003696 return ret;
3697}
3698
3699/*
3700 * Given a key and some data, insert an item into the tree.
3701 * This does all the path init required, making room in the tree if needed.
3702 */
Chris Masone089f052007-03-16 16:20:31 -04003703int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3704 *root, struct btrfs_key *cpu_key, void *data, u32
3705 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003706{
3707 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003708 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003709 struct extent_buffer *leaf;
3710 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003711
Chris Mason2c90e5d2007-04-02 10:50:19 -04003712 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003713 if (!path)
3714 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003715 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003716 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003717 leaf = path->nodes[0];
3718 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3719 write_extent_buffer(leaf, data, ptr, data_size);
3720 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003721 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003722 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003723 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003724}
3725
Chris Mason74123bd2007-02-02 11:05:29 -05003726/*
Chris Mason5de08d72007-02-24 06:24:44 -05003727 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003728 *
Chris Masond352ac62008-09-29 15:18:18 -04003729 * the tree should have been previously balanced so the deletion does not
3730 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003731 */
Chris Masone089f052007-03-16 16:20:31 -04003732static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3733 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003734{
Chris Mason5f39d392007-10-15 16:14:19 -04003735 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003736 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003737 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003738 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003739
Chris Mason5f39d392007-10-15 16:14:19 -04003740 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003741 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003742 memmove_extent_buffer(parent,
3743 btrfs_node_key_ptr_offset(slot),
3744 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003745 sizeof(struct btrfs_key_ptr) *
3746 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003747 }
Chris Mason7518a232007-03-12 12:01:18 -04003748 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003749 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003750 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003751 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003752 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003753 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003754 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003755 struct btrfs_disk_key disk_key;
3756
3757 btrfs_node_key(parent, &disk_key, 0);
3758 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003759 if (wret)
3760 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003761 }
Chris Masond6025572007-03-30 14:27:56 -04003762 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003763 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003764}
3765
Chris Mason74123bd2007-02-02 11:05:29 -05003766/*
Chris Mason323ac952008-10-01 19:05:46 -04003767 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003768 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003769 *
3770 * This deletes the pointer in path->nodes[1] and frees the leaf
3771 * block extent. zero is returned if it all worked out, < 0 otherwise.
3772 *
3773 * The path must have already been setup for deleting the leaf, including
3774 * all the proper balancing. path->nodes[1] must be locked.
3775 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003776static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3777 struct btrfs_root *root,
3778 struct btrfs_path *path,
3779 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003780{
3781 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003782
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003783 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003784 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3785 if (ret)
3786 return ret;
3787
Chris Mason4d081c42009-02-04 09:31:28 -05003788 /*
3789 * btrfs_free_extent is expensive, we want to make sure we
3790 * aren't holding any locks when we call it
3791 */
3792 btrfs_unlock_up_safe(path, 0);
3793
Yan, Zhengf0486c62010-05-16 10:46:25 -04003794 root_sub_used(root, leaf->len);
3795
3796 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3797 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003798}
3799/*
Chris Mason74123bd2007-02-02 11:05:29 -05003800 * delete the item at the leaf level in path. If that empties
3801 * the leaf, remove it from the tree
3802 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003803int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3804 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003805{
Chris Mason5f39d392007-10-15 16:14:19 -04003806 struct extent_buffer *leaf;
3807 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003808 int last_off;
3809 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003810 int ret = 0;
3811 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003812 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003813 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003814
Chris Mason5f39d392007-10-15 16:14:19 -04003815 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003816 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3817
3818 for (i = 0; i < nr; i++)
3819 dsize += btrfs_item_size_nr(leaf, slot + i);
3820
Chris Mason5f39d392007-10-15 16:14:19 -04003821 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003822
Chris Mason85e21ba2008-01-29 15:11:36 -05003823 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003824 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003825
3826 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003827 data_end + dsize,
3828 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003829 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003830
Chris Mason85e21ba2008-01-29 15:11:36 -05003831 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003832 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003833
Chris Mason5f39d392007-10-15 16:14:19 -04003834 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003835 if (!leaf->map_token) {
3836 map_extent_buffer(leaf, (unsigned long)item,
3837 sizeof(struct btrfs_item),
3838 &leaf->map_token, &leaf->kaddr,
3839 &leaf->map_start, &leaf->map_len,
3840 KM_USER1);
3841 }
Chris Mason5f39d392007-10-15 16:14:19 -04003842 ioff = btrfs_item_offset(leaf, item);
3843 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003844 }
Chris Masondb945352007-10-15 16:15:53 -04003845
3846 if (leaf->map_token) {
3847 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3848 leaf->map_token = NULL;
3849 }
3850
Chris Mason5f39d392007-10-15 16:14:19 -04003851 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003852 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003853 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003854 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003856 btrfs_set_header_nritems(leaf, nritems - nr);
3857 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003858
Chris Mason74123bd2007-02-02 11:05:29 -05003859 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003860 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003861 if (leaf == root->node) {
3862 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003863 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003864 btrfs_set_path_blocking(path);
3865 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003866 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003867 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003868 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003869 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003870 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003871 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003872 struct btrfs_disk_key disk_key;
3873
3874 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003875 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003876 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003877 if (wret)
3878 ret = wret;
3879 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003880
Chris Mason74123bd2007-02-02 11:05:29 -05003881 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003882 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003883 /* push_leaf_left fixes the path.
3884 * make sure the path still points to our leaf
3885 * for possible call to del_ptr below
3886 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003887 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003888 extent_buffer_get(leaf);
3889
Chris Masonb9473432009-03-13 11:00:37 -04003890 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003891 wret = push_leaf_left(trans, root, path, 1, 1,
3892 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003893 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003894 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003895
3896 if (path->nodes[0] == leaf &&
3897 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003898 wret = push_leaf_right(trans, root, path, 1,
3899 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003900 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003901 ret = wret;
3902 }
Chris Mason5f39d392007-10-15 16:14:19 -04003903
3904 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003905 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003906 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003907 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003908 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003909 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003910 /* if we're still in the path, make sure
3911 * we're dirty. Otherwise, one of the
3912 * push_leaf functions must have already
3913 * dirtied this buffer
3914 */
3915 if (path->nodes[0] == leaf)
3916 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003917 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003918 }
Chris Masond5719762007-03-23 10:01:08 -04003919 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003920 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003921 }
3922 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003923 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003924}
3925
Chris Mason97571fd2007-02-24 13:39:08 -05003926/*
Chris Mason925baed2008-06-25 16:01:30 -04003927 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003928 * returns 0 if it found something or 1 if there are no lesser leaves.
3929 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003930 *
3931 * This may release the path, and so you may lose any locks held at the
3932 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003933 */
3934int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3935{
Chris Mason925baed2008-06-25 16:01:30 -04003936 struct btrfs_key key;
3937 struct btrfs_disk_key found_key;
3938 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003939
Chris Mason925baed2008-06-25 16:01:30 -04003940 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003941
Chris Mason925baed2008-06-25 16:01:30 -04003942 if (key.offset > 0)
3943 key.offset--;
3944 else if (key.type > 0)
3945 key.type--;
3946 else if (key.objectid > 0)
3947 key.objectid--;
3948 else
3949 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003950
David Sterbab3b4aa72011-04-21 01:20:15 +02003951 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003952 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3953 if (ret < 0)
3954 return ret;
3955 btrfs_item_key(path->nodes[0], &found_key, 0);
3956 ret = comp_keys(&found_key, &key);
3957 if (ret < 0)
3958 return 0;
3959 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003960}
3961
Chris Mason3f157a22008-06-25 16:01:31 -04003962/*
3963 * A helper function to walk down the tree starting at min_key, and looking
3964 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003965 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003966 *
3967 * This does not cow, but it does stuff the starting key it finds back
3968 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3969 * key and get a writable path.
3970 *
3971 * This does lock as it descends, and path->keep_locks should be set
3972 * to 1 by the caller.
3973 *
3974 * This honors path->lowest_level to prevent descent past a given level
3975 * of the tree.
3976 *
Chris Masond352ac62008-09-29 15:18:18 -04003977 * min_trans indicates the oldest transaction that you are interested
3978 * in walking through. Any nodes or leaves older than min_trans are
3979 * skipped over (without reading them).
3980 *
Chris Mason3f157a22008-06-25 16:01:31 -04003981 * returns zero if something useful was found, < 0 on error and 1 if there
3982 * was nothing in the tree that matched the search criteria.
3983 */
3984int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003985 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003986 struct btrfs_path *path, int cache_only,
3987 u64 min_trans)
3988{
3989 struct extent_buffer *cur;
3990 struct btrfs_key found_key;
3991 int slot;
Yan96524802008-07-24 12:19:49 -04003992 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003993 u32 nritems;
3994 int level;
3995 int ret = 1;
3996
Chris Mason934d3752008-12-08 16:43:10 -05003997 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003998again:
3999 cur = btrfs_lock_root_node(root);
4000 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004001 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004002 path->nodes[level] = cur;
4003 path->locks[level] = 1;
4004
4005 if (btrfs_header_generation(cur) < min_trans) {
4006 ret = 1;
4007 goto out;
4008 }
Chris Masond3977122009-01-05 21:25:51 -05004009 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004010 nritems = btrfs_header_nritems(cur);
4011 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004012 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004013
Chris Mason323ac952008-10-01 19:05:46 -04004014 /* at the lowest level, we're done, setup the path and exit */
4015 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004016 if (slot >= nritems)
4017 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004018 ret = 0;
4019 path->slots[level] = slot;
4020 btrfs_item_key_to_cpu(cur, &found_key, slot);
4021 goto out;
4022 }
Yan96524802008-07-24 12:19:49 -04004023 if (sret && slot > 0)
4024 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004025 /*
4026 * check this node pointer against the cache_only and
4027 * min_trans parameters. If it isn't in cache or is too
4028 * old, skip to the next one.
4029 */
Chris Masond3977122009-01-05 21:25:51 -05004030 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004031 u64 blockptr;
4032 u64 gen;
4033 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004034 struct btrfs_disk_key disk_key;
4035
Chris Mason3f157a22008-06-25 16:01:31 -04004036 blockptr = btrfs_node_blockptr(cur, slot);
4037 gen = btrfs_node_ptr_generation(cur, slot);
4038 if (gen < min_trans) {
4039 slot++;
4040 continue;
4041 }
4042 if (!cache_only)
4043 break;
4044
Chris Masone02119d2008-09-05 16:13:11 -04004045 if (max_key) {
4046 btrfs_node_key(cur, &disk_key, slot);
4047 if (comp_keys(&disk_key, max_key) >= 0) {
4048 ret = 1;
4049 goto out;
4050 }
4051 }
4052
Chris Mason3f157a22008-06-25 16:01:31 -04004053 tmp = btrfs_find_tree_block(root, blockptr,
4054 btrfs_level_size(root, level - 1));
4055
4056 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4057 free_extent_buffer(tmp);
4058 break;
4059 }
4060 if (tmp)
4061 free_extent_buffer(tmp);
4062 slot++;
4063 }
Chris Masone02119d2008-09-05 16:13:11 -04004064find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004065 /*
4066 * we didn't find a candidate key in this node, walk forward
4067 * and find another one
4068 */
4069 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004070 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004071 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004072 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004073 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004074 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004075 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004076 goto again;
4077 } else {
4078 goto out;
4079 }
4080 }
4081 /* save our key for returning back */
4082 btrfs_node_key_to_cpu(cur, &found_key, slot);
4083 path->slots[level] = slot;
4084 if (level == path->lowest_level) {
4085 ret = 0;
4086 unlock_up(path, level, 1);
4087 goto out;
4088 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004089 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004090 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004091 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004092
4093 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004094
Chris Mason3f157a22008-06-25 16:01:31 -04004095 path->locks[level - 1] = 1;
4096 path->nodes[level - 1] = cur;
4097 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004098 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004099 }
4100out:
4101 if (ret == 0)
4102 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004103 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004104 return ret;
4105}
4106
4107/*
4108 * this is similar to btrfs_next_leaf, but does not try to preserve
4109 * and fixup the path. It looks for and returns the next key in the
4110 * tree based on the current path and the cache_only and min_trans
4111 * parameters.
4112 *
4113 * 0 is returned if another key is found, < 0 if there are any errors
4114 * and 1 is returned if there are no higher keys in the tree
4115 *
4116 * path->keep_locks should be set to 1 on the search made before
4117 * calling this function.
4118 */
Chris Masone7a84562008-06-25 16:01:31 -04004119int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004120 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004121 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004122{
Chris Masone7a84562008-06-25 16:01:31 -04004123 int slot;
4124 struct extent_buffer *c;
4125
Chris Mason934d3752008-12-08 16:43:10 -05004126 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004127 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004128 if (!path->nodes[level])
4129 return 1;
4130
4131 slot = path->slots[level] + 1;
4132 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004133next:
Chris Masone7a84562008-06-25 16:01:31 -04004134 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004135 int ret;
4136 int orig_lowest;
4137 struct btrfs_key cur_key;
4138 if (level + 1 >= BTRFS_MAX_LEVEL ||
4139 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004140 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004141
4142 if (path->locks[level + 1]) {
4143 level++;
4144 continue;
4145 }
4146
4147 slot = btrfs_header_nritems(c) - 1;
4148 if (level == 0)
4149 btrfs_item_key_to_cpu(c, &cur_key, slot);
4150 else
4151 btrfs_node_key_to_cpu(c, &cur_key, slot);
4152
4153 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004154 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004155 path->lowest_level = level;
4156 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4157 0, 0);
4158 path->lowest_level = orig_lowest;
4159 if (ret < 0)
4160 return ret;
4161
4162 c = path->nodes[level];
4163 slot = path->slots[level];
4164 if (ret == 0)
4165 slot++;
4166 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004167 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004168
Chris Masone7a84562008-06-25 16:01:31 -04004169 if (level == 0)
4170 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004171 else {
4172 u64 blockptr = btrfs_node_blockptr(c, slot);
4173 u64 gen = btrfs_node_ptr_generation(c, slot);
4174
4175 if (cache_only) {
4176 struct extent_buffer *cur;
4177 cur = btrfs_find_tree_block(root, blockptr,
4178 btrfs_level_size(root, level - 1));
4179 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4180 slot++;
4181 if (cur)
4182 free_extent_buffer(cur);
4183 goto next;
4184 }
4185 free_extent_buffer(cur);
4186 }
4187 if (gen < min_trans) {
4188 slot++;
4189 goto next;
4190 }
Chris Masone7a84562008-06-25 16:01:31 -04004191 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004192 }
Chris Masone7a84562008-06-25 16:01:31 -04004193 return 0;
4194 }
4195 return 1;
4196}
4197
Chris Mason7bb86312007-12-11 09:25:06 -05004198/*
Chris Mason925baed2008-06-25 16:01:30 -04004199 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004200 * returns 0 if it found something or 1 if there are no greater leaves.
4201 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004202 */
Chris Mason234b63a2007-03-13 10:46:10 -04004203int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004204{
4205 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004206 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004207 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004208 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004209 struct btrfs_key key;
4210 u32 nritems;
4211 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004212 int old_spinning = path->leave_spinning;
4213 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004214
4215 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004216 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004217 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004218
Chris Mason8e73f272009-04-03 10:14:18 -04004219 /*
4220 * we take the blocks in an order that upsets lockdep. Using
4221 * blocking mode is the only way around it.
4222 */
4223#ifdef CONFIG_DEBUG_LOCK_ALLOC
4224 force_blocking = 1;
4225#endif
Chris Mason925baed2008-06-25 16:01:30 -04004226
Chris Mason8e73f272009-04-03 10:14:18 -04004227 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4228again:
4229 level = 1;
4230 next = NULL;
David Sterbab3b4aa72011-04-21 01:20:15 +02004231 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004232
Chris Masona2135012008-06-25 16:01:30 -04004233 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004234
4235 if (!force_blocking)
4236 path->leave_spinning = 1;
4237
Chris Mason925baed2008-06-25 16:01:30 -04004238 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4239 path->keep_locks = 0;
4240
4241 if (ret < 0)
4242 return ret;
4243
Chris Masona2135012008-06-25 16:01:30 -04004244 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004245 /*
4246 * by releasing the path above we dropped all our locks. A balance
4247 * could have added more items next to the key that used to be
4248 * at the very end of the block. So, check again here and
4249 * advance the path if there are now more items available.
4250 */
Chris Masona2135012008-06-25 16:01:30 -04004251 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004252 if (ret == 0)
4253 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004254 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004255 goto done;
4256 }
Chris Masond97e63b2007-02-20 16:40:44 -05004257
Chris Masond3977122009-01-05 21:25:51 -05004258 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004259 if (!path->nodes[level]) {
4260 ret = 1;
4261 goto done;
4262 }
Chris Mason5f39d392007-10-15 16:14:19 -04004263
Chris Masond97e63b2007-02-20 16:40:44 -05004264 slot = path->slots[level] + 1;
4265 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004266 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004267 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004268 if (level == BTRFS_MAX_LEVEL) {
4269 ret = 1;
4270 goto done;
4271 }
Chris Masond97e63b2007-02-20 16:40:44 -05004272 continue;
4273 }
Chris Mason5f39d392007-10-15 16:14:19 -04004274
Chris Mason925baed2008-06-25 16:01:30 -04004275 if (next) {
4276 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004277 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004278 }
Chris Mason5f39d392007-10-15 16:14:19 -04004279
Chris Mason8e73f272009-04-03 10:14:18 -04004280 next = c;
4281 ret = read_block_for_search(NULL, root, path, &next, level,
4282 slot, &key);
4283 if (ret == -EAGAIN)
4284 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004285
Chris Mason76a05b32009-05-14 13:24:30 -04004286 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004287 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004288 goto done;
4289 }
4290
Chris Mason5cd57b22008-06-25 16:01:30 -04004291 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004292 ret = btrfs_try_spin_lock(next);
4293 if (!ret) {
4294 btrfs_set_path_blocking(path);
4295 btrfs_tree_lock(next);
4296 if (!force_blocking)
4297 btrfs_clear_path_blocking(path, next);
4298 }
4299 if (force_blocking)
4300 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004301 }
Chris Masond97e63b2007-02-20 16:40:44 -05004302 break;
4303 }
4304 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004305 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004306 level--;
4307 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004308 if (path->locks[level])
4309 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004310
Chris Mason5f39d392007-10-15 16:14:19 -04004311 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004312 path->nodes[level] = next;
4313 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004314 if (!path->skip_locking)
4315 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004316
Chris Masond97e63b2007-02-20 16:40:44 -05004317 if (!level)
4318 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004319
Chris Mason8e73f272009-04-03 10:14:18 -04004320 ret = read_block_for_search(NULL, root, path, &next, level,
4321 0, &key);
4322 if (ret == -EAGAIN)
4323 goto again;
4324
Chris Mason76a05b32009-05-14 13:24:30 -04004325 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004326 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004327 goto done;
4328 }
4329
Chris Mason5cd57b22008-06-25 16:01:30 -04004330 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004331 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004332 ret = btrfs_try_spin_lock(next);
4333 if (!ret) {
4334 btrfs_set_path_blocking(path);
4335 btrfs_tree_lock(next);
4336 if (!force_blocking)
4337 btrfs_clear_path_blocking(path, next);
4338 }
4339 if (force_blocking)
4340 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004341 }
Chris Masond97e63b2007-02-20 16:40:44 -05004342 }
Chris Mason8e73f272009-04-03 10:14:18 -04004343 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004344done:
4345 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004346 path->leave_spinning = old_spinning;
4347 if (!old_spinning)
4348 btrfs_set_path_blocking(path);
4349
4350 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004351}
Chris Mason0b86a832008-03-24 15:01:56 -04004352
Chris Mason3f157a22008-06-25 16:01:31 -04004353/*
4354 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4355 * searching until it gets past min_objectid or finds an item of 'type'
4356 *
4357 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4358 */
Chris Mason0b86a832008-03-24 15:01:56 -04004359int btrfs_previous_item(struct btrfs_root *root,
4360 struct btrfs_path *path, u64 min_objectid,
4361 int type)
4362{
4363 struct btrfs_key found_key;
4364 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004365 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004366 int ret;
4367
Chris Masond3977122009-01-05 21:25:51 -05004368 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004369 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004370 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004371 ret = btrfs_prev_leaf(root, path);
4372 if (ret != 0)
4373 return ret;
4374 } else {
4375 path->slots[0]--;
4376 }
4377 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004378 nritems = btrfs_header_nritems(leaf);
4379 if (nritems == 0)
4380 return 1;
4381 if (path->slots[0] == nritems)
4382 path->slots[0]--;
4383
Chris Mason0b86a832008-03-24 15:01:56 -04004384 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004385 if (found_key.objectid < min_objectid)
4386 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004387 if (found_key.type == type)
4388 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004389 if (found_key.objectid == min_objectid &&
4390 found_key.type < type)
4391 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004392 }
4393 return 1;
4394}