blob: 2e667868e0d2b8b75649572d10602ac56679a0d0 [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 Masonb9447ef2009-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 Mason5708b952007-10-25 15:43:18 -0400629 if (!parent->map_token) {
630 map_extent_buffer(parent,
631 btrfs_node_key_ptr_offset(i),
632 sizeof(struct btrfs_key_ptr),
633 &parent->map_token, &parent->kaddr,
634 &parent->map_start, &parent->map_len,
635 KM_USER1);
636 }
Chris Mason081e9572007-11-06 10:26:24 -0500637 btrfs_node_key(parent, &disk_key, i);
638 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
639 continue;
640
641 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400642 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400643 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400644 if (last_block == 0)
645 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400646
Chris Mason6702ed42007-08-07 16:15:09 -0400647 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400648 other = btrfs_node_blockptr(parent, i - 1);
649 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400650 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400651 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400652 other = btrfs_node_blockptr(parent, i + 1);
653 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400654 }
Chris Masone9d0b132007-08-10 14:06:19 -0400655 if (close) {
656 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400657 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400658 }
Chris Mason5708b952007-10-25 15:43:18 -0400659 if (parent->map_token) {
660 unmap_extent_buffer(parent, parent->map_token,
661 KM_USER1);
662 parent->map_token = NULL;
663 }
Chris Mason6702ed42007-08-07 16:15:09 -0400664
Chris Mason6b800532007-10-15 16:17:34 -0400665 cur = btrfs_find_tree_block(root, blocknr, blocksize);
666 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400667 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400668 else
669 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400670 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400671 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400672 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400673 continue;
674 }
Chris Mason6b800532007-10-15 16:17:34 -0400675 if (!cur) {
676 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400677 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000678 if (!cur)
679 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400680 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400681 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400682 }
Chris Mason6702ed42007-08-07 16:15:09 -0400683 }
Chris Masone9d0b132007-08-10 14:06:19 -0400684 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400685 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400686
Chris Masone7a84562008-06-25 16:01:31 -0400687 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500688 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400689 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400690 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400691 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400692 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400693 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400694 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400695 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400696 break;
Yan252c38f2007-08-29 09:11:44 -0400697 }
Chris Masone7a84562008-06-25 16:01:31 -0400698 search_start = cur->start;
699 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400700 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400701 btrfs_tree_unlock(cur);
702 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400703 }
Chris Mason5708b952007-10-25 15:43:18 -0400704 if (parent->map_token) {
705 unmap_extent_buffer(parent, parent->map_token,
706 KM_USER1);
707 parent->map_token = NULL;
708 }
Chris Mason6702ed42007-08-07 16:15:09 -0400709 return err;
710}
711
Chris Mason74123bd2007-02-02 11:05:29 -0500712/*
713 * The leaf data grows from end-to-front in the node.
714 * this returns the address of the start of the last item,
715 * which is the stop of the leaf data stack
716 */
Chris Mason123abc82007-03-14 14:14:43 -0400717static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400718 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500719{
Chris Mason5f39d392007-10-15 16:14:19 -0400720 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500721 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400722 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400723 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500724}
725
Chris Masonaa5d6be2007-02-28 16:35:06 -0500726
Chris Mason74123bd2007-02-02 11:05:29 -0500727/*
Chris Mason5f39d392007-10-15 16:14:19 -0400728 * search for key in the extent_buffer. The items start at offset p,
729 * and they are item_size apart. There are 'max' items in p.
730 *
Chris Mason74123bd2007-02-02 11:05:29 -0500731 * the slot in the array is returned via slot, and it points to
732 * the place where you would insert key if it is not found in
733 * the array.
734 *
735 * slot may point to max if the key is bigger than all of the keys
736 */
Chris Masone02119d2008-09-05 16:13:11 -0400737static noinline int generic_bin_search(struct extent_buffer *eb,
738 unsigned long p,
739 int item_size, struct btrfs_key *key,
740 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500741{
742 int low = 0;
743 int high = max;
744 int mid;
745 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400746 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400747 struct btrfs_disk_key unaligned;
748 unsigned long offset;
749 char *map_token = NULL;
750 char *kaddr = NULL;
751 unsigned long map_start = 0;
752 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400753 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500754
Chris Masond3977122009-01-05 21:25:51 -0500755 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500756 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400757 offset = p + mid * item_size;
758
759 if (!map_token || offset < map_start ||
760 (offset + sizeof(struct btrfs_disk_key)) >
761 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400762 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400763 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400764 map_token = NULL;
765 }
Chris Mason934d3752008-12-08 16:43:10 -0500766
767 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400768 sizeof(struct btrfs_disk_key),
769 &map_token, &kaddr,
770 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400771
Chris Mason479965d2007-10-15 16:14:27 -0400772 if (!err) {
773 tmp = (struct btrfs_disk_key *)(kaddr + offset -
774 map_start);
775 } else {
776 read_extent_buffer(eb, &unaligned,
777 offset, sizeof(unaligned));
778 tmp = &unaligned;
779 }
780
Chris Mason5f39d392007-10-15 16:14:19 -0400781 } else {
782 tmp = (struct btrfs_disk_key *)(kaddr + offset -
783 map_start);
784 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500785 ret = comp_keys(tmp, key);
786
787 if (ret < 0)
788 low = mid + 1;
789 else if (ret > 0)
790 high = mid;
791 else {
792 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400793 if (map_token)
794 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500795 return 0;
796 }
797 }
798 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400799 if (map_token)
800 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500801 return 1;
802}
803
Chris Mason97571fd2007-02-24 13:39:08 -0500804/*
805 * simple bin_search frontend that does the right thing for
806 * leaves vs nodes
807 */
Chris Mason5f39d392007-10-15 16:14:19 -0400808static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
809 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500810{
Chris Mason5f39d392007-10-15 16:14:19 -0400811 if (level == 0) {
812 return generic_bin_search(eb,
813 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400814 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400815 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400816 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500817 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400818 return generic_bin_search(eb,
819 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400820 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400821 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400822 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500823 }
824 return -1;
825}
826
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400827int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
828 int level, int *slot)
829{
830 return bin_search(eb, key, level, slot);
831}
832
Yan, Zhengf0486c62010-05-16 10:46:25 -0400833static void root_add_used(struct btrfs_root *root, u32 size)
834{
835 spin_lock(&root->accounting_lock);
836 btrfs_set_root_used(&root->root_item,
837 btrfs_root_used(&root->root_item) + size);
838 spin_unlock(&root->accounting_lock);
839}
840
841static void root_sub_used(struct btrfs_root *root, u32 size)
842{
843 spin_lock(&root->accounting_lock);
844 btrfs_set_root_used(&root->root_item,
845 btrfs_root_used(&root->root_item) - size);
846 spin_unlock(&root->accounting_lock);
847}
848
Chris Masond352ac62008-09-29 15:18:18 -0400849/* given a node and slot number, this reads the blocks it points to. The
850 * extent buffer is returned with a reference taken (but unlocked).
851 * NULL is returned on error.
852 */
Chris Masone02119d2008-09-05 16:13:11 -0400853static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400854 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500855{
Chris Masonca7a79a2008-05-12 12:59:19 -0400856 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500857 if (slot < 0)
858 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400859 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500860 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400861
862 BUG_ON(level == 0);
863
Chris Masondb945352007-10-15 16:15:53 -0400864 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400865 btrfs_level_size(root, level - 1),
866 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500867}
868
Chris Masond352ac62008-09-29 15:18:18 -0400869/*
870 * node level balancing, used to make sure nodes are in proper order for
871 * item deletion. We balance from the top down, so we have to make sure
872 * that a deletion won't leave an node completely empty later on.
873 */
Chris Masone02119d2008-09-05 16:13:11 -0400874static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500875 struct btrfs_root *root,
876 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500877{
Chris Mason5f39d392007-10-15 16:14:19 -0400878 struct extent_buffer *right = NULL;
879 struct extent_buffer *mid;
880 struct extent_buffer *left = NULL;
881 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500882 int ret = 0;
883 int wret;
884 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500885 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500886 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500887
888 if (level == 0)
889 return 0;
890
Chris Mason5f39d392007-10-15 16:14:19 -0400891 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500892
Chris Mason925baed2008-06-25 16:01:30 -0400893 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500894 WARN_ON(btrfs_header_generation(mid) != trans->transid);
895
Chris Mason1d4f8a02007-03-13 09:28:32 -0400896 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500897
Chris Mason234b63a2007-03-13 10:46:10 -0400898 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400899 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500900 pslot = path->slots[level + 1];
901
Chris Mason40689472007-03-17 14:29:23 -0400902 /*
903 * deal with the case where there is only one pointer in the root
904 * by promoting the node below to a root
905 */
Chris Mason5f39d392007-10-15 16:14:19 -0400906 if (!parent) {
907 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500908
Chris Mason5f39d392007-10-15 16:14:19 -0400909 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500910 return 0;
911
912 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400913 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500914 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400915 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500916 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400917 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400918 if (ret) {
919 btrfs_tree_unlock(child);
920 free_extent_buffer(child);
921 goto enospc;
922 }
Yan2f375ab2008-02-01 14:58:07 -0500923
Chris Mason240f62c2011-03-23 14:54:42 -0400924 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400925
Chris Mason0b86a832008-03-24 15:01:56 -0400926 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400927 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500928
Chris Mason925baed2008-06-25 16:01:30 -0400929 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500930 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400931 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400932 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500933 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400934 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400935
936 root_sub_used(root, mid->len);
937 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500938 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400939 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400940 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500941 }
Chris Mason5f39d392007-10-15 16:14:19 -0400942 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400943 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500944 return 0;
945
Andi Kleen559af822010-10-29 15:14:37 -0400946 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400947
Chris Mason5f39d392007-10-15 16:14:19 -0400948 left = read_node_slot(root, parent, pslot - 1);
949 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400950 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500951 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400952 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400953 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400954 if (wret) {
955 ret = wret;
956 goto enospc;
957 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400958 }
Chris Mason5f39d392007-10-15 16:14:19 -0400959 right = read_node_slot(root, parent, pslot + 1);
960 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400961 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500962 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400963 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400964 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400965 if (wret) {
966 ret = wret;
967 goto enospc;
968 }
969 }
970
971 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400972 if (left) {
973 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400974 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500975 if (wret < 0)
976 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -0400977 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500978 }
Chris Mason79f95c82007-03-01 15:16:26 -0500979
980 /*
981 * then try to empty the right most buffer into the middle
982 */
Chris Mason5f39d392007-10-15 16:14:19 -0400983 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400984 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400985 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500986 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400987 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400988 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400989 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -0400990 wret = del_ptr(trans, root, path, level + 1, pslot +
991 1);
Chris Masonbb803952007-03-01 12:04:21 -0500992 if (wret)
993 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400994 root_sub_used(root, right->len);
995 btrfs_free_tree_block(trans, root, right, 0, 1);
996 free_extent_buffer(right);
997 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500998 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400999 struct btrfs_disk_key right_key;
1000 btrfs_node_key(right, &right_key, 0);
1001 btrfs_set_node_key(parent, &right_key, pslot + 1);
1002 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001003 }
1004 }
Chris Mason5f39d392007-10-15 16:14:19 -04001005 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001006 /*
1007 * we're not allowed to leave a node with one item in the
1008 * tree during a delete. A deletion from lower in the tree
1009 * could try to delete the only pointer in this node.
1010 * So, pull some keys from the left.
1011 * There has to be a left pointer at this point because
1012 * otherwise we would have pulled some pointers from the
1013 * right
1014 */
Chris Mason5f39d392007-10-15 16:14:19 -04001015 BUG_ON(!left);
1016 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001017 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001018 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001019 goto enospc;
1020 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001021 if (wret == 1) {
1022 wret = push_node_left(trans, root, left, mid, 1);
1023 if (wret < 0)
1024 ret = wret;
1025 }
Chris Mason79f95c82007-03-01 15:16:26 -05001026 BUG_ON(wret == 1);
1027 }
Chris Mason5f39d392007-10-15 16:14:19 -04001028 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001029 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001030 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001031 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001032 if (wret)
1033 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001034 root_sub_used(root, mid->len);
1035 btrfs_free_tree_block(trans, root, mid, 0, 1);
1036 free_extent_buffer(mid);
1037 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001038 } else {
1039 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001040 struct btrfs_disk_key mid_key;
1041 btrfs_node_key(mid, &mid_key, 0);
1042 btrfs_set_node_key(parent, &mid_key, pslot);
1043 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001044 }
Chris Masonbb803952007-03-01 12:04:21 -05001045
Chris Mason79f95c82007-03-01 15:16:26 -05001046 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001047 if (left) {
1048 if (btrfs_header_nritems(left) > orig_slot) {
1049 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001050 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001051 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001052 path->slots[level + 1] -= 1;
1053 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001054 if (mid) {
1055 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001056 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001057 }
Chris Masonbb803952007-03-01 12:04:21 -05001058 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001059 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001060 path->slots[level] = orig_slot;
1061 }
1062 }
Chris Mason79f95c82007-03-01 15:16:26 -05001063 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001064 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001065 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001066 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001067enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001068 if (right) {
1069 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001070 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001071 }
1072 if (left) {
1073 if (path->nodes[level] != left)
1074 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001075 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001076 }
Chris Masonbb803952007-03-01 12:04:21 -05001077 return ret;
1078}
1079
Chris Masond352ac62008-09-29 15:18:18 -04001080/* Node balancing for insertion. Here we only split or push nodes around
1081 * when they are completely full. This is also done top down, so we
1082 * have to be pessimistic.
1083 */
Chris Masond3977122009-01-05 21:25:51 -05001084static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001085 struct btrfs_root *root,
1086 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001087{
Chris Mason5f39d392007-10-15 16:14:19 -04001088 struct extent_buffer *right = NULL;
1089 struct extent_buffer *mid;
1090 struct extent_buffer *left = NULL;
1091 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001092 int ret = 0;
1093 int wret;
1094 int pslot;
1095 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001096
1097 if (level == 0)
1098 return 1;
1099
Chris Mason5f39d392007-10-15 16:14:19 -04001100 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001101 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001102
1103 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001104 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001105 pslot = path->slots[level + 1];
1106
Chris Mason5f39d392007-10-15 16:14:19 -04001107 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001108 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001109
Chris Mason5f39d392007-10-15 16:14:19 -04001110 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001111
1112 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001113 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001114 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001115
1116 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001117 btrfs_set_lock_blocking(left);
1118
Chris Mason5f39d392007-10-15 16:14:19 -04001119 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001120 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1121 wret = 1;
1122 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001123 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001124 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001125 if (ret)
1126 wret = 1;
1127 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001128 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001129 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001130 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001131 }
Chris Masone66f7092007-04-20 13:16:02 -04001132 if (wret < 0)
1133 ret = wret;
1134 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001135 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001136 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001137 btrfs_node_key(mid, &disk_key, 0);
1138 btrfs_set_node_key(parent, &disk_key, pslot);
1139 btrfs_mark_buffer_dirty(parent);
1140 if (btrfs_header_nritems(left) > orig_slot) {
1141 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001142 path->slots[level + 1] -= 1;
1143 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001144 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001145 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001146 } else {
1147 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001148 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001149 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001150 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001151 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001152 }
Chris Masone66f7092007-04-20 13:16:02 -04001153 return 0;
1154 }
Chris Mason925baed2008-06-25 16:01:30 -04001155 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001156 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001157 }
Chris Mason925baed2008-06-25 16:01:30 -04001158 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001159
1160 /*
1161 * then try to empty the right most buffer into the middle
1162 */
Chris Mason5f39d392007-10-15 16:14:19 -04001163 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001164 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001165
Chris Mason925baed2008-06-25 16:01:30 -04001166 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001167 btrfs_set_lock_blocking(right);
1168
Chris Mason5f39d392007-10-15 16:14:19 -04001169 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001170 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1171 wret = 1;
1172 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001173 ret = btrfs_cow_block(trans, root, right,
1174 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001175 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001176 if (ret)
1177 wret = 1;
1178 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001179 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001180 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001182 }
Chris Masone66f7092007-04-20 13:16:02 -04001183 if (wret < 0)
1184 ret = wret;
1185 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001186 struct btrfs_disk_key disk_key;
1187
1188 btrfs_node_key(right, &disk_key, 0);
1189 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1190 btrfs_mark_buffer_dirty(parent);
1191
1192 if (btrfs_header_nritems(mid) <= orig_slot) {
1193 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001194 path->slots[level + 1] += 1;
1195 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001196 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001197 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001198 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001199 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001200 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001201 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001202 }
Chris Masone66f7092007-04-20 13:16:02 -04001203 return 0;
1204 }
Chris Mason925baed2008-06-25 16:01:30 -04001205 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001206 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001207 }
Chris Masone66f7092007-04-20 13:16:02 -04001208 return 1;
1209}
1210
Chris Mason74123bd2007-02-02 11:05:29 -05001211/*
Chris Masond352ac62008-09-29 15:18:18 -04001212 * readahead one full node of leaves, finding things that are close
1213 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001214 */
Chris Masonc8c42862009-04-03 10:14:18 -04001215static void reada_for_search(struct btrfs_root *root,
1216 struct btrfs_path *path,
1217 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001218{
Chris Mason5f39d392007-10-15 16:14:19 -04001219 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001220 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001221 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001222 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001223 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001224 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001225 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001226 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001227 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001228 u32 nr;
1229 u32 blocksize;
1230 u32 nscan = 0;
Josef Bacik25b8b932011-06-08 14:36:54 -04001231 bool map = true;
Chris Masondb945352007-10-15 16:15:53 -04001232
Chris Masona6b6e752007-10-15 16:22:39 -04001233 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001234 return;
1235
Chris Mason6702ed42007-08-07 16:15:09 -04001236 if (!path->nodes[level])
1237 return;
1238
Chris Mason5f39d392007-10-15 16:14:19 -04001239 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001240
Chris Mason3c69fae2007-08-07 15:52:22 -04001241 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001242 blocksize = btrfs_level_size(root, level - 1);
1243 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001244 if (eb) {
1245 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001246 return;
1247 }
1248
Chris Masona7175312009-01-22 09:23:10 -05001249 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001250
Chris Mason5f39d392007-10-15 16:14:19 -04001251 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001252 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001253 if (node->map_token || path->skip_locking)
1254 map = false;
1255
Chris Masond3977122009-01-05 21:25:51 -05001256 while (1) {
Josef Bacik25b8b932011-06-08 14:36:54 -04001257 if (map && !node->map_token) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001258 unsigned long offset = btrfs_node_key_ptr_offset(nr);
1259 map_private_extent_buffer(node, offset,
1260 sizeof(struct btrfs_key_ptr),
1261 &node->map_token,
1262 &node->kaddr,
1263 &node->map_start,
1264 &node->map_len, KM_USER1);
1265 }
Chris Mason6b800532007-10-15 16:17:34 -04001266 if (direction < 0) {
1267 if (nr == 0)
1268 break;
1269 nr--;
1270 } else if (direction > 0) {
1271 nr++;
1272 if (nr >= nritems)
1273 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001274 }
Chris Mason01f46652007-12-21 16:24:26 -05001275 if (path->reada < 0 && objectid) {
1276 btrfs_node_key(node, &disk_key, nr);
1277 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1278 break;
1279 }
Chris Mason6b800532007-10-15 16:17:34 -04001280 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001281 if ((search <= target && target - search <= 65536) ||
1282 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001283 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacik25b8b932011-06-08 14:36:54 -04001284 if (map && node->map_token) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001285 unmap_extent_buffer(node, node->map_token,
1286 KM_USER1);
1287 node->map_token = NULL;
1288 }
1289 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001290 nread += blocksize;
1291 }
1292 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001293 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001294 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001295 }
Josef Bacik25b8b932011-06-08 14:36:54 -04001296 if (map && node->map_token) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001297 unmap_extent_buffer(node, node->map_token, KM_USER1);
1298 node->map_token = NULL;
1299 }
Chris Mason3c69fae2007-08-07 15:52:22 -04001300}
Chris Mason925baed2008-06-25 16:01:30 -04001301
Chris Masond352ac62008-09-29 15:18:18 -04001302/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001303 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1304 * cache
1305 */
1306static noinline int reada_for_balance(struct btrfs_root *root,
1307 struct btrfs_path *path, int level)
1308{
1309 int slot;
1310 int nritems;
1311 struct extent_buffer *parent;
1312 struct extent_buffer *eb;
1313 u64 gen;
1314 u64 block1 = 0;
1315 u64 block2 = 0;
1316 int ret = 0;
1317 int blocksize;
1318
Chris Mason8c594ea2009-04-20 15:50:10 -04001319 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001320 if (!parent)
1321 return 0;
1322
1323 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001324 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001325 blocksize = btrfs_level_size(root, level);
1326
1327 if (slot > 0) {
1328 block1 = btrfs_node_blockptr(parent, slot - 1);
1329 gen = btrfs_node_ptr_generation(parent, slot - 1);
1330 eb = btrfs_find_tree_block(root, block1, blocksize);
1331 if (eb && btrfs_buffer_uptodate(eb, gen))
1332 block1 = 0;
1333 free_extent_buffer(eb);
1334 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001335 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001336 block2 = btrfs_node_blockptr(parent, slot + 1);
1337 gen = btrfs_node_ptr_generation(parent, slot + 1);
1338 eb = btrfs_find_tree_block(root, block2, blocksize);
1339 if (eb && btrfs_buffer_uptodate(eb, gen))
1340 block2 = 0;
1341 free_extent_buffer(eb);
1342 }
1343 if (block1 || block2) {
1344 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001345
1346 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001347 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001348
1349 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001350 if (block1)
1351 readahead_tree_block(root, block1, blocksize, 0);
1352 if (block2)
1353 readahead_tree_block(root, block2, blocksize, 0);
1354
1355 if (block1) {
1356 eb = read_tree_block(root, block1, blocksize, 0);
1357 free_extent_buffer(eb);
1358 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001359 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001360 eb = read_tree_block(root, block2, blocksize, 0);
1361 free_extent_buffer(eb);
1362 }
1363 }
1364 return ret;
1365}
1366
1367
1368/*
Chris Masond3977122009-01-05 21:25:51 -05001369 * when we walk down the tree, it is usually safe to unlock the higher layers
1370 * in the tree. The exceptions are when our path goes through slot 0, because
1371 * operations on the tree might require changing key pointers higher up in the
1372 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001373 *
Chris Masond3977122009-01-05 21:25:51 -05001374 * callers might also have set path->keep_locks, which tells this code to keep
1375 * the lock if the path points to the last slot in the block. This is part of
1376 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001377 *
Chris Masond3977122009-01-05 21:25:51 -05001378 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1379 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001380 */
Chris Masone02119d2008-09-05 16:13:11 -04001381static noinline void unlock_up(struct btrfs_path *path, int level,
1382 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001383{
1384 int i;
1385 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001386 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001387 struct extent_buffer *t;
1388
1389 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1390 if (!path->nodes[i])
1391 break;
1392 if (!path->locks[i])
1393 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001394 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001395 skip_level = i + 1;
1396 continue;
1397 }
Chris Mason051e1b92008-06-25 16:01:30 -04001398 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001399 u32 nritems;
1400 t = path->nodes[i];
1401 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001402 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001403 skip_level = i + 1;
1404 continue;
1405 }
1406 }
Chris Mason051e1b92008-06-25 16:01:30 -04001407 if (skip_level < i && i >= lowest_unlock)
1408 no_skips = 1;
1409
Chris Mason925baed2008-06-25 16:01:30 -04001410 t = path->nodes[i];
1411 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1412 btrfs_tree_unlock(t);
1413 path->locks[i] = 0;
1414 }
1415 }
1416}
1417
Chris Mason3c69fae2007-08-07 15:52:22 -04001418/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001419 * This releases any locks held in the path starting at level and
1420 * going all the way up to the root.
1421 *
1422 * btrfs_search_slot will keep the lock held on higher nodes in a few
1423 * corner cases, such as COW of the block at slot zero in the node. This
1424 * ignores those rules, and it should only be called when there are no
1425 * more updates to be done higher up in the tree.
1426 */
1427noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1428{
1429 int i;
1430
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001431 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001432 return;
1433
1434 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1435 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001436 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001437 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001438 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001439 btrfs_tree_unlock(path->nodes[i]);
1440 path->locks[i] = 0;
1441 }
1442}
1443
1444/*
Chris Masonc8c42862009-04-03 10:14:18 -04001445 * helper function for btrfs_search_slot. The goal is to find a block
1446 * in cache without setting the path to blocking. If we find the block
1447 * we return zero and the path is unchanged.
1448 *
1449 * If we can't find the block, we set the path blocking and do some
1450 * reada. -EAGAIN is returned and the search must be repeated.
1451 */
1452static int
1453read_block_for_search(struct btrfs_trans_handle *trans,
1454 struct btrfs_root *root, struct btrfs_path *p,
1455 struct extent_buffer **eb_ret, int level, int slot,
1456 struct btrfs_key *key)
1457{
1458 u64 blocknr;
1459 u64 gen;
1460 u32 blocksize;
1461 struct extent_buffer *b = *eb_ret;
1462 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001463 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001464
1465 blocknr = btrfs_node_blockptr(b, slot);
1466 gen = btrfs_node_ptr_generation(b, slot);
1467 blocksize = btrfs_level_size(root, level - 1);
1468
1469 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001470 if (tmp) {
1471 if (btrfs_buffer_uptodate(tmp, 0)) {
1472 if (btrfs_buffer_uptodate(tmp, gen)) {
1473 /*
1474 * we found an up to date block without
1475 * sleeping, return
1476 * right away
1477 */
1478 *eb_ret = tmp;
1479 return 0;
1480 }
1481 /* the pages were up to date, but we failed
1482 * the generation number check. Do a full
1483 * read for the generation number that is correct.
1484 * We must do this without dropping locks so
1485 * we can trust our generation number
1486 */
1487 free_extent_buffer(tmp);
1488 tmp = read_tree_block(root, blocknr, blocksize, gen);
1489 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1490 *eb_ret = tmp;
1491 return 0;
1492 }
1493 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001494 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001495 return -EIO;
1496 }
Chris Masonc8c42862009-04-03 10:14:18 -04001497 }
1498
1499 /*
1500 * reduce lock contention at high levels
1501 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001502 * we read. Don't release the lock on the current
1503 * level because we need to walk this node to figure
1504 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001505 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001506 btrfs_unlock_up_safe(p, level + 1);
1507 btrfs_set_path_blocking(p);
1508
Chris Masoncb449212010-10-24 11:01:27 -04001509 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001510 if (p->reada)
1511 reada_for_search(root, p, level, slot, key->objectid);
1512
David Sterbab3b4aa72011-04-21 01:20:15 +02001513 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001514
1515 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001516 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001517 if (tmp) {
1518 /*
1519 * If the read above didn't mark this buffer up to date,
1520 * it will never end up being up to date. Set ret to EIO now
1521 * and give up so that our caller doesn't loop forever
1522 * on our EAGAINs.
1523 */
1524 if (!btrfs_buffer_uptodate(tmp, 0))
1525 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001526 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001527 }
1528 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001529}
1530
1531/*
1532 * helper function for btrfs_search_slot. This does all of the checks
1533 * for node-level blocks and does any balancing required based on
1534 * the ins_len.
1535 *
1536 * If no extra work was required, zero is returned. If we had to
1537 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1538 * start over
1539 */
1540static int
1541setup_nodes_for_search(struct btrfs_trans_handle *trans,
1542 struct btrfs_root *root, struct btrfs_path *p,
1543 struct extent_buffer *b, int level, int ins_len)
1544{
1545 int ret;
1546 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1547 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1548 int sret;
1549
1550 sret = reada_for_balance(root, p, level);
1551 if (sret)
1552 goto again;
1553
1554 btrfs_set_path_blocking(p);
1555 sret = split_node(trans, root, p, level);
1556 btrfs_clear_path_blocking(p, NULL);
1557
1558 BUG_ON(sret > 0);
1559 if (sret) {
1560 ret = sret;
1561 goto done;
1562 }
1563 b = p->nodes[level];
1564 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001565 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001566 int sret;
1567
1568 sret = reada_for_balance(root, p, level);
1569 if (sret)
1570 goto again;
1571
1572 btrfs_set_path_blocking(p);
1573 sret = balance_level(trans, root, p, level);
1574 btrfs_clear_path_blocking(p, NULL);
1575
1576 if (sret) {
1577 ret = sret;
1578 goto done;
1579 }
1580 b = p->nodes[level];
1581 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001582 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001583 goto again;
1584 }
1585 BUG_ON(btrfs_header_nritems(b) == 1);
1586 }
1587 return 0;
1588
1589again:
1590 ret = -EAGAIN;
1591done:
1592 return ret;
1593}
1594
1595/*
Chris Mason74123bd2007-02-02 11:05:29 -05001596 * look for key in the tree. path is filled in with nodes along the way
1597 * if key is found, we return zero and you can find the item in the leaf
1598 * level of the path (level 0)
1599 *
1600 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001601 * be inserted, and 1 is returned. If there are other errors during the
1602 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001603 *
1604 * if ins_len > 0, nodes and leaves will be split as we walk down the
1605 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1606 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001607 */
Chris Masone089f052007-03-16 16:20:31 -04001608int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1609 *root, struct btrfs_key *key, struct btrfs_path *p, int
1610 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001611{
Chris Mason5f39d392007-10-15 16:14:19 -04001612 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001613 int slot;
1614 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001615 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001616 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001617 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001618 u8 lowest_level = 0;
1619
Chris Mason6702ed42007-08-07 16:15:09 -04001620 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001621 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001622 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001623
Chris Mason925baed2008-06-25 16:01:30 -04001624 if (ins_len < 0)
1625 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001626
Chris Masonbb803952007-03-01 12:04:21 -05001627again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001628 if (p->search_commit_root) {
1629 b = root->commit_root;
1630 extent_buffer_get(b);
1631 if (!p->skip_locking)
1632 btrfs_tree_lock(b);
1633 } else {
1634 if (p->skip_locking)
1635 b = btrfs_root_node(root);
1636 else
1637 b = btrfs_lock_root_node(root);
1638 }
Chris Mason925baed2008-06-25 16:01:30 -04001639
Chris Masoneb60cea2007-02-02 09:18:22 -05001640 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001641 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001642
1643 /*
1644 * setup the path here so we can release it under lock
1645 * contention with the cow code
1646 */
1647 p->nodes[level] = b;
1648 if (!p->skip_locking)
1649 p->locks[level] = 1;
1650
Chris Mason02217ed2007-03-02 16:08:05 -05001651 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001652 /*
1653 * if we don't really need to cow this block
1654 * then we don't want to set the path blocking,
1655 * so we test it here
1656 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001658 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001659
Chris Masonb4ce94d2009-02-04 09:25:08 -05001660 btrfs_set_path_blocking(p);
1661
Yan Zheng33c66f42009-07-22 09:59:00 -04001662 err = btrfs_cow_block(trans, root, b,
1663 p->nodes[level + 1],
1664 p->slots[level + 1], &b);
1665 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001666 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001667 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001668 }
Chris Mason02217ed2007-03-02 16:08:05 -05001669 }
Chris Mason65b51a02008-08-01 15:11:20 -04001670cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001671 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001672
Chris Masoneb60cea2007-02-02 09:18:22 -05001673 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001674 if (!p->skip_locking)
1675 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001676
Chris Mason4008c042009-02-12 14:09:45 -05001677 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001678
1679 /*
1680 * we have a lock on b and as long as we aren't changing
1681 * the tree, there is no way to for the items in b to change.
1682 * It is safe to drop the lock on our parent before we
1683 * go through the expensive btree search on b.
1684 *
1685 * If cow is true, then we might be changing slot zero,
1686 * which may require changing the parent. So, we can't
1687 * drop the lock until after we know which slot we're
1688 * operating on.
1689 */
1690 if (!cow)
1691 btrfs_unlock_up_safe(p, level + 1);
1692
Chris Mason5f39d392007-10-15 16:14:19 -04001693 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001694
Chris Mason5f39d392007-10-15 16:14:19 -04001695 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001696 int dec = 0;
1697 if (ret && slot > 0) {
1698 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001699 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001700 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001701 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001702 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001703 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001704 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001705 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001706 if (err) {
1707 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001708 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001709 }
Chris Masonc8c42862009-04-03 10:14:18 -04001710 b = p->nodes[level];
1711 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001712
Chris Masonf9efa9c2008-06-25 16:14:04 -04001713 unlock_up(p, level, lowest_unlock);
1714
Chris Mason925baed2008-06-25 16:01:30 -04001715 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001716 if (dec)
1717 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001718 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001719 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001720
Yan Zheng33c66f42009-07-22 09:59:00 -04001721 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001722 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001723 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001724 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001725 if (err) {
1726 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001727 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001728 }
Chris Mason76a05b32009-05-14 13:24:30 -04001729
Chris Masonb4ce94d2009-02-04 09:25:08 -05001730 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001731 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001732 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001733
Yan Zheng33c66f42009-07-22 09:59:00 -04001734 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001735 btrfs_set_path_blocking(p);
1736 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001737 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001738 }
1739 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001740 } else {
1741 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001742 if (ins_len > 0 &&
1743 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001744 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001745 err = split_leaf(trans, root, key,
1746 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001747 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001748
Yan Zheng33c66f42009-07-22 09:59:00 -04001749 BUG_ON(err > 0);
1750 if (err) {
1751 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001752 goto done;
1753 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001754 }
Chris Mason459931e2008-12-10 09:10:46 -05001755 if (!p->search_for_split)
1756 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001757 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001758 }
1759 }
Chris Mason65b51a02008-08-01 15:11:20 -04001760 ret = 1;
1761done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001762 /*
1763 * we don't really know what they plan on doing with the path
1764 * from here on, so for now just mark it as blocking
1765 */
Chris Masonb9473432009-03-13 11:00:37 -04001766 if (!p->leave_spinning)
1767 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001768 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001769 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001770 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001771}
1772
Chris Mason74123bd2007-02-02 11:05:29 -05001773/*
1774 * adjust the pointers going up the tree, starting at level
1775 * making sure the right key of each node is points to 'key'.
1776 * This is used after shifting pointers to the left, so it stops
1777 * fixing up pointers when a given leaf/node is not in slot 0 of the
1778 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001779 *
1780 * If this fails to write a tree block, it returns -1, but continues
1781 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001782 */
Chris Mason5f39d392007-10-15 16:14:19 -04001783static int fixup_low_keys(struct btrfs_trans_handle *trans,
1784 struct btrfs_root *root, struct btrfs_path *path,
1785 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001786{
1787 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001788 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001789 struct extent_buffer *t;
1790
Chris Mason234b63a2007-03-13 10:46:10 -04001791 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001792 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001793 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001794 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001795 t = path->nodes[i];
1796 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001797 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001798 if (tslot != 0)
1799 break;
1800 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001801 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001802}
1803
Chris Mason74123bd2007-02-02 11:05:29 -05001804/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001805 * update item key.
1806 *
1807 * This function isn't completely safe. It's the caller's responsibility
1808 * that the new key won't break the order
1809 */
1810int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1811 struct btrfs_root *root, struct btrfs_path *path,
1812 struct btrfs_key *new_key)
1813{
1814 struct btrfs_disk_key disk_key;
1815 struct extent_buffer *eb;
1816 int slot;
1817
1818 eb = path->nodes[0];
1819 slot = path->slots[0];
1820 if (slot > 0) {
1821 btrfs_item_key(eb, &disk_key, slot - 1);
1822 if (comp_keys(&disk_key, new_key) >= 0)
1823 return -1;
1824 }
1825 if (slot < btrfs_header_nritems(eb) - 1) {
1826 btrfs_item_key(eb, &disk_key, slot + 1);
1827 if (comp_keys(&disk_key, new_key) <= 0)
1828 return -1;
1829 }
1830
1831 btrfs_cpu_key_to_disk(&disk_key, new_key);
1832 btrfs_set_item_key(eb, &disk_key, slot);
1833 btrfs_mark_buffer_dirty(eb);
1834 if (slot == 0)
1835 fixup_low_keys(trans, root, path, &disk_key, 1);
1836 return 0;
1837}
1838
1839/*
Chris Mason74123bd2007-02-02 11:05:29 -05001840 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001841 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001842 *
1843 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1844 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001845 */
Chris Mason98ed5172008-01-03 10:01:48 -05001846static int push_node_left(struct btrfs_trans_handle *trans,
1847 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001848 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001849{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001850 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001851 int src_nritems;
1852 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001853 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001854
Chris Mason5f39d392007-10-15 16:14:19 -04001855 src_nritems = btrfs_header_nritems(src);
1856 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001857 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001858 WARN_ON(btrfs_header_generation(src) != trans->transid);
1859 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001860
Chris Masonbce4eae2008-04-24 14:42:46 -04001861 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001862 return 1;
1863
Chris Masond3977122009-01-05 21:25:51 -05001864 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001865 return 1;
1866
Chris Masonbce4eae2008-04-24 14:42:46 -04001867 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001868 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001869 if (push_items < src_nritems) {
1870 /* leave at least 8 pointers in the node if
1871 * we aren't going to empty it
1872 */
1873 if (src_nritems - push_items < 8) {
1874 if (push_items <= 8)
1875 return 1;
1876 push_items -= 8;
1877 }
1878 }
1879 } else
1880 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001881
Chris Mason5f39d392007-10-15 16:14:19 -04001882 copy_extent_buffer(dst, src,
1883 btrfs_node_key_ptr_offset(dst_nritems),
1884 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001885 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001886
Chris Masonbb803952007-03-01 12:04:21 -05001887 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001888 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1889 btrfs_node_key_ptr_offset(push_items),
1890 (src_nritems - push_items) *
1891 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001892 }
Chris Mason5f39d392007-10-15 16:14:19 -04001893 btrfs_set_header_nritems(src, src_nritems - push_items);
1894 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1895 btrfs_mark_buffer_dirty(src);
1896 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001897
Chris Masonbb803952007-03-01 12:04:21 -05001898 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001899}
1900
Chris Mason97571fd2007-02-24 13:39:08 -05001901/*
Chris Mason79f95c82007-03-01 15:16:26 -05001902 * try to push data from one node into the next node right in the
1903 * tree.
1904 *
1905 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1906 * error, and > 0 if there was no room in the right hand block.
1907 *
1908 * this will only push up to 1/2 the contents of the left node over
1909 */
Chris Mason5f39d392007-10-15 16:14:19 -04001910static int balance_node_right(struct btrfs_trans_handle *trans,
1911 struct btrfs_root *root,
1912 struct extent_buffer *dst,
1913 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001914{
Chris Mason79f95c82007-03-01 15:16:26 -05001915 int push_items = 0;
1916 int max_push;
1917 int src_nritems;
1918 int dst_nritems;
1919 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001920
Chris Mason7bb86312007-12-11 09:25:06 -05001921 WARN_ON(btrfs_header_generation(src) != trans->transid);
1922 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1923
Chris Mason5f39d392007-10-15 16:14:19 -04001924 src_nritems = btrfs_header_nritems(src);
1925 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001926 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001927 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001928 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001929
Chris Masond3977122009-01-05 21:25:51 -05001930 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001931 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001932
1933 max_push = src_nritems / 2 + 1;
1934 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001935 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001936 return 1;
Yan252c38f2007-08-29 09:11:44 -04001937
Chris Mason79f95c82007-03-01 15:16:26 -05001938 if (max_push < push_items)
1939 push_items = max_push;
1940
Chris Mason5f39d392007-10-15 16:14:19 -04001941 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1942 btrfs_node_key_ptr_offset(0),
1943 (dst_nritems) *
1944 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001945
Chris Mason5f39d392007-10-15 16:14:19 -04001946 copy_extent_buffer(dst, src,
1947 btrfs_node_key_ptr_offset(0),
1948 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001949 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001950
Chris Mason5f39d392007-10-15 16:14:19 -04001951 btrfs_set_header_nritems(src, src_nritems - push_items);
1952 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001953
Chris Mason5f39d392007-10-15 16:14:19 -04001954 btrfs_mark_buffer_dirty(src);
1955 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001956
Chris Mason79f95c82007-03-01 15:16:26 -05001957 return ret;
1958}
1959
1960/*
Chris Mason97571fd2007-02-24 13:39:08 -05001961 * helper function to insert a new root level in the tree.
1962 * A new node is allocated, and a single item is inserted to
1963 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001964 *
1965 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001966 */
Chris Masond3977122009-01-05 21:25:51 -05001967static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001968 struct btrfs_root *root,
1969 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001970{
Chris Mason7bb86312007-12-11 09:25:06 -05001971 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001972 struct extent_buffer *lower;
1973 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001974 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001975 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001976
1977 BUG_ON(path->nodes[level]);
1978 BUG_ON(path->nodes[level-1] != root->node);
1979
Chris Mason7bb86312007-12-11 09:25:06 -05001980 lower = path->nodes[level-1];
1981 if (level == 1)
1982 btrfs_item_key(lower, &lower_key, 0);
1983 else
1984 btrfs_node_key(lower, &lower_key, 0);
1985
Zheng Yan31840ae2008-09-23 13:14:14 -04001986 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001987 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001988 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001989 if (IS_ERR(c))
1990 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001991
Yan, Zhengf0486c62010-05-16 10:46:25 -04001992 root_add_used(root, root->nodesize);
1993
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001994 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04001995 btrfs_set_header_nritems(c, 1);
1996 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001997 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001998 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001999 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002000 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002001
Chris Mason5f39d392007-10-15 16:14:19 -04002002 write_extent_buffer(c, root->fs_info->fsid,
2003 (unsigned long)btrfs_header_fsid(c),
2004 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002005
2006 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2007 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2008 BTRFS_UUID_SIZE);
2009
Chris Mason5f39d392007-10-15 16:14:19 -04002010 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002011 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002012 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002013 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002014
2015 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002016
2017 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002018
Chris Mason925baed2008-06-25 16:01:30 -04002019 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002020 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002021
2022 /* the super has an extra ref to root->node */
2023 free_extent_buffer(old);
2024
Chris Mason0b86a832008-03-24 15:01:56 -04002025 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002026 extent_buffer_get(c);
2027 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002028 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002029 path->slots[level] = 0;
2030 return 0;
2031}
2032
Chris Mason74123bd2007-02-02 11:05:29 -05002033/*
2034 * worker function to insert a single pointer in a node.
2035 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002036 *
Chris Mason74123bd2007-02-02 11:05:29 -05002037 * slot and level indicate where you want the key to go, and
2038 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002039 *
2040 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002041 */
Chris Masone089f052007-03-16 16:20:31 -04002042static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2043 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002044 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002045{
Chris Mason5f39d392007-10-15 16:14:19 -04002046 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002047 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002048
2049 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002050 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 lower = path->nodes[level];
2052 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002053 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002054 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002055 BUG();
2056 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002057 memmove_extent_buffer(lower,
2058 btrfs_node_key_ptr_offset(slot + 1),
2059 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002060 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002061 }
Chris Mason5f39d392007-10-15 16:14:19 -04002062 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002063 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002064 WARN_ON(trans->transid == 0);
2065 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_set_header_nritems(lower, nritems + 1);
2067 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002068 return 0;
2069}
2070
Chris Mason97571fd2007-02-24 13:39:08 -05002071/*
2072 * split the node at the specified level in path in two.
2073 * The path is corrected to point to the appropriate node after the split
2074 *
2075 * Before splitting this tries to make some room in the node by pushing
2076 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002077 *
2078 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002079 */
Chris Masone02119d2008-09-05 16:13:11 -04002080static noinline int split_node(struct btrfs_trans_handle *trans,
2081 struct btrfs_root *root,
2082 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002083{
Chris Mason5f39d392007-10-15 16:14:19 -04002084 struct extent_buffer *c;
2085 struct extent_buffer *split;
2086 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002087 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002088 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002089 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002090 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002091
Chris Mason5f39d392007-10-15 16:14:19 -04002092 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002093 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002094 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002095 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002096 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002097 if (ret)
2098 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002099 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002100 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002101 c = path->nodes[level];
2102 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002103 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002104 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002105 if (ret < 0)
2106 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002107 }
Chris Masone66f7092007-04-20 13:16:02 -04002108
Chris Mason5f39d392007-10-15 16:14:19 -04002109 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002110 mid = (c_nritems + 1) / 2;
2111 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002112
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002114 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002115 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002116 if (IS_ERR(split))
2117 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002118
Yan, Zhengf0486c62010-05-16 10:46:25 -04002119 root_add_used(root, root->nodesize);
2120
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002121 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002122 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002123 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002124 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002125 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002126 btrfs_set_header_owner(split, root->root_key.objectid);
2127 write_extent_buffer(split, root->fs_info->fsid,
2128 (unsigned long)btrfs_header_fsid(split),
2129 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002130 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2131 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2132 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002133
Chris Mason5f39d392007-10-15 16:14:19 -04002134
2135 copy_extent_buffer(split, c,
2136 btrfs_node_key_ptr_offset(0),
2137 btrfs_node_key_ptr_offset(mid),
2138 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2139 btrfs_set_header_nritems(split, c_nritems - mid);
2140 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002141 ret = 0;
2142
Chris Mason5f39d392007-10-15 16:14:19 -04002143 btrfs_mark_buffer_dirty(c);
2144 btrfs_mark_buffer_dirty(split);
2145
Chris Masondb945352007-10-15 16:15:53 -04002146 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002147 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002148 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002149 if (wret)
2150 ret = wret;
2151
Chris Mason5de08d72007-02-24 06:24:44 -05002152 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002153 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002154 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002155 free_extent_buffer(c);
2156 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002157 path->slots[level + 1] += 1;
2158 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002159 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002160 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002161 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002162 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002163}
2164
Chris Mason74123bd2007-02-02 11:05:29 -05002165/*
2166 * how many bytes are required to store the items in a leaf. start
2167 * and nr indicate which items in the leaf to check. This totals up the
2168 * space used both by the item structs and the item data
2169 */
Chris Mason5f39d392007-10-15 16:14:19 -04002170static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002171{
2172 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002173 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002174 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002175
2176 if (!nr)
2177 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002178 data_len = btrfs_item_end_nr(l, start);
2179 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002180 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002181 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002182 return data_len;
2183}
2184
Chris Mason74123bd2007-02-02 11:05:29 -05002185/*
Chris Masond4dbff92007-04-04 14:08:15 -04002186 * The space between the end of the leaf items and
2187 * the start of the leaf data. IOW, how much room
2188 * the leaf has left for both items and data
2189 */
Chris Masond3977122009-01-05 21:25:51 -05002190noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002191 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002192{
Chris Mason5f39d392007-10-15 16:14:19 -04002193 int nritems = btrfs_header_nritems(leaf);
2194 int ret;
2195 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2196 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002197 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2198 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002199 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002200 leaf_space_used(leaf, 0, nritems), nritems);
2201 }
2202 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002203}
2204
Chris Mason99d8f832010-07-07 10:51:48 -04002205/*
2206 * min slot controls the lowest index we're willing to push to the
2207 * right. We'll push up to and including min_slot, but no lower
2208 */
Chris Mason44871b12009-03-13 10:04:31 -04002209static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2210 struct btrfs_root *root,
2211 struct btrfs_path *path,
2212 int data_size, int empty,
2213 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002214 int free_space, u32 left_nritems,
2215 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002216{
Chris Mason5f39d392007-10-15 16:14:19 -04002217 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002218 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002219 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002220 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002221 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002222 int push_space = 0;
2223 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002224 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002225 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002226 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002227 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002228 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002229
Chris Mason34a38212007-11-07 13:31:03 -05002230 if (empty)
2231 nr = 0;
2232 else
Chris Mason99d8f832010-07-07 10:51:48 -04002233 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002234
Zheng Yan31840ae2008-09-23 13:14:14 -04002235 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002236 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002237
Chris Mason44871b12009-03-13 10:04:31 -04002238 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002239 i = left_nritems - 1;
2240 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002241 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002242
Zheng Yan31840ae2008-09-23 13:14:14 -04002243 if (!empty && push_items > 0) {
2244 if (path->slots[0] > i)
2245 break;
2246 if (path->slots[0] == i) {
2247 int space = btrfs_leaf_free_space(root, left);
2248 if (space + push_space * 2 > free_space)
2249 break;
2250 }
2251 }
2252
Chris Mason00ec4c52007-02-24 12:47:20 -05002253 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002254 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002255
2256 if (!left->map_token) {
2257 map_extent_buffer(left, (unsigned long)item,
2258 sizeof(struct btrfs_item),
2259 &left->map_token, &left->kaddr,
2260 &left->map_start, &left->map_len,
2261 KM_USER1);
2262 }
2263
2264 this_item_size = btrfs_item_size(left, item);
2265 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002266 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002267
Chris Mason00ec4c52007-02-24 12:47:20 -05002268 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002269 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002270 if (i == 0)
2271 break;
2272 i--;
Chris Masondb945352007-10-15 16:15:53 -04002273 }
2274 if (left->map_token) {
2275 unmap_extent_buffer(left, left->map_token, KM_USER1);
2276 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002277 }
Chris Mason5f39d392007-10-15 16:14:19 -04002278
Chris Mason925baed2008-06-25 16:01:30 -04002279 if (push_items == 0)
2280 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002281
Chris Mason34a38212007-11-07 13:31:03 -05002282 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002283 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002284
Chris Mason00ec4c52007-02-24 12:47:20 -05002285 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002286 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002287
Chris Mason5f39d392007-10-15 16:14:19 -04002288 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002289 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002290
Chris Mason00ec4c52007-02-24 12:47:20 -05002291 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002292 data_end = leaf_data_end(root, right);
2293 memmove_extent_buffer(right,
2294 btrfs_leaf_data(right) + data_end - push_space,
2295 btrfs_leaf_data(right) + data_end,
2296 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2297
Chris Mason00ec4c52007-02-24 12:47:20 -05002298 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002299 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002300 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2301 btrfs_leaf_data(left) + leaf_data_end(root, left),
2302 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002303
2304 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2305 btrfs_item_nr_offset(0),
2306 right_nritems * sizeof(struct btrfs_item));
2307
Chris Mason00ec4c52007-02-24 12:47:20 -05002308 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002309 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2310 btrfs_item_nr_offset(left_nritems - push_items),
2311 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002312
2313 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002314 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002315 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002316 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002317 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002318 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002319 if (!right->map_token) {
2320 map_extent_buffer(right, (unsigned long)item,
2321 sizeof(struct btrfs_item),
2322 &right->map_token, &right->kaddr,
2323 &right->map_start, &right->map_len,
2324 KM_USER1);
2325 }
2326 push_space -= btrfs_item_size(right, item);
2327 btrfs_set_item_offset(right, item, push_space);
2328 }
2329
2330 if (right->map_token) {
2331 unmap_extent_buffer(right, right->map_token, KM_USER1);
2332 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002333 }
Chris Mason7518a232007-03-12 12:01:18 -04002334 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002335 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002336
Chris Mason34a38212007-11-07 13:31:03 -05002337 if (left_nritems)
2338 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002339 else
2340 clean_tree_block(trans, root, left);
2341
Chris Mason5f39d392007-10-15 16:14:19 -04002342 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002343
Chris Mason5f39d392007-10-15 16:14:19 -04002344 btrfs_item_key(right, &disk_key, 0);
2345 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002346 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002347
Chris Mason00ec4c52007-02-24 12:47:20 -05002348 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002349 if (path->slots[0] >= left_nritems) {
2350 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002351 if (btrfs_header_nritems(path->nodes[0]) == 0)
2352 clean_tree_block(trans, root, path->nodes[0]);
2353 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002354 free_extent_buffer(path->nodes[0]);
2355 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002356 path->slots[1] += 1;
2357 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002358 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002359 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002360 }
2361 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002362
2363out_unlock:
2364 btrfs_tree_unlock(right);
2365 free_extent_buffer(right);
2366 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002367}
Chris Mason925baed2008-06-25 16:01:30 -04002368
Chris Mason00ec4c52007-02-24 12:47:20 -05002369/*
Chris Mason44871b12009-03-13 10:04:31 -04002370 * push some data in the path leaf to the right, trying to free up at
2371 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2372 *
2373 * returns 1 if the push failed because the other node didn't have enough
2374 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002375 *
2376 * this will push starting from min_slot to the end of the leaf. It won't
2377 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002378 */
2379static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002380 *root, struct btrfs_path *path,
2381 int min_data_size, int data_size,
2382 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002383{
2384 struct extent_buffer *left = path->nodes[0];
2385 struct extent_buffer *right;
2386 struct extent_buffer *upper;
2387 int slot;
2388 int free_space;
2389 u32 left_nritems;
2390 int ret;
2391
2392 if (!path->nodes[1])
2393 return 1;
2394
2395 slot = path->slots[1];
2396 upper = path->nodes[1];
2397 if (slot >= btrfs_header_nritems(upper) - 1)
2398 return 1;
2399
2400 btrfs_assert_tree_locked(path->nodes[1]);
2401
2402 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002403 if (right == NULL)
2404 return 1;
2405
Chris Mason44871b12009-03-13 10:04:31 -04002406 btrfs_tree_lock(right);
2407 btrfs_set_lock_blocking(right);
2408
2409 free_space = btrfs_leaf_free_space(root, right);
2410 if (free_space < data_size)
2411 goto out_unlock;
2412
2413 /* cow and double check */
2414 ret = btrfs_cow_block(trans, root, right, upper,
2415 slot + 1, &right);
2416 if (ret)
2417 goto out_unlock;
2418
2419 free_space = btrfs_leaf_free_space(root, right);
2420 if (free_space < data_size)
2421 goto out_unlock;
2422
2423 left_nritems = btrfs_header_nritems(left);
2424 if (left_nritems == 0)
2425 goto out_unlock;
2426
Chris Mason99d8f832010-07-07 10:51:48 -04002427 return __push_leaf_right(trans, root, path, min_data_size, empty,
2428 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002429out_unlock:
2430 btrfs_tree_unlock(right);
2431 free_extent_buffer(right);
2432 return 1;
2433}
2434
2435/*
Chris Mason74123bd2007-02-02 11:05:29 -05002436 * push some data in the path leaf to the left, trying to free up at
2437 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002438 *
2439 * max_slot can put a limit on how far into the leaf we'll push items. The
2440 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2441 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002442 */
Chris Mason44871b12009-03-13 10:04:31 -04002443static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2444 struct btrfs_root *root,
2445 struct btrfs_path *path, int data_size,
2446 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002447 int free_space, u32 right_nritems,
2448 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002449{
Chris Mason5f39d392007-10-15 16:14:19 -04002450 struct btrfs_disk_key disk_key;
2451 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002452 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002453 int push_space = 0;
2454 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002455 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002456 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002457 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002458 int ret = 0;
2459 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002460 u32 this_item_size;
2461 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002462
Chris Mason34a38212007-11-07 13:31:03 -05002463 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002464 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002465 else
Chris Mason99d8f832010-07-07 10:51:48 -04002466 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002467
2468 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002469 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002470 if (!right->map_token) {
2471 map_extent_buffer(right, (unsigned long)item,
2472 sizeof(struct btrfs_item),
2473 &right->map_token, &right->kaddr,
2474 &right->map_start, &right->map_len,
2475 KM_USER1);
2476 }
2477
Zheng Yan31840ae2008-09-23 13:14:14 -04002478 if (!empty && push_items > 0) {
2479 if (path->slots[0] < i)
2480 break;
2481 if (path->slots[0] == i) {
2482 int space = btrfs_leaf_free_space(root, right);
2483 if (space + push_space * 2 > free_space)
2484 break;
2485 }
2486 }
2487
Chris Masonbe0e5c02007-01-26 15:51:26 -05002488 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002489 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002490
2491 this_item_size = btrfs_item_size(right, item);
2492 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002493 break;
Chris Masondb945352007-10-15 16:15:53 -04002494
Chris Masonbe0e5c02007-01-26 15:51:26 -05002495 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002496 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002497 }
Chris Masondb945352007-10-15 16:15:53 -04002498
2499 if (right->map_token) {
2500 unmap_extent_buffer(right, right->map_token, KM_USER1);
2501 right->map_token = NULL;
2502 }
2503
Chris Masonbe0e5c02007-01-26 15:51:26 -05002504 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002505 ret = 1;
2506 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002507 }
Chris Mason34a38212007-11-07 13:31:03 -05002508 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002509 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002510
Chris Masonbe0e5c02007-01-26 15:51:26 -05002511 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002512 copy_extent_buffer(left, right,
2513 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2514 btrfs_item_nr_offset(0),
2515 push_items * sizeof(struct btrfs_item));
2516
Chris Mason123abc82007-03-14 14:14:43 -04002517 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002518 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002519
2520 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002521 leaf_data_end(root, left) - push_space,
2522 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002523 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002524 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002525 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002526 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002527
Chris Masondb945352007-10-15 16:15:53 -04002528 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002529 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002530 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002531
Chris Mason5f39d392007-10-15 16:14:19 -04002532 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002533 if (!left->map_token) {
2534 map_extent_buffer(left, (unsigned long)item,
2535 sizeof(struct btrfs_item),
2536 &left->map_token, &left->kaddr,
2537 &left->map_start, &left->map_len,
2538 KM_USER1);
2539 }
2540
Chris Mason5f39d392007-10-15 16:14:19 -04002541 ioff = btrfs_item_offset(left, item);
2542 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002543 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002544 }
Chris Mason5f39d392007-10-15 16:14:19 -04002545 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002546 if (left->map_token) {
2547 unmap_extent_buffer(left, left->map_token, KM_USER1);
2548 left->map_token = NULL;
2549 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002550
2551 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002552 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002553 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2554 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002555 WARN_ON(1);
2556 }
Chris Mason5f39d392007-10-15 16:14:19 -04002557
Chris Mason34a38212007-11-07 13:31:03 -05002558 if (push_items < right_nritems) {
2559 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2560 leaf_data_end(root, right);
2561 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2562 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2563 btrfs_leaf_data(right) +
2564 leaf_data_end(root, right), push_space);
2565
2566 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002567 btrfs_item_nr_offset(push_items),
2568 (btrfs_header_nritems(right) - push_items) *
2569 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002570 }
Yaneef1c492007-11-26 10:58:13 -05002571 right_nritems -= push_items;
2572 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002573 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002574 for (i = 0; i < right_nritems; i++) {
2575 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002576
2577 if (!right->map_token) {
2578 map_extent_buffer(right, (unsigned long)item,
2579 sizeof(struct btrfs_item),
2580 &right->map_token, &right->kaddr,
2581 &right->map_start, &right->map_len,
2582 KM_USER1);
2583 }
2584
2585 push_space = push_space - btrfs_item_size(right, item);
2586 btrfs_set_item_offset(right, item, push_space);
2587 }
2588 if (right->map_token) {
2589 unmap_extent_buffer(right, right->map_token, KM_USER1);
2590 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002591 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002592
Chris Mason5f39d392007-10-15 16:14:19 -04002593 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002594 if (right_nritems)
2595 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002596 else
2597 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002598
Chris Mason5f39d392007-10-15 16:14:19 -04002599 btrfs_item_key(right, &disk_key, 0);
2600 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002601 if (wret)
2602 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002603
2604 /* then fixup the leaf pointer in the path */
2605 if (path->slots[0] < push_items) {
2606 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002607 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002608 free_extent_buffer(path->nodes[0]);
2609 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610 path->slots[1] -= 1;
2611 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002612 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002613 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002614 path->slots[0] -= push_items;
2615 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002616 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002617 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002618out:
2619 btrfs_tree_unlock(left);
2620 free_extent_buffer(left);
2621 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002622}
2623
Chris Mason74123bd2007-02-02 11:05:29 -05002624/*
Chris Mason44871b12009-03-13 10:04:31 -04002625 * push some data in the path leaf to the left, trying to free up at
2626 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002627 *
2628 * max_slot can put a limit on how far into the leaf we'll push items. The
2629 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2630 * items
Chris Mason44871b12009-03-13 10:04:31 -04002631 */
2632static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002633 *root, struct btrfs_path *path, int min_data_size,
2634 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002635{
2636 struct extent_buffer *right = path->nodes[0];
2637 struct extent_buffer *left;
2638 int slot;
2639 int free_space;
2640 u32 right_nritems;
2641 int ret = 0;
2642
2643 slot = path->slots[1];
2644 if (slot == 0)
2645 return 1;
2646 if (!path->nodes[1])
2647 return 1;
2648
2649 right_nritems = btrfs_header_nritems(right);
2650 if (right_nritems == 0)
2651 return 1;
2652
2653 btrfs_assert_tree_locked(path->nodes[1]);
2654
2655 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002656 if (left == NULL)
2657 return 1;
2658
Chris Mason44871b12009-03-13 10:04:31 -04002659 btrfs_tree_lock(left);
2660 btrfs_set_lock_blocking(left);
2661
2662 free_space = btrfs_leaf_free_space(root, left);
2663 if (free_space < data_size) {
2664 ret = 1;
2665 goto out;
2666 }
2667
2668 /* cow and double check */
2669 ret = btrfs_cow_block(trans, root, left,
2670 path->nodes[1], slot - 1, &left);
2671 if (ret) {
2672 /* we hit -ENOSPC, but it isn't fatal here */
2673 ret = 1;
2674 goto out;
2675 }
2676
2677 free_space = btrfs_leaf_free_space(root, left);
2678 if (free_space < data_size) {
2679 ret = 1;
2680 goto out;
2681 }
2682
Chris Mason99d8f832010-07-07 10:51:48 -04002683 return __push_leaf_left(trans, root, path, min_data_size,
2684 empty, left, free_space, right_nritems,
2685 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002686out:
2687 btrfs_tree_unlock(left);
2688 free_extent_buffer(left);
2689 return ret;
2690}
2691
2692/*
Chris Mason74123bd2007-02-02 11:05:29 -05002693 * split the path's leaf in two, making sure there is at least data_size
2694 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002695 *
2696 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002697 */
Chris Mason44871b12009-03-13 10:04:31 -04002698static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002699 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002700 struct btrfs_path *path,
2701 struct extent_buffer *l,
2702 struct extent_buffer *right,
2703 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002705 int data_copy_size;
2706 int rt_data_off;
2707 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002708 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002709 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002710 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002711
Chris Mason5f39d392007-10-15 16:14:19 -04002712 nritems = nritems - mid;
2713 btrfs_set_header_nritems(right, nritems);
2714 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2715
2716 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2717 btrfs_item_nr_offset(mid),
2718 nritems * sizeof(struct btrfs_item));
2719
2720 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002721 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2722 data_copy_size, btrfs_leaf_data(l) +
2723 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002724
Chris Mason5f39d392007-10-15 16:14:19 -04002725 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2726 btrfs_item_end_nr(l, mid);
2727
2728 for (i = 0; i < nritems; i++) {
2729 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002730 u32 ioff;
2731
2732 if (!right->map_token) {
2733 map_extent_buffer(right, (unsigned long)item,
2734 sizeof(struct btrfs_item),
2735 &right->map_token, &right->kaddr,
2736 &right->map_start, &right->map_len,
2737 KM_USER1);
2738 }
2739
2740 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002741 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002742 }
Chris Mason74123bd2007-02-02 11:05:29 -05002743
Chris Masondb945352007-10-15 16:15:53 -04002744 if (right->map_token) {
2745 unmap_extent_buffer(right, right->map_token, KM_USER1);
2746 right->map_token = NULL;
2747 }
2748
Chris Mason5f39d392007-10-15 16:14:19 -04002749 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002750 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002751 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002752 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2753 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002754 if (wret)
2755 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002756
2757 btrfs_mark_buffer_dirty(right);
2758 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002759 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002760
Chris Masonbe0e5c02007-01-26 15:51:26 -05002761 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002762 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002763 free_extent_buffer(path->nodes[0]);
2764 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002765 path->slots[0] -= mid;
2766 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002767 } else {
2768 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002769 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002770 }
Chris Mason5f39d392007-10-15 16:14:19 -04002771
Chris Masoneb60cea2007-02-02 09:18:22 -05002772 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002773
Chris Mason44871b12009-03-13 10:04:31 -04002774 return ret;
2775}
2776
2777/*
Chris Mason99d8f832010-07-07 10:51:48 -04002778 * double splits happen when we need to insert a big item in the middle
2779 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2780 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2781 * A B C
2782 *
2783 * We avoid this by trying to push the items on either side of our target
2784 * into the adjacent leaves. If all goes well we can avoid the double split
2785 * completely.
2786 */
2787static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2788 struct btrfs_root *root,
2789 struct btrfs_path *path,
2790 int data_size)
2791{
2792 int ret;
2793 int progress = 0;
2794 int slot;
2795 u32 nritems;
2796
2797 slot = path->slots[0];
2798
2799 /*
2800 * try to push all the items after our slot into the
2801 * right leaf
2802 */
2803 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2804 if (ret < 0)
2805 return ret;
2806
2807 if (ret == 0)
2808 progress++;
2809
2810 nritems = btrfs_header_nritems(path->nodes[0]);
2811 /*
2812 * our goal is to get our slot at the start or end of a leaf. If
2813 * we've done so we're done
2814 */
2815 if (path->slots[0] == 0 || path->slots[0] == nritems)
2816 return 0;
2817
2818 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2819 return 0;
2820
2821 /* try to push all the items before our slot into the next leaf */
2822 slot = path->slots[0];
2823 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2824 if (ret < 0)
2825 return ret;
2826
2827 if (ret == 0)
2828 progress++;
2829
2830 if (progress)
2831 return 0;
2832 return 1;
2833}
2834
2835/*
Chris Mason44871b12009-03-13 10:04:31 -04002836 * split the path's leaf in two, making sure there is at least data_size
2837 * available for the resulting leaf level of the path.
2838 *
2839 * returns 0 if all went well and < 0 on failure.
2840 */
2841static noinline int split_leaf(struct btrfs_trans_handle *trans,
2842 struct btrfs_root *root,
2843 struct btrfs_key *ins_key,
2844 struct btrfs_path *path, int data_size,
2845 int extend)
2846{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002847 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002848 struct extent_buffer *l;
2849 u32 nritems;
2850 int mid;
2851 int slot;
2852 struct extent_buffer *right;
2853 int ret = 0;
2854 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002855 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002856 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002857 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002858
Yan, Zhenga5719522009-09-24 09:17:31 -04002859 l = path->nodes[0];
2860 slot = path->slots[0];
2861 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2862 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2863 return -EOVERFLOW;
2864
Chris Mason44871b12009-03-13 10:04:31 -04002865 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002866 if (data_size) {
2867 wret = push_leaf_right(trans, root, path, data_size,
2868 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002869 if (wret < 0)
2870 return wret;
2871 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002872 wret = push_leaf_left(trans, root, path, data_size,
2873 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002874 if (wret < 0)
2875 return wret;
2876 }
2877 l = path->nodes[0];
2878
2879 /* did the pushes work? */
2880 if (btrfs_leaf_free_space(root, l) >= data_size)
2881 return 0;
2882 }
2883
2884 if (!path->nodes[1]) {
2885 ret = insert_new_root(trans, root, path, 1);
2886 if (ret)
2887 return ret;
2888 }
2889again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002890 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002891 l = path->nodes[0];
2892 slot = path->slots[0];
2893 nritems = btrfs_header_nritems(l);
2894 mid = (nritems + 1) / 2;
2895
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002896 if (mid <= slot) {
2897 if (nritems == 1 ||
2898 leaf_space_used(l, mid, nritems - mid) + data_size >
2899 BTRFS_LEAF_DATA_SIZE(root)) {
2900 if (slot >= nritems) {
2901 split = 0;
2902 } else {
2903 mid = slot;
2904 if (mid != nritems &&
2905 leaf_space_used(l, mid, nritems - mid) +
2906 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002907 if (data_size && !tried_avoid_double)
2908 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002909 split = 2;
2910 }
2911 }
2912 }
2913 } else {
2914 if (leaf_space_used(l, 0, mid) + data_size >
2915 BTRFS_LEAF_DATA_SIZE(root)) {
2916 if (!extend && data_size && slot == 0) {
2917 split = 0;
2918 } else if ((extend || !data_size) && slot == 0) {
2919 mid = 1;
2920 } else {
2921 mid = slot;
2922 if (mid != nritems &&
2923 leaf_space_used(l, mid, nritems - mid) +
2924 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002925 if (data_size && !tried_avoid_double)
2926 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002927 split = 2 ;
2928 }
2929 }
2930 }
2931 }
2932
2933 if (split == 0)
2934 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2935 else
2936 btrfs_item_key(l, &disk_key, mid);
2937
2938 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002939 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002940 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002941 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002942 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002943
2944 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002945
2946 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2947 btrfs_set_header_bytenr(right, right->start);
2948 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002949 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002950 btrfs_set_header_owner(right, root->root_key.objectid);
2951 btrfs_set_header_level(right, 0);
2952 write_extent_buffer(right, root->fs_info->fsid,
2953 (unsigned long)btrfs_header_fsid(right),
2954 BTRFS_FSID_SIZE);
2955
2956 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2957 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2958 BTRFS_UUID_SIZE);
2959
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002960 if (split == 0) {
2961 if (mid <= slot) {
2962 btrfs_set_header_nritems(right, 0);
2963 wret = insert_ptr(trans, root, path,
2964 &disk_key, right->start,
2965 path->slots[1] + 1, 1);
2966 if (wret)
2967 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002968
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002969 btrfs_tree_unlock(path->nodes[0]);
2970 free_extent_buffer(path->nodes[0]);
2971 path->nodes[0] = right;
2972 path->slots[0] = 0;
2973 path->slots[1] += 1;
2974 } else {
2975 btrfs_set_header_nritems(right, 0);
2976 wret = insert_ptr(trans, root, path,
2977 &disk_key,
2978 right->start,
2979 path->slots[1], 1);
2980 if (wret)
2981 ret = wret;
2982 btrfs_tree_unlock(path->nodes[0]);
2983 free_extent_buffer(path->nodes[0]);
2984 path->nodes[0] = right;
2985 path->slots[0] = 0;
2986 if (path->slots[1] == 0) {
2987 wret = fixup_low_keys(trans, root,
2988 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002989 if (wret)
2990 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002991 }
2992 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002993 btrfs_mark_buffer_dirty(right);
2994 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002995 }
2996
2997 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2998 BUG_ON(ret);
2999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003001 BUG_ON(num_doubles != 0);
3002 num_doubles++;
3003 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003004 }
Chris Mason44871b12009-03-13 10:04:31 -04003005
Chris Masonbe0e5c02007-01-26 15:51:26 -05003006 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04003007
3008push_for_double:
3009 push_for_double_split(trans, root, path, data_size);
3010 tried_avoid_double = 1;
3011 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3012 return 0;
3013 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003014}
3015
Yan, Zhengad48fd752009-11-12 09:33:58 +00003016static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3017 struct btrfs_root *root,
3018 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003019{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003020 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003021 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003022 struct btrfs_file_extent_item *fi;
3023 u64 extent_len = 0;
3024 u32 item_size;
3025 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003026
3027 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003028 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3029
3030 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3031 key.type != BTRFS_EXTENT_CSUM_KEY);
3032
3033 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3034 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003035
3036 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003037 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3038 fi = btrfs_item_ptr(leaf, path->slots[0],
3039 struct btrfs_file_extent_item);
3040 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3041 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003042 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003043
Chris Mason459931e2008-12-10 09:10:46 -05003044 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003045 path->search_for_split = 1;
3046 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003047 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003048 if (ret < 0)
3049 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003050
Yan, Zhengad48fd752009-11-12 09:33:58 +00003051 ret = -EAGAIN;
3052 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003053 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003054 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3055 goto err;
3056
Chris Mason109f6ae2010-04-02 09:20:18 -04003057 /* the leaf has changed, it now has room. return now */
3058 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3059 goto err;
3060
Yan, Zhengad48fd752009-11-12 09:33:58 +00003061 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3062 fi = btrfs_item_ptr(leaf, path->slots[0],
3063 struct btrfs_file_extent_item);
3064 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3065 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003066 }
3067
Chris Masonb9473432009-03-13 11:00:37 -04003068 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003069 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003070 if (ret)
3071 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003072
Yan, Zhengad48fd752009-11-12 09:33:58 +00003073 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003074 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003075 return 0;
3076err:
3077 path->keep_locks = 0;
3078 return ret;
3079}
3080
3081static noinline int split_item(struct btrfs_trans_handle *trans,
3082 struct btrfs_root *root,
3083 struct btrfs_path *path,
3084 struct btrfs_key *new_key,
3085 unsigned long split_offset)
3086{
3087 struct extent_buffer *leaf;
3088 struct btrfs_item *item;
3089 struct btrfs_item *new_item;
3090 int slot;
3091 char *buf;
3092 u32 nritems;
3093 u32 item_size;
3094 u32 orig_offset;
3095 struct btrfs_disk_key disk_key;
3096
Chris Masonb9473432009-03-13 11:00:37 -04003097 leaf = path->nodes[0];
3098 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3099
Chris Masonb4ce94d2009-02-04 09:25:08 -05003100 btrfs_set_path_blocking(path);
3101
Chris Mason459931e2008-12-10 09:10:46 -05003102 item = btrfs_item_nr(leaf, path->slots[0]);
3103 orig_offset = btrfs_item_offset(leaf, item);
3104 item_size = btrfs_item_size(leaf, item);
3105
Chris Mason459931e2008-12-10 09:10:46 -05003106 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003107 if (!buf)
3108 return -ENOMEM;
3109
Chris Mason459931e2008-12-10 09:10:46 -05003110 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3111 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003112
Chris Mason459931e2008-12-10 09:10:46 -05003113 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003114 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003115 if (slot != nritems) {
3116 /* shift the items */
3117 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003118 btrfs_item_nr_offset(slot),
3119 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003120 }
3121
3122 btrfs_cpu_key_to_disk(&disk_key, new_key);
3123 btrfs_set_item_key(leaf, &disk_key, slot);
3124
3125 new_item = btrfs_item_nr(leaf, slot);
3126
3127 btrfs_set_item_offset(leaf, new_item, orig_offset);
3128 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3129
3130 btrfs_set_item_offset(leaf, item,
3131 orig_offset + item_size - split_offset);
3132 btrfs_set_item_size(leaf, item, split_offset);
3133
3134 btrfs_set_header_nritems(leaf, nritems + 1);
3135
3136 /* write the data for the start of the original item */
3137 write_extent_buffer(leaf, buf,
3138 btrfs_item_ptr_offset(leaf, path->slots[0]),
3139 split_offset);
3140
3141 /* write the data for the new item */
3142 write_extent_buffer(leaf, buf + split_offset,
3143 btrfs_item_ptr_offset(leaf, slot),
3144 item_size - split_offset);
3145 btrfs_mark_buffer_dirty(leaf);
3146
Yan, Zhengad48fd752009-11-12 09:33:58 +00003147 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003148 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003149 return 0;
3150}
3151
3152/*
3153 * This function splits a single item into two items,
3154 * giving 'new_key' to the new item and splitting the
3155 * old one at split_offset (from the start of the item).
3156 *
3157 * The path may be released by this operation. After
3158 * the split, the path is pointing to the old item. The
3159 * new item is going to be in the same node as the old one.
3160 *
3161 * Note, the item being split must be smaller enough to live alone on
3162 * a tree block with room for one extra struct btrfs_item
3163 *
3164 * This allows us to split the item in place, keeping a lock on the
3165 * leaf the entire time.
3166 */
3167int btrfs_split_item(struct btrfs_trans_handle *trans,
3168 struct btrfs_root *root,
3169 struct btrfs_path *path,
3170 struct btrfs_key *new_key,
3171 unsigned long split_offset)
3172{
3173 int ret;
3174 ret = setup_leaf_for_split(trans, root, path,
3175 sizeof(struct btrfs_item));
3176 if (ret)
3177 return ret;
3178
3179 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003180 return ret;
3181}
3182
3183/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003184 * This function duplicate a item, giving 'new_key' to the new item.
3185 * It guarantees both items live in the same tree leaf and the new item
3186 * is contiguous with the original item.
3187 *
3188 * This allows us to split file extent in place, keeping a lock on the
3189 * leaf the entire time.
3190 */
3191int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3192 struct btrfs_root *root,
3193 struct btrfs_path *path,
3194 struct btrfs_key *new_key)
3195{
3196 struct extent_buffer *leaf;
3197 int ret;
3198 u32 item_size;
3199
3200 leaf = path->nodes[0];
3201 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3202 ret = setup_leaf_for_split(trans, root, path,
3203 item_size + sizeof(struct btrfs_item));
3204 if (ret)
3205 return ret;
3206
3207 path->slots[0]++;
3208 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3209 item_size, item_size +
3210 sizeof(struct btrfs_item), 1);
3211 BUG_ON(ret);
3212
3213 leaf = path->nodes[0];
3214 memcpy_extent_buffer(leaf,
3215 btrfs_item_ptr_offset(leaf, path->slots[0]),
3216 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3217 item_size);
3218 return 0;
3219}
3220
3221/*
Chris Masond352ac62008-09-29 15:18:18 -04003222 * make the item pointed to by the path smaller. new_size indicates
3223 * how small to make it, and from_end tells us if we just chop bytes
3224 * off the end of the item or if we shift the item to chop bytes off
3225 * the front.
3226 */
Chris Masonb18c6682007-04-17 13:26:50 -04003227int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3228 struct btrfs_root *root,
3229 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003230 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003231{
Chris Masonb18c6682007-04-17 13:26:50 -04003232 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003233 struct extent_buffer *leaf;
3234 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003235 u32 nritems;
3236 unsigned int data_end;
3237 unsigned int old_data_start;
3238 unsigned int old_size;
3239 unsigned int size_diff;
3240 int i;
3241
Chris Mason5f39d392007-10-15 16:14:19 -04003242 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003243 slot = path->slots[0];
3244
3245 old_size = btrfs_item_size_nr(leaf, slot);
3246 if (old_size == new_size)
3247 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003248
Chris Mason5f39d392007-10-15 16:14:19 -04003249 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003250 data_end = leaf_data_end(root, leaf);
3251
Chris Mason5f39d392007-10-15 16:14:19 -04003252 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003253
Chris Masonb18c6682007-04-17 13:26:50 -04003254 size_diff = old_size - new_size;
3255
3256 BUG_ON(slot < 0);
3257 BUG_ON(slot >= nritems);
3258
3259 /*
3260 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3261 */
3262 /* first correct the data pointers */
3263 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003264 u32 ioff;
3265 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003266
3267 if (!leaf->map_token) {
3268 map_extent_buffer(leaf, (unsigned long)item,
3269 sizeof(struct btrfs_item),
3270 &leaf->map_token, &leaf->kaddr,
3271 &leaf->map_start, &leaf->map_len,
3272 KM_USER1);
3273 }
3274
Chris Mason5f39d392007-10-15 16:14:19 -04003275 ioff = btrfs_item_offset(leaf, item);
3276 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003277 }
Chris Masondb945352007-10-15 16:15:53 -04003278
3279 if (leaf->map_token) {
3280 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3281 leaf->map_token = NULL;
3282 }
3283
Chris Masonb18c6682007-04-17 13:26:50 -04003284 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003285 if (from_end) {
3286 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3287 data_end + size_diff, btrfs_leaf_data(leaf) +
3288 data_end, old_data_start + new_size - data_end);
3289 } else {
3290 struct btrfs_disk_key disk_key;
3291 u64 offset;
3292
3293 btrfs_item_key(leaf, &disk_key, slot);
3294
3295 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3296 unsigned long ptr;
3297 struct btrfs_file_extent_item *fi;
3298
3299 fi = btrfs_item_ptr(leaf, slot,
3300 struct btrfs_file_extent_item);
3301 fi = (struct btrfs_file_extent_item *)(
3302 (unsigned long)fi - size_diff);
3303
3304 if (btrfs_file_extent_type(leaf, fi) ==
3305 BTRFS_FILE_EXTENT_INLINE) {
3306 ptr = btrfs_item_ptr_offset(leaf, slot);
3307 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003308 (unsigned long)fi,
3309 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003310 disk_bytenr));
3311 }
3312 }
3313
3314 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3315 data_end + size_diff, btrfs_leaf_data(leaf) +
3316 data_end, old_data_start - data_end);
3317
3318 offset = btrfs_disk_key_offset(&disk_key);
3319 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3320 btrfs_set_item_key(leaf, &disk_key, slot);
3321 if (slot == 0)
3322 fixup_low_keys(trans, root, path, &disk_key, 1);
3323 }
Chris Mason5f39d392007-10-15 16:14:19 -04003324
3325 item = btrfs_item_nr(leaf, slot);
3326 btrfs_set_item_size(leaf, item, new_size);
3327 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003328
Chris Mason5f39d392007-10-15 16:14:19 -04003329 if (btrfs_leaf_free_space(root, leaf) < 0) {
3330 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003331 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003332 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003333 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003334}
3335
Chris Masond352ac62008-09-29 15:18:18 -04003336/*
3337 * make the item pointed to by the path bigger, data_size is the new size.
3338 */
Chris Mason5f39d392007-10-15 16:14:19 -04003339int btrfs_extend_item(struct btrfs_trans_handle *trans,
3340 struct btrfs_root *root, struct btrfs_path *path,
3341 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003342{
Chris Mason6567e832007-04-16 09:22:45 -04003343 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003344 struct extent_buffer *leaf;
3345 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003346 u32 nritems;
3347 unsigned int data_end;
3348 unsigned int old_data;
3349 unsigned int old_size;
3350 int i;
3351
Chris Mason5f39d392007-10-15 16:14:19 -04003352 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003353
Chris Mason5f39d392007-10-15 16:14:19 -04003354 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003355 data_end = leaf_data_end(root, leaf);
3356
Chris Mason5f39d392007-10-15 16:14:19 -04003357 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3358 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003359 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003360 }
Chris Mason6567e832007-04-16 09:22:45 -04003361 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003362 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003363
3364 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003365 if (slot >= nritems) {
3366 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003367 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3368 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003369 BUG_ON(1);
3370 }
Chris Mason6567e832007-04-16 09:22:45 -04003371
3372 /*
3373 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3374 */
3375 /* first correct the data pointers */
3376 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003377 u32 ioff;
3378 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003379
3380 if (!leaf->map_token) {
3381 map_extent_buffer(leaf, (unsigned long)item,
3382 sizeof(struct btrfs_item),
3383 &leaf->map_token, &leaf->kaddr,
3384 &leaf->map_start, &leaf->map_len,
3385 KM_USER1);
3386 }
Chris Mason5f39d392007-10-15 16:14:19 -04003387 ioff = btrfs_item_offset(leaf, item);
3388 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003389 }
Chris Mason5f39d392007-10-15 16:14:19 -04003390
Chris Masondb945352007-10-15 16:15:53 -04003391 if (leaf->map_token) {
3392 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3393 leaf->map_token = NULL;
3394 }
3395
Chris Mason6567e832007-04-16 09:22:45 -04003396 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003397 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003398 data_end - data_size, btrfs_leaf_data(leaf) +
3399 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003400
Chris Mason6567e832007-04-16 09:22:45 -04003401 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003402 old_size = btrfs_item_size_nr(leaf, slot);
3403 item = btrfs_item_nr(leaf, slot);
3404 btrfs_set_item_size(leaf, item, old_size + data_size);
3405 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003406
Chris Mason5f39d392007-10-15 16:14:19 -04003407 if (btrfs_leaf_free_space(root, leaf) < 0) {
3408 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003409 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003410 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003411 return 0;
Chris Mason6567e832007-04-16 09:22:45 -04003412}
3413
Chris Mason74123bd2007-02-02 11:05:29 -05003414/*
Chris Masond352ac62008-09-29 15:18:18 -04003415 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003416 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003417 * Returns the number of keys that were inserted.
3418 */
3419int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3420 struct btrfs_root *root,
3421 struct btrfs_path *path,
3422 struct btrfs_key *cpu_key, u32 *data_size,
3423 int nr)
3424{
3425 struct extent_buffer *leaf;
3426 struct btrfs_item *item;
3427 int ret = 0;
3428 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003429 int i;
3430 u32 nritems;
3431 u32 total_data = 0;
3432 u32 total_size = 0;
3433 unsigned int data_end;
3434 struct btrfs_disk_key disk_key;
3435 struct btrfs_key found_key;
3436
Yan Zheng87b29b22008-12-17 10:21:48 -05003437 for (i = 0; i < nr; i++) {
3438 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3439 BTRFS_LEAF_DATA_SIZE(root)) {
3440 break;
3441 nr = i;
3442 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003443 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003444 total_size += data_size[i] + sizeof(struct btrfs_item);
3445 }
3446 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003447
Josef Bacikf3465ca2008-11-12 14:19:50 -05003448 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3449 if (ret == 0)
3450 return -EEXIST;
3451 if (ret < 0)
3452 goto out;
3453
Josef Bacikf3465ca2008-11-12 14:19:50 -05003454 leaf = path->nodes[0];
3455
3456 nritems = btrfs_header_nritems(leaf);
3457 data_end = leaf_data_end(root, leaf);
3458
3459 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3460 for (i = nr; i >= 0; i--) {
3461 total_data -= data_size[i];
3462 total_size -= data_size[i] + sizeof(struct btrfs_item);
3463 if (total_size < btrfs_leaf_free_space(root, leaf))
3464 break;
3465 }
3466 nr = i;
3467 }
3468
3469 slot = path->slots[0];
3470 BUG_ON(slot < 0);
3471
3472 if (slot != nritems) {
3473 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3474
3475 item = btrfs_item_nr(leaf, slot);
3476 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3477
3478 /* figure out how many keys we can insert in here */
3479 total_data = data_size[0];
3480 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003481 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003482 break;
3483 total_data += data_size[i];
3484 }
3485 nr = i;
3486
3487 if (old_data < data_end) {
3488 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003489 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003490 slot, old_data, data_end);
3491 BUG_ON(1);
3492 }
3493 /*
3494 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3495 */
3496 /* first correct the data pointers */
3497 WARN_ON(leaf->map_token);
3498 for (i = slot; i < nritems; i++) {
3499 u32 ioff;
3500
3501 item = btrfs_item_nr(leaf, i);
3502 if (!leaf->map_token) {
3503 map_extent_buffer(leaf, (unsigned long)item,
3504 sizeof(struct btrfs_item),
3505 &leaf->map_token, &leaf->kaddr,
3506 &leaf->map_start, &leaf->map_len,
3507 KM_USER1);
3508 }
3509
3510 ioff = btrfs_item_offset(leaf, item);
3511 btrfs_set_item_offset(leaf, item, ioff - total_data);
3512 }
3513 if (leaf->map_token) {
3514 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3515 leaf->map_token = NULL;
3516 }
3517
3518 /* shift the items */
3519 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3520 btrfs_item_nr_offset(slot),
3521 (nritems - slot) * sizeof(struct btrfs_item));
3522
3523 /* shift the data */
3524 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3525 data_end - total_data, btrfs_leaf_data(leaf) +
3526 data_end, old_data - data_end);
3527 data_end = old_data;
3528 } else {
3529 /*
3530 * this sucks but it has to be done, if we are inserting at
3531 * the end of the leaf only insert 1 of the items, since we
3532 * have no way of knowing whats on the next leaf and we'd have
3533 * to drop our current locks to figure it out
3534 */
3535 nr = 1;
3536 }
3537
3538 /* setup the item for the new data */
3539 for (i = 0; i < nr; i++) {
3540 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3541 btrfs_set_item_key(leaf, &disk_key, slot + i);
3542 item = btrfs_item_nr(leaf, slot + i);
3543 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3544 data_end -= data_size[i];
3545 btrfs_set_item_size(leaf, item, data_size[i]);
3546 }
3547 btrfs_set_header_nritems(leaf, nritems + nr);
3548 btrfs_mark_buffer_dirty(leaf);
3549
3550 ret = 0;
3551 if (slot == 0) {
3552 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3553 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3554 }
3555
3556 if (btrfs_leaf_free_space(root, leaf) < 0) {
3557 btrfs_print_leaf(root, leaf);
3558 BUG();
3559 }
3560out:
3561 if (!ret)
3562 ret = nr;
3563 return ret;
3564}
3565
3566/*
Chris Mason44871b12009-03-13 10:04:31 -04003567 * this is a helper for btrfs_insert_empty_items, the main goal here is
3568 * to save stack depth by doing the bulk of the work in a function
3569 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003570 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003571int setup_items_for_insert(struct btrfs_trans_handle *trans,
3572 struct btrfs_root *root, struct btrfs_path *path,
3573 struct btrfs_key *cpu_key, u32 *data_size,
3574 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003575{
Chris Mason5f39d392007-10-15 16:14:19 -04003576 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003577 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003578 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003579 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003580 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003581 int ret;
3582 struct extent_buffer *leaf;
3583 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003584
Chris Mason5f39d392007-10-15 16:14:19 -04003585 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003586 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003587
Chris Mason5f39d392007-10-15 16:14:19 -04003588 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003589 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003590
Chris Masonf25956c2008-09-12 15:32:53 -04003591 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003592 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003593 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003594 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003595 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003596 }
Chris Mason5f39d392007-10-15 16:14:19 -04003597
Chris Masonbe0e5c02007-01-26 15:51:26 -05003598 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003599 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003600
Chris Mason5f39d392007-10-15 16:14:19 -04003601 if (old_data < data_end) {
3602 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003603 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003604 slot, old_data, data_end);
3605 BUG_ON(1);
3606 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003607 /*
3608 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3609 */
3610 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003611 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003612 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003613 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003614
Chris Mason5f39d392007-10-15 16:14:19 -04003615 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003616 if (!leaf->map_token) {
3617 map_extent_buffer(leaf, (unsigned long)item,
3618 sizeof(struct btrfs_item),
3619 &leaf->map_token, &leaf->kaddr,
3620 &leaf->map_start, &leaf->map_len,
3621 KM_USER1);
3622 }
3623
Chris Mason5f39d392007-10-15 16:14:19 -04003624 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003625 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003626 }
Chris Masondb945352007-10-15 16:15:53 -04003627 if (leaf->map_token) {
3628 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3629 leaf->map_token = NULL;
3630 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003631
3632 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003633 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003634 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003635 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003636
3637 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003638 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003639 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003640 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003641 data_end = old_data;
3642 }
Chris Mason5f39d392007-10-15 16:14:19 -04003643
Chris Mason62e27492007-03-15 12:56:47 -04003644 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003645 for (i = 0; i < nr; i++) {
3646 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3647 btrfs_set_item_key(leaf, &disk_key, slot + i);
3648 item = btrfs_item_nr(leaf, slot + i);
3649 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3650 data_end -= data_size[i];
3651 btrfs_set_item_size(leaf, item, data_size[i]);
3652 }
Chris Mason44871b12009-03-13 10:04:31 -04003653
Chris Mason9c583092008-01-29 15:15:18 -05003654 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003655
3656 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003657 if (slot == 0) {
3658 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003659 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003660 }
Chris Masonb9473432009-03-13 11:00:37 -04003661 btrfs_unlock_up_safe(path, 1);
3662 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003663
Chris Mason5f39d392007-10-15 16:14:19 -04003664 if (btrfs_leaf_free_space(root, leaf) < 0) {
3665 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003666 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003667 }
Chris Mason44871b12009-03-13 10:04:31 -04003668 return ret;
3669}
3670
3671/*
3672 * Given a key and some data, insert items into the tree.
3673 * This does all the path init required, making room in the tree if needed.
3674 */
3675int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3676 struct btrfs_root *root,
3677 struct btrfs_path *path,
3678 struct btrfs_key *cpu_key, u32 *data_size,
3679 int nr)
3680{
Chris Mason44871b12009-03-13 10:04:31 -04003681 int ret = 0;
3682 int slot;
3683 int i;
3684 u32 total_size = 0;
3685 u32 total_data = 0;
3686
3687 for (i = 0; i < nr; i++)
3688 total_data += data_size[i];
3689
3690 total_size = total_data + (nr * sizeof(struct btrfs_item));
3691 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3692 if (ret == 0)
3693 return -EEXIST;
3694 if (ret < 0)
3695 goto out;
3696
Chris Mason44871b12009-03-13 10:04:31 -04003697 slot = path->slots[0];
3698 BUG_ON(slot < 0);
3699
3700 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3701 total_data, total_size, nr);
3702
Chris Masoned2ff2c2007-03-01 18:59:40 -05003703out:
Chris Mason62e27492007-03-15 12:56:47 -04003704 return ret;
3705}
3706
3707/*
3708 * Given a key and some data, insert an item into the tree.
3709 * This does all the path init required, making room in the tree if needed.
3710 */
Chris Masone089f052007-03-16 16:20:31 -04003711int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3712 *root, struct btrfs_key *cpu_key, void *data, u32
3713 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003714{
3715 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003716 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003717 struct extent_buffer *leaf;
3718 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003719
Chris Mason2c90e5d2007-04-02 10:50:19 -04003720 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003721 if (!path)
3722 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003723 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003724 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003725 leaf = path->nodes[0];
3726 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3727 write_extent_buffer(leaf, data, ptr, data_size);
3728 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003729 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003730 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003731 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003732}
3733
Chris Mason74123bd2007-02-02 11:05:29 -05003734/*
Chris Mason5de08d72007-02-24 06:24:44 -05003735 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003736 *
Chris Masond352ac62008-09-29 15:18:18 -04003737 * the tree should have been previously balanced so the deletion does not
3738 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003739 */
Chris Masone089f052007-03-16 16:20:31 -04003740static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3741 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003742{
Chris Mason5f39d392007-10-15 16:14:19 -04003743 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003744 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003745 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003746 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003747
Chris Mason5f39d392007-10-15 16:14:19 -04003748 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003749 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003750 memmove_extent_buffer(parent,
3751 btrfs_node_key_ptr_offset(slot),
3752 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003753 sizeof(struct btrfs_key_ptr) *
3754 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003755 }
Chris Mason7518a232007-03-12 12:01:18 -04003756 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003757 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003758 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003759 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003760 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003761 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003762 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003763 struct btrfs_disk_key disk_key;
3764
3765 btrfs_node_key(parent, &disk_key, 0);
3766 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003767 if (wret)
3768 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003769 }
Chris Masond6025572007-03-30 14:27:56 -04003770 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003771 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003772}
3773
Chris Mason74123bd2007-02-02 11:05:29 -05003774/*
Chris Mason323ac952008-10-01 19:05:46 -04003775 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003776 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003777 *
3778 * This deletes the pointer in path->nodes[1] and frees the leaf
3779 * block extent. zero is returned if it all worked out, < 0 otherwise.
3780 *
3781 * The path must have already been setup for deleting the leaf, including
3782 * all the proper balancing. path->nodes[1] must be locked.
3783 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003784static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3785 struct btrfs_root *root,
3786 struct btrfs_path *path,
3787 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003788{
3789 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003790
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003791 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003792 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3793 if (ret)
3794 return ret;
3795
Chris Mason4d081c42009-02-04 09:31:28 -05003796 /*
3797 * btrfs_free_extent is expensive, we want to make sure we
3798 * aren't holding any locks when we call it
3799 */
3800 btrfs_unlock_up_safe(path, 0);
3801
Yan, Zhengf0486c62010-05-16 10:46:25 -04003802 root_sub_used(root, leaf->len);
3803
3804 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3805 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003806}
3807/*
Chris Mason74123bd2007-02-02 11:05:29 -05003808 * delete the item at the leaf level in path. If that empties
3809 * the leaf, remove it from the tree
3810 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003811int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3812 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003813{
Chris Mason5f39d392007-10-15 16:14:19 -04003814 struct extent_buffer *leaf;
3815 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003816 int last_off;
3817 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003818 int ret = 0;
3819 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003820 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003821 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003822
Chris Mason5f39d392007-10-15 16:14:19 -04003823 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003824 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3825
3826 for (i = 0; i < nr; i++)
3827 dsize += btrfs_item_size_nr(leaf, slot + i);
3828
Chris Mason5f39d392007-10-15 16:14:19 -04003829 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003830
Chris Mason85e21ba2008-01-29 15:11:36 -05003831 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003832 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003833
3834 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003835 data_end + dsize,
3836 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003837 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003838
Chris Mason85e21ba2008-01-29 15:11:36 -05003839 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003840 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003841
Chris Mason5f39d392007-10-15 16:14:19 -04003842 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003843 if (!leaf->map_token) {
3844 map_extent_buffer(leaf, (unsigned long)item,
3845 sizeof(struct btrfs_item),
3846 &leaf->map_token, &leaf->kaddr,
3847 &leaf->map_start, &leaf->map_len,
3848 KM_USER1);
3849 }
Chris Mason5f39d392007-10-15 16:14:19 -04003850 ioff = btrfs_item_offset(leaf, item);
3851 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003852 }
Chris Masondb945352007-10-15 16:15:53 -04003853
3854 if (leaf->map_token) {
3855 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3856 leaf->map_token = NULL;
3857 }
3858
Chris Mason5f39d392007-10-15 16:14:19 -04003859 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003860 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003861 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003862 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003863 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003864 btrfs_set_header_nritems(leaf, nritems - nr);
3865 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003866
Chris Mason74123bd2007-02-02 11:05:29 -05003867 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003868 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003869 if (leaf == root->node) {
3870 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003871 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003872 btrfs_set_path_blocking(path);
3873 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003874 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003875 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003876 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003877 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003878 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003879 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003880 struct btrfs_disk_key disk_key;
3881
3882 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003883 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003884 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003885 if (wret)
3886 ret = wret;
3887 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003888
Chris Mason74123bd2007-02-02 11:05:29 -05003889 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003890 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003891 /* push_leaf_left fixes the path.
3892 * make sure the path still points to our leaf
3893 * for possible call to del_ptr below
3894 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003895 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003896 extent_buffer_get(leaf);
3897
Chris Masonb9473432009-03-13 11:00:37 -04003898 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003899 wret = push_leaf_left(trans, root, path, 1, 1,
3900 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003901 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003902 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003903
3904 if (path->nodes[0] == leaf &&
3905 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003906 wret = push_leaf_right(trans, root, path, 1,
3907 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003908 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003909 ret = wret;
3910 }
Chris Mason5f39d392007-10-15 16:14:19 -04003911
3912 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003913 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003914 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003915 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003916 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003917 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003918 /* if we're still in the path, make sure
3919 * we're dirty. Otherwise, one of the
3920 * push_leaf functions must have already
3921 * dirtied this buffer
3922 */
3923 if (path->nodes[0] == leaf)
3924 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003925 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003926 }
Chris Masond5719762007-03-23 10:01:08 -04003927 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003928 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003929 }
3930 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003931 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003932}
3933
Chris Mason97571fd2007-02-24 13:39:08 -05003934/*
Chris Mason925baed2008-06-25 16:01:30 -04003935 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003936 * returns 0 if it found something or 1 if there are no lesser leaves.
3937 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003938 *
3939 * This may release the path, and so you may lose any locks held at the
3940 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003941 */
3942int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3943{
Chris Mason925baed2008-06-25 16:01:30 -04003944 struct btrfs_key key;
3945 struct btrfs_disk_key found_key;
3946 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003947
Chris Mason925baed2008-06-25 16:01:30 -04003948 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003949
Chris Mason925baed2008-06-25 16:01:30 -04003950 if (key.offset > 0)
3951 key.offset--;
3952 else if (key.type > 0)
3953 key.type--;
3954 else if (key.objectid > 0)
3955 key.objectid--;
3956 else
3957 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003958
David Sterbab3b4aa72011-04-21 01:20:15 +02003959 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003960 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3961 if (ret < 0)
3962 return ret;
3963 btrfs_item_key(path->nodes[0], &found_key, 0);
3964 ret = comp_keys(&found_key, &key);
3965 if (ret < 0)
3966 return 0;
3967 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003968}
3969
Chris Mason3f157a22008-06-25 16:01:31 -04003970/*
3971 * A helper function to walk down the tree starting at min_key, and looking
3972 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003973 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003974 *
3975 * This does not cow, but it does stuff the starting key it finds back
3976 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3977 * key and get a writable path.
3978 *
3979 * This does lock as it descends, and path->keep_locks should be set
3980 * to 1 by the caller.
3981 *
3982 * This honors path->lowest_level to prevent descent past a given level
3983 * of the tree.
3984 *
Chris Masond352ac62008-09-29 15:18:18 -04003985 * min_trans indicates the oldest transaction that you are interested
3986 * in walking through. Any nodes or leaves older than min_trans are
3987 * skipped over (without reading them).
3988 *
Chris Mason3f157a22008-06-25 16:01:31 -04003989 * returns zero if something useful was found, < 0 on error and 1 if there
3990 * was nothing in the tree that matched the search criteria.
3991 */
3992int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003993 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003994 struct btrfs_path *path, int cache_only,
3995 u64 min_trans)
3996{
3997 struct extent_buffer *cur;
3998 struct btrfs_key found_key;
3999 int slot;
Yan96524802008-07-24 12:19:49 -04004000 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004001 u32 nritems;
4002 int level;
4003 int ret = 1;
4004
Chris Mason934d3752008-12-08 16:43:10 -05004005 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004006again:
4007 cur = btrfs_lock_root_node(root);
4008 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004009 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004010 path->nodes[level] = cur;
4011 path->locks[level] = 1;
4012
4013 if (btrfs_header_generation(cur) < min_trans) {
4014 ret = 1;
4015 goto out;
4016 }
Chris Masond3977122009-01-05 21:25:51 -05004017 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004018 nritems = btrfs_header_nritems(cur);
4019 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004020 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004021
Chris Mason323ac952008-10-01 19:05:46 -04004022 /* at the lowest level, we're done, setup the path and exit */
4023 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004024 if (slot >= nritems)
4025 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004026 ret = 0;
4027 path->slots[level] = slot;
4028 btrfs_item_key_to_cpu(cur, &found_key, slot);
4029 goto out;
4030 }
Yan96524802008-07-24 12:19:49 -04004031 if (sret && slot > 0)
4032 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004033 /*
4034 * check this node pointer against the cache_only and
4035 * min_trans parameters. If it isn't in cache or is too
4036 * old, skip to the next one.
4037 */
Chris Masond3977122009-01-05 21:25:51 -05004038 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004039 u64 blockptr;
4040 u64 gen;
4041 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004042 struct btrfs_disk_key disk_key;
4043
Chris Mason3f157a22008-06-25 16:01:31 -04004044 blockptr = btrfs_node_blockptr(cur, slot);
4045 gen = btrfs_node_ptr_generation(cur, slot);
4046 if (gen < min_trans) {
4047 slot++;
4048 continue;
4049 }
4050 if (!cache_only)
4051 break;
4052
Chris Masone02119d2008-09-05 16:13:11 -04004053 if (max_key) {
4054 btrfs_node_key(cur, &disk_key, slot);
4055 if (comp_keys(&disk_key, max_key) >= 0) {
4056 ret = 1;
4057 goto out;
4058 }
4059 }
4060
Chris Mason3f157a22008-06-25 16:01:31 -04004061 tmp = btrfs_find_tree_block(root, blockptr,
4062 btrfs_level_size(root, level - 1));
4063
4064 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4065 free_extent_buffer(tmp);
4066 break;
4067 }
4068 if (tmp)
4069 free_extent_buffer(tmp);
4070 slot++;
4071 }
Chris Masone02119d2008-09-05 16:13:11 -04004072find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004073 /*
4074 * we didn't find a candidate key in this node, walk forward
4075 * and find another one
4076 */
4077 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004078 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004079 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004080 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004081 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004082 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004083 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004084 goto again;
4085 } else {
4086 goto out;
4087 }
4088 }
4089 /* save our key for returning back */
4090 btrfs_node_key_to_cpu(cur, &found_key, slot);
4091 path->slots[level] = slot;
4092 if (level == path->lowest_level) {
4093 ret = 0;
4094 unlock_up(path, level, 1);
4095 goto out;
4096 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004097 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004098 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004099 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004100
4101 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004102
Chris Mason3f157a22008-06-25 16:01:31 -04004103 path->locks[level - 1] = 1;
4104 path->nodes[level - 1] = cur;
4105 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004106 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004107 }
4108out:
4109 if (ret == 0)
4110 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004111 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004112 return ret;
4113}
4114
4115/*
4116 * this is similar to btrfs_next_leaf, but does not try to preserve
4117 * and fixup the path. It looks for and returns the next key in the
4118 * tree based on the current path and the cache_only and min_trans
4119 * parameters.
4120 *
4121 * 0 is returned if another key is found, < 0 if there are any errors
4122 * and 1 is returned if there are no higher keys in the tree
4123 *
4124 * path->keep_locks should be set to 1 on the search made before
4125 * calling this function.
4126 */
Chris Masone7a84562008-06-25 16:01:31 -04004127int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004128 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004129 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004130{
Chris Masone7a84562008-06-25 16:01:31 -04004131 int slot;
4132 struct extent_buffer *c;
4133
Chris Mason934d3752008-12-08 16:43:10 -05004134 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004135 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004136 if (!path->nodes[level])
4137 return 1;
4138
4139 slot = path->slots[level] + 1;
4140 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004141next:
Chris Masone7a84562008-06-25 16:01:31 -04004142 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004143 int ret;
4144 int orig_lowest;
4145 struct btrfs_key cur_key;
4146 if (level + 1 >= BTRFS_MAX_LEVEL ||
4147 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004148 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004149
4150 if (path->locks[level + 1]) {
4151 level++;
4152 continue;
4153 }
4154
4155 slot = btrfs_header_nritems(c) - 1;
4156 if (level == 0)
4157 btrfs_item_key_to_cpu(c, &cur_key, slot);
4158 else
4159 btrfs_node_key_to_cpu(c, &cur_key, slot);
4160
4161 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004162 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004163 path->lowest_level = level;
4164 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4165 0, 0);
4166 path->lowest_level = orig_lowest;
4167 if (ret < 0)
4168 return ret;
4169
4170 c = path->nodes[level];
4171 slot = path->slots[level];
4172 if (ret == 0)
4173 slot++;
4174 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004175 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004176
Chris Masone7a84562008-06-25 16:01:31 -04004177 if (level == 0)
4178 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004179 else {
4180 u64 blockptr = btrfs_node_blockptr(c, slot);
4181 u64 gen = btrfs_node_ptr_generation(c, slot);
4182
4183 if (cache_only) {
4184 struct extent_buffer *cur;
4185 cur = btrfs_find_tree_block(root, blockptr,
4186 btrfs_level_size(root, level - 1));
4187 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4188 slot++;
4189 if (cur)
4190 free_extent_buffer(cur);
4191 goto next;
4192 }
4193 free_extent_buffer(cur);
4194 }
4195 if (gen < min_trans) {
4196 slot++;
4197 goto next;
4198 }
Chris Masone7a84562008-06-25 16:01:31 -04004199 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004200 }
Chris Masone7a84562008-06-25 16:01:31 -04004201 return 0;
4202 }
4203 return 1;
4204}
4205
Chris Mason7bb86312007-12-11 09:25:06 -05004206/*
Chris Mason925baed2008-06-25 16:01:30 -04004207 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004208 * returns 0 if it found something or 1 if there are no greater leaves.
4209 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004210 */
Chris Mason234b63a2007-03-13 10:46:10 -04004211int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004212{
4213 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004214 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004215 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004216 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004217 struct btrfs_key key;
4218 u32 nritems;
4219 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004220 int old_spinning = path->leave_spinning;
4221 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004222
4223 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004224 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004225 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004226
Chris Mason8e73f272009-04-03 10:14:18 -04004227 /*
4228 * we take the blocks in an order that upsets lockdep. Using
4229 * blocking mode is the only way around it.
4230 */
4231#ifdef CONFIG_DEBUG_LOCK_ALLOC
4232 force_blocking = 1;
4233#endif
Chris Mason925baed2008-06-25 16:01:30 -04004234
Chris Mason8e73f272009-04-03 10:14:18 -04004235 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4236again:
4237 level = 1;
4238 next = NULL;
David Sterbab3b4aa72011-04-21 01:20:15 +02004239 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004240
Chris Masona2135012008-06-25 16:01:30 -04004241 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004242
4243 if (!force_blocking)
4244 path->leave_spinning = 1;
4245
Chris Mason925baed2008-06-25 16:01:30 -04004246 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4247 path->keep_locks = 0;
4248
4249 if (ret < 0)
4250 return ret;
4251
Chris Masona2135012008-06-25 16:01:30 -04004252 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004253 /*
4254 * by releasing the path above we dropped all our locks. A balance
4255 * could have added more items next to the key that used to be
4256 * at the very end of the block. So, check again here and
4257 * advance the path if there are now more items available.
4258 */
Chris Masona2135012008-06-25 16:01:30 -04004259 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004260 if (ret == 0)
4261 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004262 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004263 goto done;
4264 }
Chris Masond97e63b2007-02-20 16:40:44 -05004265
Chris Masond3977122009-01-05 21:25:51 -05004266 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004267 if (!path->nodes[level]) {
4268 ret = 1;
4269 goto done;
4270 }
Chris Mason5f39d392007-10-15 16:14:19 -04004271
Chris Masond97e63b2007-02-20 16:40:44 -05004272 slot = path->slots[level] + 1;
4273 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004274 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004275 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004276 if (level == BTRFS_MAX_LEVEL) {
4277 ret = 1;
4278 goto done;
4279 }
Chris Masond97e63b2007-02-20 16:40:44 -05004280 continue;
4281 }
Chris Mason5f39d392007-10-15 16:14:19 -04004282
Chris Mason925baed2008-06-25 16:01:30 -04004283 if (next) {
4284 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004285 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004286 }
Chris Mason5f39d392007-10-15 16:14:19 -04004287
Chris Mason8e73f272009-04-03 10:14:18 -04004288 next = c;
4289 ret = read_block_for_search(NULL, root, path, &next, level,
4290 slot, &key);
4291 if (ret == -EAGAIN)
4292 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004293
Chris Mason76a05b32009-05-14 13:24:30 -04004294 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004295 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004296 goto done;
4297 }
4298
Chris Mason5cd57b22008-06-25 16:01:30 -04004299 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004300 ret = btrfs_try_spin_lock(next);
4301 if (!ret) {
4302 btrfs_set_path_blocking(path);
4303 btrfs_tree_lock(next);
4304 if (!force_blocking)
4305 btrfs_clear_path_blocking(path, next);
4306 }
4307 if (force_blocking)
4308 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004309 }
Chris Masond97e63b2007-02-20 16:40:44 -05004310 break;
4311 }
4312 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004313 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004314 level--;
4315 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004316 if (path->locks[level])
4317 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004318
Chris Mason5f39d392007-10-15 16:14:19 -04004319 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004320 path->nodes[level] = next;
4321 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004322 if (!path->skip_locking)
4323 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004324
Chris Masond97e63b2007-02-20 16:40:44 -05004325 if (!level)
4326 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004327
Chris Mason8e73f272009-04-03 10:14:18 -04004328 ret = read_block_for_search(NULL, root, path, &next, level,
4329 0, &key);
4330 if (ret == -EAGAIN)
4331 goto again;
4332
Chris Mason76a05b32009-05-14 13:24:30 -04004333 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004334 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004335 goto done;
4336 }
4337
Chris Mason5cd57b22008-06-25 16:01:30 -04004338 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004339 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004340 ret = btrfs_try_spin_lock(next);
4341 if (!ret) {
4342 btrfs_set_path_blocking(path);
4343 btrfs_tree_lock(next);
4344 if (!force_blocking)
4345 btrfs_clear_path_blocking(path, next);
4346 }
4347 if (force_blocking)
4348 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004349 }
Chris Masond97e63b2007-02-20 16:40:44 -05004350 }
Chris Mason8e73f272009-04-03 10:14:18 -04004351 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004352done:
4353 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004354 path->leave_spinning = old_spinning;
4355 if (!old_spinning)
4356 btrfs_set_path_blocking(path);
4357
4358 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004359}
Chris Mason0b86a832008-03-24 15:01:56 -04004360
Chris Mason3f157a22008-06-25 16:01:31 -04004361/*
4362 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4363 * searching until it gets past min_objectid or finds an item of 'type'
4364 *
4365 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4366 */
Chris Mason0b86a832008-03-24 15:01:56 -04004367int btrfs_previous_item(struct btrfs_root *root,
4368 struct btrfs_path *path, u64 min_objectid,
4369 int type)
4370{
4371 struct btrfs_key found_key;
4372 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004373 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004374 int ret;
4375
Chris Masond3977122009-01-05 21:25:51 -05004376 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004377 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004378 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004379 ret = btrfs_prev_leaf(root, path);
4380 if (ret != 0)
4381 return ret;
4382 } else {
4383 path->slots[0]--;
4384 }
4385 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004386 nritems = btrfs_header_nritems(leaf);
4387 if (nritems == 0)
4388 return 1;
4389 if (path->slots[0] == nritems)
4390 path->slots[0]--;
4391
Chris Mason0b86a832008-03-24 15:01:56 -04004392 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004393 if (found_key.objectid < min_objectid)
4394 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004395 if (found_key.type == type)
4396 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004397 if (found_key.objectid == min_objectid &&
4398 found_key.type < type)
4399 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004400 }
4401 return 1;
4402}