blob: babf7fbaec8484217393a94b2920e0f4c7b0aae7 [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>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Yan, Zhengad48fd752009-11-12 09:33:58 +000040static int setup_items_for_insert(struct btrfs_trans_handle *trans,
41 struct btrfs_root *root, struct btrfs_path *path,
42 struct btrfs_key *cpu_key, u32 *data_size,
43 u32 total_data, u32 total_size, int nr);
44
Chris Masond97e63b2007-02-20 16:40:44 -050045
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050049 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
50 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040051 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040052 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040053}
54
Chris Masonb4ce94d2009-02-04 09:25:08 -050055/*
56 * set all locked nodes in the path to blocking locks. This should
57 * be done before scheduling
58 */
59noinline void btrfs_set_path_blocking(struct btrfs_path *p)
60{
61 int i;
62 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
63 if (p->nodes[i] && p->locks[i])
64 btrfs_set_lock_blocking(p->nodes[i]);
65 }
66}
67
68/*
69 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050070 *
71 * held is used to keep lockdep happy, when lockdep is enabled
72 * we set held to a blocking lock before we go around and
73 * retake all the spinlocks in the path. You can safely use NULL
74 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050075 */
Chris Mason4008c042009-02-12 14:09:45 -050076noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
77 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050078{
79 int i;
Chris Mason4008c042009-02-12 14:09:45 -050080
81#ifdef CONFIG_DEBUG_LOCK_ALLOC
82 /* lockdep really cares that we take all of these spinlocks
83 * in the right order. If any of the locks in the path are not
84 * currently blocking, it is going to complain. So, make really
85 * really sure by forcing the path to blocking before we clear
86 * the path blocking.
87 */
88 if (held)
89 btrfs_set_lock_blocking(held);
90 btrfs_set_path_blocking(p);
91#endif
92
93 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050094 if (p->nodes[i] && p->locks[i])
95 btrfs_clear_lock_blocking(p->nodes[i]);
96 }
Chris Mason4008c042009-02-12 14:09:45 -050097
98#ifdef CONFIG_DEBUG_LOCK_ALLOC
99 if (held)
100 btrfs_clear_lock_blocking(held);
101#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500102}
103
Chris Masond352ac62008-09-29 15:18:18 -0400104/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400105void btrfs_free_path(struct btrfs_path *p)
106{
Chris Masondf24a2b2007-04-04 09:36:31 -0400107 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400108 kmem_cache_free(btrfs_path_cachep, p);
109}
110
Chris Masond352ac62008-09-29 15:18:18 -0400111/*
112 * path release drops references on the extent buffers in the path
113 * and it drops any locks held by this path
114 *
115 * It is safe to call this on paths that no locks or extent buffers held.
116 */
Chris Masond3977122009-01-05 21:25:51 -0500117noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500118{
119 int i;
Chris Masona2135012008-06-25 16:01:30 -0400120
Chris Mason234b63a2007-03-13 10:46:10 -0400121 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400122 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500123 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400124 continue;
125 if (p->locks[i]) {
126 btrfs_tree_unlock(p->nodes[i]);
127 p->locks[i] = 0;
128 }
Chris Mason5f39d392007-10-15 16:14:19 -0400129 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400130 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500131 }
132}
133
Chris Masond352ac62008-09-29 15:18:18 -0400134/*
135 * safely gets a reference on the root node of a tree. A lock
136 * is not taken, so a concurrent writer may put a different node
137 * at the root of the tree. See btrfs_lock_root_node for the
138 * looping required.
139 *
140 * The extent buffer returned by this has a reference taken, so
141 * it won't disappear. It may stop being the root of the tree
142 * at any time because there are no locks held.
143 */
Chris Mason925baed2008-06-25 16:01:30 -0400144struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
145{
146 struct extent_buffer *eb;
147 spin_lock(&root->node_lock);
148 eb = root->node;
149 extent_buffer_get(eb);
150 spin_unlock(&root->node_lock);
151 return eb;
152}
153
Chris Masond352ac62008-09-29 15:18:18 -0400154/* loop around taking references on and locking the root node of the
155 * tree until you end up with a lock on the root. A locked buffer
156 * is returned, with a reference held.
157 */
Chris Mason925baed2008-06-25 16:01:30 -0400158struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
159{
160 struct extent_buffer *eb;
161
Chris Masond3977122009-01-05 21:25:51 -0500162 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400163 eb = btrfs_root_node(root);
164 btrfs_tree_lock(eb);
165
166 spin_lock(&root->node_lock);
167 if (eb == root->node) {
168 spin_unlock(&root->node_lock);
169 break;
170 }
171 spin_unlock(&root->node_lock);
172
173 btrfs_tree_unlock(eb);
174 free_extent_buffer(eb);
175 }
176 return eb;
177}
178
Chris Masond352ac62008-09-29 15:18:18 -0400179/* cowonly root (everything not a reference counted cow subvolume), just get
180 * put onto a simple dirty list. transaction.c walks this to make sure they
181 * get properly updated on disk.
182 */
Chris Mason0b86a832008-03-24 15:01:56 -0400183static void add_root_to_dirty_list(struct btrfs_root *root)
184{
185 if (root->track_dirty && list_empty(&root->dirty_list)) {
186 list_add(&root->dirty_list,
187 &root->fs_info->dirty_cowonly_roots);
188 }
189}
190
Chris Masond352ac62008-09-29 15:18:18 -0400191/*
192 * used by snapshot creation to make a copy of a root for a tree with
193 * a given objectid. The buffer with the new root node is returned in
194 * cow_ret, and this func returns zero on success or a negative error code.
195 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500196int btrfs_copy_root(struct btrfs_trans_handle *trans,
197 struct btrfs_root *root,
198 struct extent_buffer *buf,
199 struct extent_buffer **cow_ret, u64 new_root_objectid)
200{
201 struct extent_buffer *cow;
202 u32 nritems;
203 int ret = 0;
204 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400205 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500206
207 WARN_ON(root->ref_cows && trans->transid !=
208 root->fs_info->running_transaction->transid);
209 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
210
211 level = btrfs_header_level(buf);
212 nritems = btrfs_header_nritems(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400213 if (level == 0)
214 btrfs_item_key(buf, &disk_key, 0);
215 else
216 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400217
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400218 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
219 new_root_objectid, &disk_key, level,
220 buf->start, 0);
221 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 return PTR_ERR(cow);
223
224 copy_extent_buffer(cow, buf, 0, 0, cow->len);
225 btrfs_set_header_bytenr(cow, cow->start);
226 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400227 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
228 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
229 BTRFS_HEADER_FLAG_RELOC);
230 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
231 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
232 else
233 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500234
Yan Zheng2b820322008-11-17 21:11:30 -0500235 write_extent_buffer(cow, root->fs_info->fsid,
236 (unsigned long)btrfs_header_fsid(cow),
237 BTRFS_FSID_SIZE);
238
Chris Masonbe20aa92007-12-17 20:14:01 -0500239 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400240 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
241 ret = btrfs_inc_ref(trans, root, cow, 1);
242 else
243 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500244
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 if (ret)
246 return ret;
247
248 btrfs_mark_buffer_dirty(cow);
249 *cow_ret = cow;
250 return 0;
251}
252
Chris Masond352ac62008-09-29 15:18:18 -0400253/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 * check if the tree block can be shared by multiple trees
255 */
256int btrfs_block_can_be_shared(struct btrfs_root *root,
257 struct extent_buffer *buf)
258{
259 /*
260 * Tree blocks not in refernece counted trees and tree roots
261 * are never shared. If a block was allocated after the last
262 * snapshot and the block was not allocated by tree relocation,
263 * we know the block is not shared.
264 */
265 if (root->ref_cows &&
266 buf != root->node && buf != root->commit_root &&
267 (btrfs_header_generation(buf) <=
268 btrfs_root_last_snapshot(&root->root_item) ||
269 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
270 return 1;
271#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
272 if (root->ref_cows &&
273 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
274 return 1;
275#endif
276 return 0;
277}
278
279static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
280 struct btrfs_root *root,
281 struct extent_buffer *buf,
282 struct extent_buffer *cow)
283{
284 u64 refs;
285 u64 owner;
286 u64 flags;
287 u64 new_flags = 0;
288 int ret;
289
290 /*
291 * Backrefs update rules:
292 *
293 * Always use full backrefs for extent pointers in tree block
294 * allocated by tree relocation.
295 *
296 * If a shared tree block is no longer referenced by its owner
297 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
298 * use full backrefs for extent pointers in tree block.
299 *
300 * If a tree block is been relocating
301 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
302 * use full backrefs for extent pointers in tree block.
303 * The reason for this is some operations (such as drop tree)
304 * are only allowed for blocks use full backrefs.
305 */
306
307 if (btrfs_block_can_be_shared(root, buf)) {
308 ret = btrfs_lookup_extent_info(trans, root, buf->start,
309 buf->len, &refs, &flags);
310 BUG_ON(ret);
311 BUG_ON(refs == 0);
312 } else {
313 refs = 1;
314 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
315 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
316 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
317 else
318 flags = 0;
319 }
320
321 owner = btrfs_header_owner(buf);
322 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
323 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
324
325 if (refs > 1) {
326 if ((owner == root->root_key.objectid ||
327 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
328 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
329 ret = btrfs_inc_ref(trans, root, buf, 1);
330 BUG_ON(ret);
331
332 if (root->root_key.objectid ==
333 BTRFS_TREE_RELOC_OBJECTID) {
334 ret = btrfs_dec_ref(trans, root, buf, 0);
335 BUG_ON(ret);
336 ret = btrfs_inc_ref(trans, root, cow, 1);
337 BUG_ON(ret);
338 }
339 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
340 } else {
341
342 if (root->root_key.objectid ==
343 BTRFS_TREE_RELOC_OBJECTID)
344 ret = btrfs_inc_ref(trans, root, cow, 1);
345 else
346 ret = btrfs_inc_ref(trans, root, cow, 0);
347 BUG_ON(ret);
348 }
349 if (new_flags != 0) {
350 ret = btrfs_set_disk_extent_flags(trans, root,
351 buf->start,
352 buf->len,
353 new_flags, 0);
354 BUG_ON(ret);
355 }
356 } else {
357 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
358 if (root->root_key.objectid ==
359 BTRFS_TREE_RELOC_OBJECTID)
360 ret = btrfs_inc_ref(trans, root, cow, 1);
361 else
362 ret = btrfs_inc_ref(trans, root, cow, 0);
363 BUG_ON(ret);
364 ret = btrfs_dec_ref(trans, root, buf, 1);
365 BUG_ON(ret);
366 }
367 clean_tree_block(trans, root, buf);
368 }
369 return 0;
370}
371
372/*
Chris Masond3977122009-01-05 21:25:51 -0500373 * does the dirty work in cow of a single block. The parent block (if
374 * supplied) is updated to point to the new cow copy. The new buffer is marked
375 * dirty and returned locked. If you modify the block it needs to be marked
376 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400377 *
378 * search_start -- an allocation hint for the new block
379 *
Chris Masond3977122009-01-05 21:25:51 -0500380 * empty_size -- a hint that you plan on doing more cow. This is the size in
381 * bytes the allocator should try to find free next to the block it returns.
382 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400383 */
Chris Masond3977122009-01-05 21:25:51 -0500384static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400385 struct btrfs_root *root,
386 struct extent_buffer *buf,
387 struct extent_buffer *parent, int parent_slot,
388 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400389 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400390{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400391 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400392 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500393 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400394 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400395 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400396
Chris Mason925baed2008-06-25 16:01:30 -0400397 if (*cow_ret == buf)
398 unlock_orig = 1;
399
Chris Masonb9447ef2009-03-09 11:45:38 -0400400 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400401
Chris Mason7bb86312007-12-11 09:25:06 -0500402 WARN_ON(root->ref_cows && trans->transid !=
403 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400404 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400405
Chris Mason7bb86312007-12-11 09:25:06 -0500406 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400407
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400408 if (level == 0)
409 btrfs_item_key(buf, &disk_key, 0);
410 else
411 btrfs_node_key(buf, &disk_key, 0);
412
413 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
414 if (parent)
415 parent_start = parent->start;
416 else
417 parent_start = 0;
418 } else
419 parent_start = 0;
420
421 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
422 root->root_key.objectid, &disk_key,
423 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400424 if (IS_ERR(cow))
425 return PTR_ERR(cow);
426
Chris Masonb4ce94d2009-02-04 09:25:08 -0500427 /* cow is set to blocking by btrfs_init_new_buffer */
428
Chris Mason5f39d392007-10-15 16:14:19 -0400429 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400430 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400431 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400432 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
433 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
434 BTRFS_HEADER_FLAG_RELOC);
435 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
436 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
437 else
438 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400439
Yan Zheng2b820322008-11-17 21:11:30 -0500440 write_extent_buffer(cow, root->fs_info->fsid,
441 (unsigned long)btrfs_header_fsid(cow),
442 BTRFS_FSID_SIZE);
443
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400444 update_ref_for_cow(trans, root, buf, cow);
Zheng Yan1a40e232008-09-26 10:09:34 -0400445
Chris Mason6702ed42007-08-07 16:15:09 -0400446 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400447 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400448 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
449 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
450 parent_start = buf->start;
451 else
452 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400453
454 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400455 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400456 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400457 spin_unlock(&root->node_lock);
458
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000459 btrfs_free_tree_block(trans, root, buf->start, buf->len,
460 parent_start, root->root_key.objectid, level);
Chris Mason5f39d392007-10-15 16:14:19 -0400461 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400462 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400463 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400464 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
465 parent_start = parent->start;
466 else
467 parent_start = 0;
468
469 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400470 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400471 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500472 btrfs_set_node_ptr_generation(parent, parent_slot,
473 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400474 btrfs_mark_buffer_dirty(parent);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000475 btrfs_free_tree_block(trans, root, buf->start, buf->len,
476 parent_start, root->root_key.objectid, level);
Chris Mason6702ed42007-08-07 16:15:09 -0400477 }
Chris Mason925baed2008-06-25 16:01:30 -0400478 if (unlock_orig)
479 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400480 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400481 btrfs_mark_buffer_dirty(cow);
482 *cow_ret = cow;
483 return 0;
484}
485
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400486static inline int should_cow_block(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root,
488 struct extent_buffer *buf)
489{
490 if (btrfs_header_generation(buf) == trans->transid &&
491 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
492 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
493 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
494 return 0;
495 return 1;
496}
497
Chris Masond352ac62008-09-29 15:18:18 -0400498/*
499 * cows a single block, see __btrfs_cow_block for the real work.
500 * This version of it has extra checks so that a block isn't cow'd more than
501 * once per transaction, as long as it hasn't been written yet
502 */
Chris Masond3977122009-01-05 21:25:51 -0500503noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400504 struct btrfs_root *root, struct extent_buffer *buf,
505 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400506 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500507{
Chris Mason6702ed42007-08-07 16:15:09 -0400508 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400509 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500510
Chris Masonccd467d2007-06-28 15:57:36 -0400511 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500512 printk(KERN_CRIT "trans %llu running %llu\n",
513 (unsigned long long)trans->transid,
514 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400515 root->fs_info->running_transaction->transid);
516 WARN_ON(1);
517 }
518 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500519 printk(KERN_CRIT "trans %llu running %llu\n",
520 (unsigned long long)trans->transid,
521 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400522 WARN_ON(1);
523 }
Chris Masondc17ff82008-01-08 15:46:30 -0500524
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400525 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500526 *cow_ret = buf;
527 return 0;
528 }
Chris Masonc4876852009-02-04 09:24:25 -0500529
Chris Mason0b86a832008-03-24 15:01:56 -0400530 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500531
532 if (parent)
533 btrfs_set_lock_blocking(parent);
534 btrfs_set_lock_blocking(buf);
535
Chris Masonf510cfe2007-10-15 16:14:48 -0400536 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400537 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400538 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400539}
540
Chris Masond352ac62008-09-29 15:18:18 -0400541/*
542 * helper function for defrag to decide if two blocks pointed to by a
543 * node are actually close by
544 */
Chris Mason6b800532007-10-15 16:17:34 -0400545static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400546{
Chris Mason6b800532007-10-15 16:17:34 -0400547 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400548 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400549 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400550 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500551 return 0;
552}
553
Chris Mason081e9572007-11-06 10:26:24 -0500554/*
555 * compare two keys in a memcmp fashion
556 */
557static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
558{
559 struct btrfs_key k1;
560
561 btrfs_disk_key_to_cpu(&k1, disk);
562
Diego Calleja20736ab2009-07-24 11:06:52 -0400563 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500564}
565
Josef Bacikf3465ca2008-11-12 14:19:50 -0500566/*
567 * same as comp_keys only with two btrfs_key's
568 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400569int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500570{
571 if (k1->objectid > k2->objectid)
572 return 1;
573 if (k1->objectid < k2->objectid)
574 return -1;
575 if (k1->type > k2->type)
576 return 1;
577 if (k1->type < k2->type)
578 return -1;
579 if (k1->offset > k2->offset)
580 return 1;
581 if (k1->offset < k2->offset)
582 return -1;
583 return 0;
584}
Chris Mason081e9572007-11-06 10:26:24 -0500585
Chris Masond352ac62008-09-29 15:18:18 -0400586/*
587 * this is used by the defrag code to go through all the
588 * leaves pointed to by a node and reallocate them so that
589 * disk order is close to key order
590 */
Chris Mason6702ed42007-08-07 16:15:09 -0400591int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400592 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400593 int start_slot, int cache_only, u64 *last_ret,
594 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400595{
Chris Mason6b800532007-10-15 16:17:34 -0400596 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400597 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400598 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400599 u64 search_start = *last_ret;
600 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400601 u64 other;
602 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400603 int end_slot;
604 int i;
605 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400606 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400607 int uptodate;
608 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500609 int progress_passed = 0;
610 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400611
Chris Mason5708b952007-10-25 15:43:18 -0400612 parent_level = btrfs_header_level(parent);
613 if (cache_only && parent_level != 1)
614 return 0;
615
Chris Masond3977122009-01-05 21:25:51 -0500616 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400617 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500618 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400619 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400620
Chris Mason6b800532007-10-15 16:17:34 -0400621 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400622 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400623 end_slot = parent_nritems;
624
625 if (parent_nritems == 1)
626 return 0;
627
Chris Masonb4ce94d2009-02-04 09:25:08 -0500628 btrfs_set_lock_blocking(parent);
629
Chris Mason6702ed42007-08-07 16:15:09 -0400630 for (i = start_slot; i < end_slot; i++) {
631 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400632
Chris Mason5708b952007-10-25 15:43:18 -0400633 if (!parent->map_token) {
634 map_extent_buffer(parent,
635 btrfs_node_key_ptr_offset(i),
636 sizeof(struct btrfs_key_ptr),
637 &parent->map_token, &parent->kaddr,
638 &parent->map_start, &parent->map_len,
639 KM_USER1);
640 }
Chris Mason081e9572007-11-06 10:26:24 -0500641 btrfs_node_key(parent, &disk_key, i);
642 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
643 continue;
644
645 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400646 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400647 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400648 if (last_block == 0)
649 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400650
Chris Mason6702ed42007-08-07 16:15:09 -0400651 if (i > 0) {
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 Mason0ef3e662008-05-24 14:04:53 -0400655 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400656 other = btrfs_node_blockptr(parent, i + 1);
657 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400658 }
Chris Masone9d0b132007-08-10 14:06:19 -0400659 if (close) {
660 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400661 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400662 }
Chris Mason5708b952007-10-25 15:43:18 -0400663 if (parent->map_token) {
664 unmap_extent_buffer(parent, parent->map_token,
665 KM_USER1);
666 parent->map_token = NULL;
667 }
Chris Mason6702ed42007-08-07 16:15:09 -0400668
Chris Mason6b800532007-10-15 16:17:34 -0400669 cur = btrfs_find_tree_block(root, blocknr, blocksize);
670 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400671 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400672 else
673 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400674 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400675 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400676 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400677 continue;
678 }
Chris Mason6b800532007-10-15 16:17:34 -0400679 if (!cur) {
680 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400681 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400682 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400683 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400684 }
Chris Mason6702ed42007-08-07 16:15:09 -0400685 }
Chris Masone9d0b132007-08-10 14:06:19 -0400686 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400687 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400688
Chris Masone7a84562008-06-25 16:01:31 -0400689 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500690 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400691 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400692 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400693 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400694 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400695 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400696 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400697 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400698 break;
Yan252c38f2007-08-29 09:11:44 -0400699 }
Chris Masone7a84562008-06-25 16:01:31 -0400700 search_start = cur->start;
701 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400702 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400703 btrfs_tree_unlock(cur);
704 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400705 }
Chris Mason5708b952007-10-25 15:43:18 -0400706 if (parent->map_token) {
707 unmap_extent_buffer(parent, parent->map_token,
708 KM_USER1);
709 parent->map_token = NULL;
710 }
Chris Mason6702ed42007-08-07 16:15:09 -0400711 return err;
712}
713
Chris Mason74123bd2007-02-02 11:05:29 -0500714/*
715 * The leaf data grows from end-to-front in the node.
716 * this returns the address of the start of the last item,
717 * which is the stop of the leaf data stack
718 */
Chris Mason123abc82007-03-14 14:14:43 -0400719static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400720 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500721{
Chris Mason5f39d392007-10-15 16:14:19 -0400722 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400724 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400725 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726}
727
Chris Masond352ac62008-09-29 15:18:18 -0400728/*
729 * extra debugging checks to make sure all the items in a key are
730 * well formed and in the proper order
731 */
Chris Mason123abc82007-03-14 14:14:43 -0400732static int check_node(struct btrfs_root *root, struct btrfs_path *path,
733 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500734{
Chris Mason5f39d392007-10-15 16:14:19 -0400735 struct extent_buffer *parent = NULL;
736 struct extent_buffer *node = path->nodes[level];
737 struct btrfs_disk_key parent_key;
738 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500739 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400740 int slot;
741 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400742 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500743
744 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400745 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400746
Chris Mason8d7be552007-05-10 11:24:42 -0400747 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400748 BUG_ON(nritems == 0);
749 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400750 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400751 btrfs_node_key(parent, &parent_key, parent_slot);
752 btrfs_node_key(node, &node_key, 0);
753 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400754 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400755 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400756 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500757 }
Chris Mason123abc82007-03-14 14:14:43 -0400758 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400759 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400760 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
761 btrfs_node_key(node, &node_key, slot);
762 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400763 }
764 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400765 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
766 btrfs_node_key(node, &node_key, slot);
767 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500768 }
769 return 0;
770}
771
Chris Masond352ac62008-09-29 15:18:18 -0400772/*
773 * extra checking to make sure all the items in a leaf are
774 * well formed and in the proper order
775 */
Chris Mason123abc82007-03-14 14:14:43 -0400776static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
777 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500778{
Chris Mason5f39d392007-10-15 16:14:19 -0400779 struct extent_buffer *leaf = path->nodes[level];
780 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500781 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400782 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400783 struct btrfs_disk_key parent_key;
784 struct btrfs_disk_key leaf_key;
785 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400786
Chris Mason5f39d392007-10-15 16:14:19 -0400787 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500788
789 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400790 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400791
792 if (nritems == 0)
793 return 0;
794
795 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400796 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400797 btrfs_node_key(parent, &parent_key, parent_slot);
798 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400799
Chris Mason5f39d392007-10-15 16:14:19 -0400800 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400801 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400802 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400803 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500804 }
Chris Mason5f39d392007-10-15 16:14:19 -0400805 if (slot != 0 && slot < nritems - 1) {
806 btrfs_item_key(leaf, &leaf_key, slot);
807 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
808 if (comp_keys(&leaf_key, &cpukey) <= 0) {
809 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500810 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400811 BUG_ON(1);
812 }
813 if (btrfs_item_offset_nr(leaf, slot - 1) !=
814 btrfs_item_end_nr(leaf, slot)) {
815 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500816 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400817 BUG_ON(1);
818 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500819 }
Chris Mason8d7be552007-05-10 11:24:42 -0400820 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400821 btrfs_item_key(leaf, &leaf_key, slot);
822 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
823 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
824 if (btrfs_item_offset_nr(leaf, slot) !=
825 btrfs_item_end_nr(leaf, slot + 1)) {
826 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500827 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400828 BUG_ON(1);
829 }
Chris Mason8d7be552007-05-10 11:24:42 -0400830 }
Chris Mason5f39d392007-10-15 16:14:19 -0400831 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
832 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500833 return 0;
834}
835
Chris Masond3977122009-01-05 21:25:51 -0500836static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500837 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500838{
Chris Mason85d824c2008-04-10 10:23:19 -0400839 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500840 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400841 return check_leaf(root, path, level);
842 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500843}
844
Chris Mason74123bd2007-02-02 11:05:29 -0500845/*
Chris Mason5f39d392007-10-15 16:14:19 -0400846 * search for key in the extent_buffer. The items start at offset p,
847 * and they are item_size apart. There are 'max' items in p.
848 *
Chris Mason74123bd2007-02-02 11:05:29 -0500849 * the slot in the array is returned via slot, and it points to
850 * the place where you would insert key if it is not found in
851 * the array.
852 *
853 * slot may point to max if the key is bigger than all of the keys
854 */
Chris Masone02119d2008-09-05 16:13:11 -0400855static noinline int generic_bin_search(struct extent_buffer *eb,
856 unsigned long p,
857 int item_size, struct btrfs_key *key,
858 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500859{
860 int low = 0;
861 int high = max;
862 int mid;
863 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400864 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400865 struct btrfs_disk_key unaligned;
866 unsigned long offset;
867 char *map_token = NULL;
868 char *kaddr = NULL;
869 unsigned long map_start = 0;
870 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400871 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500872
Chris Masond3977122009-01-05 21:25:51 -0500873 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500874 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400875 offset = p + mid * item_size;
876
877 if (!map_token || offset < map_start ||
878 (offset + sizeof(struct btrfs_disk_key)) >
879 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400880 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400881 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400882 map_token = NULL;
883 }
Chris Mason934d3752008-12-08 16:43:10 -0500884
885 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400886 sizeof(struct btrfs_disk_key),
887 &map_token, &kaddr,
888 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400889
Chris Mason479965d2007-10-15 16:14:27 -0400890 if (!err) {
891 tmp = (struct btrfs_disk_key *)(kaddr + offset -
892 map_start);
893 } else {
894 read_extent_buffer(eb, &unaligned,
895 offset, sizeof(unaligned));
896 tmp = &unaligned;
897 }
898
Chris Mason5f39d392007-10-15 16:14:19 -0400899 } else {
900 tmp = (struct btrfs_disk_key *)(kaddr + offset -
901 map_start);
902 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500903 ret = comp_keys(tmp, key);
904
905 if (ret < 0)
906 low = mid + 1;
907 else if (ret > 0)
908 high = mid;
909 else {
910 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400911 if (map_token)
912 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500913 return 0;
914 }
915 }
916 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400917 if (map_token)
918 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500919 return 1;
920}
921
Chris Mason97571fd2007-02-24 13:39:08 -0500922/*
923 * simple bin_search frontend that does the right thing for
924 * leaves vs nodes
925 */
Chris Mason5f39d392007-10-15 16:14:19 -0400926static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
927 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500928{
Chris Mason5f39d392007-10-15 16:14:19 -0400929 if (level == 0) {
930 return generic_bin_search(eb,
931 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400932 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400933 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400934 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500935 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400936 return generic_bin_search(eb,
937 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400938 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400939 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400940 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500941 }
942 return -1;
943}
944
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
946 int level, int *slot)
947{
948 return bin_search(eb, key, level, slot);
949}
950
Chris Masond352ac62008-09-29 15:18:18 -0400951/* given a node and slot number, this reads the blocks it points to. The
952 * extent buffer is returned with a reference taken (but unlocked).
953 * NULL is returned on error.
954 */
Chris Masone02119d2008-09-05 16:13:11 -0400955static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400956 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500957{
Chris Masonca7a79a2008-05-12 12:59:19 -0400958 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500959 if (slot < 0)
960 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400961 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500962 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400963
964 BUG_ON(level == 0);
965
Chris Masondb945352007-10-15 16:15:53 -0400966 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400967 btrfs_level_size(root, level - 1),
968 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500969}
970
Chris Masond352ac62008-09-29 15:18:18 -0400971/*
972 * node level balancing, used to make sure nodes are in proper order for
973 * item deletion. We balance from the top down, so we have to make sure
974 * that a deletion won't leave an node completely empty later on.
975 */
Chris Masone02119d2008-09-05 16:13:11 -0400976static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500977 struct btrfs_root *root,
978 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500979{
Chris Mason5f39d392007-10-15 16:14:19 -0400980 struct extent_buffer *right = NULL;
981 struct extent_buffer *mid;
982 struct extent_buffer *left = NULL;
983 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500984 int ret = 0;
985 int wret;
986 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500987 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400988 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500989 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500990
991 if (level == 0)
992 return 0;
993
Chris Mason5f39d392007-10-15 16:14:19 -0400994 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500995
Chris Mason925baed2008-06-25 16:01:30 -0400996 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500997 WARN_ON(btrfs_header_generation(mid) != trans->transid);
998
Chris Mason1d4f8a02007-03-13 09:28:32 -0400999 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001000
Chris Mason234b63a2007-03-13 10:46:10 -04001001 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001002 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001003 pslot = path->slots[level + 1];
1004
Chris Mason40689472007-03-17 14:29:23 -04001005 /*
1006 * deal with the case where there is only one pointer in the root
1007 * by promoting the node below to a root
1008 */
Chris Mason5f39d392007-10-15 16:14:19 -04001009 if (!parent) {
1010 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001011
Chris Mason5f39d392007-10-15 16:14:19 -04001012 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001013 return 0;
1014
1015 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001016 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001017 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001018 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001019 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001020 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan2f375ab2008-02-01 14:58:07 -05001021 BUG_ON(ret);
1022
Chris Mason925baed2008-06-25 16:01:30 -04001023 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001024 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001025 spin_unlock(&root->node_lock);
1026
Chris Mason0b86a832008-03-24 15:01:56 -04001027 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001028 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001029
Chris Mason925baed2008-06-25 16:01:30 -04001030 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001031 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001032 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001033 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001034 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001035 free_extent_buffer(mid);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00001036 ret = btrfs_free_tree_block(trans, root, mid->start, mid->len,
1037 0, root->root_key.objectid, level);
Chris Masonbb803952007-03-01 12:04:21 -05001038 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001039 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -04001040 return ret;
Chris Masonbb803952007-03-01 12:04:21 -05001041 }
Chris Mason5f39d392007-10-15 16:14:19 -04001042 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001043 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001044 return 0;
1045
Chris Mason5f39d392007-10-15 16:14:19 -04001046 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001047 err_on_enospc = 1;
1048
Chris Mason5f39d392007-10-15 16:14:19 -04001049 left = read_node_slot(root, parent, pslot - 1);
1050 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001051 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001052 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001053 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001054 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001055 if (wret) {
1056 ret = wret;
1057 goto enospc;
1058 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001059 }
Chris Mason5f39d392007-10-15 16:14:19 -04001060 right = read_node_slot(root, parent, pslot + 1);
1061 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001062 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001063 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001064 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001065 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001066 if (wret) {
1067 ret = wret;
1068 goto enospc;
1069 }
1070 }
1071
1072 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001073 if (left) {
1074 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001075 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001076 if (wret < 0)
1077 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001078 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001079 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001080 }
Chris Mason79f95c82007-03-01 15:16:26 -05001081
1082 /*
1083 * then try to empty the right most buffer into the middle
1084 */
Chris Mason5f39d392007-10-15 16:14:19 -04001085 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001086 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001087 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001088 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001089 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001090 u64 bytenr = right->start;
1091 u32 blocksize = right->len;
1092
Chris Mason5f39d392007-10-15 16:14:19 -04001093 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001094 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001095 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001096 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001097 wret = del_ptr(trans, root, path, level + 1, pslot +
1098 1);
Chris Masonbb803952007-03-01 12:04:21 -05001099 if (wret)
1100 ret = wret;
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00001101 wret = btrfs_free_tree_block(trans, root,
1102 bytenr, blocksize, 0,
1103 root->root_key.objectid,
1104 level);
Chris Masonbb803952007-03-01 12:04:21 -05001105 if (wret)
1106 ret = wret;
1107 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001108 struct btrfs_disk_key right_key;
1109 btrfs_node_key(right, &right_key, 0);
1110 btrfs_set_node_key(parent, &right_key, pslot + 1);
1111 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001112 }
1113 }
Chris Mason5f39d392007-10-15 16:14:19 -04001114 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001115 /*
1116 * we're not allowed to leave a node with one item in the
1117 * tree during a delete. A deletion from lower in the tree
1118 * could try to delete the only pointer in this node.
1119 * So, pull some keys from the left.
1120 * There has to be a left pointer at this point because
1121 * otherwise we would have pulled some pointers from the
1122 * right
1123 */
Chris Mason5f39d392007-10-15 16:14:19 -04001124 BUG_ON(!left);
1125 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001126 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001127 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001128 goto enospc;
1129 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001130 if (wret == 1) {
1131 wret = push_node_left(trans, root, left, mid, 1);
1132 if (wret < 0)
1133 ret = wret;
1134 }
Chris Mason79f95c82007-03-01 15:16:26 -05001135 BUG_ON(wret == 1);
1136 }
Chris Mason5f39d392007-10-15 16:14:19 -04001137 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001138 /* we've managed to empty the middle node, drop it */
Chris Masondb945352007-10-15 16:15:53 -04001139 u64 bytenr = mid->start;
1140 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001141
Chris Mason5f39d392007-10-15 16:14:19 -04001142 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001143 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001144 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001145 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001146 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001147 if (wret)
1148 ret = wret;
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00001149 wret = btrfs_free_tree_block(trans, root, bytenr, blocksize,
1150 0, root->root_key.objectid, level);
Chris Masonbb803952007-03-01 12:04:21 -05001151 if (wret)
1152 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001153 } else {
1154 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001155 struct btrfs_disk_key mid_key;
1156 btrfs_node_key(mid, &mid_key, 0);
1157 btrfs_set_node_key(parent, &mid_key, pslot);
1158 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001159 }
Chris Masonbb803952007-03-01 12:04:21 -05001160
Chris Mason79f95c82007-03-01 15:16:26 -05001161 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001162 if (left) {
1163 if (btrfs_header_nritems(left) > orig_slot) {
1164 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001165 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001166 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001167 path->slots[level + 1] -= 1;
1168 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001169 if (mid) {
1170 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001171 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001172 }
Chris Masonbb803952007-03-01 12:04:21 -05001173 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001174 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001175 path->slots[level] = orig_slot;
1176 }
1177 }
Chris Mason79f95c82007-03-01 15:16:26 -05001178 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001179 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001180 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001181 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001182 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001183enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001184 if (right) {
1185 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001186 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001187 }
1188 if (left) {
1189 if (path->nodes[level] != left)
1190 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001191 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001192 }
Chris Masonbb803952007-03-01 12:04:21 -05001193 return ret;
1194}
1195
Chris Masond352ac62008-09-29 15:18:18 -04001196/* Node balancing for insertion. Here we only split or push nodes around
1197 * when they are completely full. This is also done top down, so we
1198 * have to be pessimistic.
1199 */
Chris Masond3977122009-01-05 21:25:51 -05001200static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001201 struct btrfs_root *root,
1202 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001203{
Chris Mason5f39d392007-10-15 16:14:19 -04001204 struct extent_buffer *right = NULL;
1205 struct extent_buffer *mid;
1206 struct extent_buffer *left = NULL;
1207 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001208 int ret = 0;
1209 int wret;
1210 int pslot;
1211 int orig_slot = path->slots[level];
1212 u64 orig_ptr;
1213
1214 if (level == 0)
1215 return 1;
1216
Chris Mason5f39d392007-10-15 16:14:19 -04001217 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001218 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001219 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1220
1221 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001222 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001223 pslot = path->slots[level + 1];
1224
Chris Mason5f39d392007-10-15 16:14:19 -04001225 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001226 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001227
Chris Mason5f39d392007-10-15 16:14:19 -04001228 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001229
1230 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001231 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001232 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001233
1234 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001235 btrfs_set_lock_blocking(left);
1236
Chris Mason5f39d392007-10-15 16:14:19 -04001237 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001238 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1239 wret = 1;
1240 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001241 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001242 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001243 if (ret)
1244 wret = 1;
1245 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001246 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001247 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001248 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001249 }
Chris Masone66f7092007-04-20 13:16:02 -04001250 if (wret < 0)
1251 ret = wret;
1252 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001253 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001254 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001255 btrfs_node_key(mid, &disk_key, 0);
1256 btrfs_set_node_key(parent, &disk_key, pslot);
1257 btrfs_mark_buffer_dirty(parent);
1258 if (btrfs_header_nritems(left) > orig_slot) {
1259 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001260 path->slots[level + 1] -= 1;
1261 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001262 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001263 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001264 } else {
1265 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001266 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001267 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001268 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001269 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001270 }
Chris Masone66f7092007-04-20 13:16:02 -04001271 return 0;
1272 }
Chris Mason925baed2008-06-25 16:01:30 -04001273 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001274 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001275 }
Chris Mason925baed2008-06-25 16:01:30 -04001276 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001277
1278 /*
1279 * then try to empty the right most buffer into the middle
1280 */
Chris Mason5f39d392007-10-15 16:14:19 -04001281 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001282 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001283
Chris Mason925baed2008-06-25 16:01:30 -04001284 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001285 btrfs_set_lock_blocking(right);
1286
Chris Mason5f39d392007-10-15 16:14:19 -04001287 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001288 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1289 wret = 1;
1290 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001291 ret = btrfs_cow_block(trans, root, right,
1292 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001293 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001294 if (ret)
1295 wret = 1;
1296 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001297 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001298 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001299 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001300 }
Chris Masone66f7092007-04-20 13:16:02 -04001301 if (wret < 0)
1302 ret = wret;
1303 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001304 struct btrfs_disk_key disk_key;
1305
1306 btrfs_node_key(right, &disk_key, 0);
1307 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1308 btrfs_mark_buffer_dirty(parent);
1309
1310 if (btrfs_header_nritems(mid) <= orig_slot) {
1311 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001312 path->slots[level + 1] += 1;
1313 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001314 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001315 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001316 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001317 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001318 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001319 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001320 }
Chris Masone66f7092007-04-20 13:16:02 -04001321 return 0;
1322 }
Chris Mason925baed2008-06-25 16:01:30 -04001323 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001324 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001325 }
Chris Masone66f7092007-04-20 13:16:02 -04001326 return 1;
1327}
1328
Chris Mason74123bd2007-02-02 11:05:29 -05001329/*
Chris Masond352ac62008-09-29 15:18:18 -04001330 * readahead one full node of leaves, finding things that are close
1331 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001332 */
Chris Masonc8c42862009-04-03 10:14:18 -04001333static void reada_for_search(struct btrfs_root *root,
1334 struct btrfs_path *path,
1335 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001336{
Chris Mason5f39d392007-10-15 16:14:19 -04001337 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001338 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001339 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001340 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001341 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001342 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001343 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001344 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001345 u32 nr;
1346 u32 blocksize;
1347 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001348
Chris Masona6b6e752007-10-15 16:22:39 -04001349 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001350 return;
1351
Chris Mason6702ed42007-08-07 16:15:09 -04001352 if (!path->nodes[level])
1353 return;
1354
Chris Mason5f39d392007-10-15 16:14:19 -04001355 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001356
Chris Mason3c69fae2007-08-07 15:52:22 -04001357 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001358 blocksize = btrfs_level_size(root, level - 1);
1359 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001360 if (eb) {
1361 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001362 return;
1363 }
1364
Chris Masona7175312009-01-22 09:23:10 -05001365 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001366
Chris Mason5f39d392007-10-15 16:14:19 -04001367 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001368 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001369 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001370 if (direction < 0) {
1371 if (nr == 0)
1372 break;
1373 nr--;
1374 } else if (direction > 0) {
1375 nr++;
1376 if (nr >= nritems)
1377 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001378 }
Chris Mason01f46652007-12-21 16:24:26 -05001379 if (path->reada < 0 && objectid) {
1380 btrfs_node_key(node, &disk_key, nr);
1381 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1382 break;
1383 }
Chris Mason6b800532007-10-15 16:17:34 -04001384 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001385 if ((search <= target && target - search <= 65536) ||
1386 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001387 readahead_tree_block(root, search, blocksize,
1388 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001389 nread += blocksize;
1390 }
1391 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001392 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001393 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001394 }
1395}
Chris Mason925baed2008-06-25 16:01:30 -04001396
Chris Masond352ac62008-09-29 15:18:18 -04001397/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001398 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1399 * cache
1400 */
1401static noinline int reada_for_balance(struct btrfs_root *root,
1402 struct btrfs_path *path, int level)
1403{
1404 int slot;
1405 int nritems;
1406 struct extent_buffer *parent;
1407 struct extent_buffer *eb;
1408 u64 gen;
1409 u64 block1 = 0;
1410 u64 block2 = 0;
1411 int ret = 0;
1412 int blocksize;
1413
Chris Mason8c594ea2009-04-20 15:50:10 -04001414 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001415 if (!parent)
1416 return 0;
1417
1418 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001419 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001420 blocksize = btrfs_level_size(root, level);
1421
1422 if (slot > 0) {
1423 block1 = btrfs_node_blockptr(parent, slot - 1);
1424 gen = btrfs_node_ptr_generation(parent, slot - 1);
1425 eb = btrfs_find_tree_block(root, block1, blocksize);
1426 if (eb && btrfs_buffer_uptodate(eb, gen))
1427 block1 = 0;
1428 free_extent_buffer(eb);
1429 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001430 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001431 block2 = btrfs_node_blockptr(parent, slot + 1);
1432 gen = btrfs_node_ptr_generation(parent, slot + 1);
1433 eb = btrfs_find_tree_block(root, block2, blocksize);
1434 if (eb && btrfs_buffer_uptodate(eb, gen))
1435 block2 = 0;
1436 free_extent_buffer(eb);
1437 }
1438 if (block1 || block2) {
1439 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001440
1441 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001442 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001443
1444 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001445 if (block1)
1446 readahead_tree_block(root, block1, blocksize, 0);
1447 if (block2)
1448 readahead_tree_block(root, block2, blocksize, 0);
1449
1450 if (block1) {
1451 eb = read_tree_block(root, block1, blocksize, 0);
1452 free_extent_buffer(eb);
1453 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001454 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001455 eb = read_tree_block(root, block2, blocksize, 0);
1456 free_extent_buffer(eb);
1457 }
1458 }
1459 return ret;
1460}
1461
1462
1463/*
Chris Masond3977122009-01-05 21:25:51 -05001464 * when we walk down the tree, it is usually safe to unlock the higher layers
1465 * in the tree. The exceptions are when our path goes through slot 0, because
1466 * operations on the tree might require changing key pointers higher up in the
1467 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001468 *
Chris Masond3977122009-01-05 21:25:51 -05001469 * callers might also have set path->keep_locks, which tells this code to keep
1470 * the lock if the path points to the last slot in the block. This is part of
1471 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001472 *
Chris Masond3977122009-01-05 21:25:51 -05001473 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1474 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001475 */
Chris Masone02119d2008-09-05 16:13:11 -04001476static noinline void unlock_up(struct btrfs_path *path, int level,
1477 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001478{
1479 int i;
1480 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001481 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001482 struct extent_buffer *t;
1483
1484 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1485 if (!path->nodes[i])
1486 break;
1487 if (!path->locks[i])
1488 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001489 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001490 skip_level = i + 1;
1491 continue;
1492 }
Chris Mason051e1b92008-06-25 16:01:30 -04001493 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001494 u32 nritems;
1495 t = path->nodes[i];
1496 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001497 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001498 skip_level = i + 1;
1499 continue;
1500 }
1501 }
Chris Mason051e1b92008-06-25 16:01:30 -04001502 if (skip_level < i && i >= lowest_unlock)
1503 no_skips = 1;
1504
Chris Mason925baed2008-06-25 16:01:30 -04001505 t = path->nodes[i];
1506 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1507 btrfs_tree_unlock(t);
1508 path->locks[i] = 0;
1509 }
1510 }
1511}
1512
Chris Mason3c69fae2007-08-07 15:52:22 -04001513/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001514 * This releases any locks held in the path starting at level and
1515 * going all the way up to the root.
1516 *
1517 * btrfs_search_slot will keep the lock held on higher nodes in a few
1518 * corner cases, such as COW of the block at slot zero in the node. This
1519 * ignores those rules, and it should only be called when there are no
1520 * more updates to be done higher up in the tree.
1521 */
1522noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1523{
1524 int i;
1525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001526 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001527 return;
1528
1529 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1530 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001531 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001532 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001533 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001534 btrfs_tree_unlock(path->nodes[i]);
1535 path->locks[i] = 0;
1536 }
1537}
1538
1539/*
Chris Masonc8c42862009-04-03 10:14:18 -04001540 * helper function for btrfs_search_slot. The goal is to find a block
1541 * in cache without setting the path to blocking. If we find the block
1542 * we return zero and the path is unchanged.
1543 *
1544 * If we can't find the block, we set the path blocking and do some
1545 * reada. -EAGAIN is returned and the search must be repeated.
1546 */
1547static int
1548read_block_for_search(struct btrfs_trans_handle *trans,
1549 struct btrfs_root *root, struct btrfs_path *p,
1550 struct extent_buffer **eb_ret, int level, int slot,
1551 struct btrfs_key *key)
1552{
1553 u64 blocknr;
1554 u64 gen;
1555 u32 blocksize;
1556 struct extent_buffer *b = *eb_ret;
1557 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001558 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001559
1560 blocknr = btrfs_node_blockptr(b, slot);
1561 gen = btrfs_node_ptr_generation(b, slot);
1562 blocksize = btrfs_level_size(root, level - 1);
1563
1564 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1565 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason76a05b32009-05-14 13:24:30 -04001566 /*
1567 * we found an up to date block without sleeping, return
1568 * right away
1569 */
Chris Masonc8c42862009-04-03 10:14:18 -04001570 *eb_ret = tmp;
1571 return 0;
1572 }
1573
1574 /*
1575 * reduce lock contention at high levels
1576 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001577 * we read. Don't release the lock on the current
1578 * level because we need to walk this node to figure
1579 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001580 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001581 btrfs_unlock_up_safe(p, level + 1);
1582 btrfs_set_path_blocking(p);
1583
Chris Masonc8c42862009-04-03 10:14:18 -04001584 if (tmp)
1585 free_extent_buffer(tmp);
1586 if (p->reada)
1587 reada_for_search(root, p, level, slot, key->objectid);
1588
Chris Mason8c594ea2009-04-20 15:50:10 -04001589 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001590
1591 ret = -EAGAIN;
Chris Masonc8c42862009-04-03 10:14:18 -04001592 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Mason76a05b32009-05-14 13:24:30 -04001593 if (tmp) {
1594 /*
1595 * If the read above didn't mark this buffer up to date,
1596 * it will never end up being up to date. Set ret to EIO now
1597 * and give up so that our caller doesn't loop forever
1598 * on our EAGAINs.
1599 */
1600 if (!btrfs_buffer_uptodate(tmp, 0))
1601 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001602 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001603 }
1604 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001605}
1606
1607/*
1608 * helper function for btrfs_search_slot. This does all of the checks
1609 * for node-level blocks and does any balancing required based on
1610 * the ins_len.
1611 *
1612 * If no extra work was required, zero is returned. If we had to
1613 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1614 * start over
1615 */
1616static int
1617setup_nodes_for_search(struct btrfs_trans_handle *trans,
1618 struct btrfs_root *root, struct btrfs_path *p,
1619 struct extent_buffer *b, int level, int ins_len)
1620{
1621 int ret;
1622 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1623 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1624 int sret;
1625
1626 sret = reada_for_balance(root, p, level);
1627 if (sret)
1628 goto again;
1629
1630 btrfs_set_path_blocking(p);
1631 sret = split_node(trans, root, p, level);
1632 btrfs_clear_path_blocking(p, NULL);
1633
1634 BUG_ON(sret > 0);
1635 if (sret) {
1636 ret = sret;
1637 goto done;
1638 }
1639 b = p->nodes[level];
1640 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001641 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001642 int sret;
1643
1644 sret = reada_for_balance(root, p, level);
1645 if (sret)
1646 goto again;
1647
1648 btrfs_set_path_blocking(p);
1649 sret = balance_level(trans, root, p, level);
1650 btrfs_clear_path_blocking(p, NULL);
1651
1652 if (sret) {
1653 ret = sret;
1654 goto done;
1655 }
1656 b = p->nodes[level];
1657 if (!b) {
1658 btrfs_release_path(NULL, p);
1659 goto again;
1660 }
1661 BUG_ON(btrfs_header_nritems(b) == 1);
1662 }
1663 return 0;
1664
1665again:
1666 ret = -EAGAIN;
1667done:
1668 return ret;
1669}
1670
1671/*
Chris Mason74123bd2007-02-02 11:05:29 -05001672 * look for key in the tree. path is filled in with nodes along the way
1673 * if key is found, we return zero and you can find the item in the leaf
1674 * level of the path (level 0)
1675 *
1676 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001677 * be inserted, and 1 is returned. If there are other errors during the
1678 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001679 *
1680 * if ins_len > 0, nodes and leaves will be split as we walk down the
1681 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1682 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001683 */
Chris Masone089f052007-03-16 16:20:31 -04001684int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1685 *root, struct btrfs_key *key, struct btrfs_path *p, int
1686 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001687{
Chris Mason5f39d392007-10-15 16:14:19 -04001688 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001689 int slot;
1690 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001691 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001692 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001693 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001694 u8 lowest_level = 0;
1695
Chris Mason6702ed42007-08-07 16:15:09 -04001696 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001697 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001698 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001699
Chris Mason925baed2008-06-25 16:01:30 -04001700 if (ins_len < 0)
1701 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001702
Chris Masonbb803952007-03-01 12:04:21 -05001703again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001704 if (p->search_commit_root) {
1705 b = root->commit_root;
1706 extent_buffer_get(b);
1707 if (!p->skip_locking)
1708 btrfs_tree_lock(b);
1709 } else {
1710 if (p->skip_locking)
1711 b = btrfs_root_node(root);
1712 else
1713 b = btrfs_lock_root_node(root);
1714 }
Chris Mason925baed2008-06-25 16:01:30 -04001715
Chris Masoneb60cea2007-02-02 09:18:22 -05001716 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001717 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001718
1719 /*
1720 * setup the path here so we can release it under lock
1721 * contention with the cow code
1722 */
1723 p->nodes[level] = b;
1724 if (!p->skip_locking)
1725 p->locks[level] = 1;
1726
Chris Mason02217ed2007-03-02 16:08:05 -05001727 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001728 /*
1729 * if we don't really need to cow this block
1730 * then we don't want to set the path blocking,
1731 * so we test it here
1732 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001733 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001734 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001735
Chris Masonb4ce94d2009-02-04 09:25:08 -05001736 btrfs_set_path_blocking(p);
1737
Yan Zheng33c66f42009-07-22 09:59:00 -04001738 err = btrfs_cow_block(trans, root, b,
1739 p->nodes[level + 1],
1740 p->slots[level + 1], &b);
1741 if (err) {
Chris Mason5f39d392007-10-15 16:14:19 -04001742 free_extent_buffer(b);
Yan Zheng33c66f42009-07-22 09:59:00 -04001743 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001744 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001745 }
Chris Mason02217ed2007-03-02 16:08:05 -05001746 }
Chris Mason65b51a02008-08-01 15:11:20 -04001747cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001748 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001749 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001750 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001751 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001752
Chris Masoneb60cea2007-02-02 09:18:22 -05001753 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001754 if (!p->skip_locking)
1755 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001756
Chris Mason4008c042009-02-12 14:09:45 -05001757 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001758
1759 /*
1760 * we have a lock on b and as long as we aren't changing
1761 * the tree, there is no way to for the items in b to change.
1762 * It is safe to drop the lock on our parent before we
1763 * go through the expensive btree search on b.
1764 *
1765 * If cow is true, then we might be changing slot zero,
1766 * which may require changing the parent. So, we can't
1767 * drop the lock until after we know which slot we're
1768 * operating on.
1769 */
1770 if (!cow)
1771 btrfs_unlock_up_safe(p, level + 1);
1772
Chris Mason123abc82007-03-14 14:14:43 -04001773 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001774 if (ret) {
1775 ret = -1;
1776 goto done;
1777 }
Chris Mason925baed2008-06-25 16:01:30 -04001778
Chris Mason5f39d392007-10-15 16:14:19 -04001779 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001780
Chris Mason5f39d392007-10-15 16:14:19 -04001781 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001782 int dec = 0;
1783 if (ret && slot > 0) {
1784 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001785 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001786 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001787 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001788 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001789 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001790 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001791 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001792 if (err) {
1793 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001794 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001795 }
Chris Masonc8c42862009-04-03 10:14:18 -04001796 b = p->nodes[level];
1797 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001798
Chris Masonf9efa9c2008-06-25 16:14:04 -04001799 unlock_up(p, level, lowest_unlock);
1800
Chris Mason925baed2008-06-25 16:01:30 -04001801 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001802 if (dec)
1803 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001804 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001805 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001806
Yan Zheng33c66f42009-07-22 09:59:00 -04001807 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001808 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001809 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001810 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001811 if (err) {
1812 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001813 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001814 }
Chris Mason76a05b32009-05-14 13:24:30 -04001815
Chris Masonb4ce94d2009-02-04 09:25:08 -05001816 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001817 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001818 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001819
Yan Zheng33c66f42009-07-22 09:59:00 -04001820 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001821 btrfs_set_path_blocking(p);
1822 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001823 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001824 }
1825 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001826 } else {
1827 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001828 if (ins_len > 0 &&
1829 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001830 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001831 err = split_leaf(trans, root, key,
1832 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001833 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001834
Yan Zheng33c66f42009-07-22 09:59:00 -04001835 BUG_ON(err > 0);
1836 if (err) {
1837 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001838 goto done;
1839 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001840 }
Chris Mason459931e2008-12-10 09:10:46 -05001841 if (!p->search_for_split)
1842 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001843 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001844 }
1845 }
Chris Mason65b51a02008-08-01 15:11:20 -04001846 ret = 1;
1847done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001848 /*
1849 * we don't really know what they plan on doing with the path
1850 * from here on, so for now just mark it as blocking
1851 */
Chris Masonb9473432009-03-13 11:00:37 -04001852 if (!p->leave_spinning)
1853 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001854 if (ret < 0)
1855 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001856 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001857}
1858
Chris Mason74123bd2007-02-02 11:05:29 -05001859/*
1860 * adjust the pointers going up the tree, starting at level
1861 * making sure the right key of each node is points to 'key'.
1862 * This is used after shifting pointers to the left, so it stops
1863 * fixing up pointers when a given leaf/node is not in slot 0 of the
1864 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001865 *
1866 * If this fails to write a tree block, it returns -1, but continues
1867 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001868 */
Chris Mason5f39d392007-10-15 16:14:19 -04001869static int fixup_low_keys(struct btrfs_trans_handle *trans,
1870 struct btrfs_root *root, struct btrfs_path *path,
1871 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001872{
1873 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001874 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001875 struct extent_buffer *t;
1876
Chris Mason234b63a2007-03-13 10:46:10 -04001877 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001878 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001879 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001880 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001881 t = path->nodes[i];
1882 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001883 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001884 if (tslot != 0)
1885 break;
1886 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001887 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001888}
1889
Chris Mason74123bd2007-02-02 11:05:29 -05001890/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001891 * update item key.
1892 *
1893 * This function isn't completely safe. It's the caller's responsibility
1894 * that the new key won't break the order
1895 */
1896int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1897 struct btrfs_root *root, struct btrfs_path *path,
1898 struct btrfs_key *new_key)
1899{
1900 struct btrfs_disk_key disk_key;
1901 struct extent_buffer *eb;
1902 int slot;
1903
1904 eb = path->nodes[0];
1905 slot = path->slots[0];
1906 if (slot > 0) {
1907 btrfs_item_key(eb, &disk_key, slot - 1);
1908 if (comp_keys(&disk_key, new_key) >= 0)
1909 return -1;
1910 }
1911 if (slot < btrfs_header_nritems(eb) - 1) {
1912 btrfs_item_key(eb, &disk_key, slot + 1);
1913 if (comp_keys(&disk_key, new_key) <= 0)
1914 return -1;
1915 }
1916
1917 btrfs_cpu_key_to_disk(&disk_key, new_key);
1918 btrfs_set_item_key(eb, &disk_key, slot);
1919 btrfs_mark_buffer_dirty(eb);
1920 if (slot == 0)
1921 fixup_low_keys(trans, root, path, &disk_key, 1);
1922 return 0;
1923}
1924
1925/*
Chris Mason74123bd2007-02-02 11:05:29 -05001926 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001927 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001928 *
1929 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1930 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001931 */
Chris Mason98ed5172008-01-03 10:01:48 -05001932static int push_node_left(struct btrfs_trans_handle *trans,
1933 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001934 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001935{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001936 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001937 int src_nritems;
1938 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001939 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001940
Chris Mason5f39d392007-10-15 16:14:19 -04001941 src_nritems = btrfs_header_nritems(src);
1942 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001943 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001944 WARN_ON(btrfs_header_generation(src) != trans->transid);
1945 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001946
Chris Masonbce4eae2008-04-24 14:42:46 -04001947 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001948 return 1;
1949
Chris Masond3977122009-01-05 21:25:51 -05001950 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001951 return 1;
1952
Chris Masonbce4eae2008-04-24 14:42:46 -04001953 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001954 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001955 if (push_items < src_nritems) {
1956 /* leave at least 8 pointers in the node if
1957 * we aren't going to empty it
1958 */
1959 if (src_nritems - push_items < 8) {
1960 if (push_items <= 8)
1961 return 1;
1962 push_items -= 8;
1963 }
1964 }
1965 } else
1966 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001967
Chris Mason5f39d392007-10-15 16:14:19 -04001968 copy_extent_buffer(dst, src,
1969 btrfs_node_key_ptr_offset(dst_nritems),
1970 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001971 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001972
Chris Masonbb803952007-03-01 12:04:21 -05001973 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001974 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1975 btrfs_node_key_ptr_offset(push_items),
1976 (src_nritems - push_items) *
1977 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001978 }
Chris Mason5f39d392007-10-15 16:14:19 -04001979 btrfs_set_header_nritems(src, src_nritems - push_items);
1980 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1981 btrfs_mark_buffer_dirty(src);
1982 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001983
Chris Masonbb803952007-03-01 12:04:21 -05001984 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001985}
1986
Chris Mason97571fd2007-02-24 13:39:08 -05001987/*
Chris Mason79f95c82007-03-01 15:16:26 -05001988 * try to push data from one node into the next node right in the
1989 * tree.
1990 *
1991 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1992 * error, and > 0 if there was no room in the right hand block.
1993 *
1994 * this will only push up to 1/2 the contents of the left node over
1995 */
Chris Mason5f39d392007-10-15 16:14:19 -04001996static int balance_node_right(struct btrfs_trans_handle *trans,
1997 struct btrfs_root *root,
1998 struct extent_buffer *dst,
1999 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002000{
Chris Mason79f95c82007-03-01 15:16:26 -05002001 int push_items = 0;
2002 int max_push;
2003 int src_nritems;
2004 int dst_nritems;
2005 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002006
Chris Mason7bb86312007-12-11 09:25:06 -05002007 WARN_ON(btrfs_header_generation(src) != trans->transid);
2008 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2009
Chris Mason5f39d392007-10-15 16:14:19 -04002010 src_nritems = btrfs_header_nritems(src);
2011 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002012 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002013 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002014 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002015
Chris Masond3977122009-01-05 21:25:51 -05002016 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002017 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002018
2019 max_push = src_nritems / 2 + 1;
2020 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002021 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002022 return 1;
Yan252c38f2007-08-29 09:11:44 -04002023
Chris Mason79f95c82007-03-01 15:16:26 -05002024 if (max_push < push_items)
2025 push_items = max_push;
2026
Chris Mason5f39d392007-10-15 16:14:19 -04002027 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2028 btrfs_node_key_ptr_offset(0),
2029 (dst_nritems) *
2030 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002031
Chris Mason5f39d392007-10-15 16:14:19 -04002032 copy_extent_buffer(dst, src,
2033 btrfs_node_key_ptr_offset(0),
2034 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002035 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002036
Chris Mason5f39d392007-10-15 16:14:19 -04002037 btrfs_set_header_nritems(src, src_nritems - push_items);
2038 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002039
Chris Mason5f39d392007-10-15 16:14:19 -04002040 btrfs_mark_buffer_dirty(src);
2041 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002042
Chris Mason79f95c82007-03-01 15:16:26 -05002043 return ret;
2044}
2045
2046/*
Chris Mason97571fd2007-02-24 13:39:08 -05002047 * helper function to insert a new root level in the tree.
2048 * A new node is allocated, and a single item is inserted to
2049 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002050 *
2051 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002052 */
Chris Masond3977122009-01-05 21:25:51 -05002053static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002054 struct btrfs_root *root,
2055 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002056{
Chris Mason7bb86312007-12-11 09:25:06 -05002057 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002058 struct extent_buffer *lower;
2059 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002060 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002061 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002062
2063 BUG_ON(path->nodes[level]);
2064 BUG_ON(path->nodes[level-1] != root->node);
2065
Chris Mason7bb86312007-12-11 09:25:06 -05002066 lower = path->nodes[level-1];
2067 if (level == 1)
2068 btrfs_item_key(lower, &lower_key, 0);
2069 else
2070 btrfs_node_key(lower, &lower_key, 0);
2071
Zheng Yan31840ae2008-09-23 13:14:14 -04002072 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002073 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002074 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002075 if (IS_ERR(c))
2076 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002077
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002078 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002079 btrfs_set_header_nritems(c, 1);
2080 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002081 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002082 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002083 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002084 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002085
Chris Mason5f39d392007-10-15 16:14:19 -04002086 write_extent_buffer(c, root->fs_info->fsid,
2087 (unsigned long)btrfs_header_fsid(c),
2088 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002089
2090 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2091 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2092 BTRFS_UUID_SIZE);
2093
Chris Mason5f39d392007-10-15 16:14:19 -04002094 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002095 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002096 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002097 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002098
2099 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002100
2101 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002102
Chris Mason925baed2008-06-25 16:01:30 -04002103 spin_lock(&root->node_lock);
2104 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002105 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002106 spin_unlock(&root->node_lock);
2107
2108 /* the super has an extra ref to root->node */
2109 free_extent_buffer(old);
2110
Chris Mason0b86a832008-03-24 15:01:56 -04002111 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002112 extent_buffer_get(c);
2113 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002114 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002115 path->slots[level] = 0;
2116 return 0;
2117}
2118
Chris Mason74123bd2007-02-02 11:05:29 -05002119/*
2120 * worker function to insert a single pointer in a node.
2121 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002122 *
Chris Mason74123bd2007-02-02 11:05:29 -05002123 * slot and level indicate where you want the key to go, and
2124 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002125 *
2126 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002127 */
Chris Masone089f052007-03-16 16:20:31 -04002128static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2129 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002130 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002131{
Chris Mason5f39d392007-10-15 16:14:19 -04002132 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002133 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002134
2135 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002136 lower = path->nodes[level];
2137 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002138 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002139 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002140 BUG();
2141 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002142 memmove_extent_buffer(lower,
2143 btrfs_node_key_ptr_offset(slot + 1),
2144 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002145 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002146 }
Chris Mason5f39d392007-10-15 16:14:19 -04002147 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002148 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002149 WARN_ON(trans->transid == 0);
2150 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002151 btrfs_set_header_nritems(lower, nritems + 1);
2152 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002153 return 0;
2154}
2155
Chris Mason97571fd2007-02-24 13:39:08 -05002156/*
2157 * split the node at the specified level in path in two.
2158 * The path is corrected to point to the appropriate node after the split
2159 *
2160 * Before splitting this tries to make some room in the node by pushing
2161 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002162 *
2163 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002164 */
Chris Masone02119d2008-09-05 16:13:11 -04002165static noinline int split_node(struct btrfs_trans_handle *trans,
2166 struct btrfs_root *root,
2167 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168{
Chris Mason5f39d392007-10-15 16:14:19 -04002169 struct extent_buffer *c;
2170 struct extent_buffer *split;
2171 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002172 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002173 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002174 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002175 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002176
Chris Mason5f39d392007-10-15 16:14:19 -04002177 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002178 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002179 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002180 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002181 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002182 if (ret)
2183 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002184 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002185 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002186 c = path->nodes[level];
2187 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002188 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002189 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002190 if (ret < 0)
2191 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002192 }
Chris Masone66f7092007-04-20 13:16:02 -04002193
Chris Mason5f39d392007-10-15 16:14:19 -04002194 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 mid = (c_nritems + 1) / 2;
2196 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002197
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002199 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002200 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002201 if (IS_ERR(split))
2202 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002203
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002204 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002205 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002206 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002207 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002208 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002209 btrfs_set_header_owner(split, root->root_key.objectid);
2210 write_extent_buffer(split, root->fs_info->fsid,
2211 (unsigned long)btrfs_header_fsid(split),
2212 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002213 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2214 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2215 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002216
Chris Mason5f39d392007-10-15 16:14:19 -04002217
2218 copy_extent_buffer(split, c,
2219 btrfs_node_key_ptr_offset(0),
2220 btrfs_node_key_ptr_offset(mid),
2221 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2222 btrfs_set_header_nritems(split, c_nritems - mid);
2223 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002224 ret = 0;
2225
Chris Mason5f39d392007-10-15 16:14:19 -04002226 btrfs_mark_buffer_dirty(c);
2227 btrfs_mark_buffer_dirty(split);
2228
Chris Masondb945352007-10-15 16:15:53 -04002229 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002230 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002231 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002232 if (wret)
2233 ret = wret;
2234
Chris Mason5de08d72007-02-24 06:24:44 -05002235 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002236 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002237 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002238 free_extent_buffer(c);
2239 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002240 path->slots[level + 1] += 1;
2241 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002242 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002243 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002244 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002245 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002246}
2247
Chris Mason74123bd2007-02-02 11:05:29 -05002248/*
2249 * how many bytes are required to store the items in a leaf. start
2250 * and nr indicate which items in the leaf to check. This totals up the
2251 * space used both by the item structs and the item data
2252 */
Chris Mason5f39d392007-10-15 16:14:19 -04002253static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002254{
2255 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002256 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002257 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002258
2259 if (!nr)
2260 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002261 data_len = btrfs_item_end_nr(l, start);
2262 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002263 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002264 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002265 return data_len;
2266}
2267
Chris Mason74123bd2007-02-02 11:05:29 -05002268/*
Chris Masond4dbff92007-04-04 14:08:15 -04002269 * The space between the end of the leaf items and
2270 * the start of the leaf data. IOW, how much room
2271 * the leaf has left for both items and data
2272 */
Chris Masond3977122009-01-05 21:25:51 -05002273noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002274 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002275{
Chris Mason5f39d392007-10-15 16:14:19 -04002276 int nritems = btrfs_header_nritems(leaf);
2277 int ret;
2278 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2279 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002280 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2281 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002282 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002283 leaf_space_used(leaf, 0, nritems), nritems);
2284 }
2285 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002286}
2287
Chris Mason44871b12009-03-13 10:04:31 -04002288static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2289 struct btrfs_root *root,
2290 struct btrfs_path *path,
2291 int data_size, int empty,
2292 struct extent_buffer *right,
2293 int free_space, u32 left_nritems)
Chris Mason00ec4c52007-02-24 12:47:20 -05002294{
Chris Mason5f39d392007-10-15 16:14:19 -04002295 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002296 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002297 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002298 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002299 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002300 int push_space = 0;
2301 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002302 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002303 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002304 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002305 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002306 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002307
Chris Mason34a38212007-11-07 13:31:03 -05002308 if (empty)
2309 nr = 0;
2310 else
2311 nr = 1;
2312
Zheng Yan31840ae2008-09-23 13:14:14 -04002313 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002314 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002315
Chris Mason44871b12009-03-13 10:04:31 -04002316 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002317 i = left_nritems - 1;
2318 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002319 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002320
Zheng Yan31840ae2008-09-23 13:14:14 -04002321 if (!empty && push_items > 0) {
2322 if (path->slots[0] > i)
2323 break;
2324 if (path->slots[0] == i) {
2325 int space = btrfs_leaf_free_space(root, left);
2326 if (space + push_space * 2 > free_space)
2327 break;
2328 }
2329 }
2330
Chris Mason00ec4c52007-02-24 12:47:20 -05002331 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002332 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002333
2334 if (!left->map_token) {
2335 map_extent_buffer(left, (unsigned long)item,
2336 sizeof(struct btrfs_item),
2337 &left->map_token, &left->kaddr,
2338 &left->map_start, &left->map_len,
2339 KM_USER1);
2340 }
2341
2342 this_item_size = btrfs_item_size(left, item);
2343 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002344 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002345
Chris Mason00ec4c52007-02-24 12:47:20 -05002346 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002347 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002348 if (i == 0)
2349 break;
2350 i--;
Chris Masondb945352007-10-15 16:15:53 -04002351 }
2352 if (left->map_token) {
2353 unmap_extent_buffer(left, left->map_token, KM_USER1);
2354 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002355 }
Chris Mason5f39d392007-10-15 16:14:19 -04002356
Chris Mason925baed2008-06-25 16:01:30 -04002357 if (push_items == 0)
2358 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002359
Chris Mason34a38212007-11-07 13:31:03 -05002360 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002361 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002362
Chris Mason00ec4c52007-02-24 12:47:20 -05002363 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002364 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002365
Chris Mason5f39d392007-10-15 16:14:19 -04002366 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002367 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002368
Chris Mason00ec4c52007-02-24 12:47:20 -05002369 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002370 data_end = leaf_data_end(root, right);
2371 memmove_extent_buffer(right,
2372 btrfs_leaf_data(right) + data_end - push_space,
2373 btrfs_leaf_data(right) + data_end,
2374 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2375
Chris Mason00ec4c52007-02-24 12:47:20 -05002376 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002377 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002378 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2379 btrfs_leaf_data(left) + leaf_data_end(root, left),
2380 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002381
2382 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2383 btrfs_item_nr_offset(0),
2384 right_nritems * sizeof(struct btrfs_item));
2385
Chris Mason00ec4c52007-02-24 12:47:20 -05002386 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002387 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2388 btrfs_item_nr_offset(left_nritems - push_items),
2389 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002390
2391 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002392 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002393 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002394 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002395 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002396 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002397 if (!right->map_token) {
2398 map_extent_buffer(right, (unsigned long)item,
2399 sizeof(struct btrfs_item),
2400 &right->map_token, &right->kaddr,
2401 &right->map_start, &right->map_len,
2402 KM_USER1);
2403 }
2404 push_space -= btrfs_item_size(right, item);
2405 btrfs_set_item_offset(right, item, push_space);
2406 }
2407
2408 if (right->map_token) {
2409 unmap_extent_buffer(right, right->map_token, KM_USER1);
2410 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002411 }
Chris Mason7518a232007-03-12 12:01:18 -04002412 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002413 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002414
Chris Mason34a38212007-11-07 13:31:03 -05002415 if (left_nritems)
2416 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002417 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002418
Chris Mason5f39d392007-10-15 16:14:19 -04002419 btrfs_item_key(right, &disk_key, 0);
2420 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002421 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002422
Chris Mason00ec4c52007-02-24 12:47:20 -05002423 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002424 if (path->slots[0] >= left_nritems) {
2425 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002426 if (btrfs_header_nritems(path->nodes[0]) == 0)
2427 clean_tree_block(trans, root, path->nodes[0]);
2428 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002429 free_extent_buffer(path->nodes[0]);
2430 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002431 path->slots[1] += 1;
2432 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002433 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002434 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002435 }
2436 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002437
2438out_unlock:
2439 btrfs_tree_unlock(right);
2440 free_extent_buffer(right);
2441 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002442}
Chris Mason925baed2008-06-25 16:01:30 -04002443
Chris Mason00ec4c52007-02-24 12:47:20 -05002444/*
Chris Mason44871b12009-03-13 10:04:31 -04002445 * push some data in the path leaf to the right, trying to free up at
2446 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2447 *
2448 * returns 1 if the push failed because the other node didn't have enough
2449 * room, 0 if everything worked out and < 0 if there were major errors.
2450 */
2451static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2452 *root, struct btrfs_path *path, int data_size,
2453 int empty)
2454{
2455 struct extent_buffer *left = path->nodes[0];
2456 struct extent_buffer *right;
2457 struct extent_buffer *upper;
2458 int slot;
2459 int free_space;
2460 u32 left_nritems;
2461 int ret;
2462
2463 if (!path->nodes[1])
2464 return 1;
2465
2466 slot = path->slots[1];
2467 upper = path->nodes[1];
2468 if (slot >= btrfs_header_nritems(upper) - 1)
2469 return 1;
2470
2471 btrfs_assert_tree_locked(path->nodes[1]);
2472
2473 right = read_node_slot(root, upper, slot + 1);
2474 btrfs_tree_lock(right);
2475 btrfs_set_lock_blocking(right);
2476
2477 free_space = btrfs_leaf_free_space(root, right);
2478 if (free_space < data_size)
2479 goto out_unlock;
2480
2481 /* cow and double check */
2482 ret = btrfs_cow_block(trans, root, right, upper,
2483 slot + 1, &right);
2484 if (ret)
2485 goto out_unlock;
2486
2487 free_space = btrfs_leaf_free_space(root, right);
2488 if (free_space < data_size)
2489 goto out_unlock;
2490
2491 left_nritems = btrfs_header_nritems(left);
2492 if (left_nritems == 0)
2493 goto out_unlock;
2494
2495 return __push_leaf_right(trans, root, path, data_size, empty,
2496 right, free_space, left_nritems);
2497out_unlock:
2498 btrfs_tree_unlock(right);
2499 free_extent_buffer(right);
2500 return 1;
2501}
2502
2503/*
Chris Mason74123bd2007-02-02 11:05:29 -05002504 * push some data in the path leaf to the left, trying to free up at
2505 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2506 */
Chris Mason44871b12009-03-13 10:04:31 -04002507static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2508 struct btrfs_root *root,
2509 struct btrfs_path *path, int data_size,
2510 int empty, struct extent_buffer *left,
2511 int free_space, int right_nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002512{
Chris Mason5f39d392007-10-15 16:14:19 -04002513 struct btrfs_disk_key disk_key;
2514 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515 int slot;
2516 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002517 int push_space = 0;
2518 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002519 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002520 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002521 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002522 int ret = 0;
2523 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002524 u32 this_item_size;
2525 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002526
2527 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002528
Chris Mason34a38212007-11-07 13:31:03 -05002529 if (empty)
2530 nr = right_nritems;
2531 else
2532 nr = right_nritems - 1;
2533
2534 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002535 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002536 if (!right->map_token) {
2537 map_extent_buffer(right, (unsigned long)item,
2538 sizeof(struct btrfs_item),
2539 &right->map_token, &right->kaddr,
2540 &right->map_start, &right->map_len,
2541 KM_USER1);
2542 }
2543
Zheng Yan31840ae2008-09-23 13:14:14 -04002544 if (!empty && push_items > 0) {
2545 if (path->slots[0] < i)
2546 break;
2547 if (path->slots[0] == i) {
2548 int space = btrfs_leaf_free_space(root, right);
2549 if (space + push_space * 2 > free_space)
2550 break;
2551 }
2552 }
2553
Chris Masonbe0e5c02007-01-26 15:51:26 -05002554 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002555 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002556
2557 this_item_size = btrfs_item_size(right, item);
2558 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559 break;
Chris Masondb945352007-10-15 16:15:53 -04002560
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002562 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 }
Chris Masondb945352007-10-15 16:15:53 -04002564
2565 if (right->map_token) {
2566 unmap_extent_buffer(right, right->map_token, KM_USER1);
2567 right->map_token = NULL;
2568 }
2569
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002571 ret = 1;
2572 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573 }
Chris Mason34a38212007-11-07 13:31:03 -05002574 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002575 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002576
Chris Masonbe0e5c02007-01-26 15:51:26 -05002577 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002578 copy_extent_buffer(left, right,
2579 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2580 btrfs_item_nr_offset(0),
2581 push_items * sizeof(struct btrfs_item));
2582
Chris Mason123abc82007-03-14 14:14:43 -04002583 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002584 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002585
2586 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002587 leaf_data_end(root, left) - push_space,
2588 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002589 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002590 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002591 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002592 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002593
Chris Masondb945352007-10-15 16:15:53 -04002594 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002595 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002596 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002597
Chris Mason5f39d392007-10-15 16:14:19 -04002598 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002599 if (!left->map_token) {
2600 map_extent_buffer(left, (unsigned long)item,
2601 sizeof(struct btrfs_item),
2602 &left->map_token, &left->kaddr,
2603 &left->map_start, &left->map_len,
2604 KM_USER1);
2605 }
2606
Chris Mason5f39d392007-10-15 16:14:19 -04002607 ioff = btrfs_item_offset(left, item);
2608 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002609 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610 }
Chris Mason5f39d392007-10-15 16:14:19 -04002611 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002612 if (left->map_token) {
2613 unmap_extent_buffer(left, left->map_token, KM_USER1);
2614 left->map_token = NULL;
2615 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616
2617 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002618 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002619 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2620 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002621 WARN_ON(1);
2622 }
Chris Mason5f39d392007-10-15 16:14:19 -04002623
Chris Mason34a38212007-11-07 13:31:03 -05002624 if (push_items < right_nritems) {
2625 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2626 leaf_data_end(root, right);
2627 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2628 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2629 btrfs_leaf_data(right) +
2630 leaf_data_end(root, right), push_space);
2631
2632 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002633 btrfs_item_nr_offset(push_items),
2634 (btrfs_header_nritems(right) - push_items) *
2635 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002636 }
Yaneef1c492007-11-26 10:58:13 -05002637 right_nritems -= push_items;
2638 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002639 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002640 for (i = 0; i < right_nritems; i++) {
2641 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002642
2643 if (!right->map_token) {
2644 map_extent_buffer(right, (unsigned long)item,
2645 sizeof(struct btrfs_item),
2646 &right->map_token, &right->kaddr,
2647 &right->map_start, &right->map_len,
2648 KM_USER1);
2649 }
2650
2651 push_space = push_space - btrfs_item_size(right, item);
2652 btrfs_set_item_offset(right, item, push_space);
2653 }
2654 if (right->map_token) {
2655 unmap_extent_buffer(right, right->map_token, KM_USER1);
2656 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002657 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002658
Chris Mason5f39d392007-10-15 16:14:19 -04002659 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002660 if (right_nritems)
2661 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002662
Chris Mason5f39d392007-10-15 16:14:19 -04002663 btrfs_item_key(right, &disk_key, 0);
2664 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002665 if (wret)
2666 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002667
2668 /* then fixup the leaf pointer in the path */
2669 if (path->slots[0] < push_items) {
2670 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002671 if (btrfs_header_nritems(path->nodes[0]) == 0)
2672 clean_tree_block(trans, root, path->nodes[0]);
2673 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002674 free_extent_buffer(path->nodes[0]);
2675 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002676 path->slots[1] -= 1;
2677 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002678 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002679 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002680 path->slots[0] -= push_items;
2681 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002682 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002683 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002684out:
2685 btrfs_tree_unlock(left);
2686 free_extent_buffer(left);
2687 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002688}
2689
Chris Mason74123bd2007-02-02 11:05:29 -05002690/*
Chris Mason44871b12009-03-13 10:04:31 -04002691 * push some data in the path leaf to the left, trying to free up at
2692 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2693 */
2694static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2695 *root, struct btrfs_path *path, int data_size,
2696 int empty)
2697{
2698 struct extent_buffer *right = path->nodes[0];
2699 struct extent_buffer *left;
2700 int slot;
2701 int free_space;
2702 u32 right_nritems;
2703 int ret = 0;
2704
2705 slot = path->slots[1];
2706 if (slot == 0)
2707 return 1;
2708 if (!path->nodes[1])
2709 return 1;
2710
2711 right_nritems = btrfs_header_nritems(right);
2712 if (right_nritems == 0)
2713 return 1;
2714
2715 btrfs_assert_tree_locked(path->nodes[1]);
2716
2717 left = read_node_slot(root, path->nodes[1], slot - 1);
2718 btrfs_tree_lock(left);
2719 btrfs_set_lock_blocking(left);
2720
2721 free_space = btrfs_leaf_free_space(root, left);
2722 if (free_space < data_size) {
2723 ret = 1;
2724 goto out;
2725 }
2726
2727 /* cow and double check */
2728 ret = btrfs_cow_block(trans, root, left,
2729 path->nodes[1], slot - 1, &left);
2730 if (ret) {
2731 /* we hit -ENOSPC, but it isn't fatal here */
2732 ret = 1;
2733 goto out;
2734 }
2735
2736 free_space = btrfs_leaf_free_space(root, left);
2737 if (free_space < data_size) {
2738 ret = 1;
2739 goto out;
2740 }
2741
2742 return __push_leaf_left(trans, root, path, data_size,
2743 empty, left, free_space, right_nritems);
2744out:
2745 btrfs_tree_unlock(left);
2746 free_extent_buffer(left);
2747 return ret;
2748}
2749
2750/*
Chris Mason74123bd2007-02-02 11:05:29 -05002751 * split the path's leaf in two, making sure there is at least data_size
2752 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002753 *
2754 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002755 */
Chris Mason44871b12009-03-13 10:04:31 -04002756static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002757 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002758 struct btrfs_path *path,
2759 struct extent_buffer *l,
2760 struct extent_buffer *right,
2761 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002762{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002763 int data_copy_size;
2764 int rt_data_off;
2765 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002766 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002767 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002768 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002769
Chris Mason5f39d392007-10-15 16:14:19 -04002770 nritems = nritems - mid;
2771 btrfs_set_header_nritems(right, nritems);
2772 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2773
2774 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2775 btrfs_item_nr_offset(mid),
2776 nritems * sizeof(struct btrfs_item));
2777
2778 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002779 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2780 data_copy_size, btrfs_leaf_data(l) +
2781 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002782
Chris Mason5f39d392007-10-15 16:14:19 -04002783 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2784 btrfs_item_end_nr(l, mid);
2785
2786 for (i = 0; i < nritems; i++) {
2787 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002788 u32 ioff;
2789
2790 if (!right->map_token) {
2791 map_extent_buffer(right, (unsigned long)item,
2792 sizeof(struct btrfs_item),
2793 &right->map_token, &right->kaddr,
2794 &right->map_start, &right->map_len,
2795 KM_USER1);
2796 }
2797
2798 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002799 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002800 }
Chris Mason74123bd2007-02-02 11:05:29 -05002801
Chris Masondb945352007-10-15 16:15:53 -04002802 if (right->map_token) {
2803 unmap_extent_buffer(right, right->map_token, KM_USER1);
2804 right->map_token = NULL;
2805 }
2806
Chris Mason5f39d392007-10-15 16:14:19 -04002807 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002808 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002809 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002810 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2811 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002812 if (wret)
2813 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002814
2815 btrfs_mark_buffer_dirty(right);
2816 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002817 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002818
Chris Masonbe0e5c02007-01-26 15:51:26 -05002819 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002820 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002821 free_extent_buffer(path->nodes[0]);
2822 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002823 path->slots[0] -= mid;
2824 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002825 } else {
2826 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002827 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002828 }
Chris Mason5f39d392007-10-15 16:14:19 -04002829
Chris Masoneb60cea2007-02-02 09:18:22 -05002830 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002831
Chris Mason44871b12009-03-13 10:04:31 -04002832 return ret;
2833}
2834
2835/*
2836 * 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;
2857
Yan, Zhenga5719522009-09-24 09:17:31 -04002858 l = path->nodes[0];
2859 slot = path->slots[0];
2860 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2861 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2862 return -EOVERFLOW;
2863
Chris Mason44871b12009-03-13 10:04:31 -04002864 /* first try to make some room by pushing left and right */
Chris Masonb3612422009-05-13 19:12:15 -04002865 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason44871b12009-03-13 10:04:31 -04002866 wret = push_leaf_right(trans, root, path, data_size, 0);
2867 if (wret < 0)
2868 return wret;
2869 if (wret) {
2870 wret = push_leaf_left(trans, root, path, data_size, 0);
2871 if (wret < 0)
2872 return wret;
2873 }
2874 l = path->nodes[0];
2875
2876 /* did the pushes work? */
2877 if (btrfs_leaf_free_space(root, l) >= data_size)
2878 return 0;
2879 }
2880
2881 if (!path->nodes[1]) {
2882 ret = insert_new_root(trans, root, path, 1);
2883 if (ret)
2884 return ret;
2885 }
2886again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002887 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002888 l = path->nodes[0];
2889 slot = path->slots[0];
2890 nritems = btrfs_header_nritems(l);
2891 mid = (nritems + 1) / 2;
2892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002893 if (mid <= slot) {
2894 if (nritems == 1 ||
2895 leaf_space_used(l, mid, nritems - mid) + data_size >
2896 BTRFS_LEAF_DATA_SIZE(root)) {
2897 if (slot >= nritems) {
2898 split = 0;
2899 } else {
2900 mid = slot;
2901 if (mid != nritems &&
2902 leaf_space_used(l, mid, nritems - mid) +
2903 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2904 split = 2;
2905 }
2906 }
2907 }
2908 } else {
2909 if (leaf_space_used(l, 0, mid) + data_size >
2910 BTRFS_LEAF_DATA_SIZE(root)) {
2911 if (!extend && data_size && slot == 0) {
2912 split = 0;
2913 } else if ((extend || !data_size) && slot == 0) {
2914 mid = 1;
2915 } else {
2916 mid = slot;
2917 if (mid != nritems &&
2918 leaf_space_used(l, mid, nritems - mid) +
2919 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2920 split = 2 ;
2921 }
2922 }
2923 }
2924 }
2925
2926 if (split == 0)
2927 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2928 else
2929 btrfs_item_key(l, &disk_key, mid);
2930
2931 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002932 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002933 &disk_key, 0, l->start, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002934 if (IS_ERR(right)) {
2935 BUG_ON(1);
2936 return PTR_ERR(right);
2937 }
2938
2939 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2940 btrfs_set_header_bytenr(right, right->start);
2941 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002942 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002943 btrfs_set_header_owner(right, root->root_key.objectid);
2944 btrfs_set_header_level(right, 0);
2945 write_extent_buffer(right, root->fs_info->fsid,
2946 (unsigned long)btrfs_header_fsid(right),
2947 BTRFS_FSID_SIZE);
2948
2949 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2950 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2951 BTRFS_UUID_SIZE);
2952
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002953 if (split == 0) {
2954 if (mid <= slot) {
2955 btrfs_set_header_nritems(right, 0);
2956 wret = insert_ptr(trans, root, path,
2957 &disk_key, right->start,
2958 path->slots[1] + 1, 1);
2959 if (wret)
2960 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002962 btrfs_tree_unlock(path->nodes[0]);
2963 free_extent_buffer(path->nodes[0]);
2964 path->nodes[0] = right;
2965 path->slots[0] = 0;
2966 path->slots[1] += 1;
2967 } else {
2968 btrfs_set_header_nritems(right, 0);
2969 wret = insert_ptr(trans, root, path,
2970 &disk_key,
2971 right->start,
2972 path->slots[1], 1);
2973 if (wret)
2974 ret = wret;
2975 btrfs_tree_unlock(path->nodes[0]);
2976 free_extent_buffer(path->nodes[0]);
2977 path->nodes[0] = right;
2978 path->slots[0] = 0;
2979 if (path->slots[1] == 0) {
2980 wret = fixup_low_keys(trans, root,
2981 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002982 if (wret)
2983 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002984 }
2985 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002986 btrfs_mark_buffer_dirty(right);
2987 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002988 }
2989
2990 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2991 BUG_ON(ret);
2992
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002993 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002994 BUG_ON(num_doubles != 0);
2995 num_doubles++;
2996 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002997 }
Chris Mason44871b12009-03-13 10:04:31 -04002998
Chris Masonbe0e5c02007-01-26 15:51:26 -05002999 return ret;
3000}
3001
Yan, Zhengad48fd752009-11-12 09:33:58 +00003002static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3003 struct btrfs_root *root,
3004 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003005{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003006 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003007 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003008 struct btrfs_file_extent_item *fi;
3009 u64 extent_len = 0;
3010 u32 item_size;
3011 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003012
3013 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003014 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3015
3016 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3017 key.type != BTRFS_EXTENT_CSUM_KEY);
3018
3019 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3020 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003021
3022 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003023 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3024 fi = btrfs_item_ptr(leaf, path->slots[0],
3025 struct btrfs_file_extent_item);
3026 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3027 }
Chris Mason459931e2008-12-10 09:10:46 -05003028 btrfs_release_path(root, path);
3029
Chris Mason459931e2008-12-10 09:10:46 -05003030 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003031 path->search_for_split = 1;
3032 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003033 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003034 if (ret < 0)
3035 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003036
Yan, Zhengad48fd752009-11-12 09:33:58 +00003037 ret = -EAGAIN;
3038 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003039 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003040 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3041 goto err;
3042
Chris Mason109f6ae2010-04-02 09:20:18 -04003043 /* the leaf has changed, it now has room. return now */
3044 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3045 goto err;
3046
Yan, Zhengad48fd752009-11-12 09:33:58 +00003047 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3048 fi = btrfs_item_ptr(leaf, path->slots[0],
3049 struct btrfs_file_extent_item);
3050 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3051 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003052 }
3053
Chris Masonb9473432009-03-13 11:00:37 -04003054 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003055 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003056 BUG_ON(ret);
3057
Yan, Zhengad48fd752009-11-12 09:33:58 +00003058 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003059 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003060 return 0;
3061err:
3062 path->keep_locks = 0;
3063 return ret;
3064}
3065
3066static noinline int split_item(struct btrfs_trans_handle *trans,
3067 struct btrfs_root *root,
3068 struct btrfs_path *path,
3069 struct btrfs_key *new_key,
3070 unsigned long split_offset)
3071{
3072 struct extent_buffer *leaf;
3073 struct btrfs_item *item;
3074 struct btrfs_item *new_item;
3075 int slot;
3076 char *buf;
3077 u32 nritems;
3078 u32 item_size;
3079 u32 orig_offset;
3080 struct btrfs_disk_key disk_key;
3081
Chris Masonb9473432009-03-13 11:00:37 -04003082 leaf = path->nodes[0];
3083 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3084
Chris Masonb4ce94d2009-02-04 09:25:08 -05003085 btrfs_set_path_blocking(path);
3086
Chris Mason459931e2008-12-10 09:10:46 -05003087 item = btrfs_item_nr(leaf, path->slots[0]);
3088 orig_offset = btrfs_item_offset(leaf, item);
3089 item_size = btrfs_item_size(leaf, item);
3090
Chris Mason459931e2008-12-10 09:10:46 -05003091 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003092 if (!buf)
3093 return -ENOMEM;
3094
Chris Mason459931e2008-12-10 09:10:46 -05003095 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3096 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003097
Chris Mason459931e2008-12-10 09:10:46 -05003098 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003099 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003100 if (slot != nritems) {
3101 /* shift the items */
3102 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003103 btrfs_item_nr_offset(slot),
3104 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003105 }
3106
3107 btrfs_cpu_key_to_disk(&disk_key, new_key);
3108 btrfs_set_item_key(leaf, &disk_key, slot);
3109
3110 new_item = btrfs_item_nr(leaf, slot);
3111
3112 btrfs_set_item_offset(leaf, new_item, orig_offset);
3113 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3114
3115 btrfs_set_item_offset(leaf, item,
3116 orig_offset + item_size - split_offset);
3117 btrfs_set_item_size(leaf, item, split_offset);
3118
3119 btrfs_set_header_nritems(leaf, nritems + 1);
3120
3121 /* write the data for the start of the original item */
3122 write_extent_buffer(leaf, buf,
3123 btrfs_item_ptr_offset(leaf, path->slots[0]),
3124 split_offset);
3125
3126 /* write the data for the new item */
3127 write_extent_buffer(leaf, buf + split_offset,
3128 btrfs_item_ptr_offset(leaf, slot),
3129 item_size - split_offset);
3130 btrfs_mark_buffer_dirty(leaf);
3131
Yan, Zhengad48fd752009-11-12 09:33:58 +00003132 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003133 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003134 return 0;
3135}
3136
3137/*
3138 * This function splits a single item into two items,
3139 * giving 'new_key' to the new item and splitting the
3140 * old one at split_offset (from the start of the item).
3141 *
3142 * The path may be released by this operation. After
3143 * the split, the path is pointing to the old item. The
3144 * new item is going to be in the same node as the old one.
3145 *
3146 * Note, the item being split must be smaller enough to live alone on
3147 * a tree block with room for one extra struct btrfs_item
3148 *
3149 * This allows us to split the item in place, keeping a lock on the
3150 * leaf the entire time.
3151 */
3152int btrfs_split_item(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *root,
3154 struct btrfs_path *path,
3155 struct btrfs_key *new_key,
3156 unsigned long split_offset)
3157{
3158 int ret;
3159 ret = setup_leaf_for_split(trans, root, path,
3160 sizeof(struct btrfs_item));
3161 if (ret)
3162 return ret;
3163
3164 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003165 return ret;
3166}
3167
3168/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003169 * This function duplicate a item, giving 'new_key' to the new item.
3170 * It guarantees both items live in the same tree leaf and the new item
3171 * is contiguous with the original item.
3172 *
3173 * This allows us to split file extent in place, keeping a lock on the
3174 * leaf the entire time.
3175 */
3176int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3177 struct btrfs_root *root,
3178 struct btrfs_path *path,
3179 struct btrfs_key *new_key)
3180{
3181 struct extent_buffer *leaf;
3182 int ret;
3183 u32 item_size;
3184
3185 leaf = path->nodes[0];
3186 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3187 ret = setup_leaf_for_split(trans, root, path,
3188 item_size + sizeof(struct btrfs_item));
3189 if (ret)
3190 return ret;
3191
3192 path->slots[0]++;
3193 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3194 item_size, item_size +
3195 sizeof(struct btrfs_item), 1);
3196 BUG_ON(ret);
3197
3198 leaf = path->nodes[0];
3199 memcpy_extent_buffer(leaf,
3200 btrfs_item_ptr_offset(leaf, path->slots[0]),
3201 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3202 item_size);
3203 return 0;
3204}
3205
3206/*
Chris Masond352ac62008-09-29 15:18:18 -04003207 * make the item pointed to by the path smaller. new_size indicates
3208 * how small to make it, and from_end tells us if we just chop bytes
3209 * off the end of the item or if we shift the item to chop bytes off
3210 * the front.
3211 */
Chris Masonb18c6682007-04-17 13:26:50 -04003212int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3213 struct btrfs_root *root,
3214 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003215 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003216{
3217 int ret = 0;
3218 int slot;
3219 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003220 struct extent_buffer *leaf;
3221 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003222 u32 nritems;
3223 unsigned int data_end;
3224 unsigned int old_data_start;
3225 unsigned int old_size;
3226 unsigned int size_diff;
3227 int i;
3228
3229 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003230 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003231 slot = path->slots[0];
3232
3233 old_size = btrfs_item_size_nr(leaf, slot);
3234 if (old_size == new_size)
3235 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003236
Chris Mason5f39d392007-10-15 16:14:19 -04003237 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003238 data_end = leaf_data_end(root, leaf);
3239
Chris Mason5f39d392007-10-15 16:14:19 -04003240 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003241
Chris Masonb18c6682007-04-17 13:26:50 -04003242 size_diff = old_size - new_size;
3243
3244 BUG_ON(slot < 0);
3245 BUG_ON(slot >= nritems);
3246
3247 /*
3248 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3249 */
3250 /* first correct the data pointers */
3251 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003252 u32 ioff;
3253 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003254
3255 if (!leaf->map_token) {
3256 map_extent_buffer(leaf, (unsigned long)item,
3257 sizeof(struct btrfs_item),
3258 &leaf->map_token, &leaf->kaddr,
3259 &leaf->map_start, &leaf->map_len,
3260 KM_USER1);
3261 }
3262
Chris Mason5f39d392007-10-15 16:14:19 -04003263 ioff = btrfs_item_offset(leaf, item);
3264 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003265 }
Chris Masondb945352007-10-15 16:15:53 -04003266
3267 if (leaf->map_token) {
3268 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3269 leaf->map_token = NULL;
3270 }
3271
Chris Masonb18c6682007-04-17 13:26:50 -04003272 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003273 if (from_end) {
3274 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3275 data_end + size_diff, btrfs_leaf_data(leaf) +
3276 data_end, old_data_start + new_size - data_end);
3277 } else {
3278 struct btrfs_disk_key disk_key;
3279 u64 offset;
3280
3281 btrfs_item_key(leaf, &disk_key, slot);
3282
3283 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3284 unsigned long ptr;
3285 struct btrfs_file_extent_item *fi;
3286
3287 fi = btrfs_item_ptr(leaf, slot,
3288 struct btrfs_file_extent_item);
3289 fi = (struct btrfs_file_extent_item *)(
3290 (unsigned long)fi - size_diff);
3291
3292 if (btrfs_file_extent_type(leaf, fi) ==
3293 BTRFS_FILE_EXTENT_INLINE) {
3294 ptr = btrfs_item_ptr_offset(leaf, slot);
3295 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003296 (unsigned long)fi,
3297 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003298 disk_bytenr));
3299 }
3300 }
3301
3302 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3303 data_end + size_diff, btrfs_leaf_data(leaf) +
3304 data_end, old_data_start - data_end);
3305
3306 offset = btrfs_disk_key_offset(&disk_key);
3307 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3308 btrfs_set_item_key(leaf, &disk_key, slot);
3309 if (slot == 0)
3310 fixup_low_keys(trans, root, path, &disk_key, 1);
3311 }
Chris Mason5f39d392007-10-15 16:14:19 -04003312
3313 item = btrfs_item_nr(leaf, slot);
3314 btrfs_set_item_size(leaf, item, new_size);
3315 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003316
3317 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003318 if (btrfs_leaf_free_space(root, leaf) < 0) {
3319 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003320 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003321 }
Chris Masonb18c6682007-04-17 13:26:50 -04003322 return ret;
3323}
3324
Chris Masond352ac62008-09-29 15:18:18 -04003325/*
3326 * make the item pointed to by the path bigger, data_size is the new size.
3327 */
Chris Mason5f39d392007-10-15 16:14:19 -04003328int btrfs_extend_item(struct btrfs_trans_handle *trans,
3329 struct btrfs_root *root, struct btrfs_path *path,
3330 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003331{
3332 int ret = 0;
3333 int slot;
3334 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003335 struct extent_buffer *leaf;
3336 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003337 u32 nritems;
3338 unsigned int data_end;
3339 unsigned int old_data;
3340 unsigned int old_size;
3341 int i;
3342
3343 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003344 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003345
Chris Mason5f39d392007-10-15 16:14:19 -04003346 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003347 data_end = leaf_data_end(root, leaf);
3348
Chris Mason5f39d392007-10-15 16:14:19 -04003349 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3350 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003351 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003352 }
Chris Mason6567e832007-04-16 09:22:45 -04003353 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003354 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003355
3356 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003357 if (slot >= nritems) {
3358 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003359 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3360 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003361 BUG_ON(1);
3362 }
Chris Mason6567e832007-04-16 09:22:45 -04003363
3364 /*
3365 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3366 */
3367 /* first correct the data pointers */
3368 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003369 u32 ioff;
3370 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003371
3372 if (!leaf->map_token) {
3373 map_extent_buffer(leaf, (unsigned long)item,
3374 sizeof(struct btrfs_item),
3375 &leaf->map_token, &leaf->kaddr,
3376 &leaf->map_start, &leaf->map_len,
3377 KM_USER1);
3378 }
Chris Mason5f39d392007-10-15 16:14:19 -04003379 ioff = btrfs_item_offset(leaf, item);
3380 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003381 }
Chris Mason5f39d392007-10-15 16:14:19 -04003382
Chris Masondb945352007-10-15 16:15:53 -04003383 if (leaf->map_token) {
3384 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3385 leaf->map_token = NULL;
3386 }
3387
Chris Mason6567e832007-04-16 09:22:45 -04003388 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003389 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003390 data_end - data_size, btrfs_leaf_data(leaf) +
3391 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003392
Chris Mason6567e832007-04-16 09:22:45 -04003393 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003394 old_size = btrfs_item_size_nr(leaf, slot);
3395 item = btrfs_item_nr(leaf, slot);
3396 btrfs_set_item_size(leaf, item, old_size + data_size);
3397 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003398
3399 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003400 if (btrfs_leaf_free_space(root, leaf) < 0) {
3401 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003402 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003403 }
Chris Mason6567e832007-04-16 09:22:45 -04003404 return ret;
3405}
3406
Chris Mason74123bd2007-02-02 11:05:29 -05003407/*
Chris Masond352ac62008-09-29 15:18:18 -04003408 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003409 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003410 * Returns the number of keys that were inserted.
3411 */
3412int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3413 struct btrfs_root *root,
3414 struct btrfs_path *path,
3415 struct btrfs_key *cpu_key, u32 *data_size,
3416 int nr)
3417{
3418 struct extent_buffer *leaf;
3419 struct btrfs_item *item;
3420 int ret = 0;
3421 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003422 int i;
3423 u32 nritems;
3424 u32 total_data = 0;
3425 u32 total_size = 0;
3426 unsigned int data_end;
3427 struct btrfs_disk_key disk_key;
3428 struct btrfs_key found_key;
3429
Yan Zheng87b29b22008-12-17 10:21:48 -05003430 for (i = 0; i < nr; i++) {
3431 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3432 BTRFS_LEAF_DATA_SIZE(root)) {
3433 break;
3434 nr = i;
3435 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003436 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003437 total_size += data_size[i] + sizeof(struct btrfs_item);
3438 }
3439 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003440
Josef Bacikf3465ca2008-11-12 14:19:50 -05003441 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3442 if (ret == 0)
3443 return -EEXIST;
3444 if (ret < 0)
3445 goto out;
3446
Josef Bacikf3465ca2008-11-12 14:19:50 -05003447 leaf = path->nodes[0];
3448
3449 nritems = btrfs_header_nritems(leaf);
3450 data_end = leaf_data_end(root, leaf);
3451
3452 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3453 for (i = nr; i >= 0; i--) {
3454 total_data -= data_size[i];
3455 total_size -= data_size[i] + sizeof(struct btrfs_item);
3456 if (total_size < btrfs_leaf_free_space(root, leaf))
3457 break;
3458 }
3459 nr = i;
3460 }
3461
3462 slot = path->slots[0];
3463 BUG_ON(slot < 0);
3464
3465 if (slot != nritems) {
3466 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3467
3468 item = btrfs_item_nr(leaf, slot);
3469 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3470
3471 /* figure out how many keys we can insert in here */
3472 total_data = data_size[0];
3473 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003474 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003475 break;
3476 total_data += data_size[i];
3477 }
3478 nr = i;
3479
3480 if (old_data < data_end) {
3481 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003482 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003483 slot, old_data, data_end);
3484 BUG_ON(1);
3485 }
3486 /*
3487 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3488 */
3489 /* first correct the data pointers */
3490 WARN_ON(leaf->map_token);
3491 for (i = slot; i < nritems; i++) {
3492 u32 ioff;
3493
3494 item = btrfs_item_nr(leaf, i);
3495 if (!leaf->map_token) {
3496 map_extent_buffer(leaf, (unsigned long)item,
3497 sizeof(struct btrfs_item),
3498 &leaf->map_token, &leaf->kaddr,
3499 &leaf->map_start, &leaf->map_len,
3500 KM_USER1);
3501 }
3502
3503 ioff = btrfs_item_offset(leaf, item);
3504 btrfs_set_item_offset(leaf, item, ioff - total_data);
3505 }
3506 if (leaf->map_token) {
3507 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3508 leaf->map_token = NULL;
3509 }
3510
3511 /* shift the items */
3512 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3513 btrfs_item_nr_offset(slot),
3514 (nritems - slot) * sizeof(struct btrfs_item));
3515
3516 /* shift the data */
3517 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3518 data_end - total_data, btrfs_leaf_data(leaf) +
3519 data_end, old_data - data_end);
3520 data_end = old_data;
3521 } else {
3522 /*
3523 * this sucks but it has to be done, if we are inserting at
3524 * the end of the leaf only insert 1 of the items, since we
3525 * have no way of knowing whats on the next leaf and we'd have
3526 * to drop our current locks to figure it out
3527 */
3528 nr = 1;
3529 }
3530
3531 /* setup the item for the new data */
3532 for (i = 0; i < nr; i++) {
3533 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3534 btrfs_set_item_key(leaf, &disk_key, slot + i);
3535 item = btrfs_item_nr(leaf, slot + i);
3536 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3537 data_end -= data_size[i];
3538 btrfs_set_item_size(leaf, item, data_size[i]);
3539 }
3540 btrfs_set_header_nritems(leaf, nritems + nr);
3541 btrfs_mark_buffer_dirty(leaf);
3542
3543 ret = 0;
3544 if (slot == 0) {
3545 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3546 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3547 }
3548
3549 if (btrfs_leaf_free_space(root, leaf) < 0) {
3550 btrfs_print_leaf(root, leaf);
3551 BUG();
3552 }
3553out:
3554 if (!ret)
3555 ret = nr;
3556 return ret;
3557}
3558
3559/*
Chris Mason44871b12009-03-13 10:04:31 -04003560 * this is a helper for btrfs_insert_empty_items, the main goal here is
3561 * to save stack depth by doing the bulk of the work in a function
3562 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003563 */
Chris Mason44871b12009-03-13 10:04:31 -04003564static noinline_for_stack int
3565setup_items_for_insert(struct btrfs_trans_handle *trans,
3566 struct btrfs_root *root, struct btrfs_path *path,
3567 struct btrfs_key *cpu_key, u32 *data_size,
3568 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003569{
Chris Mason5f39d392007-10-15 16:14:19 -04003570 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003571 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003572 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003573 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003574 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003575 int ret;
3576 struct extent_buffer *leaf;
3577 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003578
Chris Mason5f39d392007-10-15 16:14:19 -04003579 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003580 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003581
Chris Mason5f39d392007-10-15 16:14:19 -04003582 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003583 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003584
Chris Masonf25956c2008-09-12 15:32:53 -04003585 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003586 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003587 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003588 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003589 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003590 }
Chris Mason5f39d392007-10-15 16:14:19 -04003591
Chris Masonbe0e5c02007-01-26 15:51:26 -05003592 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003593 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003594
Chris Mason5f39d392007-10-15 16:14:19 -04003595 if (old_data < data_end) {
3596 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003597 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003598 slot, old_data, data_end);
3599 BUG_ON(1);
3600 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003601 /*
3602 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3603 */
3604 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003605 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003606 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003607 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003608
Chris Mason5f39d392007-10-15 16:14:19 -04003609 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003610 if (!leaf->map_token) {
3611 map_extent_buffer(leaf, (unsigned long)item,
3612 sizeof(struct btrfs_item),
3613 &leaf->map_token, &leaf->kaddr,
3614 &leaf->map_start, &leaf->map_len,
3615 KM_USER1);
3616 }
3617
Chris Mason5f39d392007-10-15 16:14:19 -04003618 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003619 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003620 }
Chris Masondb945352007-10-15 16:15:53 -04003621 if (leaf->map_token) {
3622 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3623 leaf->map_token = NULL;
3624 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003625
3626 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003627 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003628 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003629 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003630
3631 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003632 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003633 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003634 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003635 data_end = old_data;
3636 }
Chris Mason5f39d392007-10-15 16:14:19 -04003637
Chris Mason62e27492007-03-15 12:56:47 -04003638 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003639 for (i = 0; i < nr; i++) {
3640 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3641 btrfs_set_item_key(leaf, &disk_key, slot + i);
3642 item = btrfs_item_nr(leaf, slot + i);
3643 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3644 data_end -= data_size[i];
3645 btrfs_set_item_size(leaf, item, data_size[i]);
3646 }
Chris Mason44871b12009-03-13 10:04:31 -04003647
Chris Mason9c583092008-01-29 15:15:18 -05003648 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003649
3650 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003651 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003652 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003653 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003654 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003655 }
Chris Masonb9473432009-03-13 11:00:37 -04003656 btrfs_unlock_up_safe(path, 1);
3657 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003658
Chris Mason5f39d392007-10-15 16:14:19 -04003659 if (btrfs_leaf_free_space(root, leaf) < 0) {
3660 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003661 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003662 }
Chris Mason44871b12009-03-13 10:04:31 -04003663 return ret;
3664}
3665
3666/*
3667 * Given a key and some data, insert items into the tree.
3668 * This does all the path init required, making room in the tree if needed.
3669 */
3670int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3671 struct btrfs_root *root,
3672 struct btrfs_path *path,
3673 struct btrfs_key *cpu_key, u32 *data_size,
3674 int nr)
3675{
3676 struct extent_buffer *leaf;
3677 int ret = 0;
3678 int slot;
3679 int i;
3680 u32 total_size = 0;
3681 u32 total_data = 0;
3682
3683 for (i = 0; i < nr; i++)
3684 total_data += data_size[i];
3685
3686 total_size = total_data + (nr * sizeof(struct btrfs_item));
3687 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3688 if (ret == 0)
3689 return -EEXIST;
3690 if (ret < 0)
3691 goto out;
3692
3693 leaf = path->nodes[0];
3694 slot = path->slots[0];
3695 BUG_ON(slot < 0);
3696
3697 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3698 total_data, total_size, nr);
3699
Chris Masoned2ff2c2007-03-01 18:59:40 -05003700out:
Chris Mason62e27492007-03-15 12:56:47 -04003701 return ret;
3702}
3703
3704/*
3705 * Given a key and some data, insert an item into the tree.
3706 * This does all the path init required, making room in the tree if needed.
3707 */
Chris Masone089f052007-03-16 16:20:31 -04003708int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3709 *root, struct btrfs_key *cpu_key, void *data, u32
3710 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003711{
3712 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003713 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003714 struct extent_buffer *leaf;
3715 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003716
Chris Mason2c90e5d2007-04-02 10:50:19 -04003717 path = btrfs_alloc_path();
3718 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003719 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003720 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003721 leaf = path->nodes[0];
3722 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3723 write_extent_buffer(leaf, data, ptr, data_size);
3724 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003725 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003726 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003727 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003728}
3729
Chris Mason74123bd2007-02-02 11:05:29 -05003730/*
Chris Mason5de08d72007-02-24 06:24:44 -05003731 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003732 *
Chris Masond352ac62008-09-29 15:18:18 -04003733 * the tree should have been previously balanced so the deletion does not
3734 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003735 */
Chris Masone089f052007-03-16 16:20:31 -04003736static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3737 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003738{
Chris Mason5f39d392007-10-15 16:14:19 -04003739 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003740 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003741 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003742 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003743
Chris Mason5f39d392007-10-15 16:14:19 -04003744 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003745 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003746 memmove_extent_buffer(parent,
3747 btrfs_node_key_ptr_offset(slot),
3748 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003749 sizeof(struct btrfs_key_ptr) *
3750 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003751 }
Chris Mason7518a232007-03-12 12:01:18 -04003752 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003753 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003754 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003755 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003756 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003757 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003758 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003759 struct btrfs_disk_key disk_key;
3760
3761 btrfs_node_key(parent, &disk_key, 0);
3762 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003763 if (wret)
3764 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003765 }
Chris Masond6025572007-03-30 14:27:56 -04003766 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003767 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003768}
3769
Chris Mason74123bd2007-02-02 11:05:29 -05003770/*
Chris Mason323ac952008-10-01 19:05:46 -04003771 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003772 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003773 *
3774 * This deletes the pointer in path->nodes[1] and frees the leaf
3775 * block extent. zero is returned if it all worked out, < 0 otherwise.
3776 *
3777 * The path must have already been setup for deleting the leaf, including
3778 * all the proper balancing. path->nodes[1] must be locked.
3779 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003780static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3781 struct btrfs_root *root,
3782 struct btrfs_path *path,
3783 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003784{
3785 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003786
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003787 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003788 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3789 if (ret)
3790 return ret;
3791
Chris Mason4d081c42009-02-04 09:31:28 -05003792 /*
3793 * btrfs_free_extent is expensive, we want to make sure we
3794 * aren't holding any locks when we call it
3795 */
3796 btrfs_unlock_up_safe(path, 0);
3797
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00003798 ret = btrfs_free_tree_block(trans, root, leaf->start, leaf->len,
3799 0, root->root_key.objectid, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003800 return ret;
3801}
3802/*
Chris Mason74123bd2007-02-02 11:05:29 -05003803 * delete the item at the leaf level in path. If that empties
3804 * the leaf, remove it from the tree
3805 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003806int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3807 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003808{
Chris Mason5f39d392007-10-15 16:14:19 -04003809 struct extent_buffer *leaf;
3810 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003811 int last_off;
3812 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003813 int ret = 0;
3814 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003815 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003816 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003817
Chris Mason5f39d392007-10-15 16:14:19 -04003818 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003819 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3820
3821 for (i = 0; i < nr; i++)
3822 dsize += btrfs_item_size_nr(leaf, slot + i);
3823
Chris Mason5f39d392007-10-15 16:14:19 -04003824 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003825
Chris Mason85e21ba2008-01-29 15:11:36 -05003826 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003827 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003828
3829 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003830 data_end + dsize,
3831 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003832 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003833
Chris Mason85e21ba2008-01-29 15:11:36 -05003834 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003835 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003836
Chris Mason5f39d392007-10-15 16:14:19 -04003837 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003838 if (!leaf->map_token) {
3839 map_extent_buffer(leaf, (unsigned long)item,
3840 sizeof(struct btrfs_item),
3841 &leaf->map_token, &leaf->kaddr,
3842 &leaf->map_start, &leaf->map_len,
3843 KM_USER1);
3844 }
Chris Mason5f39d392007-10-15 16:14:19 -04003845 ioff = btrfs_item_offset(leaf, item);
3846 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003847 }
Chris Masondb945352007-10-15 16:15:53 -04003848
3849 if (leaf->map_token) {
3850 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3851 leaf->map_token = NULL;
3852 }
3853
Chris Mason5f39d392007-10-15 16:14:19 -04003854 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003855 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003856 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003857 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003858 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003859 btrfs_set_header_nritems(leaf, nritems - nr);
3860 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003861
Chris Mason74123bd2007-02-02 11:05:29 -05003862 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003863 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003864 if (leaf == root->node) {
3865 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003866 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003867 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003868 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003869 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003870 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003871 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003872 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003873 struct btrfs_disk_key disk_key;
3874
3875 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003876 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003877 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003878 if (wret)
3879 ret = wret;
3880 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003881
Chris Mason74123bd2007-02-02 11:05:29 -05003882 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003883 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003884 /* push_leaf_left fixes the path.
3885 * make sure the path still points to our leaf
3886 * for possible call to del_ptr below
3887 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003888 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003889 extent_buffer_get(leaf);
3890
Chris Masonb9473432009-03-13 11:00:37 -04003891 btrfs_set_path_blocking(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003892 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003893 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003894 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003895
3896 if (path->nodes[0] == leaf &&
3897 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003898 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003899 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003900 ret = wret;
3901 }
Chris Mason5f39d392007-10-15 16:14:19 -04003902
3903 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003904 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003905 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003906 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003907 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003908 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003909 /* if we're still in the path, make sure
3910 * we're dirty. Otherwise, one of the
3911 * push_leaf functions must have already
3912 * dirtied this buffer
3913 */
3914 if (path->nodes[0] == leaf)
3915 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003916 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003917 }
Chris Masond5719762007-03-23 10:01:08 -04003918 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003919 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003920 }
3921 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003922 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003923}
3924
Chris Mason97571fd2007-02-24 13:39:08 -05003925/*
Chris Mason925baed2008-06-25 16:01:30 -04003926 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003927 * returns 0 if it found something or 1 if there are no lesser leaves.
3928 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003929 *
3930 * This may release the path, and so you may lose any locks held at the
3931 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003932 */
3933int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3934{
Chris Mason925baed2008-06-25 16:01:30 -04003935 struct btrfs_key key;
3936 struct btrfs_disk_key found_key;
3937 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003938
Chris Mason925baed2008-06-25 16:01:30 -04003939 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003940
Chris Mason925baed2008-06-25 16:01:30 -04003941 if (key.offset > 0)
3942 key.offset--;
3943 else if (key.type > 0)
3944 key.type--;
3945 else if (key.objectid > 0)
3946 key.objectid--;
3947 else
3948 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003949
Chris Mason925baed2008-06-25 16:01:30 -04003950 btrfs_release_path(root, path);
3951 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3952 if (ret < 0)
3953 return ret;
3954 btrfs_item_key(path->nodes[0], &found_key, 0);
3955 ret = comp_keys(&found_key, &key);
3956 if (ret < 0)
3957 return 0;
3958 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003959}
3960
Chris Mason3f157a22008-06-25 16:01:31 -04003961/*
3962 * A helper function to walk down the tree starting at min_key, and looking
3963 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003964 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003965 *
3966 * This does not cow, but it does stuff the starting key it finds back
3967 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3968 * key and get a writable path.
3969 *
3970 * This does lock as it descends, and path->keep_locks should be set
3971 * to 1 by the caller.
3972 *
3973 * This honors path->lowest_level to prevent descent past a given level
3974 * of the tree.
3975 *
Chris Masond352ac62008-09-29 15:18:18 -04003976 * min_trans indicates the oldest transaction that you are interested
3977 * in walking through. Any nodes or leaves older than min_trans are
3978 * skipped over (without reading them).
3979 *
Chris Mason3f157a22008-06-25 16:01:31 -04003980 * returns zero if something useful was found, < 0 on error and 1 if there
3981 * was nothing in the tree that matched the search criteria.
3982 */
3983int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003984 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003985 struct btrfs_path *path, int cache_only,
3986 u64 min_trans)
3987{
3988 struct extent_buffer *cur;
3989 struct btrfs_key found_key;
3990 int slot;
Yan96524802008-07-24 12:19:49 -04003991 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003992 u32 nritems;
3993 int level;
3994 int ret = 1;
3995
Chris Mason934d3752008-12-08 16:43:10 -05003996 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003997again:
3998 cur = btrfs_lock_root_node(root);
3999 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004000 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004001 path->nodes[level] = cur;
4002 path->locks[level] = 1;
4003
4004 if (btrfs_header_generation(cur) < min_trans) {
4005 ret = 1;
4006 goto out;
4007 }
Chris Masond3977122009-01-05 21:25:51 -05004008 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004009 nritems = btrfs_header_nritems(cur);
4010 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004011 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004012
Chris Mason323ac952008-10-01 19:05:46 -04004013 /* at the lowest level, we're done, setup the path and exit */
4014 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004015 if (slot >= nritems)
4016 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004017 ret = 0;
4018 path->slots[level] = slot;
4019 btrfs_item_key_to_cpu(cur, &found_key, slot);
4020 goto out;
4021 }
Yan96524802008-07-24 12:19:49 -04004022 if (sret && slot > 0)
4023 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004024 /*
4025 * check this node pointer against the cache_only and
4026 * min_trans parameters. If it isn't in cache or is too
4027 * old, skip to the next one.
4028 */
Chris Masond3977122009-01-05 21:25:51 -05004029 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004030 u64 blockptr;
4031 u64 gen;
4032 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004033 struct btrfs_disk_key disk_key;
4034
Chris Mason3f157a22008-06-25 16:01:31 -04004035 blockptr = btrfs_node_blockptr(cur, slot);
4036 gen = btrfs_node_ptr_generation(cur, slot);
4037 if (gen < min_trans) {
4038 slot++;
4039 continue;
4040 }
4041 if (!cache_only)
4042 break;
4043
Chris Masone02119d2008-09-05 16:13:11 -04004044 if (max_key) {
4045 btrfs_node_key(cur, &disk_key, slot);
4046 if (comp_keys(&disk_key, max_key) >= 0) {
4047 ret = 1;
4048 goto out;
4049 }
4050 }
4051
Chris Mason3f157a22008-06-25 16:01:31 -04004052 tmp = btrfs_find_tree_block(root, blockptr,
4053 btrfs_level_size(root, level - 1));
4054
4055 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4056 free_extent_buffer(tmp);
4057 break;
4058 }
4059 if (tmp)
4060 free_extent_buffer(tmp);
4061 slot++;
4062 }
Chris Masone02119d2008-09-05 16:13:11 -04004063find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004064 /*
4065 * we didn't find a candidate key in this node, walk forward
4066 * and find another one
4067 */
4068 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004069 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004070 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004071 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004072 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004073 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004074 btrfs_release_path(root, path);
4075 goto again;
4076 } else {
4077 goto out;
4078 }
4079 }
4080 /* save our key for returning back */
4081 btrfs_node_key_to_cpu(cur, &found_key, slot);
4082 path->slots[level] = slot;
4083 if (level == path->lowest_level) {
4084 ret = 0;
4085 unlock_up(path, level, 1);
4086 goto out;
4087 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004088 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004089 cur = read_node_slot(root, cur, slot);
4090
4091 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004092
Chris Mason3f157a22008-06-25 16:01:31 -04004093 path->locks[level - 1] = 1;
4094 path->nodes[level - 1] = cur;
4095 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004096 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004097 }
4098out:
4099 if (ret == 0)
4100 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004101 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004102 return ret;
4103}
4104
4105/*
4106 * this is similar to btrfs_next_leaf, but does not try to preserve
4107 * and fixup the path. It looks for and returns the next key in the
4108 * tree based on the current path and the cache_only and min_trans
4109 * parameters.
4110 *
4111 * 0 is returned if another key is found, < 0 if there are any errors
4112 * and 1 is returned if there are no higher keys in the tree
4113 *
4114 * path->keep_locks should be set to 1 on the search made before
4115 * calling this function.
4116 */
Chris Masone7a84562008-06-25 16:01:31 -04004117int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004118 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004119 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004120{
Chris Masone7a84562008-06-25 16:01:31 -04004121 int slot;
4122 struct extent_buffer *c;
4123
Chris Mason934d3752008-12-08 16:43:10 -05004124 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004125 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004126 if (!path->nodes[level])
4127 return 1;
4128
4129 slot = path->slots[level] + 1;
4130 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004131next:
Chris Masone7a84562008-06-25 16:01:31 -04004132 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004133 int ret;
4134 int orig_lowest;
4135 struct btrfs_key cur_key;
4136 if (level + 1 >= BTRFS_MAX_LEVEL ||
4137 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004138 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004139
4140 if (path->locks[level + 1]) {
4141 level++;
4142 continue;
4143 }
4144
4145 slot = btrfs_header_nritems(c) - 1;
4146 if (level == 0)
4147 btrfs_item_key_to_cpu(c, &cur_key, slot);
4148 else
4149 btrfs_node_key_to_cpu(c, &cur_key, slot);
4150
4151 orig_lowest = path->lowest_level;
4152 btrfs_release_path(root, path);
4153 path->lowest_level = level;
4154 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4155 0, 0);
4156 path->lowest_level = orig_lowest;
4157 if (ret < 0)
4158 return ret;
4159
4160 c = path->nodes[level];
4161 slot = path->slots[level];
4162 if (ret == 0)
4163 slot++;
4164 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004165 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004166
Chris Masone7a84562008-06-25 16:01:31 -04004167 if (level == 0)
4168 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004169 else {
4170 u64 blockptr = btrfs_node_blockptr(c, slot);
4171 u64 gen = btrfs_node_ptr_generation(c, slot);
4172
4173 if (cache_only) {
4174 struct extent_buffer *cur;
4175 cur = btrfs_find_tree_block(root, blockptr,
4176 btrfs_level_size(root, level - 1));
4177 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4178 slot++;
4179 if (cur)
4180 free_extent_buffer(cur);
4181 goto next;
4182 }
4183 free_extent_buffer(cur);
4184 }
4185 if (gen < min_trans) {
4186 slot++;
4187 goto next;
4188 }
Chris Masone7a84562008-06-25 16:01:31 -04004189 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004190 }
Chris Masone7a84562008-06-25 16:01:31 -04004191 return 0;
4192 }
4193 return 1;
4194}
4195
Chris Mason7bb86312007-12-11 09:25:06 -05004196/*
Chris Mason925baed2008-06-25 16:01:30 -04004197 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004198 * returns 0 if it found something or 1 if there are no greater leaves.
4199 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004200 */
Chris Mason234b63a2007-03-13 10:46:10 -04004201int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004202{
4203 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004204 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004205 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004206 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004207 struct btrfs_key key;
4208 u32 nritems;
4209 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004210 int old_spinning = path->leave_spinning;
4211 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004212
4213 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004214 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004215 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004216
Chris Mason8e73f272009-04-03 10:14:18 -04004217 /*
4218 * we take the blocks in an order that upsets lockdep. Using
4219 * blocking mode is the only way around it.
4220 */
4221#ifdef CONFIG_DEBUG_LOCK_ALLOC
4222 force_blocking = 1;
4223#endif
Chris Mason925baed2008-06-25 16:01:30 -04004224
Chris Mason8e73f272009-04-03 10:14:18 -04004225 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4226again:
4227 level = 1;
4228 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004229 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004230
Chris Masona2135012008-06-25 16:01:30 -04004231 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004232
4233 if (!force_blocking)
4234 path->leave_spinning = 1;
4235
Chris Mason925baed2008-06-25 16:01:30 -04004236 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4237 path->keep_locks = 0;
4238
4239 if (ret < 0)
4240 return ret;
4241
Chris Masona2135012008-06-25 16:01:30 -04004242 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004243 /*
4244 * by releasing the path above we dropped all our locks. A balance
4245 * could have added more items next to the key that used to be
4246 * at the very end of the block. So, check again here and
4247 * advance the path if there are now more items available.
4248 */
Chris Masona2135012008-06-25 16:01:30 -04004249 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004250 if (ret == 0)
4251 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004252 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004253 goto done;
4254 }
Chris Masond97e63b2007-02-20 16:40:44 -05004255
Chris Masond3977122009-01-05 21:25:51 -05004256 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004257 if (!path->nodes[level]) {
4258 ret = 1;
4259 goto done;
4260 }
Chris Mason5f39d392007-10-15 16:14:19 -04004261
Chris Masond97e63b2007-02-20 16:40:44 -05004262 slot = path->slots[level] + 1;
4263 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004264 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004265 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004266 if (level == BTRFS_MAX_LEVEL) {
4267 ret = 1;
4268 goto done;
4269 }
Chris Masond97e63b2007-02-20 16:40:44 -05004270 continue;
4271 }
Chris Mason5f39d392007-10-15 16:14:19 -04004272
Chris Mason925baed2008-06-25 16:01:30 -04004273 if (next) {
4274 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004275 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004276 }
Chris Mason5f39d392007-10-15 16:14:19 -04004277
Chris Mason8e73f272009-04-03 10:14:18 -04004278 next = c;
4279 ret = read_block_for_search(NULL, root, path, &next, level,
4280 slot, &key);
4281 if (ret == -EAGAIN)
4282 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004283
Chris Mason76a05b32009-05-14 13:24:30 -04004284 if (ret < 0) {
4285 btrfs_release_path(root, path);
4286 goto done;
4287 }
4288
Chris Mason5cd57b22008-06-25 16:01:30 -04004289 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004290 ret = btrfs_try_spin_lock(next);
4291 if (!ret) {
4292 btrfs_set_path_blocking(path);
4293 btrfs_tree_lock(next);
4294 if (!force_blocking)
4295 btrfs_clear_path_blocking(path, next);
4296 }
4297 if (force_blocking)
4298 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004299 }
Chris Masond97e63b2007-02-20 16:40:44 -05004300 break;
4301 }
4302 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004303 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004304 level--;
4305 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004306 if (path->locks[level])
4307 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004308
Chris Mason5f39d392007-10-15 16:14:19 -04004309 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004310 path->nodes[level] = next;
4311 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004312 if (!path->skip_locking)
4313 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004314
Chris Masond97e63b2007-02-20 16:40:44 -05004315 if (!level)
4316 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004317
Chris Mason8e73f272009-04-03 10:14:18 -04004318 ret = read_block_for_search(NULL, root, path, &next, level,
4319 0, &key);
4320 if (ret == -EAGAIN)
4321 goto again;
4322
Chris Mason76a05b32009-05-14 13:24:30 -04004323 if (ret < 0) {
4324 btrfs_release_path(root, path);
4325 goto done;
4326 }
4327
Chris Mason5cd57b22008-06-25 16:01:30 -04004328 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004329 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004330 ret = btrfs_try_spin_lock(next);
4331 if (!ret) {
4332 btrfs_set_path_blocking(path);
4333 btrfs_tree_lock(next);
4334 if (!force_blocking)
4335 btrfs_clear_path_blocking(path, next);
4336 }
4337 if (force_blocking)
4338 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004339 }
Chris Masond97e63b2007-02-20 16:40:44 -05004340 }
Chris Mason8e73f272009-04-03 10:14:18 -04004341 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004342done:
4343 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004344 path->leave_spinning = old_spinning;
4345 if (!old_spinning)
4346 btrfs_set_path_blocking(path);
4347
4348 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004349}
Chris Mason0b86a832008-03-24 15:01:56 -04004350
Chris Mason3f157a22008-06-25 16:01:31 -04004351/*
4352 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4353 * searching until it gets past min_objectid or finds an item of 'type'
4354 *
4355 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4356 */
Chris Mason0b86a832008-03-24 15:01:56 -04004357int btrfs_previous_item(struct btrfs_root *root,
4358 struct btrfs_path *path, u64 min_objectid,
4359 int type)
4360{
4361 struct btrfs_key found_key;
4362 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004363 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004364 int ret;
4365
Chris Masond3977122009-01-05 21:25:51 -05004366 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004367 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004368 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004369 ret = btrfs_prev_leaf(root, path);
4370 if (ret != 0)
4371 return ret;
4372 } else {
4373 path->slots[0]--;
4374 }
4375 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004376 nritems = btrfs_header_nritems(leaf);
4377 if (nritems == 0)
4378 return 1;
4379 if (path->slots[0] == nritems)
4380 path->slots[0]--;
4381
Chris Mason0b86a832008-03-24 15:01:56 -04004382 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004383 if (found_key.objectid < min_objectid)
4384 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004385 if (found_key.type == type)
4386 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004387 if (found_key.objectid == min_objectid &&
4388 found_key.type < type)
4389 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004390 }
4391 return 1;
4392}