blob: d2431284b913e19f3d6060d56c63d3f55eccc2ef [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);
Chris Masond97e63b2007-02-20 16:40:44 -050041
Chris Mason2c90e5d2007-04-02 10:50:19 -040042struct btrfs_path *btrfs_alloc_path(void)
43{
Chris Masondf24a2b2007-04-04 09:36:31 -040044 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050045 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040046 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040047}
48
Chris Masonb4ce94d2009-02-04 09:25:08 -050049/*
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
52 */
53noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54{
55 int i;
56 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
57 if (p->nodes[i] && p->locks[i])
58 btrfs_set_lock_blocking(p->nodes[i]);
59 }
60}
61
62/*
63 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050064 *
65 * held is used to keep lockdep happy, when lockdep is enabled
66 * we set held to a blocking lock before we go around and
67 * retake all the spinlocks in the path. You can safely use NULL
68 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050069 */
Chris Mason4008c042009-02-12 14:09:45 -050070noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
71 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050072{
73 int i;
Chris Mason4008c042009-02-12 14:09:45 -050074
75#ifdef CONFIG_DEBUG_LOCK_ALLOC
76 /* lockdep really cares that we take all of these spinlocks
77 * in the right order. If any of the locks in the path are not
78 * currently blocking, it is going to complain. So, make really
79 * really sure by forcing the path to blocking before we clear
80 * the path blocking.
81 */
82 if (held)
83 btrfs_set_lock_blocking(held);
84 btrfs_set_path_blocking(p);
85#endif
86
87 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050088 if (p->nodes[i] && p->locks[i])
89 btrfs_clear_lock_blocking(p->nodes[i]);
90 }
Chris Mason4008c042009-02-12 14:09:45 -050091
92#ifdef CONFIG_DEBUG_LOCK_ALLOC
93 if (held)
94 btrfs_clear_lock_blocking(held);
95#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -050096}
97
Chris Masond352ac62008-09-29 15:18:18 -040098/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040099void btrfs_free_path(struct btrfs_path *p)
100{
Jesper Juhlff175d52010-12-25 21:22:30 +0000101 if (!p)
102 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200103 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400104 kmem_cache_free(btrfs_path_cachep, p);
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/*
108 * path release drops references on the extent buffers in the path
109 * and it drops any locks held by this path
110 *
111 * It is safe to call this on paths that no locks or extent buffers held.
112 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200113noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500114{
115 int i;
Chris Masona2135012008-06-25 16:01:30 -0400116
Chris Mason234b63a2007-03-13 10:46:10 -0400117 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400118 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500119 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400120 continue;
121 if (p->locks[i]) {
122 btrfs_tree_unlock(p->nodes[i]);
123 p->locks[i] = 0;
124 }
Chris Mason5f39d392007-10-15 16:14:19 -0400125 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400126 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500127 }
128}
129
Chris Masond352ac62008-09-29 15:18:18 -0400130/*
131 * safely gets a reference on the root node of a tree. A lock
132 * is not taken, so a concurrent writer may put a different node
133 * at the root of the tree. See btrfs_lock_root_node for the
134 * looping required.
135 *
136 * The extent buffer returned by this has a reference taken, so
137 * it won't disappear. It may stop being the root of the tree
138 * at any time because there are no locks held.
139 */
Chris Mason925baed2008-06-25 16:01:30 -0400140struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
141{
142 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400143
144 rcu_read_lock();
145 eb = rcu_dereference(root->node);
Chris Mason925baed2008-06-25 16:01:30 -0400146 extent_buffer_get(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400147 rcu_read_unlock();
Chris Mason925baed2008-06-25 16:01:30 -0400148 return eb;
149}
150
Chris Masond352ac62008-09-29 15:18:18 -0400151/* loop around taking references on and locking the root node of the
152 * tree until you end up with a lock on the root. A locked buffer
153 * is returned, with a reference held.
154 */
Chris Mason925baed2008-06-25 16:01:30 -0400155struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
156{
157 struct extent_buffer *eb;
158
Chris Masond3977122009-01-05 21:25:51 -0500159 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400160 eb = btrfs_root_node(root);
161 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400162 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400163 break;
Chris Mason925baed2008-06-25 16:01:30 -0400164 btrfs_tree_unlock(eb);
165 free_extent_buffer(eb);
166 }
167 return eb;
168}
169
Chris Masond352ac62008-09-29 15:18:18 -0400170/* cowonly root (everything not a reference counted cow subvolume), just get
171 * put onto a simple dirty list. transaction.c walks this to make sure they
172 * get properly updated on disk.
173 */
Chris Mason0b86a832008-03-24 15:01:56 -0400174static void add_root_to_dirty_list(struct btrfs_root *root)
175{
176 if (root->track_dirty && list_empty(&root->dirty_list)) {
177 list_add(&root->dirty_list,
178 &root->fs_info->dirty_cowonly_roots);
179 }
180}
181
Chris Masond352ac62008-09-29 15:18:18 -0400182/*
183 * used by snapshot creation to make a copy of a root for a tree with
184 * a given objectid. The buffer with the new root node is returned in
185 * cow_ret, and this func returns zero on success or a negative error code.
186 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500187int btrfs_copy_root(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root,
189 struct extent_buffer *buf,
190 struct extent_buffer **cow_ret, u64 new_root_objectid)
191{
192 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500193 int ret = 0;
194 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400195 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500196
197 WARN_ON(root->ref_cows && trans->transid !=
198 root->fs_info->running_transaction->transid);
199 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
200
201 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400202 if (level == 0)
203 btrfs_item_key(buf, &disk_key, 0);
204 else
205 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400206
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400207 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
208 new_root_objectid, &disk_key, level,
209 buf->start, 0);
210 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500211 return PTR_ERR(cow);
212
213 copy_extent_buffer(cow, buf, 0, 0, cow->len);
214 btrfs_set_header_bytenr(cow, cow->start);
215 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400216 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
217 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
218 BTRFS_HEADER_FLAG_RELOC);
219 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
220 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
221 else
222 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500223
Yan Zheng2b820322008-11-17 21:11:30 -0500224 write_extent_buffer(cow, root->fs_info->fsid,
225 (unsigned long)btrfs_header_fsid(cow),
226 BTRFS_FSID_SIZE);
227
Chris Masonbe20aa92007-12-17 20:14:01 -0500228 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400229 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
230 ret = btrfs_inc_ref(trans, root, cow, 1);
231 else
232 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500233
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 if (ret)
235 return ret;
236
237 btrfs_mark_buffer_dirty(cow);
238 *cow_ret = cow;
239 return 0;
240}
241
Chris Masond352ac62008-09-29 15:18:18 -0400242/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400243 * check if the tree block can be shared by multiple trees
244 */
245int btrfs_block_can_be_shared(struct btrfs_root *root,
246 struct extent_buffer *buf)
247{
248 /*
249 * Tree blocks not in refernece counted trees and tree roots
250 * are never shared. If a block was allocated after the last
251 * snapshot and the block was not allocated by tree relocation,
252 * we know the block is not shared.
253 */
254 if (root->ref_cows &&
255 buf != root->node && buf != root->commit_root &&
256 (btrfs_header_generation(buf) <=
257 btrfs_root_last_snapshot(&root->root_item) ||
258 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
259 return 1;
260#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
261 if (root->ref_cows &&
262 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
263 return 1;
264#endif
265 return 0;
266}
267
268static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
269 struct btrfs_root *root,
270 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400271 struct extent_buffer *cow,
272 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400273{
274 u64 refs;
275 u64 owner;
276 u64 flags;
277 u64 new_flags = 0;
278 int ret;
279
280 /*
281 * Backrefs update rules:
282 *
283 * Always use full backrefs for extent pointers in tree block
284 * allocated by tree relocation.
285 *
286 * If a shared tree block is no longer referenced by its owner
287 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
288 * use full backrefs for extent pointers in tree block.
289 *
290 * If a tree block is been relocating
291 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
292 * use full backrefs for extent pointers in tree block.
293 * The reason for this is some operations (such as drop tree)
294 * are only allowed for blocks use full backrefs.
295 */
296
297 if (btrfs_block_can_be_shared(root, buf)) {
298 ret = btrfs_lookup_extent_info(trans, root, buf->start,
299 buf->len, &refs, &flags);
300 BUG_ON(ret);
301 BUG_ON(refs == 0);
302 } else {
303 refs = 1;
304 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
305 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
306 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
307 else
308 flags = 0;
309 }
310
311 owner = btrfs_header_owner(buf);
312 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
313 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
314
315 if (refs > 1) {
316 if ((owner == root->root_key.objectid ||
317 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
318 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
319 ret = btrfs_inc_ref(trans, root, buf, 1);
320 BUG_ON(ret);
321
322 if (root->root_key.objectid ==
323 BTRFS_TREE_RELOC_OBJECTID) {
324 ret = btrfs_dec_ref(trans, root, buf, 0);
325 BUG_ON(ret);
326 ret = btrfs_inc_ref(trans, root, cow, 1);
327 BUG_ON(ret);
328 }
329 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
330 } else {
331
332 if (root->root_key.objectid ==
333 BTRFS_TREE_RELOC_OBJECTID)
334 ret = btrfs_inc_ref(trans, root, cow, 1);
335 else
336 ret = btrfs_inc_ref(trans, root, cow, 0);
337 BUG_ON(ret);
338 }
339 if (new_flags != 0) {
340 ret = btrfs_set_disk_extent_flags(trans, root,
341 buf->start,
342 buf->len,
343 new_flags, 0);
344 BUG_ON(ret);
345 }
346 } else {
347 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
348 if (root->root_key.objectid ==
349 BTRFS_TREE_RELOC_OBJECTID)
350 ret = btrfs_inc_ref(trans, root, cow, 1);
351 else
352 ret = btrfs_inc_ref(trans, root, cow, 0);
353 BUG_ON(ret);
354 ret = btrfs_dec_ref(trans, root, buf, 1);
355 BUG_ON(ret);
356 }
357 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400358 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400359 }
360 return 0;
361}
362
363/*
Chris Masond3977122009-01-05 21:25:51 -0500364 * does the dirty work in cow of a single block. The parent block (if
365 * supplied) is updated to point to the new cow copy. The new buffer is marked
366 * dirty and returned locked. If you modify the block it needs to be marked
367 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400368 *
369 * search_start -- an allocation hint for the new block
370 *
Chris Masond3977122009-01-05 21:25:51 -0500371 * empty_size -- a hint that you plan on doing more cow. This is the size in
372 * bytes the allocator should try to find free next to the block it returns.
373 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400374 */
Chris Masond3977122009-01-05 21:25:51 -0500375static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400376 struct btrfs_root *root,
377 struct extent_buffer *buf,
378 struct extent_buffer *parent, int parent_slot,
379 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400380 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400381{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400382 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400383 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500384 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400385 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400386 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400387 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400388
Chris Mason925baed2008-06-25 16:01:30 -0400389 if (*cow_ret == buf)
390 unlock_orig = 1;
391
Chris Masonb9447ef82009-03-09 11:45:38 -0400392 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400393
Chris Mason7bb86312007-12-11 09:25:06 -0500394 WARN_ON(root->ref_cows && trans->transid !=
395 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400396 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400397
Chris Mason7bb86312007-12-11 09:25:06 -0500398 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400399
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400400 if (level == 0)
401 btrfs_item_key(buf, &disk_key, 0);
402 else
403 btrfs_node_key(buf, &disk_key, 0);
404
405 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
406 if (parent)
407 parent_start = parent->start;
408 else
409 parent_start = 0;
410 } else
411 parent_start = 0;
412
413 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
414 root->root_key.objectid, &disk_key,
415 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400416 if (IS_ERR(cow))
417 return PTR_ERR(cow);
418
Chris Masonb4ce94d2009-02-04 09:25:08 -0500419 /* cow is set to blocking by btrfs_init_new_buffer */
420
Chris Mason5f39d392007-10-15 16:14:19 -0400421 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400422 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400423 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400424 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
425 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
426 BTRFS_HEADER_FLAG_RELOC);
427 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
428 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
429 else
430 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400431
Yan Zheng2b820322008-11-17 21:11:30 -0500432 write_extent_buffer(cow, root->fs_info->fsid,
433 (unsigned long)btrfs_header_fsid(cow),
434 BTRFS_FSID_SIZE);
435
Yan, Zhengf0486c62010-05-16 10:46:25 -0400436 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400437
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400438 if (root->ref_cows)
439 btrfs_reloc_cow_block(trans, root, buf, cow);
440
Chris Mason6702ed42007-08-07 16:15:09 -0400441 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400442 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400443 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
444 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
445 parent_start = buf->start;
446 else
447 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400448
Chris Mason5f39d392007-10-15 16:14:19 -0400449 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400450 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400451
Yan, Zhengf0486c62010-05-16 10:46:25 -0400452 btrfs_free_tree_block(trans, root, buf, parent_start,
453 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400454 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400455 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400456 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400457 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
458 parent_start = parent->start;
459 else
460 parent_start = 0;
461
462 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400463 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400464 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500465 btrfs_set_node_ptr_generation(parent, parent_slot,
466 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400467 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400468 btrfs_free_tree_block(trans, root, buf, parent_start,
469 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400470 }
Chris Mason925baed2008-06-25 16:01:30 -0400471 if (unlock_orig)
472 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400473 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400474 btrfs_mark_buffer_dirty(cow);
475 *cow_ret = cow;
476 return 0;
477}
478
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400479static inline int should_cow_block(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root,
481 struct extent_buffer *buf)
482{
483 if (btrfs_header_generation(buf) == trans->transid &&
484 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
485 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
486 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
487 return 0;
488 return 1;
489}
490
Chris Masond352ac62008-09-29 15:18:18 -0400491/*
492 * cows a single block, see __btrfs_cow_block for the real work.
493 * This version of it has extra checks so that a block isn't cow'd more than
494 * once per transaction, as long as it hasn't been written yet
495 */
Chris Masond3977122009-01-05 21:25:51 -0500496noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400497 struct btrfs_root *root, struct extent_buffer *buf,
498 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400499 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500500{
Chris Mason6702ed42007-08-07 16:15:09 -0400501 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400502 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500503
Chris Masonccd467d2007-06-28 15:57:36 -0400504 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500505 printk(KERN_CRIT "trans %llu running %llu\n",
506 (unsigned long long)trans->transid,
507 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400508 root->fs_info->running_transaction->transid);
509 WARN_ON(1);
510 }
511 if (trans->transid != root->fs_info->generation) {
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)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400515 WARN_ON(1);
516 }
Chris Masondc17ff82008-01-08 15:46:30 -0500517
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400518 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500519 *cow_ret = buf;
520 return 0;
521 }
Chris Masonc4876852009-02-04 09:24:25 -0500522
Chris Mason0b86a832008-03-24 15:01:56 -0400523 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500524
525 if (parent)
526 btrfs_set_lock_blocking(parent);
527 btrfs_set_lock_blocking(buf);
528
Chris Masonf510cfe2007-10-15 16:14:48 -0400529 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400530 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000531
532 trace_btrfs_cow_block(root, buf, *cow_ret);
533
Chris Masonf510cfe2007-10-15 16:14:48 -0400534 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400535}
536
Chris Masond352ac62008-09-29 15:18:18 -0400537/*
538 * helper function for defrag to decide if two blocks pointed to by a
539 * node are actually close by
540 */
Chris Mason6b800532007-10-15 16:17:34 -0400541static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400542{
Chris Mason6b800532007-10-15 16:17:34 -0400543 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400544 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400545 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400546 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500547 return 0;
548}
549
Chris Mason081e9572007-11-06 10:26:24 -0500550/*
551 * compare two keys in a memcmp fashion
552 */
553static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
554{
555 struct btrfs_key k1;
556
557 btrfs_disk_key_to_cpu(&k1, disk);
558
Diego Calleja20736ab2009-07-24 11:06:52 -0400559 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500560}
561
Josef Bacikf3465ca2008-11-12 14:19:50 -0500562/*
563 * same as comp_keys only with two btrfs_key's
564 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400565int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500566{
567 if (k1->objectid > k2->objectid)
568 return 1;
569 if (k1->objectid < k2->objectid)
570 return -1;
571 if (k1->type > k2->type)
572 return 1;
573 if (k1->type < k2->type)
574 return -1;
575 if (k1->offset > k2->offset)
576 return 1;
577 if (k1->offset < k2->offset)
578 return -1;
579 return 0;
580}
Chris Mason081e9572007-11-06 10:26:24 -0500581
Chris Masond352ac62008-09-29 15:18:18 -0400582/*
583 * this is used by the defrag code to go through all the
584 * leaves pointed to by a node and reallocate them so that
585 * disk order is close to key order
586 */
Chris Mason6702ed42007-08-07 16:15:09 -0400587int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400588 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400589 int start_slot, int cache_only, u64 *last_ret,
590 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400591{
Chris Mason6b800532007-10-15 16:17:34 -0400592 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400593 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400594 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400595 u64 search_start = *last_ret;
596 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400597 u64 other;
598 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400599 int end_slot;
600 int i;
601 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400602 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400603 int uptodate;
604 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500605 int progress_passed = 0;
606 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400607
Chris Mason5708b952007-10-25 15:43:18 -0400608 parent_level = btrfs_header_level(parent);
609 if (cache_only && parent_level != 1)
610 return 0;
611
Chris Masond3977122009-01-05 21:25:51 -0500612 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400613 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500614 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400615 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400616
Chris Mason6b800532007-10-15 16:17:34 -0400617 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400618 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400619 end_slot = parent_nritems;
620
621 if (parent_nritems == 1)
622 return 0;
623
Chris Masonb4ce94d2009-02-04 09:25:08 -0500624 btrfs_set_lock_blocking(parent);
625
Chris Mason6702ed42007-08-07 16:15:09 -0400626 for (i = start_slot; i < end_slot; i++) {
627 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400628
Chris Mason081e9572007-11-06 10:26:24 -0500629 btrfs_node_key(parent, &disk_key, i);
630 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
631 continue;
632
633 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400634 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400635 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400636 if (last_block == 0)
637 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400638
Chris Mason6702ed42007-08-07 16:15:09 -0400639 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400640 other = btrfs_node_blockptr(parent, i - 1);
641 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400642 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400643 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400644 other = btrfs_node_blockptr(parent, i + 1);
645 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400646 }
Chris Masone9d0b132007-08-10 14:06:19 -0400647 if (close) {
648 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400649 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400650 }
Chris Mason6702ed42007-08-07 16:15:09 -0400651
Chris Mason6b800532007-10-15 16:17:34 -0400652 cur = btrfs_find_tree_block(root, blocknr, blocksize);
653 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400654 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400655 else
656 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400657 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400658 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400659 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400660 continue;
661 }
Chris Mason6b800532007-10-15 16:17:34 -0400662 if (!cur) {
663 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400664 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000665 if (!cur)
666 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400667 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400668 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400669 }
Chris Mason6702ed42007-08-07 16:15:09 -0400670 }
Chris Masone9d0b132007-08-10 14:06:19 -0400671 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400672 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400673
Chris Masone7a84562008-06-25 16:01:31 -0400674 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500675 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400676 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400677 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400678 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400679 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400680 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400681 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400682 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400683 break;
Yan252c38f2007-08-29 09:11:44 -0400684 }
Chris Masone7a84562008-06-25 16:01:31 -0400685 search_start = cur->start;
686 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400687 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400688 btrfs_tree_unlock(cur);
689 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400690 }
691 return err;
692}
693
Chris Mason74123bd2007-02-02 11:05:29 -0500694/*
695 * The leaf data grows from end-to-front in the node.
696 * this returns the address of the start of the last item,
697 * which is the stop of the leaf data stack
698 */
Chris Mason123abc82007-03-14 14:14:43 -0400699static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400700 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500701{
Chris Mason5f39d392007-10-15 16:14:19 -0400702 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500703 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400704 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400705 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500706}
707
Chris Masonaa5d6be2007-02-28 16:35:06 -0500708
Chris Mason74123bd2007-02-02 11:05:29 -0500709/*
Chris Mason5f39d392007-10-15 16:14:19 -0400710 * search for key in the extent_buffer. The items start at offset p,
711 * and they are item_size apart. There are 'max' items in p.
712 *
Chris Mason74123bd2007-02-02 11:05:29 -0500713 * the slot in the array is returned via slot, and it points to
714 * the place where you would insert key if it is not found in
715 * the array.
716 *
717 * slot may point to max if the key is bigger than all of the keys
718 */
Chris Masone02119d2008-09-05 16:13:11 -0400719static noinline int generic_bin_search(struct extent_buffer *eb,
720 unsigned long p,
721 int item_size, struct btrfs_key *key,
722 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723{
724 int low = 0;
725 int high = max;
726 int mid;
727 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400728 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400729 struct btrfs_disk_key unaligned;
730 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400731 char *kaddr = NULL;
732 unsigned long map_start = 0;
733 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400734 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500735
Chris Masond3977122009-01-05 21:25:51 -0500736 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500737 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400738 offset = p + mid * item_size;
739
Chris Masona6591712011-07-19 12:04:14 -0400740 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -0400741 (offset + sizeof(struct btrfs_disk_key)) >
742 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -0500743
744 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400745 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -0400746 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400747
Chris Mason479965d2007-10-15 16:14:27 -0400748 if (!err) {
749 tmp = (struct btrfs_disk_key *)(kaddr + offset -
750 map_start);
751 } else {
752 read_extent_buffer(eb, &unaligned,
753 offset, sizeof(unaligned));
754 tmp = &unaligned;
755 }
756
Chris Mason5f39d392007-10-15 16:14:19 -0400757 } else {
758 tmp = (struct btrfs_disk_key *)(kaddr + offset -
759 map_start);
760 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500761 ret = comp_keys(tmp, key);
762
763 if (ret < 0)
764 low = mid + 1;
765 else if (ret > 0)
766 high = mid;
767 else {
768 *slot = mid;
769 return 0;
770 }
771 }
772 *slot = low;
773 return 1;
774}
775
Chris Mason97571fd2007-02-24 13:39:08 -0500776/*
777 * simple bin_search frontend that does the right thing for
778 * leaves vs nodes
779 */
Chris Mason5f39d392007-10-15 16:14:19 -0400780static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
781 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500782{
Chris Mason5f39d392007-10-15 16:14:19 -0400783 if (level == 0) {
784 return generic_bin_search(eb,
785 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400786 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400787 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400788 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500789 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400790 return generic_bin_search(eb,
791 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400792 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400793 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400794 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500795 }
796 return -1;
797}
798
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400799int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
800 int level, int *slot)
801{
802 return bin_search(eb, key, level, slot);
803}
804
Yan, Zhengf0486c62010-05-16 10:46:25 -0400805static void root_add_used(struct btrfs_root *root, u32 size)
806{
807 spin_lock(&root->accounting_lock);
808 btrfs_set_root_used(&root->root_item,
809 btrfs_root_used(&root->root_item) + size);
810 spin_unlock(&root->accounting_lock);
811}
812
813static void root_sub_used(struct btrfs_root *root, u32 size)
814{
815 spin_lock(&root->accounting_lock);
816 btrfs_set_root_used(&root->root_item,
817 btrfs_root_used(&root->root_item) - size);
818 spin_unlock(&root->accounting_lock);
819}
820
Chris Masond352ac62008-09-29 15:18:18 -0400821/* given a node and slot number, this reads the blocks it points to. The
822 * extent buffer is returned with a reference taken (but unlocked).
823 * NULL is returned on error.
824 */
Chris Masone02119d2008-09-05 16:13:11 -0400825static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400826 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500827{
Chris Masonca7a79a2008-05-12 12:59:19 -0400828 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500829 if (slot < 0)
830 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400831 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500832 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400833
834 BUG_ON(level == 0);
835
Chris Masondb945352007-10-15 16:15:53 -0400836 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400837 btrfs_level_size(root, level - 1),
838 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500839}
840
Chris Masond352ac62008-09-29 15:18:18 -0400841/*
842 * node level balancing, used to make sure nodes are in proper order for
843 * item deletion. We balance from the top down, so we have to make sure
844 * that a deletion won't leave an node completely empty later on.
845 */
Chris Masone02119d2008-09-05 16:13:11 -0400846static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500847 struct btrfs_root *root,
848 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500849{
Chris Mason5f39d392007-10-15 16:14:19 -0400850 struct extent_buffer *right = NULL;
851 struct extent_buffer *mid;
852 struct extent_buffer *left = NULL;
853 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500854 int ret = 0;
855 int wret;
856 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500857 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500858 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500859
860 if (level == 0)
861 return 0;
862
Chris Mason5f39d392007-10-15 16:14:19 -0400863 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500864
Chris Mason925baed2008-06-25 16:01:30 -0400865 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500866 WARN_ON(btrfs_header_generation(mid) != trans->transid);
867
Chris Mason1d4f8a02007-03-13 09:28:32 -0400868 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500869
Chris Mason234b63a2007-03-13 10:46:10 -0400870 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400871 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500872 pslot = path->slots[level + 1];
873
Chris Mason40689472007-03-17 14:29:23 -0400874 /*
875 * deal with the case where there is only one pointer in the root
876 * by promoting the node below to a root
877 */
Chris Mason5f39d392007-10-15 16:14:19 -0400878 if (!parent) {
879 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500880
Chris Mason5f39d392007-10-15 16:14:19 -0400881 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500882 return 0;
883
884 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400885 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500886 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400887 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500888 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400889 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400890 if (ret) {
891 btrfs_tree_unlock(child);
892 free_extent_buffer(child);
893 goto enospc;
894 }
Yan2f375ab2008-02-01 14:58:07 -0500895
Chris Mason240f62c2011-03-23 14:54:42 -0400896 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400897
Chris Mason0b86a832008-03-24 15:01:56 -0400898 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400899 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500900
Chris Mason925baed2008-06-25 16:01:30 -0400901 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500902 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400903 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400904 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500905 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400906 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400907
908 root_sub_used(root, mid->len);
909 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500910 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400911 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400912 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500913 }
Chris Mason5f39d392007-10-15 16:14:19 -0400914 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400915 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500916 return 0;
917
Andi Kleen559af822010-10-29 15:14:37 -0400918 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400919
Chris Mason5f39d392007-10-15 16:14:19 -0400920 left = read_node_slot(root, parent, pslot - 1);
921 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400922 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500923 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400924 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400925 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400926 if (wret) {
927 ret = wret;
928 goto enospc;
929 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400930 }
Chris Mason5f39d392007-10-15 16:14:19 -0400931 right = read_node_slot(root, parent, pslot + 1);
932 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400933 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500934 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400935 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400936 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400937 if (wret) {
938 ret = wret;
939 goto enospc;
940 }
941 }
942
943 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400944 if (left) {
945 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400946 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500947 if (wret < 0)
948 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -0400949 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500950 }
Chris Mason79f95c82007-03-01 15:16:26 -0500951
952 /*
953 * then try to empty the right most buffer into the middle
954 */
Chris Mason5f39d392007-10-15 16:14:19 -0400955 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400956 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400957 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500958 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400959 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400960 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400961 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -0400962 wret = del_ptr(trans, root, path, level + 1, pslot +
963 1);
Chris Masonbb803952007-03-01 12:04:21 -0500964 if (wret)
965 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400966 root_sub_used(root, right->len);
967 btrfs_free_tree_block(trans, root, right, 0, 1);
968 free_extent_buffer(right);
969 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500970 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400971 struct btrfs_disk_key right_key;
972 btrfs_node_key(right, &right_key, 0);
973 btrfs_set_node_key(parent, &right_key, pslot + 1);
974 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500975 }
976 }
Chris Mason5f39d392007-10-15 16:14:19 -0400977 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500978 /*
979 * we're not allowed to leave a node with one item in the
980 * tree during a delete. A deletion from lower in the tree
981 * could try to delete the only pointer in this node.
982 * So, pull some keys from the left.
983 * There has to be a left pointer at this point because
984 * otherwise we would have pulled some pointers from the
985 * right
986 */
Chris Mason5f39d392007-10-15 16:14:19 -0400987 BUG_ON(!left);
988 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400989 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500990 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400991 goto enospc;
992 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400993 if (wret == 1) {
994 wret = push_node_left(trans, root, left, mid, 1);
995 if (wret < 0)
996 ret = wret;
997 }
Chris Mason79f95c82007-03-01 15:16:26 -0500998 BUG_ON(wret == 1);
999 }
Chris Mason5f39d392007-10-15 16:14:19 -04001000 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001001 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001002 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001003 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001004 if (wret)
1005 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001006 root_sub_used(root, mid->len);
1007 btrfs_free_tree_block(trans, root, mid, 0, 1);
1008 free_extent_buffer(mid);
1009 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001010 } else {
1011 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001012 struct btrfs_disk_key mid_key;
1013 btrfs_node_key(mid, &mid_key, 0);
1014 btrfs_set_node_key(parent, &mid_key, pslot);
1015 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001016 }
Chris Masonbb803952007-03-01 12:04:21 -05001017
Chris Mason79f95c82007-03-01 15:16:26 -05001018 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001019 if (left) {
1020 if (btrfs_header_nritems(left) > orig_slot) {
1021 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001022 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001023 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001024 path->slots[level + 1] -= 1;
1025 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001026 if (mid) {
1027 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001028 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001029 }
Chris Masonbb803952007-03-01 12:04:21 -05001030 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001031 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001032 path->slots[level] = orig_slot;
1033 }
1034 }
Chris Mason79f95c82007-03-01 15:16:26 -05001035 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001036 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001037 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001038 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001039enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001040 if (right) {
1041 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001042 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001043 }
1044 if (left) {
1045 if (path->nodes[level] != left)
1046 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001047 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001048 }
Chris Masonbb803952007-03-01 12:04:21 -05001049 return ret;
1050}
1051
Chris Masond352ac62008-09-29 15:18:18 -04001052/* Node balancing for insertion. Here we only split or push nodes around
1053 * when they are completely full. This is also done top down, so we
1054 * have to be pessimistic.
1055 */
Chris Masond3977122009-01-05 21:25:51 -05001056static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001057 struct btrfs_root *root,
1058 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001059{
Chris Mason5f39d392007-10-15 16:14:19 -04001060 struct extent_buffer *right = NULL;
1061 struct extent_buffer *mid;
1062 struct extent_buffer *left = NULL;
1063 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001064 int ret = 0;
1065 int wret;
1066 int pslot;
1067 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001068
1069 if (level == 0)
1070 return 1;
1071
Chris Mason5f39d392007-10-15 16:14:19 -04001072 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001073 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001074
1075 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001076 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001077 pslot = path->slots[level + 1];
1078
Chris Mason5f39d392007-10-15 16:14:19 -04001079 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001080 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001081
Chris Mason5f39d392007-10-15 16:14:19 -04001082 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001083
1084 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001085 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001086 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001087
1088 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001089 btrfs_set_lock_blocking(left);
1090
Chris Mason5f39d392007-10-15 16:14:19 -04001091 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001092 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1093 wret = 1;
1094 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001095 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001096 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001097 if (ret)
1098 wret = 1;
1099 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001100 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001101 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001102 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001103 }
Chris Masone66f7092007-04-20 13:16:02 -04001104 if (wret < 0)
1105 ret = wret;
1106 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001107 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001108 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001109 btrfs_node_key(mid, &disk_key, 0);
1110 btrfs_set_node_key(parent, &disk_key, pslot);
1111 btrfs_mark_buffer_dirty(parent);
1112 if (btrfs_header_nritems(left) > orig_slot) {
1113 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001114 path->slots[level + 1] -= 1;
1115 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001116 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001117 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001118 } else {
1119 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001120 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001121 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001122 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001123 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001124 }
Chris Masone66f7092007-04-20 13:16:02 -04001125 return 0;
1126 }
Chris Mason925baed2008-06-25 16:01:30 -04001127 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001128 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001129 }
Chris Mason925baed2008-06-25 16:01:30 -04001130 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001131
1132 /*
1133 * then try to empty the right most buffer into the middle
1134 */
Chris Mason5f39d392007-10-15 16:14:19 -04001135 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001136 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001137
Chris Mason925baed2008-06-25 16:01:30 -04001138 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001139 btrfs_set_lock_blocking(right);
1140
Chris Mason5f39d392007-10-15 16:14:19 -04001141 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001142 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1143 wret = 1;
1144 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001145 ret = btrfs_cow_block(trans, root, right,
1146 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001147 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001148 if (ret)
1149 wret = 1;
1150 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001151 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001152 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001153 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001154 }
Chris Masone66f7092007-04-20 13:16:02 -04001155 if (wret < 0)
1156 ret = wret;
1157 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001158 struct btrfs_disk_key disk_key;
1159
1160 btrfs_node_key(right, &disk_key, 0);
1161 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1162 btrfs_mark_buffer_dirty(parent);
1163
1164 if (btrfs_header_nritems(mid) <= orig_slot) {
1165 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001166 path->slots[level + 1] += 1;
1167 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001168 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001169 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001170 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001171 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001172 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001173 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001174 }
Chris Masone66f7092007-04-20 13:16:02 -04001175 return 0;
1176 }
Chris Mason925baed2008-06-25 16:01:30 -04001177 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001178 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001179 }
Chris Masone66f7092007-04-20 13:16:02 -04001180 return 1;
1181}
1182
Chris Mason74123bd2007-02-02 11:05:29 -05001183/*
Chris Masond352ac62008-09-29 15:18:18 -04001184 * readahead one full node of leaves, finding things that are close
1185 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001186 */
Chris Masonc8c42862009-04-03 10:14:18 -04001187static void reada_for_search(struct btrfs_root *root,
1188 struct btrfs_path *path,
1189 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001190{
Chris Mason5f39d392007-10-15 16:14:19 -04001191 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001192 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001193 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001194 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001195 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001196 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001197 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001198 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001199 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001200 u32 nr;
1201 u32 blocksize;
1202 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001203
Chris Masona6b6e752007-10-15 16:22:39 -04001204 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001205 return;
1206
Chris Mason6702ed42007-08-07 16:15:09 -04001207 if (!path->nodes[level])
1208 return;
1209
Chris Mason5f39d392007-10-15 16:14:19 -04001210 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001211
Chris Mason3c69fae2007-08-07 15:52:22 -04001212 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001213 blocksize = btrfs_level_size(root, level - 1);
1214 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001215 if (eb) {
1216 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001217 return;
1218 }
1219
Chris Masona7175312009-01-22 09:23:10 -05001220 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001221
Chris Mason5f39d392007-10-15 16:14:19 -04001222 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001223 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001224
Chris Masond3977122009-01-05 21:25:51 -05001225 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001226 if (direction < 0) {
1227 if (nr == 0)
1228 break;
1229 nr--;
1230 } else if (direction > 0) {
1231 nr++;
1232 if (nr >= nritems)
1233 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001234 }
Chris Mason01f46652007-12-21 16:24:26 -05001235 if (path->reada < 0 && objectid) {
1236 btrfs_node_key(node, &disk_key, nr);
1237 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1238 break;
1239 }
Chris Mason6b800532007-10-15 16:17:34 -04001240 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001241 if ((search <= target && target - search <= 65536) ||
1242 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001243 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001244 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001245 nread += blocksize;
1246 }
1247 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001248 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001249 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001250 }
1251}
Chris Mason925baed2008-06-25 16:01:30 -04001252
Chris Masond352ac62008-09-29 15:18:18 -04001253/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001254 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1255 * cache
1256 */
1257static noinline int reada_for_balance(struct btrfs_root *root,
1258 struct btrfs_path *path, int level)
1259{
1260 int slot;
1261 int nritems;
1262 struct extent_buffer *parent;
1263 struct extent_buffer *eb;
1264 u64 gen;
1265 u64 block1 = 0;
1266 u64 block2 = 0;
1267 int ret = 0;
1268 int blocksize;
1269
Chris Mason8c594ea2009-04-20 15:50:10 -04001270 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001271 if (!parent)
1272 return 0;
1273
1274 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001275 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001276 blocksize = btrfs_level_size(root, level);
1277
1278 if (slot > 0) {
1279 block1 = btrfs_node_blockptr(parent, slot - 1);
1280 gen = btrfs_node_ptr_generation(parent, slot - 1);
1281 eb = btrfs_find_tree_block(root, block1, blocksize);
1282 if (eb && btrfs_buffer_uptodate(eb, gen))
1283 block1 = 0;
1284 free_extent_buffer(eb);
1285 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001286 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001287 block2 = btrfs_node_blockptr(parent, slot + 1);
1288 gen = btrfs_node_ptr_generation(parent, slot + 1);
1289 eb = btrfs_find_tree_block(root, block2, blocksize);
1290 if (eb && btrfs_buffer_uptodate(eb, gen))
1291 block2 = 0;
1292 free_extent_buffer(eb);
1293 }
1294 if (block1 || block2) {
1295 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001296
1297 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001298 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001299
1300 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001301 if (block1)
1302 readahead_tree_block(root, block1, blocksize, 0);
1303 if (block2)
1304 readahead_tree_block(root, block2, blocksize, 0);
1305
1306 if (block1) {
1307 eb = read_tree_block(root, block1, blocksize, 0);
1308 free_extent_buffer(eb);
1309 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001310 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001311 eb = read_tree_block(root, block2, blocksize, 0);
1312 free_extent_buffer(eb);
1313 }
1314 }
1315 return ret;
1316}
1317
1318
1319/*
Chris Masond3977122009-01-05 21:25:51 -05001320 * when we walk down the tree, it is usually safe to unlock the higher layers
1321 * in the tree. The exceptions are when our path goes through slot 0, because
1322 * operations on the tree might require changing key pointers higher up in the
1323 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001324 *
Chris Masond3977122009-01-05 21:25:51 -05001325 * callers might also have set path->keep_locks, which tells this code to keep
1326 * the lock if the path points to the last slot in the block. This is part of
1327 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001328 *
Chris Masond3977122009-01-05 21:25:51 -05001329 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1330 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001331 */
Chris Masone02119d2008-09-05 16:13:11 -04001332static noinline void unlock_up(struct btrfs_path *path, int level,
1333 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001334{
1335 int i;
1336 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001337 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001338 struct extent_buffer *t;
1339
1340 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1341 if (!path->nodes[i])
1342 break;
1343 if (!path->locks[i])
1344 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001345 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001346 skip_level = i + 1;
1347 continue;
1348 }
Chris Mason051e1b92008-06-25 16:01:30 -04001349 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001350 u32 nritems;
1351 t = path->nodes[i];
1352 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001353 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001354 skip_level = i + 1;
1355 continue;
1356 }
1357 }
Chris Mason051e1b92008-06-25 16:01:30 -04001358 if (skip_level < i && i >= lowest_unlock)
1359 no_skips = 1;
1360
Chris Mason925baed2008-06-25 16:01:30 -04001361 t = path->nodes[i];
1362 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1363 btrfs_tree_unlock(t);
1364 path->locks[i] = 0;
1365 }
1366 }
1367}
1368
Chris Mason3c69fae2007-08-07 15:52:22 -04001369/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001370 * This releases any locks held in the path starting at level and
1371 * going all the way up to the root.
1372 *
1373 * btrfs_search_slot will keep the lock held on higher nodes in a few
1374 * corner cases, such as COW of the block at slot zero in the node. This
1375 * ignores those rules, and it should only be called when there are no
1376 * more updates to be done higher up in the tree.
1377 */
1378noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1379{
1380 int i;
1381
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001382 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001383 return;
1384
1385 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1386 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001387 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001388 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001389 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001390 btrfs_tree_unlock(path->nodes[i]);
1391 path->locks[i] = 0;
1392 }
1393}
1394
1395/*
Chris Masonc8c42862009-04-03 10:14:18 -04001396 * helper function for btrfs_search_slot. The goal is to find a block
1397 * in cache without setting the path to blocking. If we find the block
1398 * we return zero and the path is unchanged.
1399 *
1400 * If we can't find the block, we set the path blocking and do some
1401 * reada. -EAGAIN is returned and the search must be repeated.
1402 */
1403static int
1404read_block_for_search(struct btrfs_trans_handle *trans,
1405 struct btrfs_root *root, struct btrfs_path *p,
1406 struct extent_buffer **eb_ret, int level, int slot,
1407 struct btrfs_key *key)
1408{
1409 u64 blocknr;
1410 u64 gen;
1411 u32 blocksize;
1412 struct extent_buffer *b = *eb_ret;
1413 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001414 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001415
1416 blocknr = btrfs_node_blockptr(b, slot);
1417 gen = btrfs_node_ptr_generation(b, slot);
1418 blocksize = btrfs_level_size(root, level - 1);
1419
1420 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001421 if (tmp) {
1422 if (btrfs_buffer_uptodate(tmp, 0)) {
1423 if (btrfs_buffer_uptodate(tmp, gen)) {
1424 /*
1425 * we found an up to date block without
1426 * sleeping, return
1427 * right away
1428 */
1429 *eb_ret = tmp;
1430 return 0;
1431 }
1432 /* the pages were up to date, but we failed
1433 * the generation number check. Do a full
1434 * read for the generation number that is correct.
1435 * We must do this without dropping locks so
1436 * we can trust our generation number
1437 */
1438 free_extent_buffer(tmp);
1439 tmp = read_tree_block(root, blocknr, blocksize, gen);
1440 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1441 *eb_ret = tmp;
1442 return 0;
1443 }
1444 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001445 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001446 return -EIO;
1447 }
Chris Masonc8c42862009-04-03 10:14:18 -04001448 }
1449
1450 /*
1451 * reduce lock contention at high levels
1452 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001453 * we read. Don't release the lock on the current
1454 * level because we need to walk this node to figure
1455 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001456 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001457 btrfs_unlock_up_safe(p, level + 1);
1458 btrfs_set_path_blocking(p);
1459
Chris Masoncb449212010-10-24 11:01:27 -04001460 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001461 if (p->reada)
1462 reada_for_search(root, p, level, slot, key->objectid);
1463
David Sterbab3b4aa72011-04-21 01:20:15 +02001464 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001465
1466 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001467 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001468 if (tmp) {
1469 /*
1470 * If the read above didn't mark this buffer up to date,
1471 * it will never end up being up to date. Set ret to EIO now
1472 * and give up so that our caller doesn't loop forever
1473 * on our EAGAINs.
1474 */
1475 if (!btrfs_buffer_uptodate(tmp, 0))
1476 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001477 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001478 }
1479 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001480}
1481
1482/*
1483 * helper function for btrfs_search_slot. This does all of the checks
1484 * for node-level blocks and does any balancing required based on
1485 * the ins_len.
1486 *
1487 * If no extra work was required, zero is returned. If we had to
1488 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1489 * start over
1490 */
1491static int
1492setup_nodes_for_search(struct btrfs_trans_handle *trans,
1493 struct btrfs_root *root, struct btrfs_path *p,
1494 struct extent_buffer *b, int level, int ins_len)
1495{
1496 int ret;
1497 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1498 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1499 int sret;
1500
1501 sret = reada_for_balance(root, p, level);
1502 if (sret)
1503 goto again;
1504
1505 btrfs_set_path_blocking(p);
1506 sret = split_node(trans, root, p, level);
1507 btrfs_clear_path_blocking(p, NULL);
1508
1509 BUG_ON(sret > 0);
1510 if (sret) {
1511 ret = sret;
1512 goto done;
1513 }
1514 b = p->nodes[level];
1515 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001516 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001517 int sret;
1518
1519 sret = reada_for_balance(root, p, level);
1520 if (sret)
1521 goto again;
1522
1523 btrfs_set_path_blocking(p);
1524 sret = balance_level(trans, root, p, level);
1525 btrfs_clear_path_blocking(p, NULL);
1526
1527 if (sret) {
1528 ret = sret;
1529 goto done;
1530 }
1531 b = p->nodes[level];
1532 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001533 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001534 goto again;
1535 }
1536 BUG_ON(btrfs_header_nritems(b) == 1);
1537 }
1538 return 0;
1539
1540again:
1541 ret = -EAGAIN;
1542done:
1543 return ret;
1544}
1545
1546/*
Chris Mason74123bd2007-02-02 11:05:29 -05001547 * look for key in the tree. path is filled in with nodes along the way
1548 * if key is found, we return zero and you can find the item in the leaf
1549 * level of the path (level 0)
1550 *
1551 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001552 * be inserted, and 1 is returned. If there are other errors during the
1553 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001554 *
1555 * if ins_len > 0, nodes and leaves will be split as we walk down the
1556 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1557 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001558 */
Chris Masone089f052007-03-16 16:20:31 -04001559int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1560 *root, struct btrfs_key *key, struct btrfs_path *p, int
1561 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001562{
Chris Mason5f39d392007-10-15 16:14:19 -04001563 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001564 int slot;
1565 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001566 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001567 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001568 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001569 u8 lowest_level = 0;
1570
Chris Mason6702ed42007-08-07 16:15:09 -04001571 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001572 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001573 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001574
Chris Mason925baed2008-06-25 16:01:30 -04001575 if (ins_len < 0)
1576 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001577
Chris Masonbb803952007-03-01 12:04:21 -05001578again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001579 if (p->search_commit_root) {
1580 b = root->commit_root;
1581 extent_buffer_get(b);
1582 if (!p->skip_locking)
1583 btrfs_tree_lock(b);
1584 } else {
1585 if (p->skip_locking)
1586 b = btrfs_root_node(root);
1587 else
1588 b = btrfs_lock_root_node(root);
1589 }
Chris Mason925baed2008-06-25 16:01:30 -04001590
Chris Masoneb60cea2007-02-02 09:18:22 -05001591 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001592 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001593
1594 /*
1595 * setup the path here so we can release it under lock
1596 * contention with the cow code
1597 */
1598 p->nodes[level] = b;
1599 if (!p->skip_locking)
1600 p->locks[level] = 1;
1601
Chris Mason02217ed2007-03-02 16:08:05 -05001602 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001603 /*
1604 * if we don't really need to cow this block
1605 * then we don't want to set the path blocking,
1606 * so we test it here
1607 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001608 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001609 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001610
Chris Masonb4ce94d2009-02-04 09:25:08 -05001611 btrfs_set_path_blocking(p);
1612
Yan Zheng33c66f42009-07-22 09:59:00 -04001613 err = btrfs_cow_block(trans, root, b,
1614 p->nodes[level + 1],
1615 p->slots[level + 1], &b);
1616 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001617 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001618 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001619 }
Chris Mason02217ed2007-03-02 16:08:05 -05001620 }
Chris Mason65b51a02008-08-01 15:11:20 -04001621cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001622 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001623
Chris Masoneb60cea2007-02-02 09:18:22 -05001624 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001625 if (!p->skip_locking)
1626 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001627
Chris Mason4008c042009-02-12 14:09:45 -05001628 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001629
1630 /*
1631 * we have a lock on b and as long as we aren't changing
1632 * the tree, there is no way to for the items in b to change.
1633 * It is safe to drop the lock on our parent before we
1634 * go through the expensive btree search on b.
1635 *
1636 * If cow is true, then we might be changing slot zero,
1637 * which may require changing the parent. So, we can't
1638 * drop the lock until after we know which slot we're
1639 * operating on.
1640 */
1641 if (!cow)
1642 btrfs_unlock_up_safe(p, level + 1);
1643
Chris Mason5f39d392007-10-15 16:14:19 -04001644 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001645
Chris Mason5f39d392007-10-15 16:14:19 -04001646 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001647 int dec = 0;
1648 if (ret && slot > 0) {
1649 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001650 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001651 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001652 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001653 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001654 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001655 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001656 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001657 if (err) {
1658 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001659 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001660 }
Chris Masonc8c42862009-04-03 10:14:18 -04001661 b = p->nodes[level];
1662 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001663
Chris Masonf9efa9c2008-06-25 16:14:04 -04001664 unlock_up(p, level, lowest_unlock);
1665
Chris Mason925baed2008-06-25 16:01:30 -04001666 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001667 if (dec)
1668 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001669 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001670 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001671
Yan Zheng33c66f42009-07-22 09:59:00 -04001672 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001673 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001674 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001675 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001676 if (err) {
1677 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001678 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001679 }
Chris Mason76a05b32009-05-14 13:24:30 -04001680
Chris Masonb4ce94d2009-02-04 09:25:08 -05001681 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001682 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001683 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001684
Yan Zheng33c66f42009-07-22 09:59:00 -04001685 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001686 btrfs_set_path_blocking(p);
1687 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001688 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001689 }
1690 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001691 } else {
1692 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001693 if (ins_len > 0 &&
1694 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001695 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001696 err = split_leaf(trans, root, key,
1697 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001698 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001699
Yan Zheng33c66f42009-07-22 09:59:00 -04001700 BUG_ON(err > 0);
1701 if (err) {
1702 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001703 goto done;
1704 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001705 }
Chris Mason459931e2008-12-10 09:10:46 -05001706 if (!p->search_for_split)
1707 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001708 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001709 }
1710 }
Chris Mason65b51a02008-08-01 15:11:20 -04001711 ret = 1;
1712done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001713 /*
1714 * we don't really know what they plan on doing with the path
1715 * from here on, so for now just mark it as blocking
1716 */
Chris Masonb9473432009-03-13 11:00:37 -04001717 if (!p->leave_spinning)
1718 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001719 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001720 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001721 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001722}
1723
Chris Mason74123bd2007-02-02 11:05:29 -05001724/*
1725 * adjust the pointers going up the tree, starting at level
1726 * making sure the right key of each node is points to 'key'.
1727 * This is used after shifting pointers to the left, so it stops
1728 * fixing up pointers when a given leaf/node is not in slot 0 of the
1729 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001730 *
1731 * If this fails to write a tree block, it returns -1, but continues
1732 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001733 */
Chris Mason5f39d392007-10-15 16:14:19 -04001734static int fixup_low_keys(struct btrfs_trans_handle *trans,
1735 struct btrfs_root *root, struct btrfs_path *path,
1736 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001737{
1738 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001739 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001740 struct extent_buffer *t;
1741
Chris Mason234b63a2007-03-13 10:46:10 -04001742 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001743 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001744 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001745 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001746 t = path->nodes[i];
1747 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001748 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001749 if (tslot != 0)
1750 break;
1751 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001752 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001753}
1754
Chris Mason74123bd2007-02-02 11:05:29 -05001755/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001756 * update item key.
1757 *
1758 * This function isn't completely safe. It's the caller's responsibility
1759 * that the new key won't break the order
1760 */
1761int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1762 struct btrfs_root *root, struct btrfs_path *path,
1763 struct btrfs_key *new_key)
1764{
1765 struct btrfs_disk_key disk_key;
1766 struct extent_buffer *eb;
1767 int slot;
1768
1769 eb = path->nodes[0];
1770 slot = path->slots[0];
1771 if (slot > 0) {
1772 btrfs_item_key(eb, &disk_key, slot - 1);
1773 if (comp_keys(&disk_key, new_key) >= 0)
1774 return -1;
1775 }
1776 if (slot < btrfs_header_nritems(eb) - 1) {
1777 btrfs_item_key(eb, &disk_key, slot + 1);
1778 if (comp_keys(&disk_key, new_key) <= 0)
1779 return -1;
1780 }
1781
1782 btrfs_cpu_key_to_disk(&disk_key, new_key);
1783 btrfs_set_item_key(eb, &disk_key, slot);
1784 btrfs_mark_buffer_dirty(eb);
1785 if (slot == 0)
1786 fixup_low_keys(trans, root, path, &disk_key, 1);
1787 return 0;
1788}
1789
1790/*
Chris Mason74123bd2007-02-02 11:05:29 -05001791 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001792 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001793 *
1794 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1795 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001796 */
Chris Mason98ed5172008-01-03 10:01:48 -05001797static int push_node_left(struct btrfs_trans_handle *trans,
1798 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001799 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001800{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001801 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001802 int src_nritems;
1803 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001804 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001805
Chris Mason5f39d392007-10-15 16:14:19 -04001806 src_nritems = btrfs_header_nritems(src);
1807 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001808 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001809 WARN_ON(btrfs_header_generation(src) != trans->transid);
1810 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001811
Chris Masonbce4eae2008-04-24 14:42:46 -04001812 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001813 return 1;
1814
Chris Masond3977122009-01-05 21:25:51 -05001815 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001816 return 1;
1817
Chris Masonbce4eae2008-04-24 14:42:46 -04001818 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001819 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001820 if (push_items < src_nritems) {
1821 /* leave at least 8 pointers in the node if
1822 * we aren't going to empty it
1823 */
1824 if (src_nritems - push_items < 8) {
1825 if (push_items <= 8)
1826 return 1;
1827 push_items -= 8;
1828 }
1829 }
1830 } else
1831 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001832
Chris Mason5f39d392007-10-15 16:14:19 -04001833 copy_extent_buffer(dst, src,
1834 btrfs_node_key_ptr_offset(dst_nritems),
1835 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001836 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001837
Chris Masonbb803952007-03-01 12:04:21 -05001838 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001839 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1840 btrfs_node_key_ptr_offset(push_items),
1841 (src_nritems - push_items) *
1842 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001843 }
Chris Mason5f39d392007-10-15 16:14:19 -04001844 btrfs_set_header_nritems(src, src_nritems - push_items);
1845 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1846 btrfs_mark_buffer_dirty(src);
1847 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001848
Chris Masonbb803952007-03-01 12:04:21 -05001849 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001850}
1851
Chris Mason97571fd2007-02-24 13:39:08 -05001852/*
Chris Mason79f95c82007-03-01 15:16:26 -05001853 * try to push data from one node into the next node right in the
1854 * tree.
1855 *
1856 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1857 * error, and > 0 if there was no room in the right hand block.
1858 *
1859 * this will only push up to 1/2 the contents of the left node over
1860 */
Chris Mason5f39d392007-10-15 16:14:19 -04001861static int balance_node_right(struct btrfs_trans_handle *trans,
1862 struct btrfs_root *root,
1863 struct extent_buffer *dst,
1864 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001865{
Chris Mason79f95c82007-03-01 15:16:26 -05001866 int push_items = 0;
1867 int max_push;
1868 int src_nritems;
1869 int dst_nritems;
1870 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001871
Chris Mason7bb86312007-12-11 09:25:06 -05001872 WARN_ON(btrfs_header_generation(src) != trans->transid);
1873 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1874
Chris Mason5f39d392007-10-15 16:14:19 -04001875 src_nritems = btrfs_header_nritems(src);
1876 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001877 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001878 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001879 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001880
Chris Masond3977122009-01-05 21:25:51 -05001881 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001882 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001883
1884 max_push = src_nritems / 2 + 1;
1885 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001886 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001887 return 1;
Yan252c38f2007-08-29 09:11:44 -04001888
Chris Mason79f95c82007-03-01 15:16:26 -05001889 if (max_push < push_items)
1890 push_items = max_push;
1891
Chris Mason5f39d392007-10-15 16:14:19 -04001892 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1893 btrfs_node_key_ptr_offset(0),
1894 (dst_nritems) *
1895 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001896
Chris Mason5f39d392007-10-15 16:14:19 -04001897 copy_extent_buffer(dst, src,
1898 btrfs_node_key_ptr_offset(0),
1899 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001900 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001901
Chris Mason5f39d392007-10-15 16:14:19 -04001902 btrfs_set_header_nritems(src, src_nritems - push_items);
1903 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001904
Chris Mason5f39d392007-10-15 16:14:19 -04001905 btrfs_mark_buffer_dirty(src);
1906 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001907
Chris Mason79f95c82007-03-01 15:16:26 -05001908 return ret;
1909}
1910
1911/*
Chris Mason97571fd2007-02-24 13:39:08 -05001912 * helper function to insert a new root level in the tree.
1913 * A new node is allocated, and a single item is inserted to
1914 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001915 *
1916 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001917 */
Chris Masond3977122009-01-05 21:25:51 -05001918static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001919 struct btrfs_root *root,
1920 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001921{
Chris Mason7bb86312007-12-11 09:25:06 -05001922 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001923 struct extent_buffer *lower;
1924 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001925 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001926 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001927
1928 BUG_ON(path->nodes[level]);
1929 BUG_ON(path->nodes[level-1] != root->node);
1930
Chris Mason7bb86312007-12-11 09:25:06 -05001931 lower = path->nodes[level-1];
1932 if (level == 1)
1933 btrfs_item_key(lower, &lower_key, 0);
1934 else
1935 btrfs_node_key(lower, &lower_key, 0);
1936
Zheng Yan31840ae2008-09-23 13:14:14 -04001937 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001938 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001939 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001940 if (IS_ERR(c))
1941 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001942
Yan, Zhengf0486c62010-05-16 10:46:25 -04001943 root_add_used(root, root->nodesize);
1944
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001945 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04001946 btrfs_set_header_nritems(c, 1);
1947 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001948 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001949 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001950 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04001951 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001952
Chris Mason5f39d392007-10-15 16:14:19 -04001953 write_extent_buffer(c, root->fs_info->fsid,
1954 (unsigned long)btrfs_header_fsid(c),
1955 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001956
1957 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1958 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1959 BTRFS_UUID_SIZE);
1960
Chris Mason5f39d392007-10-15 16:14:19 -04001961 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001962 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001963 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001964 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001965
1966 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001967
1968 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001969
Chris Mason925baed2008-06-25 16:01:30 -04001970 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04001971 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04001972
1973 /* the super has an extra ref to root->node */
1974 free_extent_buffer(old);
1975
Chris Mason0b86a832008-03-24 15:01:56 -04001976 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001977 extent_buffer_get(c);
1978 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001979 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001980 path->slots[level] = 0;
1981 return 0;
1982}
1983
Chris Mason74123bd2007-02-02 11:05:29 -05001984/*
1985 * worker function to insert a single pointer in a node.
1986 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001987 *
Chris Mason74123bd2007-02-02 11:05:29 -05001988 * slot and level indicate where you want the key to go, and
1989 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001990 *
1991 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001992 */
Chris Masone089f052007-03-16 16:20:31 -04001993static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1994 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001995 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001996{
Chris Mason5f39d392007-10-15 16:14:19 -04001997 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001998 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001999
2000 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002001 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002002 lower = path->nodes[level];
2003 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002004 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002005 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002006 BUG();
2007 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002008 memmove_extent_buffer(lower,
2009 btrfs_node_key_ptr_offset(slot + 1),
2010 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002011 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002012 }
Chris Mason5f39d392007-10-15 16:14:19 -04002013 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002014 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002015 WARN_ON(trans->transid == 0);
2016 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002017 btrfs_set_header_nritems(lower, nritems + 1);
2018 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002019 return 0;
2020}
2021
Chris Mason97571fd2007-02-24 13:39:08 -05002022/*
2023 * split the node at the specified level in path in two.
2024 * The path is corrected to point to the appropriate node after the split
2025 *
2026 * Before splitting this tries to make some room in the node by pushing
2027 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002028 *
2029 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002030 */
Chris Masone02119d2008-09-05 16:13:11 -04002031static noinline int split_node(struct btrfs_trans_handle *trans,
2032 struct btrfs_root *root,
2033 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002034{
Chris Mason5f39d392007-10-15 16:14:19 -04002035 struct extent_buffer *c;
2036 struct extent_buffer *split;
2037 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002038 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002039 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002040 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002041 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002044 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002045 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002046 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002047 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002048 if (ret)
2049 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002050 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002051 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002052 c = path->nodes[level];
2053 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002054 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002055 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002056 if (ret < 0)
2057 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002058 }
Chris Masone66f7092007-04-20 13:16:02 -04002059
Chris Mason5f39d392007-10-15 16:14:19 -04002060 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002061 mid = (c_nritems + 1) / 2;
2062 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002063
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002065 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002066 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002067 if (IS_ERR(split))
2068 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002069
Yan, Zhengf0486c62010-05-16 10:46:25 -04002070 root_add_used(root, root->nodesize);
2071
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002072 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002073 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002074 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002075 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002077 btrfs_set_header_owner(split, root->root_key.objectid);
2078 write_extent_buffer(split, root->fs_info->fsid,
2079 (unsigned long)btrfs_header_fsid(split),
2080 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002081 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2082 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2083 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002084
Chris Mason5f39d392007-10-15 16:14:19 -04002085
2086 copy_extent_buffer(split, c,
2087 btrfs_node_key_ptr_offset(0),
2088 btrfs_node_key_ptr_offset(mid),
2089 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2090 btrfs_set_header_nritems(split, c_nritems - mid);
2091 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002092 ret = 0;
2093
Chris Mason5f39d392007-10-15 16:14:19 -04002094 btrfs_mark_buffer_dirty(c);
2095 btrfs_mark_buffer_dirty(split);
2096
Chris Masondb945352007-10-15 16:15:53 -04002097 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002098 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002099 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002100 if (wret)
2101 ret = wret;
2102
Chris Mason5de08d72007-02-24 06:24:44 -05002103 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002104 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002105 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 free_extent_buffer(c);
2107 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002108 path->slots[level + 1] += 1;
2109 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002110 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002111 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002112 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002113 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002114}
2115
Chris Mason74123bd2007-02-02 11:05:29 -05002116/*
2117 * how many bytes are required to store the items in a leaf. start
2118 * and nr indicate which items in the leaf to check. This totals up the
2119 * space used both by the item structs and the item data
2120 */
Chris Mason5f39d392007-10-15 16:14:19 -04002121static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002122{
2123 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002124 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002125 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002126
2127 if (!nr)
2128 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002129 data_len = btrfs_item_end_nr(l, start);
2130 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002131 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002132 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002133 return data_len;
2134}
2135
Chris Mason74123bd2007-02-02 11:05:29 -05002136/*
Chris Masond4dbff92007-04-04 14:08:15 -04002137 * The space between the end of the leaf items and
2138 * the start of the leaf data. IOW, how much room
2139 * the leaf has left for both items and data
2140 */
Chris Masond3977122009-01-05 21:25:51 -05002141noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002142 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002143{
Chris Mason5f39d392007-10-15 16:14:19 -04002144 int nritems = btrfs_header_nritems(leaf);
2145 int ret;
2146 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2147 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002148 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2149 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002150 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002151 leaf_space_used(leaf, 0, nritems), nritems);
2152 }
2153 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002154}
2155
Chris Mason99d8f832010-07-07 10:51:48 -04002156/*
2157 * min slot controls the lowest index we're willing to push to the
2158 * right. We'll push up to and including min_slot, but no lower
2159 */
Chris Mason44871b12009-03-13 10:04:31 -04002160static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2161 struct btrfs_root *root,
2162 struct btrfs_path *path,
2163 int data_size, int empty,
2164 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002165 int free_space, u32 left_nritems,
2166 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002167{
Chris Mason5f39d392007-10-15 16:14:19 -04002168 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002169 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002170 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002171 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002172 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002173 int push_space = 0;
2174 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002175 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002176 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002177 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002178 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002179 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002180
Chris Mason34a38212007-11-07 13:31:03 -05002181 if (empty)
2182 nr = 0;
2183 else
Chris Mason99d8f832010-07-07 10:51:48 -04002184 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002185
Zheng Yan31840ae2008-09-23 13:14:14 -04002186 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002187 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002188
Chris Mason44871b12009-03-13 10:04:31 -04002189 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002190 i = left_nritems - 1;
2191 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002192 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002193
Zheng Yan31840ae2008-09-23 13:14:14 -04002194 if (!empty && push_items > 0) {
2195 if (path->slots[0] > i)
2196 break;
2197 if (path->slots[0] == i) {
2198 int space = btrfs_leaf_free_space(root, left);
2199 if (space + push_space * 2 > free_space)
2200 break;
2201 }
2202 }
2203
Chris Mason00ec4c52007-02-24 12:47:20 -05002204 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002205 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002206
Chris Masondb945352007-10-15 16:15:53 -04002207 this_item_size = btrfs_item_size(left, item);
2208 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002209 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002210
Chris Mason00ec4c52007-02-24 12:47:20 -05002211 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002212 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002213 if (i == 0)
2214 break;
2215 i--;
Chris Masondb945352007-10-15 16:15:53 -04002216 }
Chris Mason5f39d392007-10-15 16:14:19 -04002217
Chris Mason925baed2008-06-25 16:01:30 -04002218 if (push_items == 0)
2219 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002220
Chris Mason34a38212007-11-07 13:31:03 -05002221 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002222 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002223
Chris Mason00ec4c52007-02-24 12:47:20 -05002224 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002225 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002226
Chris Mason5f39d392007-10-15 16:14:19 -04002227 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002228 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002229
Chris Mason00ec4c52007-02-24 12:47:20 -05002230 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002231 data_end = leaf_data_end(root, right);
2232 memmove_extent_buffer(right,
2233 btrfs_leaf_data(right) + data_end - push_space,
2234 btrfs_leaf_data(right) + data_end,
2235 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2236
Chris Mason00ec4c52007-02-24 12:47:20 -05002237 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002238 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002239 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2240 btrfs_leaf_data(left) + leaf_data_end(root, left),
2241 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002242
2243 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2244 btrfs_item_nr_offset(0),
2245 right_nritems * sizeof(struct btrfs_item));
2246
Chris Mason00ec4c52007-02-24 12:47:20 -05002247 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002248 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2249 btrfs_item_nr_offset(left_nritems - push_items),
2250 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002251
2252 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002253 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002254 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002255 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002256 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002257 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002258 push_space -= btrfs_item_size(right, item);
2259 btrfs_set_item_offset(right, item, push_space);
2260 }
2261
Chris Mason7518a232007-03-12 12:01:18 -04002262 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002263 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002264
Chris Mason34a38212007-11-07 13:31:03 -05002265 if (left_nritems)
2266 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002267 else
2268 clean_tree_block(trans, root, left);
2269
Chris Mason5f39d392007-10-15 16:14:19 -04002270 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002271
Chris Mason5f39d392007-10-15 16:14:19 -04002272 btrfs_item_key(right, &disk_key, 0);
2273 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002274 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002275
Chris Mason00ec4c52007-02-24 12:47:20 -05002276 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002277 if (path->slots[0] >= left_nritems) {
2278 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002279 if (btrfs_header_nritems(path->nodes[0]) == 0)
2280 clean_tree_block(trans, root, path->nodes[0]);
2281 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002282 free_extent_buffer(path->nodes[0]);
2283 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002284 path->slots[1] += 1;
2285 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002286 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002287 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002288 }
2289 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002290
2291out_unlock:
2292 btrfs_tree_unlock(right);
2293 free_extent_buffer(right);
2294 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002295}
Chris Mason925baed2008-06-25 16:01:30 -04002296
Chris Mason00ec4c52007-02-24 12:47:20 -05002297/*
Chris Mason44871b12009-03-13 10:04:31 -04002298 * push some data in the path leaf to the right, trying to free up at
2299 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2300 *
2301 * returns 1 if the push failed because the other node didn't have enough
2302 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002303 *
2304 * this will push starting from min_slot to the end of the leaf. It won't
2305 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002306 */
2307static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002308 *root, struct btrfs_path *path,
2309 int min_data_size, int data_size,
2310 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002311{
2312 struct extent_buffer *left = path->nodes[0];
2313 struct extent_buffer *right;
2314 struct extent_buffer *upper;
2315 int slot;
2316 int free_space;
2317 u32 left_nritems;
2318 int ret;
2319
2320 if (!path->nodes[1])
2321 return 1;
2322
2323 slot = path->slots[1];
2324 upper = path->nodes[1];
2325 if (slot >= btrfs_header_nritems(upper) - 1)
2326 return 1;
2327
2328 btrfs_assert_tree_locked(path->nodes[1]);
2329
2330 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002331 if (right == NULL)
2332 return 1;
2333
Chris Mason44871b12009-03-13 10:04:31 -04002334 btrfs_tree_lock(right);
2335 btrfs_set_lock_blocking(right);
2336
2337 free_space = btrfs_leaf_free_space(root, right);
2338 if (free_space < data_size)
2339 goto out_unlock;
2340
2341 /* cow and double check */
2342 ret = btrfs_cow_block(trans, root, right, upper,
2343 slot + 1, &right);
2344 if (ret)
2345 goto out_unlock;
2346
2347 free_space = btrfs_leaf_free_space(root, right);
2348 if (free_space < data_size)
2349 goto out_unlock;
2350
2351 left_nritems = btrfs_header_nritems(left);
2352 if (left_nritems == 0)
2353 goto out_unlock;
2354
Chris Mason99d8f832010-07-07 10:51:48 -04002355 return __push_leaf_right(trans, root, path, min_data_size, empty,
2356 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002357out_unlock:
2358 btrfs_tree_unlock(right);
2359 free_extent_buffer(right);
2360 return 1;
2361}
2362
2363/*
Chris Mason74123bd2007-02-02 11:05:29 -05002364 * push some data in the path leaf to the left, trying to free up at
2365 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002366 *
2367 * max_slot can put a limit on how far into the leaf we'll push items. The
2368 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2369 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002370 */
Chris Mason44871b12009-03-13 10:04:31 -04002371static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2372 struct btrfs_root *root,
2373 struct btrfs_path *path, int data_size,
2374 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002375 int free_space, u32 right_nritems,
2376 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002377{
Chris Mason5f39d392007-10-15 16:14:19 -04002378 struct btrfs_disk_key disk_key;
2379 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002380 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002381 int push_space = 0;
2382 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002383 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002384 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002385 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002386 int ret = 0;
2387 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002388 u32 this_item_size;
2389 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002390
Chris Mason34a38212007-11-07 13:31:03 -05002391 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002392 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002393 else
Chris Mason99d8f832010-07-07 10:51:48 -04002394 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002395
2396 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002397 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002398
Zheng Yan31840ae2008-09-23 13:14:14 -04002399 if (!empty && push_items > 0) {
2400 if (path->slots[0] < i)
2401 break;
2402 if (path->slots[0] == i) {
2403 int space = btrfs_leaf_free_space(root, right);
2404 if (space + push_space * 2 > free_space)
2405 break;
2406 }
2407 }
2408
Chris Masonbe0e5c02007-01-26 15:51:26 -05002409 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002410 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002411
2412 this_item_size = btrfs_item_size(right, item);
2413 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002414 break;
Chris Masondb945352007-10-15 16:15:53 -04002415
Chris Masonbe0e5c02007-01-26 15:51:26 -05002416 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002417 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002418 }
Chris Masondb945352007-10-15 16:15:53 -04002419
Chris Masonbe0e5c02007-01-26 15:51:26 -05002420 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002421 ret = 1;
2422 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002423 }
Chris Mason34a38212007-11-07 13:31:03 -05002424 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002425 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002426
Chris Masonbe0e5c02007-01-26 15:51:26 -05002427 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002428 copy_extent_buffer(left, right,
2429 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2430 btrfs_item_nr_offset(0),
2431 push_items * sizeof(struct btrfs_item));
2432
Chris Mason123abc82007-03-14 14:14:43 -04002433 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002434 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002435
2436 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002437 leaf_data_end(root, left) - push_space,
2438 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002439 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002440 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002441 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002442 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002443
Chris Masondb945352007-10-15 16:15:53 -04002444 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002445 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002446 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002447
Chris Mason5f39d392007-10-15 16:14:19 -04002448 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002449
Chris Mason5f39d392007-10-15 16:14:19 -04002450 ioff = btrfs_item_offset(left, item);
2451 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002452 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002453 }
Chris Mason5f39d392007-10-15 16:14:19 -04002454 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002455
2456 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002457 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002458 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2459 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002460 WARN_ON(1);
2461 }
Chris Mason5f39d392007-10-15 16:14:19 -04002462
Chris Mason34a38212007-11-07 13:31:03 -05002463 if (push_items < right_nritems) {
2464 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2465 leaf_data_end(root, right);
2466 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2467 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2468 btrfs_leaf_data(right) +
2469 leaf_data_end(root, right), push_space);
2470
2471 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002472 btrfs_item_nr_offset(push_items),
2473 (btrfs_header_nritems(right) - push_items) *
2474 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002475 }
Yaneef1c492007-11-26 10:58:13 -05002476 right_nritems -= push_items;
2477 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002478 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002479 for (i = 0; i < right_nritems; i++) {
2480 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002481
Chris Masondb945352007-10-15 16:15:53 -04002482 push_space = push_space - btrfs_item_size(right, item);
2483 btrfs_set_item_offset(right, item, push_space);
2484 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002485
Chris Mason5f39d392007-10-15 16:14:19 -04002486 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002487 if (right_nritems)
2488 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002489 else
2490 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002491
Chris Mason5f39d392007-10-15 16:14:19 -04002492 btrfs_item_key(right, &disk_key, 0);
2493 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002494 if (wret)
2495 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002496
2497 /* then fixup the leaf pointer in the path */
2498 if (path->slots[0] < push_items) {
2499 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002500 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002501 free_extent_buffer(path->nodes[0]);
2502 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002503 path->slots[1] -= 1;
2504 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002505 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002506 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002507 path->slots[0] -= push_items;
2508 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002509 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002510 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002511out:
2512 btrfs_tree_unlock(left);
2513 free_extent_buffer(left);
2514 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515}
2516
Chris Mason74123bd2007-02-02 11:05:29 -05002517/*
Chris Mason44871b12009-03-13 10:04:31 -04002518 * push some data in the path leaf to the left, trying to free up at
2519 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002520 *
2521 * max_slot can put a limit on how far into the leaf we'll push items. The
2522 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2523 * items
Chris Mason44871b12009-03-13 10:04:31 -04002524 */
2525static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002526 *root, struct btrfs_path *path, int min_data_size,
2527 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002528{
2529 struct extent_buffer *right = path->nodes[0];
2530 struct extent_buffer *left;
2531 int slot;
2532 int free_space;
2533 u32 right_nritems;
2534 int ret = 0;
2535
2536 slot = path->slots[1];
2537 if (slot == 0)
2538 return 1;
2539 if (!path->nodes[1])
2540 return 1;
2541
2542 right_nritems = btrfs_header_nritems(right);
2543 if (right_nritems == 0)
2544 return 1;
2545
2546 btrfs_assert_tree_locked(path->nodes[1]);
2547
2548 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002549 if (left == NULL)
2550 return 1;
2551
Chris Mason44871b12009-03-13 10:04:31 -04002552 btrfs_tree_lock(left);
2553 btrfs_set_lock_blocking(left);
2554
2555 free_space = btrfs_leaf_free_space(root, left);
2556 if (free_space < data_size) {
2557 ret = 1;
2558 goto out;
2559 }
2560
2561 /* cow and double check */
2562 ret = btrfs_cow_block(trans, root, left,
2563 path->nodes[1], slot - 1, &left);
2564 if (ret) {
2565 /* we hit -ENOSPC, but it isn't fatal here */
2566 ret = 1;
2567 goto out;
2568 }
2569
2570 free_space = btrfs_leaf_free_space(root, left);
2571 if (free_space < data_size) {
2572 ret = 1;
2573 goto out;
2574 }
2575
Chris Mason99d8f832010-07-07 10:51:48 -04002576 return __push_leaf_left(trans, root, path, min_data_size,
2577 empty, left, free_space, right_nritems,
2578 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002579out:
2580 btrfs_tree_unlock(left);
2581 free_extent_buffer(left);
2582 return ret;
2583}
2584
2585/*
Chris Mason74123bd2007-02-02 11:05:29 -05002586 * split the path's leaf in two, making sure there is at least data_size
2587 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002588 *
2589 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002590 */
Chris Mason44871b12009-03-13 10:04:31 -04002591static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002592 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002593 struct btrfs_path *path,
2594 struct extent_buffer *l,
2595 struct extent_buffer *right,
2596 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002597{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598 int data_copy_size;
2599 int rt_data_off;
2600 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002601 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002602 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002603 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002604
Chris Mason5f39d392007-10-15 16:14:19 -04002605 nritems = nritems - mid;
2606 btrfs_set_header_nritems(right, nritems);
2607 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2608
2609 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2610 btrfs_item_nr_offset(mid),
2611 nritems * sizeof(struct btrfs_item));
2612
2613 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002614 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2615 data_copy_size, btrfs_leaf_data(l) +
2616 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002617
Chris Mason5f39d392007-10-15 16:14:19 -04002618 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2619 btrfs_item_end_nr(l, mid);
2620
2621 for (i = 0; i < nritems; i++) {
2622 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002623 u32 ioff;
2624
Chris Masondb945352007-10-15 16:15:53 -04002625 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002626 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002627 }
Chris Mason74123bd2007-02-02 11:05:29 -05002628
Chris Mason5f39d392007-10-15 16:14:19 -04002629 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002630 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002631 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002632 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2633 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002634 if (wret)
2635 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002636
2637 btrfs_mark_buffer_dirty(right);
2638 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002639 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002640
Chris Masonbe0e5c02007-01-26 15:51:26 -05002641 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002642 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002643 free_extent_buffer(path->nodes[0]);
2644 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002645 path->slots[0] -= mid;
2646 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002647 } else {
2648 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002649 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002650 }
Chris Mason5f39d392007-10-15 16:14:19 -04002651
Chris Masoneb60cea2007-02-02 09:18:22 -05002652 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002653
Chris Mason44871b12009-03-13 10:04:31 -04002654 return ret;
2655}
2656
2657/*
Chris Mason99d8f832010-07-07 10:51:48 -04002658 * double splits happen when we need to insert a big item in the middle
2659 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2660 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2661 * A B C
2662 *
2663 * We avoid this by trying to push the items on either side of our target
2664 * into the adjacent leaves. If all goes well we can avoid the double split
2665 * completely.
2666 */
2667static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2668 struct btrfs_root *root,
2669 struct btrfs_path *path,
2670 int data_size)
2671{
2672 int ret;
2673 int progress = 0;
2674 int slot;
2675 u32 nritems;
2676
2677 slot = path->slots[0];
2678
2679 /*
2680 * try to push all the items after our slot into the
2681 * right leaf
2682 */
2683 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2684 if (ret < 0)
2685 return ret;
2686
2687 if (ret == 0)
2688 progress++;
2689
2690 nritems = btrfs_header_nritems(path->nodes[0]);
2691 /*
2692 * our goal is to get our slot at the start or end of a leaf. If
2693 * we've done so we're done
2694 */
2695 if (path->slots[0] == 0 || path->slots[0] == nritems)
2696 return 0;
2697
2698 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2699 return 0;
2700
2701 /* try to push all the items before our slot into the next leaf */
2702 slot = path->slots[0];
2703 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2704 if (ret < 0)
2705 return ret;
2706
2707 if (ret == 0)
2708 progress++;
2709
2710 if (progress)
2711 return 0;
2712 return 1;
2713}
2714
2715/*
Chris Mason44871b12009-03-13 10:04:31 -04002716 * split the path's leaf in two, making sure there is at least data_size
2717 * available for the resulting leaf level of the path.
2718 *
2719 * returns 0 if all went well and < 0 on failure.
2720 */
2721static noinline int split_leaf(struct btrfs_trans_handle *trans,
2722 struct btrfs_root *root,
2723 struct btrfs_key *ins_key,
2724 struct btrfs_path *path, int data_size,
2725 int extend)
2726{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002727 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002728 struct extent_buffer *l;
2729 u32 nritems;
2730 int mid;
2731 int slot;
2732 struct extent_buffer *right;
2733 int ret = 0;
2734 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002735 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002736 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002737 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002738
Yan, Zhenga5719522009-09-24 09:17:31 -04002739 l = path->nodes[0];
2740 slot = path->slots[0];
2741 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2742 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2743 return -EOVERFLOW;
2744
Chris Mason44871b12009-03-13 10:04:31 -04002745 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002746 if (data_size) {
2747 wret = push_leaf_right(trans, root, path, data_size,
2748 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002749 if (wret < 0)
2750 return wret;
2751 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002752 wret = push_leaf_left(trans, root, path, data_size,
2753 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002754 if (wret < 0)
2755 return wret;
2756 }
2757 l = path->nodes[0];
2758
2759 /* did the pushes work? */
2760 if (btrfs_leaf_free_space(root, l) >= data_size)
2761 return 0;
2762 }
2763
2764 if (!path->nodes[1]) {
2765 ret = insert_new_root(trans, root, path, 1);
2766 if (ret)
2767 return ret;
2768 }
2769again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002770 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002771 l = path->nodes[0];
2772 slot = path->slots[0];
2773 nritems = btrfs_header_nritems(l);
2774 mid = (nritems + 1) / 2;
2775
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002776 if (mid <= slot) {
2777 if (nritems == 1 ||
2778 leaf_space_used(l, mid, nritems - mid) + data_size >
2779 BTRFS_LEAF_DATA_SIZE(root)) {
2780 if (slot >= nritems) {
2781 split = 0;
2782 } else {
2783 mid = slot;
2784 if (mid != nritems &&
2785 leaf_space_used(l, mid, nritems - mid) +
2786 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002787 if (data_size && !tried_avoid_double)
2788 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002789 split = 2;
2790 }
2791 }
2792 }
2793 } else {
2794 if (leaf_space_used(l, 0, mid) + data_size >
2795 BTRFS_LEAF_DATA_SIZE(root)) {
2796 if (!extend && data_size && slot == 0) {
2797 split = 0;
2798 } else if ((extend || !data_size) && slot == 0) {
2799 mid = 1;
2800 } else {
2801 mid = slot;
2802 if (mid != nritems &&
2803 leaf_space_used(l, mid, nritems - mid) +
2804 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002805 if (data_size && !tried_avoid_double)
2806 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002807 split = 2 ;
2808 }
2809 }
2810 }
2811 }
2812
2813 if (split == 0)
2814 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2815 else
2816 btrfs_item_key(l, &disk_key, mid);
2817
2818 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002819 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002820 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002821 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002822 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002823
2824 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002825
2826 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2827 btrfs_set_header_bytenr(right, right->start);
2828 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002829 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002830 btrfs_set_header_owner(right, root->root_key.objectid);
2831 btrfs_set_header_level(right, 0);
2832 write_extent_buffer(right, root->fs_info->fsid,
2833 (unsigned long)btrfs_header_fsid(right),
2834 BTRFS_FSID_SIZE);
2835
2836 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2837 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2838 BTRFS_UUID_SIZE);
2839
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002840 if (split == 0) {
2841 if (mid <= slot) {
2842 btrfs_set_header_nritems(right, 0);
2843 wret = insert_ptr(trans, root, path,
2844 &disk_key, right->start,
2845 path->slots[1] + 1, 1);
2846 if (wret)
2847 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002848
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002849 btrfs_tree_unlock(path->nodes[0]);
2850 free_extent_buffer(path->nodes[0]);
2851 path->nodes[0] = right;
2852 path->slots[0] = 0;
2853 path->slots[1] += 1;
2854 } else {
2855 btrfs_set_header_nritems(right, 0);
2856 wret = insert_ptr(trans, root, path,
2857 &disk_key,
2858 right->start,
2859 path->slots[1], 1);
2860 if (wret)
2861 ret = wret;
2862 btrfs_tree_unlock(path->nodes[0]);
2863 free_extent_buffer(path->nodes[0]);
2864 path->nodes[0] = right;
2865 path->slots[0] = 0;
2866 if (path->slots[1] == 0) {
2867 wret = fixup_low_keys(trans, root,
2868 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002869 if (wret)
2870 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002871 }
2872 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002873 btrfs_mark_buffer_dirty(right);
2874 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002875 }
2876
2877 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2878 BUG_ON(ret);
2879
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002880 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002881 BUG_ON(num_doubles != 0);
2882 num_doubles++;
2883 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002884 }
Chris Mason44871b12009-03-13 10:04:31 -04002885
Chris Masonbe0e5c02007-01-26 15:51:26 -05002886 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04002887
2888push_for_double:
2889 push_for_double_split(trans, root, path, data_size);
2890 tried_avoid_double = 1;
2891 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2892 return 0;
2893 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002894}
2895
Yan, Zhengad48fd752009-11-12 09:33:58 +00002896static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
2897 struct btrfs_root *root,
2898 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05002899{
Yan, Zhengad48fd752009-11-12 09:33:58 +00002900 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05002901 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00002902 struct btrfs_file_extent_item *fi;
2903 u64 extent_len = 0;
2904 u32 item_size;
2905 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05002906
2907 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00002908 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2909
2910 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
2911 key.type != BTRFS_EXTENT_CSUM_KEY);
2912
2913 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
2914 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05002915
2916 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00002917 if (key.type == BTRFS_EXTENT_DATA_KEY) {
2918 fi = btrfs_item_ptr(leaf, path->slots[0],
2919 struct btrfs_file_extent_item);
2920 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2921 }
David Sterbab3b4aa72011-04-21 01:20:15 +02002922 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05002923
Chris Mason459931e2008-12-10 09:10:46 -05002924 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00002925 path->search_for_split = 1;
2926 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05002927 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00002928 if (ret < 0)
2929 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05002930
Yan, Zhengad48fd752009-11-12 09:33:58 +00002931 ret = -EAGAIN;
2932 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05002933 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00002934 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
2935 goto err;
2936
Chris Mason109f6ae2010-04-02 09:20:18 -04002937 /* the leaf has changed, it now has room. return now */
2938 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
2939 goto err;
2940
Yan, Zhengad48fd752009-11-12 09:33:58 +00002941 if (key.type == BTRFS_EXTENT_DATA_KEY) {
2942 fi = btrfs_item_ptr(leaf, path->slots[0],
2943 struct btrfs_file_extent_item);
2944 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
2945 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05002946 }
2947
Chris Masonb9473432009-03-13 11:00:37 -04002948 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00002949 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002950 if (ret)
2951 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05002952
Yan, Zhengad48fd752009-11-12 09:33:58 +00002953 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04002954 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00002955 return 0;
2956err:
2957 path->keep_locks = 0;
2958 return ret;
2959}
2960
2961static noinline int split_item(struct btrfs_trans_handle *trans,
2962 struct btrfs_root *root,
2963 struct btrfs_path *path,
2964 struct btrfs_key *new_key,
2965 unsigned long split_offset)
2966{
2967 struct extent_buffer *leaf;
2968 struct btrfs_item *item;
2969 struct btrfs_item *new_item;
2970 int slot;
2971 char *buf;
2972 u32 nritems;
2973 u32 item_size;
2974 u32 orig_offset;
2975 struct btrfs_disk_key disk_key;
2976
Chris Masonb9473432009-03-13 11:00:37 -04002977 leaf = path->nodes[0];
2978 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2979
Chris Masonb4ce94d2009-02-04 09:25:08 -05002980 btrfs_set_path_blocking(path);
2981
Chris Mason459931e2008-12-10 09:10:46 -05002982 item = btrfs_item_nr(leaf, path->slots[0]);
2983 orig_offset = btrfs_item_offset(leaf, item);
2984 item_size = btrfs_item_size(leaf, item);
2985
Chris Mason459931e2008-12-10 09:10:46 -05002986 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00002987 if (!buf)
2988 return -ENOMEM;
2989
Chris Mason459931e2008-12-10 09:10:46 -05002990 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2991 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00002992
Chris Mason459931e2008-12-10 09:10:46 -05002993 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05002994 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05002995 if (slot != nritems) {
2996 /* shift the items */
2997 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00002998 btrfs_item_nr_offset(slot),
2999 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003000 }
3001
3002 btrfs_cpu_key_to_disk(&disk_key, new_key);
3003 btrfs_set_item_key(leaf, &disk_key, slot);
3004
3005 new_item = btrfs_item_nr(leaf, slot);
3006
3007 btrfs_set_item_offset(leaf, new_item, orig_offset);
3008 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3009
3010 btrfs_set_item_offset(leaf, item,
3011 orig_offset + item_size - split_offset);
3012 btrfs_set_item_size(leaf, item, split_offset);
3013
3014 btrfs_set_header_nritems(leaf, nritems + 1);
3015
3016 /* write the data for the start of the original item */
3017 write_extent_buffer(leaf, buf,
3018 btrfs_item_ptr_offset(leaf, path->slots[0]),
3019 split_offset);
3020
3021 /* write the data for the new item */
3022 write_extent_buffer(leaf, buf + split_offset,
3023 btrfs_item_ptr_offset(leaf, slot),
3024 item_size - split_offset);
3025 btrfs_mark_buffer_dirty(leaf);
3026
Yan, Zhengad48fd752009-11-12 09:33:58 +00003027 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003028 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003029 return 0;
3030}
3031
3032/*
3033 * This function splits a single item into two items,
3034 * giving 'new_key' to the new item and splitting the
3035 * old one at split_offset (from the start of the item).
3036 *
3037 * The path may be released by this operation. After
3038 * the split, the path is pointing to the old item. The
3039 * new item is going to be in the same node as the old one.
3040 *
3041 * Note, the item being split must be smaller enough to live alone on
3042 * a tree block with room for one extra struct btrfs_item
3043 *
3044 * This allows us to split the item in place, keeping a lock on the
3045 * leaf the entire time.
3046 */
3047int btrfs_split_item(struct btrfs_trans_handle *trans,
3048 struct btrfs_root *root,
3049 struct btrfs_path *path,
3050 struct btrfs_key *new_key,
3051 unsigned long split_offset)
3052{
3053 int ret;
3054 ret = setup_leaf_for_split(trans, root, path,
3055 sizeof(struct btrfs_item));
3056 if (ret)
3057 return ret;
3058
3059 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003060 return ret;
3061}
3062
3063/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003064 * This function duplicate a item, giving 'new_key' to the new item.
3065 * It guarantees both items live in the same tree leaf and the new item
3066 * is contiguous with the original item.
3067 *
3068 * This allows us to split file extent in place, keeping a lock on the
3069 * leaf the entire time.
3070 */
3071int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3072 struct btrfs_root *root,
3073 struct btrfs_path *path,
3074 struct btrfs_key *new_key)
3075{
3076 struct extent_buffer *leaf;
3077 int ret;
3078 u32 item_size;
3079
3080 leaf = path->nodes[0];
3081 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3082 ret = setup_leaf_for_split(trans, root, path,
3083 item_size + sizeof(struct btrfs_item));
3084 if (ret)
3085 return ret;
3086
3087 path->slots[0]++;
3088 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3089 item_size, item_size +
3090 sizeof(struct btrfs_item), 1);
3091 BUG_ON(ret);
3092
3093 leaf = path->nodes[0];
3094 memcpy_extent_buffer(leaf,
3095 btrfs_item_ptr_offset(leaf, path->slots[0]),
3096 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3097 item_size);
3098 return 0;
3099}
3100
3101/*
Chris Masond352ac62008-09-29 15:18:18 -04003102 * make the item pointed to by the path smaller. new_size indicates
3103 * how small to make it, and from_end tells us if we just chop bytes
3104 * off the end of the item or if we shift the item to chop bytes off
3105 * the front.
3106 */
Chris Masonb18c6682007-04-17 13:26:50 -04003107int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3108 struct btrfs_root *root,
3109 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003110 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003111{
Chris Masonb18c6682007-04-17 13:26:50 -04003112 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003113 struct extent_buffer *leaf;
3114 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003115 u32 nritems;
3116 unsigned int data_end;
3117 unsigned int old_data_start;
3118 unsigned int old_size;
3119 unsigned int size_diff;
3120 int i;
3121
Chris Mason5f39d392007-10-15 16:14:19 -04003122 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003123 slot = path->slots[0];
3124
3125 old_size = btrfs_item_size_nr(leaf, slot);
3126 if (old_size == new_size)
3127 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003128
Chris Mason5f39d392007-10-15 16:14:19 -04003129 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003130 data_end = leaf_data_end(root, leaf);
3131
Chris Mason5f39d392007-10-15 16:14:19 -04003132 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003133
Chris Masonb18c6682007-04-17 13:26:50 -04003134 size_diff = old_size - new_size;
3135
3136 BUG_ON(slot < 0);
3137 BUG_ON(slot >= nritems);
3138
3139 /*
3140 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3141 */
3142 /* first correct the data pointers */
3143 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003144 u32 ioff;
3145 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003146
Chris Mason5f39d392007-10-15 16:14:19 -04003147 ioff = btrfs_item_offset(leaf, item);
3148 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003149 }
Chris Masondb945352007-10-15 16:15:53 -04003150
Chris Masonb18c6682007-04-17 13:26:50 -04003151 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003152 if (from_end) {
3153 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3154 data_end + size_diff, btrfs_leaf_data(leaf) +
3155 data_end, old_data_start + new_size - data_end);
3156 } else {
3157 struct btrfs_disk_key disk_key;
3158 u64 offset;
3159
3160 btrfs_item_key(leaf, &disk_key, slot);
3161
3162 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3163 unsigned long ptr;
3164 struct btrfs_file_extent_item *fi;
3165
3166 fi = btrfs_item_ptr(leaf, slot,
3167 struct btrfs_file_extent_item);
3168 fi = (struct btrfs_file_extent_item *)(
3169 (unsigned long)fi - size_diff);
3170
3171 if (btrfs_file_extent_type(leaf, fi) ==
3172 BTRFS_FILE_EXTENT_INLINE) {
3173 ptr = btrfs_item_ptr_offset(leaf, slot);
3174 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003175 (unsigned long)fi,
3176 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003177 disk_bytenr));
3178 }
3179 }
3180
3181 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3182 data_end + size_diff, btrfs_leaf_data(leaf) +
3183 data_end, old_data_start - data_end);
3184
3185 offset = btrfs_disk_key_offset(&disk_key);
3186 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3187 btrfs_set_item_key(leaf, &disk_key, slot);
3188 if (slot == 0)
3189 fixup_low_keys(trans, root, path, &disk_key, 1);
3190 }
Chris Mason5f39d392007-10-15 16:14:19 -04003191
3192 item = btrfs_item_nr(leaf, slot);
3193 btrfs_set_item_size(leaf, item, new_size);
3194 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003195
Chris Mason5f39d392007-10-15 16:14:19 -04003196 if (btrfs_leaf_free_space(root, leaf) < 0) {
3197 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003198 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003199 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003200 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003201}
3202
Chris Masond352ac62008-09-29 15:18:18 -04003203/*
3204 * make the item pointed to by the path bigger, data_size is the new size.
3205 */
Chris Mason5f39d392007-10-15 16:14:19 -04003206int btrfs_extend_item(struct btrfs_trans_handle *trans,
3207 struct btrfs_root *root, struct btrfs_path *path,
3208 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003209{
Chris Mason6567e832007-04-16 09:22:45 -04003210 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003211 struct extent_buffer *leaf;
3212 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003213 u32 nritems;
3214 unsigned int data_end;
3215 unsigned int old_data;
3216 unsigned int old_size;
3217 int i;
3218
Chris Mason5f39d392007-10-15 16:14:19 -04003219 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003220
Chris Mason5f39d392007-10-15 16:14:19 -04003221 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003222 data_end = leaf_data_end(root, leaf);
3223
Chris Mason5f39d392007-10-15 16:14:19 -04003224 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3225 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003226 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003227 }
Chris Mason6567e832007-04-16 09:22:45 -04003228 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003229 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003230
3231 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003232 if (slot >= nritems) {
3233 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003234 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3235 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003236 BUG_ON(1);
3237 }
Chris Mason6567e832007-04-16 09:22:45 -04003238
3239 /*
3240 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3241 */
3242 /* first correct the data pointers */
3243 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003244 u32 ioff;
3245 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003246
Chris Mason5f39d392007-10-15 16:14:19 -04003247 ioff = btrfs_item_offset(leaf, item);
3248 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003249 }
Chris Mason5f39d392007-10-15 16:14:19 -04003250
Chris Mason6567e832007-04-16 09:22:45 -04003251 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003252 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003253 data_end - data_size, btrfs_leaf_data(leaf) +
3254 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003255
Chris Mason6567e832007-04-16 09:22:45 -04003256 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003257 old_size = btrfs_item_size_nr(leaf, slot);
3258 item = btrfs_item_nr(leaf, slot);
3259 btrfs_set_item_size(leaf, item, old_size + data_size);
3260 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003261
Chris Mason5f39d392007-10-15 16:14:19 -04003262 if (btrfs_leaf_free_space(root, leaf) < 0) {
3263 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003264 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003265 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003266 return 0;
Chris Mason6567e832007-04-16 09:22:45 -04003267}
3268
Chris Mason74123bd2007-02-02 11:05:29 -05003269/*
Chris Masond352ac62008-09-29 15:18:18 -04003270 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003271 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003272 * Returns the number of keys that were inserted.
3273 */
3274int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3275 struct btrfs_root *root,
3276 struct btrfs_path *path,
3277 struct btrfs_key *cpu_key, u32 *data_size,
3278 int nr)
3279{
3280 struct extent_buffer *leaf;
3281 struct btrfs_item *item;
3282 int ret = 0;
3283 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003284 int i;
3285 u32 nritems;
3286 u32 total_data = 0;
3287 u32 total_size = 0;
3288 unsigned int data_end;
3289 struct btrfs_disk_key disk_key;
3290 struct btrfs_key found_key;
3291
Yan Zheng87b29b22008-12-17 10:21:48 -05003292 for (i = 0; i < nr; i++) {
3293 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3294 BTRFS_LEAF_DATA_SIZE(root)) {
3295 break;
3296 nr = i;
3297 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003298 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003299 total_size += data_size[i] + sizeof(struct btrfs_item);
3300 }
3301 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003302
Josef Bacikf3465ca2008-11-12 14:19:50 -05003303 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3304 if (ret == 0)
3305 return -EEXIST;
3306 if (ret < 0)
3307 goto out;
3308
Josef Bacikf3465ca2008-11-12 14:19:50 -05003309 leaf = path->nodes[0];
3310
3311 nritems = btrfs_header_nritems(leaf);
3312 data_end = leaf_data_end(root, leaf);
3313
3314 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3315 for (i = nr; i >= 0; i--) {
3316 total_data -= data_size[i];
3317 total_size -= data_size[i] + sizeof(struct btrfs_item);
3318 if (total_size < btrfs_leaf_free_space(root, leaf))
3319 break;
3320 }
3321 nr = i;
3322 }
3323
3324 slot = path->slots[0];
3325 BUG_ON(slot < 0);
3326
3327 if (slot != nritems) {
3328 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3329
3330 item = btrfs_item_nr(leaf, slot);
3331 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3332
3333 /* figure out how many keys we can insert in here */
3334 total_data = data_size[0];
3335 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003336 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003337 break;
3338 total_data += data_size[i];
3339 }
3340 nr = i;
3341
3342 if (old_data < data_end) {
3343 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003344 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003345 slot, old_data, data_end);
3346 BUG_ON(1);
3347 }
3348 /*
3349 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3350 */
3351 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003352 for (i = slot; i < nritems; i++) {
3353 u32 ioff;
3354
3355 item = btrfs_item_nr(leaf, i);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003356 ioff = btrfs_item_offset(leaf, item);
3357 btrfs_set_item_offset(leaf, item, ioff - total_data);
3358 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003359 /* shift the items */
3360 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3361 btrfs_item_nr_offset(slot),
3362 (nritems - slot) * sizeof(struct btrfs_item));
3363
3364 /* shift the data */
3365 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3366 data_end - total_data, btrfs_leaf_data(leaf) +
3367 data_end, old_data - data_end);
3368 data_end = old_data;
3369 } else {
3370 /*
3371 * this sucks but it has to be done, if we are inserting at
3372 * the end of the leaf only insert 1 of the items, since we
3373 * have no way of knowing whats on the next leaf and we'd have
3374 * to drop our current locks to figure it out
3375 */
3376 nr = 1;
3377 }
3378
3379 /* setup the item for the new data */
3380 for (i = 0; i < nr; i++) {
3381 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3382 btrfs_set_item_key(leaf, &disk_key, slot + i);
3383 item = btrfs_item_nr(leaf, slot + i);
3384 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3385 data_end -= data_size[i];
3386 btrfs_set_item_size(leaf, item, data_size[i]);
3387 }
3388 btrfs_set_header_nritems(leaf, nritems + nr);
3389 btrfs_mark_buffer_dirty(leaf);
3390
3391 ret = 0;
3392 if (slot == 0) {
3393 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3394 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3395 }
3396
3397 if (btrfs_leaf_free_space(root, leaf) < 0) {
3398 btrfs_print_leaf(root, leaf);
3399 BUG();
3400 }
3401out:
3402 if (!ret)
3403 ret = nr;
3404 return ret;
3405}
3406
3407/*
Chris Mason44871b12009-03-13 10:04:31 -04003408 * this is a helper for btrfs_insert_empty_items, the main goal here is
3409 * to save stack depth by doing the bulk of the work in a function
3410 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003411 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003412int setup_items_for_insert(struct btrfs_trans_handle *trans,
3413 struct btrfs_root *root, struct btrfs_path *path,
3414 struct btrfs_key *cpu_key, u32 *data_size,
3415 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003416{
Chris Mason5f39d392007-10-15 16:14:19 -04003417 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003418 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003419 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003420 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003421 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003422 int ret;
3423 struct extent_buffer *leaf;
3424 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003425
Chris Mason5f39d392007-10-15 16:14:19 -04003426 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003427 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003428
Chris Mason5f39d392007-10-15 16:14:19 -04003429 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003430 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003431
Chris Masonf25956c2008-09-12 15:32:53 -04003432 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003433 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003434 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003435 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003436 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003437 }
Chris Mason5f39d392007-10-15 16:14:19 -04003438
Chris Masonbe0e5c02007-01-26 15:51:26 -05003439 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003440 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003441
Chris Mason5f39d392007-10-15 16:14:19 -04003442 if (old_data < data_end) {
3443 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003444 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003445 slot, old_data, data_end);
3446 BUG_ON(1);
3447 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003448 /*
3449 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3450 */
3451 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003452 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003453 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003454
Chris Mason5f39d392007-10-15 16:14:19 -04003455 item = btrfs_item_nr(leaf, i);
3456 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003457 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003458 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003459 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003460 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003461 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003462 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003463
3464 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003465 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003466 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003467 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003468 data_end = old_data;
3469 }
Chris Mason5f39d392007-10-15 16:14:19 -04003470
Chris Mason62e27492007-03-15 12:56:47 -04003471 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003472 for (i = 0; i < nr; i++) {
3473 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3474 btrfs_set_item_key(leaf, &disk_key, slot + i);
3475 item = btrfs_item_nr(leaf, slot + i);
3476 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3477 data_end -= data_size[i];
3478 btrfs_set_item_size(leaf, item, data_size[i]);
3479 }
Chris Mason44871b12009-03-13 10:04:31 -04003480
Chris Mason9c583092008-01-29 15:15:18 -05003481 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003482
3483 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003484 if (slot == 0) {
3485 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003486 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003487 }
Chris Masonb9473432009-03-13 11:00:37 -04003488 btrfs_unlock_up_safe(path, 1);
3489 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003490
Chris Mason5f39d392007-10-15 16:14:19 -04003491 if (btrfs_leaf_free_space(root, leaf) < 0) {
3492 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003493 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003494 }
Chris Mason44871b12009-03-13 10:04:31 -04003495 return ret;
3496}
3497
3498/*
3499 * Given a key and some data, insert items into the tree.
3500 * This does all the path init required, making room in the tree if needed.
3501 */
3502int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3503 struct btrfs_root *root,
3504 struct btrfs_path *path,
3505 struct btrfs_key *cpu_key, u32 *data_size,
3506 int nr)
3507{
Chris Mason44871b12009-03-13 10:04:31 -04003508 int ret = 0;
3509 int slot;
3510 int i;
3511 u32 total_size = 0;
3512 u32 total_data = 0;
3513
3514 for (i = 0; i < nr; i++)
3515 total_data += data_size[i];
3516
3517 total_size = total_data + (nr * sizeof(struct btrfs_item));
3518 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3519 if (ret == 0)
3520 return -EEXIST;
3521 if (ret < 0)
3522 goto out;
3523
Chris Mason44871b12009-03-13 10:04:31 -04003524 slot = path->slots[0];
3525 BUG_ON(slot < 0);
3526
3527 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3528 total_data, total_size, nr);
3529
Chris Masoned2ff2c2007-03-01 18:59:40 -05003530out:
Chris Mason62e27492007-03-15 12:56:47 -04003531 return ret;
3532}
3533
3534/*
3535 * Given a key and some data, insert an item into the tree.
3536 * This does all the path init required, making room in the tree if needed.
3537 */
Chris Masone089f052007-03-16 16:20:31 -04003538int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3539 *root, struct btrfs_key *cpu_key, void *data, u32
3540 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003541{
3542 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003543 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003544 struct extent_buffer *leaf;
3545 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003546
Chris Mason2c90e5d2007-04-02 10:50:19 -04003547 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003548 if (!path)
3549 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003550 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003551 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003552 leaf = path->nodes[0];
3553 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3554 write_extent_buffer(leaf, data, ptr, data_size);
3555 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003556 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003557 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003558 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003559}
3560
Chris Mason74123bd2007-02-02 11:05:29 -05003561/*
Chris Mason5de08d72007-02-24 06:24:44 -05003562 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003563 *
Chris Masond352ac62008-09-29 15:18:18 -04003564 * the tree should have been previously balanced so the deletion does not
3565 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003566 */
Chris Masone089f052007-03-16 16:20:31 -04003567static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3568 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003569{
Chris Mason5f39d392007-10-15 16:14:19 -04003570 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003571 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003572 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003573 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003574
Chris Mason5f39d392007-10-15 16:14:19 -04003575 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003576 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003577 memmove_extent_buffer(parent,
3578 btrfs_node_key_ptr_offset(slot),
3579 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003580 sizeof(struct btrfs_key_ptr) *
3581 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003582 }
Chris Mason7518a232007-03-12 12:01:18 -04003583 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003584 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003585 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003586 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003587 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003588 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003589 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003590 struct btrfs_disk_key disk_key;
3591
3592 btrfs_node_key(parent, &disk_key, 0);
3593 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003594 if (wret)
3595 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003596 }
Chris Masond6025572007-03-30 14:27:56 -04003597 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003598 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003599}
3600
Chris Mason74123bd2007-02-02 11:05:29 -05003601/*
Chris Mason323ac952008-10-01 19:05:46 -04003602 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003603 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003604 *
3605 * This deletes the pointer in path->nodes[1] and frees the leaf
3606 * block extent. zero is returned if it all worked out, < 0 otherwise.
3607 *
3608 * The path must have already been setup for deleting the leaf, including
3609 * all the proper balancing. path->nodes[1] must be locked.
3610 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003611static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3612 struct btrfs_root *root,
3613 struct btrfs_path *path,
3614 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003615{
3616 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003617
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003618 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003619 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3620 if (ret)
3621 return ret;
3622
Chris Mason4d081c42009-02-04 09:31:28 -05003623 /*
3624 * btrfs_free_extent is expensive, we want to make sure we
3625 * aren't holding any locks when we call it
3626 */
3627 btrfs_unlock_up_safe(path, 0);
3628
Yan, Zhengf0486c62010-05-16 10:46:25 -04003629 root_sub_used(root, leaf->len);
3630
3631 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3632 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003633}
3634/*
Chris Mason74123bd2007-02-02 11:05:29 -05003635 * delete the item at the leaf level in path. If that empties
3636 * the leaf, remove it from the tree
3637 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003638int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3639 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003640{
Chris Mason5f39d392007-10-15 16:14:19 -04003641 struct extent_buffer *leaf;
3642 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003643 int last_off;
3644 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003645 int ret = 0;
3646 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003647 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003648 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003649
Chris Mason5f39d392007-10-15 16:14:19 -04003650 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003651 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3652
3653 for (i = 0; i < nr; i++)
3654 dsize += btrfs_item_size_nr(leaf, slot + i);
3655
Chris Mason5f39d392007-10-15 16:14:19 -04003656 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003657
Chris Mason85e21ba2008-01-29 15:11:36 -05003658 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003659 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003660
3661 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003662 data_end + dsize,
3663 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003664 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003665
Chris Mason85e21ba2008-01-29 15:11:36 -05003666 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003667 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003668
Chris Mason5f39d392007-10-15 16:14:19 -04003669 item = btrfs_item_nr(leaf, i);
3670 ioff = btrfs_item_offset(leaf, item);
3671 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003672 }
Chris Masondb945352007-10-15 16:15:53 -04003673
Chris Mason5f39d392007-10-15 16:14:19 -04003674 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003675 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003676 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003677 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003678 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003679 btrfs_set_header_nritems(leaf, nritems - nr);
3680 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003681
Chris Mason74123bd2007-02-02 11:05:29 -05003682 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003683 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003684 if (leaf == root->node) {
3685 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003686 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003687 btrfs_set_path_blocking(path);
3688 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003689 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003690 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003691 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003692 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003693 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003694 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003695 struct btrfs_disk_key disk_key;
3696
3697 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003698 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003699 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003700 if (wret)
3701 ret = wret;
3702 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003703
Chris Mason74123bd2007-02-02 11:05:29 -05003704 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003705 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003706 /* push_leaf_left fixes the path.
3707 * make sure the path still points to our leaf
3708 * for possible call to del_ptr below
3709 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003710 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003711 extent_buffer_get(leaf);
3712
Chris Masonb9473432009-03-13 11:00:37 -04003713 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003714 wret = push_leaf_left(trans, root, path, 1, 1,
3715 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003716 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003717 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003718
3719 if (path->nodes[0] == leaf &&
3720 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003721 wret = push_leaf_right(trans, root, path, 1,
3722 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003723 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003724 ret = wret;
3725 }
Chris Mason5f39d392007-10-15 16:14:19 -04003726
3727 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003728 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003729 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003730 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003731 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003732 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003733 /* if we're still in the path, make sure
3734 * we're dirty. Otherwise, one of the
3735 * push_leaf functions must have already
3736 * dirtied this buffer
3737 */
3738 if (path->nodes[0] == leaf)
3739 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003740 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003741 }
Chris Masond5719762007-03-23 10:01:08 -04003742 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003743 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003744 }
3745 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003746 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003747}
3748
Chris Mason97571fd2007-02-24 13:39:08 -05003749/*
Chris Mason925baed2008-06-25 16:01:30 -04003750 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003751 * returns 0 if it found something or 1 if there are no lesser leaves.
3752 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003753 *
3754 * This may release the path, and so you may lose any locks held at the
3755 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003756 */
3757int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3758{
Chris Mason925baed2008-06-25 16:01:30 -04003759 struct btrfs_key key;
3760 struct btrfs_disk_key found_key;
3761 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003762
Chris Mason925baed2008-06-25 16:01:30 -04003763 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003764
Chris Mason925baed2008-06-25 16:01:30 -04003765 if (key.offset > 0)
3766 key.offset--;
3767 else if (key.type > 0)
3768 key.type--;
3769 else if (key.objectid > 0)
3770 key.objectid--;
3771 else
3772 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003773
David Sterbab3b4aa72011-04-21 01:20:15 +02003774 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3776 if (ret < 0)
3777 return ret;
3778 btrfs_item_key(path->nodes[0], &found_key, 0);
3779 ret = comp_keys(&found_key, &key);
3780 if (ret < 0)
3781 return 0;
3782 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003783}
3784
Chris Mason3f157a22008-06-25 16:01:31 -04003785/*
3786 * A helper function to walk down the tree starting at min_key, and looking
3787 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003788 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003789 *
3790 * This does not cow, but it does stuff the starting key it finds back
3791 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3792 * key and get a writable path.
3793 *
3794 * This does lock as it descends, and path->keep_locks should be set
3795 * to 1 by the caller.
3796 *
3797 * This honors path->lowest_level to prevent descent past a given level
3798 * of the tree.
3799 *
Chris Masond352ac62008-09-29 15:18:18 -04003800 * min_trans indicates the oldest transaction that you are interested
3801 * in walking through. Any nodes or leaves older than min_trans are
3802 * skipped over (without reading them).
3803 *
Chris Mason3f157a22008-06-25 16:01:31 -04003804 * returns zero if something useful was found, < 0 on error and 1 if there
3805 * was nothing in the tree that matched the search criteria.
3806 */
3807int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003808 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003809 struct btrfs_path *path, int cache_only,
3810 u64 min_trans)
3811{
3812 struct extent_buffer *cur;
3813 struct btrfs_key found_key;
3814 int slot;
Yan96524802008-07-24 12:19:49 -04003815 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003816 u32 nritems;
3817 int level;
3818 int ret = 1;
3819
Chris Mason934d3752008-12-08 16:43:10 -05003820 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003821again:
3822 cur = btrfs_lock_root_node(root);
3823 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003824 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003825 path->nodes[level] = cur;
3826 path->locks[level] = 1;
3827
3828 if (btrfs_header_generation(cur) < min_trans) {
3829 ret = 1;
3830 goto out;
3831 }
Chris Masond3977122009-01-05 21:25:51 -05003832 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003833 nritems = btrfs_header_nritems(cur);
3834 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003835 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003836
Chris Mason323ac952008-10-01 19:05:46 -04003837 /* at the lowest level, we're done, setup the path and exit */
3838 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003839 if (slot >= nritems)
3840 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003841 ret = 0;
3842 path->slots[level] = slot;
3843 btrfs_item_key_to_cpu(cur, &found_key, slot);
3844 goto out;
3845 }
Yan96524802008-07-24 12:19:49 -04003846 if (sret && slot > 0)
3847 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003848 /*
3849 * check this node pointer against the cache_only and
3850 * min_trans parameters. If it isn't in cache or is too
3851 * old, skip to the next one.
3852 */
Chris Masond3977122009-01-05 21:25:51 -05003853 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003854 u64 blockptr;
3855 u64 gen;
3856 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003857 struct btrfs_disk_key disk_key;
3858
Chris Mason3f157a22008-06-25 16:01:31 -04003859 blockptr = btrfs_node_blockptr(cur, slot);
3860 gen = btrfs_node_ptr_generation(cur, slot);
3861 if (gen < min_trans) {
3862 slot++;
3863 continue;
3864 }
3865 if (!cache_only)
3866 break;
3867
Chris Masone02119d2008-09-05 16:13:11 -04003868 if (max_key) {
3869 btrfs_node_key(cur, &disk_key, slot);
3870 if (comp_keys(&disk_key, max_key) >= 0) {
3871 ret = 1;
3872 goto out;
3873 }
3874 }
3875
Chris Mason3f157a22008-06-25 16:01:31 -04003876 tmp = btrfs_find_tree_block(root, blockptr,
3877 btrfs_level_size(root, level - 1));
3878
3879 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3880 free_extent_buffer(tmp);
3881 break;
3882 }
3883 if (tmp)
3884 free_extent_buffer(tmp);
3885 slot++;
3886 }
Chris Masone02119d2008-09-05 16:13:11 -04003887find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003888 /*
3889 * we didn't find a candidate key in this node, walk forward
3890 * and find another one
3891 */
3892 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003893 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003894 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003895 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003896 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003897 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003898 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003899 goto again;
3900 } else {
3901 goto out;
3902 }
3903 }
3904 /* save our key for returning back */
3905 btrfs_node_key_to_cpu(cur, &found_key, slot);
3906 path->slots[level] = slot;
3907 if (level == path->lowest_level) {
3908 ret = 0;
3909 unlock_up(path, level, 1);
3910 goto out;
3911 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05003912 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003913 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00003914 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04003915
3916 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003917
Chris Mason3f157a22008-06-25 16:01:31 -04003918 path->locks[level - 1] = 1;
3919 path->nodes[level - 1] = cur;
3920 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05003921 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04003922 }
3923out:
3924 if (ret == 0)
3925 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05003926 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003927 return ret;
3928}
3929
3930/*
3931 * this is similar to btrfs_next_leaf, but does not try to preserve
3932 * and fixup the path. It looks for and returns the next key in the
3933 * tree based on the current path and the cache_only and min_trans
3934 * parameters.
3935 *
3936 * 0 is returned if another key is found, < 0 if there are any errors
3937 * and 1 is returned if there are no higher keys in the tree
3938 *
3939 * path->keep_locks should be set to 1 on the search made before
3940 * calling this function.
3941 */
Chris Masone7a84562008-06-25 16:01:31 -04003942int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04003943 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04003944 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003945{
Chris Masone7a84562008-06-25 16:01:31 -04003946 int slot;
3947 struct extent_buffer *c;
3948
Chris Mason934d3752008-12-08 16:43:10 -05003949 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05003950 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04003951 if (!path->nodes[level])
3952 return 1;
3953
3954 slot = path->slots[level] + 1;
3955 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003956next:
Chris Masone7a84562008-06-25 16:01:31 -04003957 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04003958 int ret;
3959 int orig_lowest;
3960 struct btrfs_key cur_key;
3961 if (level + 1 >= BTRFS_MAX_LEVEL ||
3962 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04003963 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04003964
3965 if (path->locks[level + 1]) {
3966 level++;
3967 continue;
3968 }
3969
3970 slot = btrfs_header_nritems(c) - 1;
3971 if (level == 0)
3972 btrfs_item_key_to_cpu(c, &cur_key, slot);
3973 else
3974 btrfs_node_key_to_cpu(c, &cur_key, slot);
3975
3976 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02003977 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04003978 path->lowest_level = level;
3979 ret = btrfs_search_slot(NULL, root, &cur_key, path,
3980 0, 0);
3981 path->lowest_level = orig_lowest;
3982 if (ret < 0)
3983 return ret;
3984
3985 c = path->nodes[level];
3986 slot = path->slots[level];
3987 if (ret == 0)
3988 slot++;
3989 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04003990 }
Yan Zheng33c66f42009-07-22 09:59:00 -04003991
Chris Masone7a84562008-06-25 16:01:31 -04003992 if (level == 0)
3993 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003994 else {
3995 u64 blockptr = btrfs_node_blockptr(c, slot);
3996 u64 gen = btrfs_node_ptr_generation(c, slot);
3997
3998 if (cache_only) {
3999 struct extent_buffer *cur;
4000 cur = btrfs_find_tree_block(root, blockptr,
4001 btrfs_level_size(root, level - 1));
4002 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4003 slot++;
4004 if (cur)
4005 free_extent_buffer(cur);
4006 goto next;
4007 }
4008 free_extent_buffer(cur);
4009 }
4010 if (gen < min_trans) {
4011 slot++;
4012 goto next;
4013 }
Chris Masone7a84562008-06-25 16:01:31 -04004014 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004015 }
Chris Masone7a84562008-06-25 16:01:31 -04004016 return 0;
4017 }
4018 return 1;
4019}
4020
Chris Mason7bb86312007-12-11 09:25:06 -05004021/*
Chris Mason925baed2008-06-25 16:01:30 -04004022 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004023 * returns 0 if it found something or 1 if there are no greater leaves.
4024 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004025 */
Chris Mason234b63a2007-03-13 10:46:10 -04004026int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004027{
4028 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004029 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004030 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004031 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004032 struct btrfs_key key;
4033 u32 nritems;
4034 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004035 int old_spinning = path->leave_spinning;
4036 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004037
4038 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004039 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004040 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004041
Chris Mason8e73f272009-04-03 10:14:18 -04004042 /*
4043 * we take the blocks in an order that upsets lockdep. Using
4044 * blocking mode is the only way around it.
4045 */
4046#ifdef CONFIG_DEBUG_LOCK_ALLOC
4047 force_blocking = 1;
4048#endif
Chris Mason925baed2008-06-25 16:01:30 -04004049
Chris Mason8e73f272009-04-03 10:14:18 -04004050 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4051again:
4052 level = 1;
4053 next = NULL;
David Sterbab3b4aa72011-04-21 01:20:15 +02004054 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004055
Chris Masona2135012008-06-25 16:01:30 -04004056 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004057
4058 if (!force_blocking)
4059 path->leave_spinning = 1;
4060
Chris Mason925baed2008-06-25 16:01:30 -04004061 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4062 path->keep_locks = 0;
4063
4064 if (ret < 0)
4065 return ret;
4066
Chris Masona2135012008-06-25 16:01:30 -04004067 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004068 /*
4069 * by releasing the path above we dropped all our locks. A balance
4070 * could have added more items next to the key that used to be
4071 * at the very end of the block. So, check again here and
4072 * advance the path if there are now more items available.
4073 */
Chris Masona2135012008-06-25 16:01:30 -04004074 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004075 if (ret == 0)
4076 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004077 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004078 goto done;
4079 }
Chris Masond97e63b2007-02-20 16:40:44 -05004080
Chris Masond3977122009-01-05 21:25:51 -05004081 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004082 if (!path->nodes[level]) {
4083 ret = 1;
4084 goto done;
4085 }
Chris Mason5f39d392007-10-15 16:14:19 -04004086
Chris Masond97e63b2007-02-20 16:40:44 -05004087 slot = path->slots[level] + 1;
4088 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004089 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004090 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004091 if (level == BTRFS_MAX_LEVEL) {
4092 ret = 1;
4093 goto done;
4094 }
Chris Masond97e63b2007-02-20 16:40:44 -05004095 continue;
4096 }
Chris Mason5f39d392007-10-15 16:14:19 -04004097
Chris Mason925baed2008-06-25 16:01:30 -04004098 if (next) {
4099 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004100 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004101 }
Chris Mason5f39d392007-10-15 16:14:19 -04004102
Chris Mason8e73f272009-04-03 10:14:18 -04004103 next = c;
4104 ret = read_block_for_search(NULL, root, path, &next, level,
4105 slot, &key);
4106 if (ret == -EAGAIN)
4107 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004108
Chris Mason76a05b32009-05-14 13:24:30 -04004109 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004110 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004111 goto done;
4112 }
4113
Chris Mason5cd57b22008-06-25 16:01:30 -04004114 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004115 ret = btrfs_try_spin_lock(next);
4116 if (!ret) {
4117 btrfs_set_path_blocking(path);
4118 btrfs_tree_lock(next);
4119 if (!force_blocking)
4120 btrfs_clear_path_blocking(path, next);
4121 }
4122 if (force_blocking)
4123 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004124 }
Chris Masond97e63b2007-02-20 16:40:44 -05004125 break;
4126 }
4127 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004128 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004129 level--;
4130 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004131 if (path->locks[level])
4132 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004133
Chris Mason5f39d392007-10-15 16:14:19 -04004134 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004135 path->nodes[level] = next;
4136 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004137 if (!path->skip_locking)
4138 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004139
Chris Masond97e63b2007-02-20 16:40:44 -05004140 if (!level)
4141 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004142
Chris Mason8e73f272009-04-03 10:14:18 -04004143 ret = read_block_for_search(NULL, root, path, &next, level,
4144 0, &key);
4145 if (ret == -EAGAIN)
4146 goto again;
4147
Chris Mason76a05b32009-05-14 13:24:30 -04004148 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004149 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004150 goto done;
4151 }
4152
Chris Mason5cd57b22008-06-25 16:01:30 -04004153 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004154 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004155 ret = btrfs_try_spin_lock(next);
4156 if (!ret) {
4157 btrfs_set_path_blocking(path);
4158 btrfs_tree_lock(next);
4159 if (!force_blocking)
4160 btrfs_clear_path_blocking(path, next);
4161 }
4162 if (force_blocking)
4163 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004164 }
Chris Masond97e63b2007-02-20 16:40:44 -05004165 }
Chris Mason8e73f272009-04-03 10:14:18 -04004166 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004167done:
4168 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004169 path->leave_spinning = old_spinning;
4170 if (!old_spinning)
4171 btrfs_set_path_blocking(path);
4172
4173 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004174}
Chris Mason0b86a832008-03-24 15:01:56 -04004175
Chris Mason3f157a22008-06-25 16:01:31 -04004176/*
4177 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4178 * searching until it gets past min_objectid or finds an item of 'type'
4179 *
4180 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4181 */
Chris Mason0b86a832008-03-24 15:01:56 -04004182int btrfs_previous_item(struct btrfs_root *root,
4183 struct btrfs_path *path, u64 min_objectid,
4184 int type)
4185{
4186 struct btrfs_key found_key;
4187 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004188 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004189 int ret;
4190
Chris Masond3977122009-01-05 21:25:51 -05004191 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004192 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004193 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004194 ret = btrfs_prev_leaf(root, path);
4195 if (ret != 0)
4196 return ret;
4197 } else {
4198 path->slots[0]--;
4199 }
4200 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004201 nritems = btrfs_header_nritems(leaf);
4202 if (nritems == 0)
4203 return 1;
4204 if (path->slots[0] == nritems)
4205 path->slots[0]--;
4206
Chris Mason0b86a832008-03-24 15:01:56 -04004207 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004208 if (found_key.objectid < min_objectid)
4209 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004210 if (found_key.type == type)
4211 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004212 if (found_key.objectid == min_objectid &&
4213 found_key.type < type)
4214 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004215 }
4216 return 1;
4217}