blob: 9d4ba3470c1737396ee756b9c96a6369ee3c1f69 [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 Zheng5d4f98a2009-06-10 10:45:14 -0400459 btrfs_free_extent(trans, root, buf->start, buf->len,
460 parent_start, root->root_key.objectid,
461 level, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400462 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400463 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400464 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400465 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
466 parent_start = parent->start;
467 else
468 parent_start = 0;
469
470 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400471 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400472 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500473 btrfs_set_node_ptr_generation(parent, parent_slot,
474 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400475 btrfs_mark_buffer_dirty(parent);
Chris Mason7bb86312007-12-11 09:25:06 -0500476 btrfs_free_extent(trans, root, buf->start, buf->len,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400477 parent_start, root->root_key.objectid,
478 level, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400479 }
Chris Mason925baed2008-06-25 16:01:30 -0400480 if (unlock_orig)
481 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400482 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400483 btrfs_mark_buffer_dirty(cow);
484 *cow_ret = cow;
485 return 0;
486}
487
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400488static inline int should_cow_block(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root,
490 struct extent_buffer *buf)
491{
492 if (btrfs_header_generation(buf) == trans->transid &&
493 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
494 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
495 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
496 return 0;
497 return 1;
498}
499
Chris Masond352ac62008-09-29 15:18:18 -0400500/*
501 * cows a single block, see __btrfs_cow_block for the real work.
502 * This version of it has extra checks so that a block isn't cow'd more than
503 * once per transaction, as long as it hasn't been written yet
504 */
Chris Masond3977122009-01-05 21:25:51 -0500505noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400506 struct btrfs_root *root, struct extent_buffer *buf,
507 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400508 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500509{
Chris Mason6702ed42007-08-07 16:15:09 -0400510 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400511 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500512
Chris Masonccd467d2007-06-28 15:57:36 -0400513 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500514 printk(KERN_CRIT "trans %llu running %llu\n",
515 (unsigned long long)trans->transid,
516 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400517 root->fs_info->running_transaction->transid);
518 WARN_ON(1);
519 }
520 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500521 printk(KERN_CRIT "trans %llu running %llu\n",
522 (unsigned long long)trans->transid,
523 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400524 WARN_ON(1);
525 }
Chris Masondc17ff82008-01-08 15:46:30 -0500526
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400527 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500528 *cow_ret = buf;
529 return 0;
530 }
Chris Masonc4876852009-02-04 09:24:25 -0500531
Chris Mason0b86a832008-03-24 15:01:56 -0400532 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500533
534 if (parent)
535 btrfs_set_lock_blocking(parent);
536 btrfs_set_lock_blocking(buf);
537
Chris Masonf510cfe2007-10-15 16:14:48 -0400538 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400539 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400540 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400541}
542
Chris Masond352ac62008-09-29 15:18:18 -0400543/*
544 * helper function for defrag to decide if two blocks pointed to by a
545 * node are actually close by
546 */
Chris Mason6b800532007-10-15 16:17:34 -0400547static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400548{
Chris Mason6b800532007-10-15 16:17:34 -0400549 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400550 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400551 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400552 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500553 return 0;
554}
555
Chris Mason081e9572007-11-06 10:26:24 -0500556/*
557 * compare two keys in a memcmp fashion
558 */
559static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
560{
561 struct btrfs_key k1;
562
563 btrfs_disk_key_to_cpu(&k1, disk);
564
Diego Calleja20736ab2009-07-24 11:06:52 -0400565 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500566}
567
Josef Bacikf3465ca2008-11-12 14:19:50 -0500568/*
569 * same as comp_keys only with two btrfs_key's
570 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400571int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500572{
573 if (k1->objectid > k2->objectid)
574 return 1;
575 if (k1->objectid < k2->objectid)
576 return -1;
577 if (k1->type > k2->type)
578 return 1;
579 if (k1->type < k2->type)
580 return -1;
581 if (k1->offset > k2->offset)
582 return 1;
583 if (k1->offset < k2->offset)
584 return -1;
585 return 0;
586}
Chris Mason081e9572007-11-06 10:26:24 -0500587
Chris Masond352ac62008-09-29 15:18:18 -0400588/*
589 * this is used by the defrag code to go through all the
590 * leaves pointed to by a node and reallocate them so that
591 * disk order is close to key order
592 */
Chris Mason6702ed42007-08-07 16:15:09 -0400593int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400594 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400595 int start_slot, int cache_only, u64 *last_ret,
596 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400597{
Chris Mason6b800532007-10-15 16:17:34 -0400598 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400599 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400600 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400601 u64 search_start = *last_ret;
602 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400603 u64 other;
604 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400605 int end_slot;
606 int i;
607 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400608 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400609 int uptodate;
610 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500611 int progress_passed = 0;
612 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400613
Chris Mason5708b952007-10-25 15:43:18 -0400614 parent_level = btrfs_header_level(parent);
615 if (cache_only && parent_level != 1)
616 return 0;
617
Chris Masond3977122009-01-05 21:25:51 -0500618 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400619 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500620 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400621 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400622
Chris Mason6b800532007-10-15 16:17:34 -0400623 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400624 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400625 end_slot = parent_nritems;
626
627 if (parent_nritems == 1)
628 return 0;
629
Chris Masonb4ce94d2009-02-04 09:25:08 -0500630 btrfs_set_lock_blocking(parent);
631
Chris Mason6702ed42007-08-07 16:15:09 -0400632 for (i = start_slot; i < end_slot; i++) {
633 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400634
Chris Mason5708b952007-10-25 15:43:18 -0400635 if (!parent->map_token) {
636 map_extent_buffer(parent,
637 btrfs_node_key_ptr_offset(i),
638 sizeof(struct btrfs_key_ptr),
639 &parent->map_token, &parent->kaddr,
640 &parent->map_start, &parent->map_len,
641 KM_USER1);
642 }
Chris Mason081e9572007-11-06 10:26:24 -0500643 btrfs_node_key(parent, &disk_key, i);
644 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
645 continue;
646
647 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400648 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400649 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400650 if (last_block == 0)
651 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400652
Chris Mason6702ed42007-08-07 16:15:09 -0400653 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400654 other = btrfs_node_blockptr(parent, i - 1);
655 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400656 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400657 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400658 other = btrfs_node_blockptr(parent, i + 1);
659 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400660 }
Chris Masone9d0b132007-08-10 14:06:19 -0400661 if (close) {
662 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400663 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400664 }
Chris Mason5708b952007-10-25 15:43:18 -0400665 if (parent->map_token) {
666 unmap_extent_buffer(parent, parent->map_token,
667 KM_USER1);
668 parent->map_token = NULL;
669 }
Chris Mason6702ed42007-08-07 16:15:09 -0400670
Chris Mason6b800532007-10-15 16:17:34 -0400671 cur = btrfs_find_tree_block(root, blocknr, blocksize);
672 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400673 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400674 else
675 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400676 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400677 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400678 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400679 continue;
680 }
Chris Mason6b800532007-10-15 16:17:34 -0400681 if (!cur) {
682 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400683 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400684 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400685 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400686 }
Chris Mason6702ed42007-08-07 16:15:09 -0400687 }
Chris Masone9d0b132007-08-10 14:06:19 -0400688 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400689 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400690
Chris Masone7a84562008-06-25 16:01:31 -0400691 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500692 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400693 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400694 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400695 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400696 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400697 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400698 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400699 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400700 break;
Yan252c38f2007-08-29 09:11:44 -0400701 }
Chris Masone7a84562008-06-25 16:01:31 -0400702 search_start = cur->start;
703 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400704 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400705 btrfs_tree_unlock(cur);
706 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400707 }
Chris Mason5708b952007-10-25 15:43:18 -0400708 if (parent->map_token) {
709 unmap_extent_buffer(parent, parent->map_token,
710 KM_USER1);
711 parent->map_token = NULL;
712 }
Chris Mason6702ed42007-08-07 16:15:09 -0400713 return err;
714}
715
Chris Mason74123bd2007-02-02 11:05:29 -0500716/*
717 * The leaf data grows from end-to-front in the node.
718 * this returns the address of the start of the last item,
719 * which is the stop of the leaf data stack
720 */
Chris Mason123abc82007-03-14 14:14:43 -0400721static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400722 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723{
Chris Mason5f39d392007-10-15 16:14:19 -0400724 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500725 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400726 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400727 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728}
729
Chris Masond352ac62008-09-29 15:18:18 -0400730/*
731 * extra debugging checks to make sure all the items in a key are
732 * well formed and in the proper order
733 */
Chris Mason123abc82007-03-14 14:14:43 -0400734static int check_node(struct btrfs_root *root, struct btrfs_path *path,
735 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500736{
Chris Mason5f39d392007-10-15 16:14:19 -0400737 struct extent_buffer *parent = NULL;
738 struct extent_buffer *node = path->nodes[level];
739 struct btrfs_disk_key parent_key;
740 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500741 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400742 int slot;
743 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400744 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500745
746 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400747 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400748
Chris Mason8d7be552007-05-10 11:24:42 -0400749 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400750 BUG_ON(nritems == 0);
751 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400752 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400753 btrfs_node_key(parent, &parent_key, parent_slot);
754 btrfs_node_key(node, &node_key, 0);
755 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400756 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400757 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400758 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500759 }
Chris Mason123abc82007-03-14 14:14:43 -0400760 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400761 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400762 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
763 btrfs_node_key(node, &node_key, slot);
764 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400765 }
766 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400767 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
768 btrfs_node_key(node, &node_key, slot);
769 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500770 }
771 return 0;
772}
773
Chris Masond352ac62008-09-29 15:18:18 -0400774/*
775 * extra checking to make sure all the items in a leaf are
776 * well formed and in the proper order
777 */
Chris Mason123abc82007-03-14 14:14:43 -0400778static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
779 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500780{
Chris Mason5f39d392007-10-15 16:14:19 -0400781 struct extent_buffer *leaf = path->nodes[level];
782 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500783 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400784 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400785 struct btrfs_disk_key parent_key;
786 struct btrfs_disk_key leaf_key;
787 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400788
Chris Mason5f39d392007-10-15 16:14:19 -0400789 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500790
791 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400792 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400793
794 if (nritems == 0)
795 return 0;
796
797 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400798 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400799 btrfs_node_key(parent, &parent_key, parent_slot);
800 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400801
Chris Mason5f39d392007-10-15 16:14:19 -0400802 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400803 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400804 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400805 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500806 }
Chris Mason5f39d392007-10-15 16:14:19 -0400807 if (slot != 0 && slot < nritems - 1) {
808 btrfs_item_key(leaf, &leaf_key, slot);
809 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
810 if (comp_keys(&leaf_key, &cpukey) <= 0) {
811 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500812 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400813 BUG_ON(1);
814 }
815 if (btrfs_item_offset_nr(leaf, slot - 1) !=
816 btrfs_item_end_nr(leaf, slot)) {
817 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500818 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400819 BUG_ON(1);
820 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500821 }
Chris Mason8d7be552007-05-10 11:24:42 -0400822 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400823 btrfs_item_key(leaf, &leaf_key, slot);
824 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
825 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
826 if (btrfs_item_offset_nr(leaf, slot) !=
827 btrfs_item_end_nr(leaf, slot + 1)) {
828 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500829 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400830 BUG_ON(1);
831 }
Chris Mason8d7be552007-05-10 11:24:42 -0400832 }
Chris Mason5f39d392007-10-15 16:14:19 -0400833 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
834 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500835 return 0;
836}
837
Chris Masond3977122009-01-05 21:25:51 -0500838static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500839 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500840{
Chris Mason85d824c2008-04-10 10:23:19 -0400841 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500842 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400843 return check_leaf(root, path, level);
844 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500845}
846
Chris Mason74123bd2007-02-02 11:05:29 -0500847/*
Chris Mason5f39d392007-10-15 16:14:19 -0400848 * search for key in the extent_buffer. The items start at offset p,
849 * and they are item_size apart. There are 'max' items in p.
850 *
Chris Mason74123bd2007-02-02 11:05:29 -0500851 * the slot in the array is returned via slot, and it points to
852 * the place where you would insert key if it is not found in
853 * the array.
854 *
855 * slot may point to max if the key is bigger than all of the keys
856 */
Chris Masone02119d2008-09-05 16:13:11 -0400857static noinline int generic_bin_search(struct extent_buffer *eb,
858 unsigned long p,
859 int item_size, struct btrfs_key *key,
860 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500861{
862 int low = 0;
863 int high = max;
864 int mid;
865 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400866 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400867 struct btrfs_disk_key unaligned;
868 unsigned long offset;
869 char *map_token = NULL;
870 char *kaddr = NULL;
871 unsigned long map_start = 0;
872 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400873 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500874
Chris Masond3977122009-01-05 21:25:51 -0500875 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500876 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400877 offset = p + mid * item_size;
878
879 if (!map_token || offset < map_start ||
880 (offset + sizeof(struct btrfs_disk_key)) >
881 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400882 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400883 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400884 map_token = NULL;
885 }
Chris Mason934d3752008-12-08 16:43:10 -0500886
887 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400888 sizeof(struct btrfs_disk_key),
889 &map_token, &kaddr,
890 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400891
Chris Mason479965d2007-10-15 16:14:27 -0400892 if (!err) {
893 tmp = (struct btrfs_disk_key *)(kaddr + offset -
894 map_start);
895 } else {
896 read_extent_buffer(eb, &unaligned,
897 offset, sizeof(unaligned));
898 tmp = &unaligned;
899 }
900
Chris Mason5f39d392007-10-15 16:14:19 -0400901 } else {
902 tmp = (struct btrfs_disk_key *)(kaddr + offset -
903 map_start);
904 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500905 ret = comp_keys(tmp, key);
906
907 if (ret < 0)
908 low = mid + 1;
909 else if (ret > 0)
910 high = mid;
911 else {
912 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400913 if (map_token)
914 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500915 return 0;
916 }
917 }
918 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400919 if (map_token)
920 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500921 return 1;
922}
923
Chris Mason97571fd2007-02-24 13:39:08 -0500924/*
925 * simple bin_search frontend that does the right thing for
926 * leaves vs nodes
927 */
Chris Mason5f39d392007-10-15 16:14:19 -0400928static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
929 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500930{
Chris Mason5f39d392007-10-15 16:14:19 -0400931 if (level == 0) {
932 return generic_bin_search(eb,
933 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400934 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400935 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400936 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500937 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400938 return generic_bin_search(eb,
939 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400940 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400941 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400942 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500943 }
944 return -1;
945}
946
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
948 int level, int *slot)
949{
950 return bin_search(eb, key, level, slot);
951}
952
Chris Masond352ac62008-09-29 15:18:18 -0400953/* given a node and slot number, this reads the blocks it points to. The
954 * extent buffer is returned with a reference taken (but unlocked).
955 * NULL is returned on error.
956 */
Chris Masone02119d2008-09-05 16:13:11 -0400957static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400958 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500959{
Chris Masonca7a79a2008-05-12 12:59:19 -0400960 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500961 if (slot < 0)
962 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400963 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500964 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400965
966 BUG_ON(level == 0);
967
Chris Masondb945352007-10-15 16:15:53 -0400968 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400969 btrfs_level_size(root, level - 1),
970 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500971}
972
Chris Masond352ac62008-09-29 15:18:18 -0400973/*
974 * node level balancing, used to make sure nodes are in proper order for
975 * item deletion. We balance from the top down, so we have to make sure
976 * that a deletion won't leave an node completely empty later on.
977 */
Chris Masone02119d2008-09-05 16:13:11 -0400978static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500979 struct btrfs_root *root,
980 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500981{
Chris Mason5f39d392007-10-15 16:14:19 -0400982 struct extent_buffer *right = NULL;
983 struct extent_buffer *mid;
984 struct extent_buffer *left = NULL;
985 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500986 int ret = 0;
987 int wret;
988 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500989 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400990 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500991 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500992
993 if (level == 0)
994 return 0;
995
Chris Mason5f39d392007-10-15 16:14:19 -0400996 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500997
Chris Mason925baed2008-06-25 16:01:30 -0400998 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500999 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1000
Chris Mason1d4f8a02007-03-13 09:28:32 -04001001 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001002
Chris Mason234b63a2007-03-13 10:46:10 -04001003 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001004 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001005 pslot = path->slots[level + 1];
1006
Chris Mason40689472007-03-17 14:29:23 -04001007 /*
1008 * deal with the case where there is only one pointer in the root
1009 * by promoting the node below to a root
1010 */
Chris Mason5f39d392007-10-15 16:14:19 -04001011 if (!parent) {
1012 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001013
Chris Mason5f39d392007-10-15 16:14:19 -04001014 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001015 return 0;
1016
1017 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001018 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001019 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001020 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001021 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001022 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan2f375ab2008-02-01 14:58:07 -05001023 BUG_ON(ret);
1024
Chris Mason925baed2008-06-25 16:01:30 -04001025 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001026 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001027 spin_unlock(&root->node_lock);
1028
Chris Mason0b86a832008-03-24 15:01:56 -04001029 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001030 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001031
Chris Mason925baed2008-06-25 16:01:30 -04001032 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001033 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001034 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001035 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001036 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001037 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -05001038 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039 0, root->root_key.objectid, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001040 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001041 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -04001042 return ret;
Chris Masonbb803952007-03-01 12:04:21 -05001043 }
Chris Mason5f39d392007-10-15 16:14:19 -04001044 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001045 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001046 return 0;
1047
Chris Mason5f39d392007-10-15 16:14:19 -04001048 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001049 err_on_enospc = 1;
1050
Chris Mason5f39d392007-10-15 16:14:19 -04001051 left = read_node_slot(root, parent, pslot - 1);
1052 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001053 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001054 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001055 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001056 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001057 if (wret) {
1058 ret = wret;
1059 goto enospc;
1060 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001061 }
Chris Mason5f39d392007-10-15 16:14:19 -04001062 right = read_node_slot(root, parent, pslot + 1);
1063 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001064 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001065 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001066 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001067 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001068 if (wret) {
1069 ret = wret;
1070 goto enospc;
1071 }
1072 }
1073
1074 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001075 if (left) {
1076 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001077 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001078 if (wret < 0)
1079 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001080 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001081 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001082 }
Chris Mason79f95c82007-03-01 15:16:26 -05001083
1084 /*
1085 * then try to empty the right most buffer into the middle
1086 */
Chris Mason5f39d392007-10-15 16:14:19 -04001087 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001088 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001089 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001090 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001091 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001092 u64 bytenr = right->start;
1093 u32 blocksize = right->len;
1094
Chris Mason5f39d392007-10-15 16:14:19 -04001095 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001096 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001097 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001098 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001099 wret = del_ptr(trans, root, path, level + 1, pslot +
1100 1);
Chris Masonbb803952007-03-01 12:04:21 -05001101 if (wret)
1102 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001103 wret = btrfs_free_extent(trans, root, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 blocksize, 0,
1105 root->root_key.objectid,
1106 level, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001107 if (wret)
1108 ret = wret;
1109 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001110 struct btrfs_disk_key right_key;
1111 btrfs_node_key(right, &right_key, 0);
1112 btrfs_set_node_key(parent, &right_key, pslot + 1);
1113 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001114 }
1115 }
Chris Mason5f39d392007-10-15 16:14:19 -04001116 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001117 /*
1118 * we're not allowed to leave a node with one item in the
1119 * tree during a delete. A deletion from lower in the tree
1120 * could try to delete the only pointer in this node.
1121 * So, pull some keys from the left.
1122 * There has to be a left pointer at this point because
1123 * otherwise we would have pulled some pointers from the
1124 * right
1125 */
Chris Mason5f39d392007-10-15 16:14:19 -04001126 BUG_ON(!left);
1127 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001128 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001129 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001130 goto enospc;
1131 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001132 if (wret == 1) {
1133 wret = push_node_left(trans, root, left, mid, 1);
1134 if (wret < 0)
1135 ret = wret;
1136 }
Chris Mason79f95c82007-03-01 15:16:26 -05001137 BUG_ON(wret == 1);
1138 }
Chris Mason5f39d392007-10-15 16:14:19 -04001139 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001140 /* we've managed to empty the middle node, drop it */
Chris Masondb945352007-10-15 16:15:53 -04001141 u64 bytenr = mid->start;
1142 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001143
Chris Mason5f39d392007-10-15 16:14:19 -04001144 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001145 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001146 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001147 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001148 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001149 if (wret)
1150 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001151 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001152 0, root->root_key.objectid,
1153 level, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001154 if (wret)
1155 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001156 } else {
1157 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001158 struct btrfs_disk_key mid_key;
1159 btrfs_node_key(mid, &mid_key, 0);
1160 btrfs_set_node_key(parent, &mid_key, pslot);
1161 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001162 }
Chris Masonbb803952007-03-01 12:04:21 -05001163
Chris Mason79f95c82007-03-01 15:16:26 -05001164 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001165 if (left) {
1166 if (btrfs_header_nritems(left) > orig_slot) {
1167 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001168 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001169 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001170 path->slots[level + 1] -= 1;
1171 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001172 if (mid) {
1173 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001174 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001175 }
Chris Masonbb803952007-03-01 12:04:21 -05001176 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001177 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001178 path->slots[level] = orig_slot;
1179 }
1180 }
Chris Mason79f95c82007-03-01 15:16:26 -05001181 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001182 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001183 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001184 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001185 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001186enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001187 if (right) {
1188 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001189 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001190 }
1191 if (left) {
1192 if (path->nodes[level] != left)
1193 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001194 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001195 }
Chris Masonbb803952007-03-01 12:04:21 -05001196 return ret;
1197}
1198
Chris Masond352ac62008-09-29 15:18:18 -04001199/* Node balancing for insertion. Here we only split or push nodes around
1200 * when they are completely full. This is also done top down, so we
1201 * have to be pessimistic.
1202 */
Chris Masond3977122009-01-05 21:25:51 -05001203static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001204 struct btrfs_root *root,
1205 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001206{
Chris Mason5f39d392007-10-15 16:14:19 -04001207 struct extent_buffer *right = NULL;
1208 struct extent_buffer *mid;
1209 struct extent_buffer *left = NULL;
1210 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001211 int ret = 0;
1212 int wret;
1213 int pslot;
1214 int orig_slot = path->slots[level];
1215 u64 orig_ptr;
1216
1217 if (level == 0)
1218 return 1;
1219
Chris Mason5f39d392007-10-15 16:14:19 -04001220 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001221 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001222 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1223
1224 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001225 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001226 pslot = path->slots[level + 1];
1227
Chris Mason5f39d392007-10-15 16:14:19 -04001228 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001229 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001230
Chris Mason5f39d392007-10-15 16:14:19 -04001231 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001232
1233 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001234 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001235 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001236
1237 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001238 btrfs_set_lock_blocking(left);
1239
Chris Mason5f39d392007-10-15 16:14:19 -04001240 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001241 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1242 wret = 1;
1243 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001244 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001245 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001246 if (ret)
1247 wret = 1;
1248 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001249 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001250 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001251 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001252 }
Chris Masone66f7092007-04-20 13:16:02 -04001253 if (wret < 0)
1254 ret = wret;
1255 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001256 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001257 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001258 btrfs_node_key(mid, &disk_key, 0);
1259 btrfs_set_node_key(parent, &disk_key, pslot);
1260 btrfs_mark_buffer_dirty(parent);
1261 if (btrfs_header_nritems(left) > orig_slot) {
1262 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001263 path->slots[level + 1] -= 1;
1264 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001265 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001266 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001267 } else {
1268 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001269 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001270 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001271 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001272 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001273 }
Chris Masone66f7092007-04-20 13:16:02 -04001274 return 0;
1275 }
Chris Mason925baed2008-06-25 16:01:30 -04001276 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001277 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001278 }
Chris Mason925baed2008-06-25 16:01:30 -04001279 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001280
1281 /*
1282 * then try to empty the right most buffer into the middle
1283 */
Chris Mason5f39d392007-10-15 16:14:19 -04001284 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001285 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001286
Chris Mason925baed2008-06-25 16:01:30 -04001287 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001288 btrfs_set_lock_blocking(right);
1289
Chris Mason5f39d392007-10-15 16:14:19 -04001290 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001291 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1292 wret = 1;
1293 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001294 ret = btrfs_cow_block(trans, root, right,
1295 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001296 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001297 if (ret)
1298 wret = 1;
1299 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001300 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001301 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001302 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001303 }
Chris Masone66f7092007-04-20 13:16:02 -04001304 if (wret < 0)
1305 ret = wret;
1306 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001307 struct btrfs_disk_key disk_key;
1308
1309 btrfs_node_key(right, &disk_key, 0);
1310 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1311 btrfs_mark_buffer_dirty(parent);
1312
1313 if (btrfs_header_nritems(mid) <= orig_slot) {
1314 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001315 path->slots[level + 1] += 1;
1316 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001317 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001318 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001319 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001320 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001321 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001322 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001323 }
Chris Masone66f7092007-04-20 13:16:02 -04001324 return 0;
1325 }
Chris Mason925baed2008-06-25 16:01:30 -04001326 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001327 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001328 }
Chris Masone66f7092007-04-20 13:16:02 -04001329 return 1;
1330}
1331
Chris Mason74123bd2007-02-02 11:05:29 -05001332/*
Chris Masond352ac62008-09-29 15:18:18 -04001333 * readahead one full node of leaves, finding things that are close
1334 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001335 */
Chris Masonc8c42862009-04-03 10:14:18 -04001336static void reada_for_search(struct btrfs_root *root,
1337 struct btrfs_path *path,
1338 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001339{
Chris Mason5f39d392007-10-15 16:14:19 -04001340 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001341 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001342 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001343 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001344 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001345 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001346 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001347 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001348 u32 nr;
1349 u32 blocksize;
1350 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001351
Chris Masona6b6e752007-10-15 16:22:39 -04001352 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001353 return;
1354
Chris Mason6702ed42007-08-07 16:15:09 -04001355 if (!path->nodes[level])
1356 return;
1357
Chris Mason5f39d392007-10-15 16:14:19 -04001358 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001359
Chris Mason3c69fae2007-08-07 15:52:22 -04001360 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001361 blocksize = btrfs_level_size(root, level - 1);
1362 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001363 if (eb) {
1364 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001365 return;
1366 }
1367
Chris Masona7175312009-01-22 09:23:10 -05001368 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001369
Chris Mason5f39d392007-10-15 16:14:19 -04001370 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001371 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001372 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001373 if (direction < 0) {
1374 if (nr == 0)
1375 break;
1376 nr--;
1377 } else if (direction > 0) {
1378 nr++;
1379 if (nr >= nritems)
1380 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001381 }
Chris Mason01f46652007-12-21 16:24:26 -05001382 if (path->reada < 0 && objectid) {
1383 btrfs_node_key(node, &disk_key, nr);
1384 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1385 break;
1386 }
Chris Mason6b800532007-10-15 16:17:34 -04001387 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001388 if ((search <= target && target - search <= 65536) ||
1389 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001390 readahead_tree_block(root, search, blocksize,
1391 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001392 nread += blocksize;
1393 }
1394 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001395 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001396 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001397 }
1398}
Chris Mason925baed2008-06-25 16:01:30 -04001399
Chris Masond352ac62008-09-29 15:18:18 -04001400/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001401 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1402 * cache
1403 */
1404static noinline int reada_for_balance(struct btrfs_root *root,
1405 struct btrfs_path *path, int level)
1406{
1407 int slot;
1408 int nritems;
1409 struct extent_buffer *parent;
1410 struct extent_buffer *eb;
1411 u64 gen;
1412 u64 block1 = 0;
1413 u64 block2 = 0;
1414 int ret = 0;
1415 int blocksize;
1416
Chris Mason8c594ea2009-04-20 15:50:10 -04001417 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001418 if (!parent)
1419 return 0;
1420
1421 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001422 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001423 blocksize = btrfs_level_size(root, level);
1424
1425 if (slot > 0) {
1426 block1 = btrfs_node_blockptr(parent, slot - 1);
1427 gen = btrfs_node_ptr_generation(parent, slot - 1);
1428 eb = btrfs_find_tree_block(root, block1, blocksize);
1429 if (eb && btrfs_buffer_uptodate(eb, gen))
1430 block1 = 0;
1431 free_extent_buffer(eb);
1432 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001433 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001434 block2 = btrfs_node_blockptr(parent, slot + 1);
1435 gen = btrfs_node_ptr_generation(parent, slot + 1);
1436 eb = btrfs_find_tree_block(root, block2, blocksize);
1437 if (eb && btrfs_buffer_uptodate(eb, gen))
1438 block2 = 0;
1439 free_extent_buffer(eb);
1440 }
1441 if (block1 || block2) {
1442 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001443
1444 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001445 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001446
1447 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001448 if (block1)
1449 readahead_tree_block(root, block1, blocksize, 0);
1450 if (block2)
1451 readahead_tree_block(root, block2, blocksize, 0);
1452
1453 if (block1) {
1454 eb = read_tree_block(root, block1, blocksize, 0);
1455 free_extent_buffer(eb);
1456 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001457 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001458 eb = read_tree_block(root, block2, blocksize, 0);
1459 free_extent_buffer(eb);
1460 }
1461 }
1462 return ret;
1463}
1464
1465
1466/*
Chris Masond3977122009-01-05 21:25:51 -05001467 * when we walk down the tree, it is usually safe to unlock the higher layers
1468 * in the tree. The exceptions are when our path goes through slot 0, because
1469 * operations on the tree might require changing key pointers higher up in the
1470 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001471 *
Chris Masond3977122009-01-05 21:25:51 -05001472 * callers might also have set path->keep_locks, which tells this code to keep
1473 * the lock if the path points to the last slot in the block. This is part of
1474 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001475 *
Chris Masond3977122009-01-05 21:25:51 -05001476 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1477 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001478 */
Chris Masone02119d2008-09-05 16:13:11 -04001479static noinline void unlock_up(struct btrfs_path *path, int level,
1480 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001481{
1482 int i;
1483 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001484 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001485 struct extent_buffer *t;
1486
1487 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1488 if (!path->nodes[i])
1489 break;
1490 if (!path->locks[i])
1491 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001492 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001493 skip_level = i + 1;
1494 continue;
1495 }
Chris Mason051e1b92008-06-25 16:01:30 -04001496 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001497 u32 nritems;
1498 t = path->nodes[i];
1499 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001500 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001501 skip_level = i + 1;
1502 continue;
1503 }
1504 }
Chris Mason051e1b92008-06-25 16:01:30 -04001505 if (skip_level < i && i >= lowest_unlock)
1506 no_skips = 1;
1507
Chris Mason925baed2008-06-25 16:01:30 -04001508 t = path->nodes[i];
1509 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1510 btrfs_tree_unlock(t);
1511 path->locks[i] = 0;
1512 }
1513 }
1514}
1515
Chris Mason3c69fae2007-08-07 15:52:22 -04001516/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001517 * This releases any locks held in the path starting at level and
1518 * going all the way up to the root.
1519 *
1520 * btrfs_search_slot will keep the lock held on higher nodes in a few
1521 * corner cases, such as COW of the block at slot zero in the node. This
1522 * ignores those rules, and it should only be called when there are no
1523 * more updates to be done higher up in the tree.
1524 */
1525noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1526{
1527 int i;
1528
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001529 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001530 return;
1531
1532 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1533 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001534 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001535 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001536 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001537 btrfs_tree_unlock(path->nodes[i]);
1538 path->locks[i] = 0;
1539 }
1540}
1541
1542/*
Chris Masonc8c42862009-04-03 10:14:18 -04001543 * helper function for btrfs_search_slot. The goal is to find a block
1544 * in cache without setting the path to blocking. If we find the block
1545 * we return zero and the path is unchanged.
1546 *
1547 * If we can't find the block, we set the path blocking and do some
1548 * reada. -EAGAIN is returned and the search must be repeated.
1549 */
1550static int
1551read_block_for_search(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root, struct btrfs_path *p,
1553 struct extent_buffer **eb_ret, int level, int slot,
1554 struct btrfs_key *key)
1555{
1556 u64 blocknr;
1557 u64 gen;
1558 u32 blocksize;
1559 struct extent_buffer *b = *eb_ret;
1560 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001561 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001562
1563 blocknr = btrfs_node_blockptr(b, slot);
1564 gen = btrfs_node_ptr_generation(b, slot);
1565 blocksize = btrfs_level_size(root, level - 1);
1566
1567 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1568 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason76a05b32009-05-14 13:24:30 -04001569 /*
1570 * we found an up to date block without sleeping, return
1571 * right away
1572 */
Chris Masonc8c42862009-04-03 10:14:18 -04001573 *eb_ret = tmp;
1574 return 0;
1575 }
1576
1577 /*
1578 * reduce lock contention at high levels
1579 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001580 * we read. Don't release the lock on the current
1581 * level because we need to walk this node to figure
1582 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001583 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001584 btrfs_unlock_up_safe(p, level + 1);
1585 btrfs_set_path_blocking(p);
1586
Chris Masonc8c42862009-04-03 10:14:18 -04001587 if (tmp)
1588 free_extent_buffer(tmp);
1589 if (p->reada)
1590 reada_for_search(root, p, level, slot, key->objectid);
1591
Chris Mason8c594ea2009-04-20 15:50:10 -04001592 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001593
1594 ret = -EAGAIN;
Chris Masonc8c42862009-04-03 10:14:18 -04001595 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Mason76a05b32009-05-14 13:24:30 -04001596 if (tmp) {
1597 /*
1598 * If the read above didn't mark this buffer up to date,
1599 * it will never end up being up to date. Set ret to EIO now
1600 * and give up so that our caller doesn't loop forever
1601 * on our EAGAINs.
1602 */
1603 if (!btrfs_buffer_uptodate(tmp, 0))
1604 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001605 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001606 }
1607 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001608}
1609
1610/*
1611 * helper function for btrfs_search_slot. This does all of the checks
1612 * for node-level blocks and does any balancing required based on
1613 * the ins_len.
1614 *
1615 * If no extra work was required, zero is returned. If we had to
1616 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1617 * start over
1618 */
1619static int
1620setup_nodes_for_search(struct btrfs_trans_handle *trans,
1621 struct btrfs_root *root, struct btrfs_path *p,
1622 struct extent_buffer *b, int level, int ins_len)
1623{
1624 int ret;
1625 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1626 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1627 int sret;
1628
1629 sret = reada_for_balance(root, p, level);
1630 if (sret)
1631 goto again;
1632
1633 btrfs_set_path_blocking(p);
1634 sret = split_node(trans, root, p, level);
1635 btrfs_clear_path_blocking(p, NULL);
1636
1637 BUG_ON(sret > 0);
1638 if (sret) {
1639 ret = sret;
1640 goto done;
1641 }
1642 b = p->nodes[level];
1643 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001644 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001645 int sret;
1646
1647 sret = reada_for_balance(root, p, level);
1648 if (sret)
1649 goto again;
1650
1651 btrfs_set_path_blocking(p);
1652 sret = balance_level(trans, root, p, level);
1653 btrfs_clear_path_blocking(p, NULL);
1654
1655 if (sret) {
1656 ret = sret;
1657 goto done;
1658 }
1659 b = p->nodes[level];
1660 if (!b) {
1661 btrfs_release_path(NULL, p);
1662 goto again;
1663 }
1664 BUG_ON(btrfs_header_nritems(b) == 1);
1665 }
1666 return 0;
1667
1668again:
1669 ret = -EAGAIN;
1670done:
1671 return ret;
1672}
1673
1674/*
Chris Mason74123bd2007-02-02 11:05:29 -05001675 * look for key in the tree. path is filled in with nodes along the way
1676 * if key is found, we return zero and you can find the item in the leaf
1677 * level of the path (level 0)
1678 *
1679 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001680 * be inserted, and 1 is returned. If there are other errors during the
1681 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001682 *
1683 * if ins_len > 0, nodes and leaves will be split as we walk down the
1684 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1685 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001686 */
Chris Masone089f052007-03-16 16:20:31 -04001687int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1688 *root, struct btrfs_key *key, struct btrfs_path *p, int
1689 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001690{
Chris Mason5f39d392007-10-15 16:14:19 -04001691 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001692 int slot;
1693 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001694 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001695 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001696 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001697 u8 lowest_level = 0;
1698
Chris Mason6702ed42007-08-07 16:15:09 -04001699 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001700 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001701 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001702
Chris Mason925baed2008-06-25 16:01:30 -04001703 if (ins_len < 0)
1704 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001705
Chris Masonbb803952007-03-01 12:04:21 -05001706again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001707 if (p->search_commit_root) {
1708 b = root->commit_root;
1709 extent_buffer_get(b);
1710 if (!p->skip_locking)
1711 btrfs_tree_lock(b);
1712 } else {
1713 if (p->skip_locking)
1714 b = btrfs_root_node(root);
1715 else
1716 b = btrfs_lock_root_node(root);
1717 }
Chris Mason925baed2008-06-25 16:01:30 -04001718
Chris Masoneb60cea2007-02-02 09:18:22 -05001719 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001720 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001721
1722 /*
1723 * setup the path here so we can release it under lock
1724 * contention with the cow code
1725 */
1726 p->nodes[level] = b;
1727 if (!p->skip_locking)
1728 p->locks[level] = 1;
1729
Chris Mason02217ed2007-03-02 16:08:05 -05001730 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001731 /*
1732 * if we don't really need to cow this block
1733 * then we don't want to set the path blocking,
1734 * so we test it here
1735 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001736 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001737 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001738
Chris Masonb4ce94d2009-02-04 09:25:08 -05001739 btrfs_set_path_blocking(p);
1740
Yan Zheng33c66f42009-07-22 09:59:00 -04001741 err = btrfs_cow_block(trans, root, b,
1742 p->nodes[level + 1],
1743 p->slots[level + 1], &b);
1744 if (err) {
Chris Mason5f39d392007-10-15 16:14:19 -04001745 free_extent_buffer(b);
Yan Zheng33c66f42009-07-22 09:59:00 -04001746 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001747 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001748 }
Chris Mason02217ed2007-03-02 16:08:05 -05001749 }
Chris Mason65b51a02008-08-01 15:11:20 -04001750cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001751 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001752 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001753 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001754 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001755
Chris Masoneb60cea2007-02-02 09:18:22 -05001756 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001757 if (!p->skip_locking)
1758 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001759
Chris Mason4008c042009-02-12 14:09:45 -05001760 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001761
1762 /*
1763 * we have a lock on b and as long as we aren't changing
1764 * the tree, there is no way to for the items in b to change.
1765 * It is safe to drop the lock on our parent before we
1766 * go through the expensive btree search on b.
1767 *
1768 * If cow is true, then we might be changing slot zero,
1769 * which may require changing the parent. So, we can't
1770 * drop the lock until after we know which slot we're
1771 * operating on.
1772 */
1773 if (!cow)
1774 btrfs_unlock_up_safe(p, level + 1);
1775
Chris Mason123abc82007-03-14 14:14:43 -04001776 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001777 if (ret) {
1778 ret = -1;
1779 goto done;
1780 }
Chris Mason925baed2008-06-25 16:01:30 -04001781
Chris Mason5f39d392007-10-15 16:14:19 -04001782 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001783
Chris Mason5f39d392007-10-15 16:14:19 -04001784 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001785 int dec = 0;
1786 if (ret && slot > 0) {
1787 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001788 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001789 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001790 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001791 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001792 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001793 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001794 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001795 if (err) {
1796 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001797 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001798 }
Chris Masonc8c42862009-04-03 10:14:18 -04001799 b = p->nodes[level];
1800 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001801
Chris Masonf9efa9c2008-06-25 16:14:04 -04001802 unlock_up(p, level, lowest_unlock);
1803
Chris Mason925baed2008-06-25 16:01:30 -04001804 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001805 if (dec)
1806 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001807 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001808 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001809
Yan Zheng33c66f42009-07-22 09:59:00 -04001810 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001811 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001812 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001813 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001814 if (err) {
1815 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001816 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001817 }
Chris Mason76a05b32009-05-14 13:24:30 -04001818
Chris Masonb4ce94d2009-02-04 09:25:08 -05001819 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001820 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001821 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001822
Yan Zheng33c66f42009-07-22 09:59:00 -04001823 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001824 btrfs_set_path_blocking(p);
1825 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001826 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001827 }
1828 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001829 } else {
1830 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001831 if (ins_len > 0 &&
1832 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001833 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001834 err = split_leaf(trans, root, key,
1835 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001836 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001837
Yan Zheng33c66f42009-07-22 09:59:00 -04001838 BUG_ON(err > 0);
1839 if (err) {
1840 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001841 goto done;
1842 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001843 }
Chris Mason459931e2008-12-10 09:10:46 -05001844 if (!p->search_for_split)
1845 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001846 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001847 }
1848 }
Chris Mason65b51a02008-08-01 15:11:20 -04001849 ret = 1;
1850done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001851 /*
1852 * we don't really know what they plan on doing with the path
1853 * from here on, so for now just mark it as blocking
1854 */
Chris Masonb9473432009-03-13 11:00:37 -04001855 if (!p->leave_spinning)
1856 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001857 if (ret < 0)
1858 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001859 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001860}
1861
Chris Mason74123bd2007-02-02 11:05:29 -05001862/*
1863 * adjust the pointers going up the tree, starting at level
1864 * making sure the right key of each node is points to 'key'.
1865 * This is used after shifting pointers to the left, so it stops
1866 * fixing up pointers when a given leaf/node is not in slot 0 of the
1867 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001868 *
1869 * If this fails to write a tree block, it returns -1, but continues
1870 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001871 */
Chris Mason5f39d392007-10-15 16:14:19 -04001872static int fixup_low_keys(struct btrfs_trans_handle *trans,
1873 struct btrfs_root *root, struct btrfs_path *path,
1874 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001875{
1876 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001877 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001878 struct extent_buffer *t;
1879
Chris Mason234b63a2007-03-13 10:46:10 -04001880 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001881 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001882 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001883 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001884 t = path->nodes[i];
1885 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001886 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001887 if (tslot != 0)
1888 break;
1889 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001890 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001891}
1892
Chris Mason74123bd2007-02-02 11:05:29 -05001893/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001894 * update item key.
1895 *
1896 * This function isn't completely safe. It's the caller's responsibility
1897 * that the new key won't break the order
1898 */
1899int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1900 struct btrfs_root *root, struct btrfs_path *path,
1901 struct btrfs_key *new_key)
1902{
1903 struct btrfs_disk_key disk_key;
1904 struct extent_buffer *eb;
1905 int slot;
1906
1907 eb = path->nodes[0];
1908 slot = path->slots[0];
1909 if (slot > 0) {
1910 btrfs_item_key(eb, &disk_key, slot - 1);
1911 if (comp_keys(&disk_key, new_key) >= 0)
1912 return -1;
1913 }
1914 if (slot < btrfs_header_nritems(eb) - 1) {
1915 btrfs_item_key(eb, &disk_key, slot + 1);
1916 if (comp_keys(&disk_key, new_key) <= 0)
1917 return -1;
1918 }
1919
1920 btrfs_cpu_key_to_disk(&disk_key, new_key);
1921 btrfs_set_item_key(eb, &disk_key, slot);
1922 btrfs_mark_buffer_dirty(eb);
1923 if (slot == 0)
1924 fixup_low_keys(trans, root, path, &disk_key, 1);
1925 return 0;
1926}
1927
1928/*
Chris Mason74123bd2007-02-02 11:05:29 -05001929 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001930 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001931 *
1932 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1933 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001934 */
Chris Mason98ed5172008-01-03 10:01:48 -05001935static int push_node_left(struct btrfs_trans_handle *trans,
1936 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001937 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001939 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001940 int src_nritems;
1941 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001942 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001943
Chris Mason5f39d392007-10-15 16:14:19 -04001944 src_nritems = btrfs_header_nritems(src);
1945 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001946 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001947 WARN_ON(btrfs_header_generation(src) != trans->transid);
1948 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001949
Chris Masonbce4eae2008-04-24 14:42:46 -04001950 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001951 return 1;
1952
Chris Masond3977122009-01-05 21:25:51 -05001953 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001954 return 1;
1955
Chris Masonbce4eae2008-04-24 14:42:46 -04001956 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001957 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001958 if (push_items < src_nritems) {
1959 /* leave at least 8 pointers in the node if
1960 * we aren't going to empty it
1961 */
1962 if (src_nritems - push_items < 8) {
1963 if (push_items <= 8)
1964 return 1;
1965 push_items -= 8;
1966 }
1967 }
1968 } else
1969 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001970
Chris Mason5f39d392007-10-15 16:14:19 -04001971 copy_extent_buffer(dst, src,
1972 btrfs_node_key_ptr_offset(dst_nritems),
1973 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001974 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001975
Chris Masonbb803952007-03-01 12:04:21 -05001976 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001977 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1978 btrfs_node_key_ptr_offset(push_items),
1979 (src_nritems - push_items) *
1980 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001981 }
Chris Mason5f39d392007-10-15 16:14:19 -04001982 btrfs_set_header_nritems(src, src_nritems - push_items);
1983 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1984 btrfs_mark_buffer_dirty(src);
1985 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001986
Chris Masonbb803952007-03-01 12:04:21 -05001987 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001988}
1989
Chris Mason97571fd2007-02-24 13:39:08 -05001990/*
Chris Mason79f95c82007-03-01 15:16:26 -05001991 * try to push data from one node into the next node right in the
1992 * tree.
1993 *
1994 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1995 * error, and > 0 if there was no room in the right hand block.
1996 *
1997 * this will only push up to 1/2 the contents of the left node over
1998 */
Chris Mason5f39d392007-10-15 16:14:19 -04001999static int balance_node_right(struct btrfs_trans_handle *trans,
2000 struct btrfs_root *root,
2001 struct extent_buffer *dst,
2002 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002003{
Chris Mason79f95c82007-03-01 15:16:26 -05002004 int push_items = 0;
2005 int max_push;
2006 int src_nritems;
2007 int dst_nritems;
2008 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002009
Chris Mason7bb86312007-12-11 09:25:06 -05002010 WARN_ON(btrfs_header_generation(src) != trans->transid);
2011 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2012
Chris Mason5f39d392007-10-15 16:14:19 -04002013 src_nritems = btrfs_header_nritems(src);
2014 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002015 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002016 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002017 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002018
Chris Masond3977122009-01-05 21:25:51 -05002019 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002020 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002021
2022 max_push = src_nritems / 2 + 1;
2023 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002024 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002025 return 1;
Yan252c38f2007-08-29 09:11:44 -04002026
Chris Mason79f95c82007-03-01 15:16:26 -05002027 if (max_push < push_items)
2028 push_items = max_push;
2029
Chris Mason5f39d392007-10-15 16:14:19 -04002030 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2031 btrfs_node_key_ptr_offset(0),
2032 (dst_nritems) *
2033 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002034
Chris Mason5f39d392007-10-15 16:14:19 -04002035 copy_extent_buffer(dst, src,
2036 btrfs_node_key_ptr_offset(0),
2037 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002038 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002039
Chris Mason5f39d392007-10-15 16:14:19 -04002040 btrfs_set_header_nritems(src, src_nritems - push_items);
2041 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 btrfs_mark_buffer_dirty(src);
2044 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002045
Chris Mason79f95c82007-03-01 15:16:26 -05002046 return ret;
2047}
2048
2049/*
Chris Mason97571fd2007-02-24 13:39:08 -05002050 * helper function to insert a new root level in the tree.
2051 * A new node is allocated, and a single item is inserted to
2052 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002053 *
2054 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002055 */
Chris Masond3977122009-01-05 21:25:51 -05002056static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002057 struct btrfs_root *root,
2058 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002059{
Chris Mason7bb86312007-12-11 09:25:06 -05002060 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002061 struct extent_buffer *lower;
2062 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002063 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002064 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002065
2066 BUG_ON(path->nodes[level]);
2067 BUG_ON(path->nodes[level-1] != root->node);
2068
Chris Mason7bb86312007-12-11 09:25:06 -05002069 lower = path->nodes[level-1];
2070 if (level == 1)
2071 btrfs_item_key(lower, &lower_key, 0);
2072 else
2073 btrfs_node_key(lower, &lower_key, 0);
2074
Zheng Yan31840ae2008-09-23 13:14:14 -04002075 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002077 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002078 if (IS_ERR(c))
2079 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002080
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002081 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002082 btrfs_set_header_nritems(c, 1);
2083 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002084 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002085 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002087 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002088
Chris Mason5f39d392007-10-15 16:14:19 -04002089 write_extent_buffer(c, root->fs_info->fsid,
2090 (unsigned long)btrfs_header_fsid(c),
2091 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002092
2093 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2094 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2095 BTRFS_UUID_SIZE);
2096
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002098 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002099 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002100 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002101
2102 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002103
2104 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002105
Chris Mason925baed2008-06-25 16:01:30 -04002106 spin_lock(&root->node_lock);
2107 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002108 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002109 spin_unlock(&root->node_lock);
2110
2111 /* the super has an extra ref to root->node */
2112 free_extent_buffer(old);
2113
Chris Mason0b86a832008-03-24 15:01:56 -04002114 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002115 extent_buffer_get(c);
2116 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002117 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002118 path->slots[level] = 0;
2119 return 0;
2120}
2121
Chris Mason74123bd2007-02-02 11:05:29 -05002122/*
2123 * worker function to insert a single pointer in a node.
2124 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002125 *
Chris Mason74123bd2007-02-02 11:05:29 -05002126 * slot and level indicate where you want the key to go, and
2127 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002128 *
2129 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002130 */
Chris Masone089f052007-03-16 16:20:31 -04002131static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2132 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002133 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002134{
Chris Mason5f39d392007-10-15 16:14:19 -04002135 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002136 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002137
2138 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002139 lower = path->nodes[level];
2140 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002141 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002142 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002143 BUG();
2144 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002145 memmove_extent_buffer(lower,
2146 btrfs_node_key_ptr_offset(slot + 1),
2147 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002148 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002149 }
Chris Mason5f39d392007-10-15 16:14:19 -04002150 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002151 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002152 WARN_ON(trans->transid == 0);
2153 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002154 btrfs_set_header_nritems(lower, nritems + 1);
2155 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002156 return 0;
2157}
2158
Chris Mason97571fd2007-02-24 13:39:08 -05002159/*
2160 * split the node at the specified level in path in two.
2161 * The path is corrected to point to the appropriate node after the split
2162 *
2163 * Before splitting this tries to make some room in the node by pushing
2164 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002165 *
2166 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002167 */
Chris Masone02119d2008-09-05 16:13:11 -04002168static noinline int split_node(struct btrfs_trans_handle *trans,
2169 struct btrfs_root *root,
2170 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002171{
Chris Mason5f39d392007-10-15 16:14:19 -04002172 struct extent_buffer *c;
2173 struct extent_buffer *split;
2174 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002175 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002176 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002177 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002178 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002179
Chris Mason5f39d392007-10-15 16:14:19 -04002180 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002181 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002183 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002184 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002185 if (ret)
2186 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002187 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002188 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002189 c = path->nodes[level];
2190 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002191 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002192 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002193 if (ret < 0)
2194 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002195 }
Chris Masone66f7092007-04-20 13:16:02 -04002196
Chris Mason5f39d392007-10-15 16:14:19 -04002197 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 mid = (c_nritems + 1) / 2;
2199 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002200
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002202 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002203 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002204 if (IS_ERR(split))
2205 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002206
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002207 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002208 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002209 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002210 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002211 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002212 btrfs_set_header_owner(split, root->root_key.objectid);
2213 write_extent_buffer(split, root->fs_info->fsid,
2214 (unsigned long)btrfs_header_fsid(split),
2215 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002216 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2217 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2218 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002219
Chris Mason5f39d392007-10-15 16:14:19 -04002220
2221 copy_extent_buffer(split, c,
2222 btrfs_node_key_ptr_offset(0),
2223 btrfs_node_key_ptr_offset(mid),
2224 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2225 btrfs_set_header_nritems(split, c_nritems - mid);
2226 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002227 ret = 0;
2228
Chris Mason5f39d392007-10-15 16:14:19 -04002229 btrfs_mark_buffer_dirty(c);
2230 btrfs_mark_buffer_dirty(split);
2231
Chris Masondb945352007-10-15 16:15:53 -04002232 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002233 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002234 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002235 if (wret)
2236 ret = wret;
2237
Chris Mason5de08d72007-02-24 06:24:44 -05002238 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002239 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002240 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002241 free_extent_buffer(c);
2242 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002243 path->slots[level + 1] += 1;
2244 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002245 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002246 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002247 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002248 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002249}
2250
Chris Mason74123bd2007-02-02 11:05:29 -05002251/*
2252 * how many bytes are required to store the items in a leaf. start
2253 * and nr indicate which items in the leaf to check. This totals up the
2254 * space used both by the item structs and the item data
2255 */
Chris Mason5f39d392007-10-15 16:14:19 -04002256static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002257{
2258 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002259 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002260 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002261
2262 if (!nr)
2263 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002264 data_len = btrfs_item_end_nr(l, start);
2265 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002266 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002267 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002268 return data_len;
2269}
2270
Chris Mason74123bd2007-02-02 11:05:29 -05002271/*
Chris Masond4dbff92007-04-04 14:08:15 -04002272 * The space between the end of the leaf items and
2273 * the start of the leaf data. IOW, how much room
2274 * the leaf has left for both items and data
2275 */
Chris Masond3977122009-01-05 21:25:51 -05002276noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002277 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002278{
Chris Mason5f39d392007-10-15 16:14:19 -04002279 int nritems = btrfs_header_nritems(leaf);
2280 int ret;
2281 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2282 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002283 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2284 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002285 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002286 leaf_space_used(leaf, 0, nritems), nritems);
2287 }
2288 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002289}
2290
Chris Mason44871b12009-03-13 10:04:31 -04002291static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2292 struct btrfs_root *root,
2293 struct btrfs_path *path,
2294 int data_size, int empty,
2295 struct extent_buffer *right,
2296 int free_space, u32 left_nritems)
Chris Mason00ec4c52007-02-24 12:47:20 -05002297{
Chris Mason5f39d392007-10-15 16:14:19 -04002298 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002299 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002300 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002301 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002302 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002303 int push_space = 0;
2304 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002305 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002306 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002307 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002308 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002309 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002310
Chris Mason34a38212007-11-07 13:31:03 -05002311 if (empty)
2312 nr = 0;
2313 else
2314 nr = 1;
2315
Zheng Yan31840ae2008-09-23 13:14:14 -04002316 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002317 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002318
Chris Mason44871b12009-03-13 10:04:31 -04002319 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002320 i = left_nritems - 1;
2321 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002322 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002323
Zheng Yan31840ae2008-09-23 13:14:14 -04002324 if (!empty && push_items > 0) {
2325 if (path->slots[0] > i)
2326 break;
2327 if (path->slots[0] == i) {
2328 int space = btrfs_leaf_free_space(root, left);
2329 if (space + push_space * 2 > free_space)
2330 break;
2331 }
2332 }
2333
Chris Mason00ec4c52007-02-24 12:47:20 -05002334 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002335 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002336
2337 if (!left->map_token) {
2338 map_extent_buffer(left, (unsigned long)item,
2339 sizeof(struct btrfs_item),
2340 &left->map_token, &left->kaddr,
2341 &left->map_start, &left->map_len,
2342 KM_USER1);
2343 }
2344
2345 this_item_size = btrfs_item_size(left, item);
2346 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002347 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002348
Chris Mason00ec4c52007-02-24 12:47:20 -05002349 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002350 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002351 if (i == 0)
2352 break;
2353 i--;
Chris Masondb945352007-10-15 16:15:53 -04002354 }
2355 if (left->map_token) {
2356 unmap_extent_buffer(left, left->map_token, KM_USER1);
2357 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002358 }
Chris Mason5f39d392007-10-15 16:14:19 -04002359
Chris Mason925baed2008-06-25 16:01:30 -04002360 if (push_items == 0)
2361 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002362
Chris Mason34a38212007-11-07 13:31:03 -05002363 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002364 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002365
Chris Mason00ec4c52007-02-24 12:47:20 -05002366 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002367 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002368
Chris Mason5f39d392007-10-15 16:14:19 -04002369 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002370 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002371
Chris Mason00ec4c52007-02-24 12:47:20 -05002372 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002373 data_end = leaf_data_end(root, right);
2374 memmove_extent_buffer(right,
2375 btrfs_leaf_data(right) + data_end - push_space,
2376 btrfs_leaf_data(right) + data_end,
2377 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2378
Chris Mason00ec4c52007-02-24 12:47:20 -05002379 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002380 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002381 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2382 btrfs_leaf_data(left) + leaf_data_end(root, left),
2383 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002384
2385 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2386 btrfs_item_nr_offset(0),
2387 right_nritems * sizeof(struct btrfs_item));
2388
Chris Mason00ec4c52007-02-24 12:47:20 -05002389 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002390 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2391 btrfs_item_nr_offset(left_nritems - push_items),
2392 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002393
2394 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002395 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002396 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002397 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002398 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002399 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002400 if (!right->map_token) {
2401 map_extent_buffer(right, (unsigned long)item,
2402 sizeof(struct btrfs_item),
2403 &right->map_token, &right->kaddr,
2404 &right->map_start, &right->map_len,
2405 KM_USER1);
2406 }
2407 push_space -= btrfs_item_size(right, item);
2408 btrfs_set_item_offset(right, item, push_space);
2409 }
2410
2411 if (right->map_token) {
2412 unmap_extent_buffer(right, right->map_token, KM_USER1);
2413 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002414 }
Chris Mason7518a232007-03-12 12:01:18 -04002415 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002416 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002417
Chris Mason34a38212007-11-07 13:31:03 -05002418 if (left_nritems)
2419 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002420 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002421
Chris Mason5f39d392007-10-15 16:14:19 -04002422 btrfs_item_key(right, &disk_key, 0);
2423 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002424 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002425
Chris Mason00ec4c52007-02-24 12:47:20 -05002426 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002427 if (path->slots[0] >= left_nritems) {
2428 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002429 if (btrfs_header_nritems(path->nodes[0]) == 0)
2430 clean_tree_block(trans, root, path->nodes[0]);
2431 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002432 free_extent_buffer(path->nodes[0]);
2433 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002434 path->slots[1] += 1;
2435 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002436 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002437 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002438 }
2439 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002440
2441out_unlock:
2442 btrfs_tree_unlock(right);
2443 free_extent_buffer(right);
2444 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002445}
Chris Mason925baed2008-06-25 16:01:30 -04002446
Chris Mason00ec4c52007-02-24 12:47:20 -05002447/*
Chris Mason44871b12009-03-13 10:04:31 -04002448 * push some data in the path leaf to the right, trying to free up at
2449 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2450 *
2451 * returns 1 if the push failed because the other node didn't have enough
2452 * room, 0 if everything worked out and < 0 if there were major errors.
2453 */
2454static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2455 *root, struct btrfs_path *path, int data_size,
2456 int empty)
2457{
2458 struct extent_buffer *left = path->nodes[0];
2459 struct extent_buffer *right;
2460 struct extent_buffer *upper;
2461 int slot;
2462 int free_space;
2463 u32 left_nritems;
2464 int ret;
2465
2466 if (!path->nodes[1])
2467 return 1;
2468
2469 slot = path->slots[1];
2470 upper = path->nodes[1];
2471 if (slot >= btrfs_header_nritems(upper) - 1)
2472 return 1;
2473
2474 btrfs_assert_tree_locked(path->nodes[1]);
2475
2476 right = read_node_slot(root, upper, slot + 1);
2477 btrfs_tree_lock(right);
2478 btrfs_set_lock_blocking(right);
2479
2480 free_space = btrfs_leaf_free_space(root, right);
2481 if (free_space < data_size)
2482 goto out_unlock;
2483
2484 /* cow and double check */
2485 ret = btrfs_cow_block(trans, root, right, upper,
2486 slot + 1, &right);
2487 if (ret)
2488 goto out_unlock;
2489
2490 free_space = btrfs_leaf_free_space(root, right);
2491 if (free_space < data_size)
2492 goto out_unlock;
2493
2494 left_nritems = btrfs_header_nritems(left);
2495 if (left_nritems == 0)
2496 goto out_unlock;
2497
2498 return __push_leaf_right(trans, root, path, data_size, empty,
2499 right, free_space, left_nritems);
2500out_unlock:
2501 btrfs_tree_unlock(right);
2502 free_extent_buffer(right);
2503 return 1;
2504}
2505
2506/*
Chris Mason74123bd2007-02-02 11:05:29 -05002507 * push some data in the path leaf to the left, trying to free up at
2508 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2509 */
Chris Mason44871b12009-03-13 10:04:31 -04002510static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2511 struct btrfs_root *root,
2512 struct btrfs_path *path, int data_size,
2513 int empty, struct extent_buffer *left,
2514 int free_space, int right_nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515{
Chris Mason5f39d392007-10-15 16:14:19 -04002516 struct btrfs_disk_key disk_key;
2517 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002518 int slot;
2519 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002520 int push_space = 0;
2521 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002522 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002523 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002524 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002525 int ret = 0;
2526 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002527 u32 this_item_size;
2528 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002529
2530 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002531
Chris Mason34a38212007-11-07 13:31:03 -05002532 if (empty)
2533 nr = right_nritems;
2534 else
2535 nr = right_nritems - 1;
2536
2537 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002538 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002539 if (!right->map_token) {
2540 map_extent_buffer(right, (unsigned long)item,
2541 sizeof(struct btrfs_item),
2542 &right->map_token, &right->kaddr,
2543 &right->map_start, &right->map_len,
2544 KM_USER1);
2545 }
2546
Zheng Yan31840ae2008-09-23 13:14:14 -04002547 if (!empty && push_items > 0) {
2548 if (path->slots[0] < i)
2549 break;
2550 if (path->slots[0] == i) {
2551 int space = btrfs_leaf_free_space(root, right);
2552 if (space + push_space * 2 > free_space)
2553 break;
2554 }
2555 }
2556
Chris Masonbe0e5c02007-01-26 15:51:26 -05002557 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002558 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002559
2560 this_item_size = btrfs_item_size(right, item);
2561 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002562 break;
Chris Masondb945352007-10-15 16:15:53 -04002563
Chris Masonbe0e5c02007-01-26 15:51:26 -05002564 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002565 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002566 }
Chris Masondb945352007-10-15 16:15:53 -04002567
2568 if (right->map_token) {
2569 unmap_extent_buffer(right, right->map_token, KM_USER1);
2570 right->map_token = NULL;
2571 }
2572
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002574 ret = 1;
2575 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002576 }
Chris Mason34a38212007-11-07 13:31:03 -05002577 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002578 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002579
Chris Masonbe0e5c02007-01-26 15:51:26 -05002580 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002581 copy_extent_buffer(left, right,
2582 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2583 btrfs_item_nr_offset(0),
2584 push_items * sizeof(struct btrfs_item));
2585
Chris Mason123abc82007-03-14 14:14:43 -04002586 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002587 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002588
2589 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002590 leaf_data_end(root, left) - push_space,
2591 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002592 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002593 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002594 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002595 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002596
Chris Masondb945352007-10-15 16:15:53 -04002597 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002598 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002599 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002600
Chris Mason5f39d392007-10-15 16:14:19 -04002601 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002602 if (!left->map_token) {
2603 map_extent_buffer(left, (unsigned long)item,
2604 sizeof(struct btrfs_item),
2605 &left->map_token, &left->kaddr,
2606 &left->map_start, &left->map_len,
2607 KM_USER1);
2608 }
2609
Chris Mason5f39d392007-10-15 16:14:19 -04002610 ioff = btrfs_item_offset(left, item);
2611 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002612 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002613 }
Chris Mason5f39d392007-10-15 16:14:19 -04002614 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002615 if (left->map_token) {
2616 unmap_extent_buffer(left, left->map_token, KM_USER1);
2617 left->map_token = NULL;
2618 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002619
2620 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002621 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002622 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2623 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002624 WARN_ON(1);
2625 }
Chris Mason5f39d392007-10-15 16:14:19 -04002626
Chris Mason34a38212007-11-07 13:31:03 -05002627 if (push_items < right_nritems) {
2628 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2629 leaf_data_end(root, right);
2630 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2631 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2632 btrfs_leaf_data(right) +
2633 leaf_data_end(root, right), push_space);
2634
2635 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002636 btrfs_item_nr_offset(push_items),
2637 (btrfs_header_nritems(right) - push_items) *
2638 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002639 }
Yaneef1c492007-11-26 10:58:13 -05002640 right_nritems -= push_items;
2641 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002642 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002643 for (i = 0; i < right_nritems; i++) {
2644 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002645
2646 if (!right->map_token) {
2647 map_extent_buffer(right, (unsigned long)item,
2648 sizeof(struct btrfs_item),
2649 &right->map_token, &right->kaddr,
2650 &right->map_start, &right->map_len,
2651 KM_USER1);
2652 }
2653
2654 push_space = push_space - btrfs_item_size(right, item);
2655 btrfs_set_item_offset(right, item, push_space);
2656 }
2657 if (right->map_token) {
2658 unmap_extent_buffer(right, right->map_token, KM_USER1);
2659 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002660 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002661
Chris Mason5f39d392007-10-15 16:14:19 -04002662 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002663 if (right_nritems)
2664 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002665
Chris Mason5f39d392007-10-15 16:14:19 -04002666 btrfs_item_key(right, &disk_key, 0);
2667 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002668 if (wret)
2669 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002670
2671 /* then fixup the leaf pointer in the path */
2672 if (path->slots[0] < push_items) {
2673 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002674 if (btrfs_header_nritems(path->nodes[0]) == 0)
2675 clean_tree_block(trans, root, path->nodes[0]);
2676 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 free_extent_buffer(path->nodes[0]);
2678 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002679 path->slots[1] -= 1;
2680 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002681 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002682 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002683 path->slots[0] -= push_items;
2684 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002685 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002686 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002687out:
2688 btrfs_tree_unlock(left);
2689 free_extent_buffer(left);
2690 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002691}
2692
Chris Mason74123bd2007-02-02 11:05:29 -05002693/*
Chris Mason44871b12009-03-13 10:04:31 -04002694 * push some data in the path leaf to the left, trying to free up at
2695 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2696 */
2697static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2698 *root, struct btrfs_path *path, int data_size,
2699 int empty)
2700{
2701 struct extent_buffer *right = path->nodes[0];
2702 struct extent_buffer *left;
2703 int slot;
2704 int free_space;
2705 u32 right_nritems;
2706 int ret = 0;
2707
2708 slot = path->slots[1];
2709 if (slot == 0)
2710 return 1;
2711 if (!path->nodes[1])
2712 return 1;
2713
2714 right_nritems = btrfs_header_nritems(right);
2715 if (right_nritems == 0)
2716 return 1;
2717
2718 btrfs_assert_tree_locked(path->nodes[1]);
2719
2720 left = read_node_slot(root, path->nodes[1], slot - 1);
2721 btrfs_tree_lock(left);
2722 btrfs_set_lock_blocking(left);
2723
2724 free_space = btrfs_leaf_free_space(root, left);
2725 if (free_space < data_size) {
2726 ret = 1;
2727 goto out;
2728 }
2729
2730 /* cow and double check */
2731 ret = btrfs_cow_block(trans, root, left,
2732 path->nodes[1], slot - 1, &left);
2733 if (ret) {
2734 /* we hit -ENOSPC, but it isn't fatal here */
2735 ret = 1;
2736 goto out;
2737 }
2738
2739 free_space = btrfs_leaf_free_space(root, left);
2740 if (free_space < data_size) {
2741 ret = 1;
2742 goto out;
2743 }
2744
2745 return __push_leaf_left(trans, root, path, data_size,
2746 empty, left, free_space, right_nritems);
2747out:
2748 btrfs_tree_unlock(left);
2749 free_extent_buffer(left);
2750 return ret;
2751}
2752
2753/*
Chris Mason74123bd2007-02-02 11:05:29 -05002754 * split the path's leaf in two, making sure there is at least data_size
2755 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002756 *
2757 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002758 */
Chris Mason44871b12009-03-13 10:04:31 -04002759static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002760 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002761 struct btrfs_path *path,
2762 struct extent_buffer *l,
2763 struct extent_buffer *right,
2764 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002765{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002766 int data_copy_size;
2767 int rt_data_off;
2768 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002769 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002770 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002771 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002772
Chris Mason5f39d392007-10-15 16:14:19 -04002773 nritems = nritems - mid;
2774 btrfs_set_header_nritems(right, nritems);
2775 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2776
2777 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2778 btrfs_item_nr_offset(mid),
2779 nritems * sizeof(struct btrfs_item));
2780
2781 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002782 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2783 data_copy_size, btrfs_leaf_data(l) +
2784 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002785
Chris Mason5f39d392007-10-15 16:14:19 -04002786 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2787 btrfs_item_end_nr(l, mid);
2788
2789 for (i = 0; i < nritems; i++) {
2790 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002791 u32 ioff;
2792
2793 if (!right->map_token) {
2794 map_extent_buffer(right, (unsigned long)item,
2795 sizeof(struct btrfs_item),
2796 &right->map_token, &right->kaddr,
2797 &right->map_start, &right->map_len,
2798 KM_USER1);
2799 }
2800
2801 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002802 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002803 }
Chris Mason74123bd2007-02-02 11:05:29 -05002804
Chris Masondb945352007-10-15 16:15:53 -04002805 if (right->map_token) {
2806 unmap_extent_buffer(right, right->map_token, KM_USER1);
2807 right->map_token = NULL;
2808 }
2809
Chris Mason5f39d392007-10-15 16:14:19 -04002810 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002811 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002812 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002813 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2814 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002815 if (wret)
2816 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002817
2818 btrfs_mark_buffer_dirty(right);
2819 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002820 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002821
Chris Masonbe0e5c02007-01-26 15:51:26 -05002822 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002823 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002824 free_extent_buffer(path->nodes[0]);
2825 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002826 path->slots[0] -= mid;
2827 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002828 } else {
2829 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002830 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002831 }
Chris Mason5f39d392007-10-15 16:14:19 -04002832
Chris Masoneb60cea2007-02-02 09:18:22 -05002833 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002834
Chris Mason44871b12009-03-13 10:04:31 -04002835 return ret;
2836}
2837
2838/*
2839 * split the path's leaf in two, making sure there is at least data_size
2840 * available for the resulting leaf level of the path.
2841 *
2842 * returns 0 if all went well and < 0 on failure.
2843 */
2844static noinline int split_leaf(struct btrfs_trans_handle *trans,
2845 struct btrfs_root *root,
2846 struct btrfs_key *ins_key,
2847 struct btrfs_path *path, int data_size,
2848 int extend)
2849{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002850 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002851 struct extent_buffer *l;
2852 u32 nritems;
2853 int mid;
2854 int slot;
2855 struct extent_buffer *right;
2856 int ret = 0;
2857 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002858 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002859 int num_doubles = 0;
2860
Yan, Zhenga5719522009-09-24 09:17:31 -04002861 l = path->nodes[0];
2862 slot = path->slots[0];
2863 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2864 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2865 return -EOVERFLOW;
2866
Chris Mason44871b12009-03-13 10:04:31 -04002867 /* first try to make some room by pushing left and right */
Chris Masonb3612422009-05-13 19:12:15 -04002868 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason44871b12009-03-13 10:04:31 -04002869 wret = push_leaf_right(trans, root, path, data_size, 0);
2870 if (wret < 0)
2871 return wret;
2872 if (wret) {
2873 wret = push_leaf_left(trans, root, path, data_size, 0);
2874 if (wret < 0)
2875 return wret;
2876 }
2877 l = path->nodes[0];
2878
2879 /* did the pushes work? */
2880 if (btrfs_leaf_free_space(root, l) >= data_size)
2881 return 0;
2882 }
2883
2884 if (!path->nodes[1]) {
2885 ret = insert_new_root(trans, root, path, 1);
2886 if (ret)
2887 return ret;
2888 }
2889again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002890 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002891 l = path->nodes[0];
2892 slot = path->slots[0];
2893 nritems = btrfs_header_nritems(l);
2894 mid = (nritems + 1) / 2;
2895
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002896 if (mid <= slot) {
2897 if (nritems == 1 ||
2898 leaf_space_used(l, mid, nritems - mid) + data_size >
2899 BTRFS_LEAF_DATA_SIZE(root)) {
2900 if (slot >= nritems) {
2901 split = 0;
2902 } else {
2903 mid = slot;
2904 if (mid != nritems &&
2905 leaf_space_used(l, mid, nritems - mid) +
2906 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2907 split = 2;
2908 }
2909 }
2910 }
2911 } else {
2912 if (leaf_space_used(l, 0, mid) + data_size >
2913 BTRFS_LEAF_DATA_SIZE(root)) {
2914 if (!extend && data_size && slot == 0) {
2915 split = 0;
2916 } else if ((extend || !data_size) && slot == 0) {
2917 mid = 1;
2918 } else {
2919 mid = slot;
2920 if (mid != nritems &&
2921 leaf_space_used(l, mid, nritems - mid) +
2922 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2923 split = 2 ;
2924 }
2925 }
2926 }
2927 }
2928
2929 if (split == 0)
2930 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2931 else
2932 btrfs_item_key(l, &disk_key, mid);
2933
2934 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002935 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002936 &disk_key, 0, l->start, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002937 if (IS_ERR(right)) {
2938 BUG_ON(1);
2939 return PTR_ERR(right);
2940 }
2941
2942 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2943 btrfs_set_header_bytenr(right, right->start);
2944 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002945 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002946 btrfs_set_header_owner(right, root->root_key.objectid);
2947 btrfs_set_header_level(right, 0);
2948 write_extent_buffer(right, root->fs_info->fsid,
2949 (unsigned long)btrfs_header_fsid(right),
2950 BTRFS_FSID_SIZE);
2951
2952 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2953 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2954 BTRFS_UUID_SIZE);
2955
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002956 if (split == 0) {
2957 if (mid <= slot) {
2958 btrfs_set_header_nritems(right, 0);
2959 wret = insert_ptr(trans, root, path,
2960 &disk_key, right->start,
2961 path->slots[1] + 1, 1);
2962 if (wret)
2963 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002964
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002965 btrfs_tree_unlock(path->nodes[0]);
2966 free_extent_buffer(path->nodes[0]);
2967 path->nodes[0] = right;
2968 path->slots[0] = 0;
2969 path->slots[1] += 1;
2970 } else {
2971 btrfs_set_header_nritems(right, 0);
2972 wret = insert_ptr(trans, root, path,
2973 &disk_key,
2974 right->start,
2975 path->slots[1], 1);
2976 if (wret)
2977 ret = wret;
2978 btrfs_tree_unlock(path->nodes[0]);
2979 free_extent_buffer(path->nodes[0]);
2980 path->nodes[0] = right;
2981 path->slots[0] = 0;
2982 if (path->slots[1] == 0) {
2983 wret = fixup_low_keys(trans, root,
2984 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002985 if (wret)
2986 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002987 }
2988 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002989 btrfs_mark_buffer_dirty(right);
2990 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002991 }
2992
2993 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2994 BUG_ON(ret);
2995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002996 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002997 BUG_ON(num_doubles != 0);
2998 num_doubles++;
2999 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003000 }
Chris Mason44871b12009-03-13 10:04:31 -04003001
Chris Masonbe0e5c02007-01-26 15:51:26 -05003002 return ret;
3003}
3004
Yan, Zhengad48fd752009-11-12 09:33:58 +00003005static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3006 struct btrfs_root *root,
3007 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003008{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003009 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003010 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003011 struct btrfs_file_extent_item *fi;
3012 u64 extent_len = 0;
3013 u32 item_size;
3014 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003015
3016 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003017 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3018
3019 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3020 key.type != BTRFS_EXTENT_CSUM_KEY);
3021
3022 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3023 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003024
3025 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003026 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3027 fi = btrfs_item_ptr(leaf, path->slots[0],
3028 struct btrfs_file_extent_item);
3029 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3030 }
Chris Mason459931e2008-12-10 09:10:46 -05003031 btrfs_release_path(root, path);
3032
Chris Mason459931e2008-12-10 09:10:46 -05003033 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003034 path->search_for_split = 1;
3035 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003036 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003037 if (ret < 0)
3038 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003039
Yan, Zhengad48fd752009-11-12 09:33:58 +00003040 ret = -EAGAIN;
3041 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003042 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003043 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3044 goto err;
3045
3046 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3047 fi = btrfs_item_ptr(leaf, path->slots[0],
3048 struct btrfs_file_extent_item);
3049 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3050 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003051 }
3052
Chris Masonb9473432009-03-13 11:00:37 -04003053 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003054 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003055 BUG_ON(ret);
3056
Yan, Zhengad48fd752009-11-12 09:33:58 +00003057 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003058 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003059 return 0;
3060err:
3061 path->keep_locks = 0;
3062 return ret;
3063}
3064
3065static noinline int split_item(struct btrfs_trans_handle *trans,
3066 struct btrfs_root *root,
3067 struct btrfs_path *path,
3068 struct btrfs_key *new_key,
3069 unsigned long split_offset)
3070{
3071 struct extent_buffer *leaf;
3072 struct btrfs_item *item;
3073 struct btrfs_item *new_item;
3074 int slot;
3075 char *buf;
3076 u32 nritems;
3077 u32 item_size;
3078 u32 orig_offset;
3079 struct btrfs_disk_key disk_key;
3080
Chris Masonb9473432009-03-13 11:00:37 -04003081 leaf = path->nodes[0];
3082 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3083
Chris Masonb4ce94d2009-02-04 09:25:08 -05003084 btrfs_set_path_blocking(path);
3085
Chris Mason459931e2008-12-10 09:10:46 -05003086 item = btrfs_item_nr(leaf, path->slots[0]);
3087 orig_offset = btrfs_item_offset(leaf, item);
3088 item_size = btrfs_item_size(leaf, item);
3089
Chris Mason459931e2008-12-10 09:10:46 -05003090 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003091 if (!buf)
3092 return -ENOMEM;
3093
Chris Mason459931e2008-12-10 09:10:46 -05003094 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3095 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003096
Chris Mason459931e2008-12-10 09:10:46 -05003097 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003098 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003099 if (slot != nritems) {
3100 /* shift the items */
3101 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003102 btrfs_item_nr_offset(slot),
3103 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003104 }
3105
3106 btrfs_cpu_key_to_disk(&disk_key, new_key);
3107 btrfs_set_item_key(leaf, &disk_key, slot);
3108
3109 new_item = btrfs_item_nr(leaf, slot);
3110
3111 btrfs_set_item_offset(leaf, new_item, orig_offset);
3112 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3113
3114 btrfs_set_item_offset(leaf, item,
3115 orig_offset + item_size - split_offset);
3116 btrfs_set_item_size(leaf, item, split_offset);
3117
3118 btrfs_set_header_nritems(leaf, nritems + 1);
3119
3120 /* write the data for the start of the original item */
3121 write_extent_buffer(leaf, buf,
3122 btrfs_item_ptr_offset(leaf, path->slots[0]),
3123 split_offset);
3124
3125 /* write the data for the new item */
3126 write_extent_buffer(leaf, buf + split_offset,
3127 btrfs_item_ptr_offset(leaf, slot),
3128 item_size - split_offset);
3129 btrfs_mark_buffer_dirty(leaf);
3130
Yan, Zhengad48fd752009-11-12 09:33:58 +00003131 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003132 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003133 return 0;
3134}
3135
3136/*
3137 * This function splits a single item into two items,
3138 * giving 'new_key' to the new item and splitting the
3139 * old one at split_offset (from the start of the item).
3140 *
3141 * The path may be released by this operation. After
3142 * the split, the path is pointing to the old item. The
3143 * new item is going to be in the same node as the old one.
3144 *
3145 * Note, the item being split must be smaller enough to live alone on
3146 * a tree block with room for one extra struct btrfs_item
3147 *
3148 * This allows us to split the item in place, keeping a lock on the
3149 * leaf the entire time.
3150 */
3151int btrfs_split_item(struct btrfs_trans_handle *trans,
3152 struct btrfs_root *root,
3153 struct btrfs_path *path,
3154 struct btrfs_key *new_key,
3155 unsigned long split_offset)
3156{
3157 int ret;
3158 ret = setup_leaf_for_split(trans, root, path,
3159 sizeof(struct btrfs_item));
3160 if (ret)
3161 return ret;
3162
3163 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003164 return ret;
3165}
3166
3167/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003168 * This function duplicate a item, giving 'new_key' to the new item.
3169 * It guarantees both items live in the same tree leaf and the new item
3170 * is contiguous with the original item.
3171 *
3172 * This allows us to split file extent in place, keeping a lock on the
3173 * leaf the entire time.
3174 */
3175int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3176 struct btrfs_root *root,
3177 struct btrfs_path *path,
3178 struct btrfs_key *new_key)
3179{
3180 struct extent_buffer *leaf;
3181 int ret;
3182 u32 item_size;
3183
3184 leaf = path->nodes[0];
3185 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3186 ret = setup_leaf_for_split(trans, root, path,
3187 item_size + sizeof(struct btrfs_item));
3188 if (ret)
3189 return ret;
3190
3191 path->slots[0]++;
3192 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3193 item_size, item_size +
3194 sizeof(struct btrfs_item), 1);
3195 BUG_ON(ret);
3196
3197 leaf = path->nodes[0];
3198 memcpy_extent_buffer(leaf,
3199 btrfs_item_ptr_offset(leaf, path->slots[0]),
3200 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3201 item_size);
3202 return 0;
3203}
3204
3205/*
Chris Masond352ac62008-09-29 15:18:18 -04003206 * make the item pointed to by the path smaller. new_size indicates
3207 * how small to make it, and from_end tells us if we just chop bytes
3208 * off the end of the item or if we shift the item to chop bytes off
3209 * the front.
3210 */
Chris Masonb18c6682007-04-17 13:26:50 -04003211int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3212 struct btrfs_root *root,
3213 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003214 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003215{
3216 int ret = 0;
3217 int slot;
3218 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003219 struct extent_buffer *leaf;
3220 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003221 u32 nritems;
3222 unsigned int data_end;
3223 unsigned int old_data_start;
3224 unsigned int old_size;
3225 unsigned int size_diff;
3226 int i;
3227
3228 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003229 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003230 slot = path->slots[0];
3231
3232 old_size = btrfs_item_size_nr(leaf, slot);
3233 if (old_size == new_size)
3234 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003235
Chris Mason5f39d392007-10-15 16:14:19 -04003236 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003237 data_end = leaf_data_end(root, leaf);
3238
Chris Mason5f39d392007-10-15 16:14:19 -04003239 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003240
Chris Masonb18c6682007-04-17 13:26:50 -04003241 size_diff = old_size - new_size;
3242
3243 BUG_ON(slot < 0);
3244 BUG_ON(slot >= nritems);
3245
3246 /*
3247 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3248 */
3249 /* first correct the data pointers */
3250 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003251 u32 ioff;
3252 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003253
3254 if (!leaf->map_token) {
3255 map_extent_buffer(leaf, (unsigned long)item,
3256 sizeof(struct btrfs_item),
3257 &leaf->map_token, &leaf->kaddr,
3258 &leaf->map_start, &leaf->map_len,
3259 KM_USER1);
3260 }
3261
Chris Mason5f39d392007-10-15 16:14:19 -04003262 ioff = btrfs_item_offset(leaf, item);
3263 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003264 }
Chris Masondb945352007-10-15 16:15:53 -04003265
3266 if (leaf->map_token) {
3267 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3268 leaf->map_token = NULL;
3269 }
3270
Chris Masonb18c6682007-04-17 13:26:50 -04003271 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003272 if (from_end) {
3273 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3274 data_end + size_diff, btrfs_leaf_data(leaf) +
3275 data_end, old_data_start + new_size - data_end);
3276 } else {
3277 struct btrfs_disk_key disk_key;
3278 u64 offset;
3279
3280 btrfs_item_key(leaf, &disk_key, slot);
3281
3282 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3283 unsigned long ptr;
3284 struct btrfs_file_extent_item *fi;
3285
3286 fi = btrfs_item_ptr(leaf, slot,
3287 struct btrfs_file_extent_item);
3288 fi = (struct btrfs_file_extent_item *)(
3289 (unsigned long)fi - size_diff);
3290
3291 if (btrfs_file_extent_type(leaf, fi) ==
3292 BTRFS_FILE_EXTENT_INLINE) {
3293 ptr = btrfs_item_ptr_offset(leaf, slot);
3294 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003295 (unsigned long)fi,
3296 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003297 disk_bytenr));
3298 }
3299 }
3300
3301 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3302 data_end + size_diff, btrfs_leaf_data(leaf) +
3303 data_end, old_data_start - data_end);
3304
3305 offset = btrfs_disk_key_offset(&disk_key);
3306 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3307 btrfs_set_item_key(leaf, &disk_key, slot);
3308 if (slot == 0)
3309 fixup_low_keys(trans, root, path, &disk_key, 1);
3310 }
Chris Mason5f39d392007-10-15 16:14:19 -04003311
3312 item = btrfs_item_nr(leaf, slot);
3313 btrfs_set_item_size(leaf, item, new_size);
3314 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003315
3316 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003317 if (btrfs_leaf_free_space(root, leaf) < 0) {
3318 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003319 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003320 }
Chris Masonb18c6682007-04-17 13:26:50 -04003321 return ret;
3322}
3323
Chris Masond352ac62008-09-29 15:18:18 -04003324/*
3325 * make the item pointed to by the path bigger, data_size is the new size.
3326 */
Chris Mason5f39d392007-10-15 16:14:19 -04003327int btrfs_extend_item(struct btrfs_trans_handle *trans,
3328 struct btrfs_root *root, struct btrfs_path *path,
3329 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003330{
3331 int ret = 0;
3332 int slot;
3333 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003334 struct extent_buffer *leaf;
3335 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003336 u32 nritems;
3337 unsigned int data_end;
3338 unsigned int old_data;
3339 unsigned int old_size;
3340 int i;
3341
3342 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003343 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003344
Chris Mason5f39d392007-10-15 16:14:19 -04003345 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003346 data_end = leaf_data_end(root, leaf);
3347
Chris Mason5f39d392007-10-15 16:14:19 -04003348 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3349 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003350 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003351 }
Chris Mason6567e832007-04-16 09:22:45 -04003352 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003353 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003354
3355 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003356 if (slot >= nritems) {
3357 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003358 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3359 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003360 BUG_ON(1);
3361 }
Chris Mason6567e832007-04-16 09:22:45 -04003362
3363 /*
3364 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3365 */
3366 /* first correct the data pointers */
3367 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003368 u32 ioff;
3369 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003370
3371 if (!leaf->map_token) {
3372 map_extent_buffer(leaf, (unsigned long)item,
3373 sizeof(struct btrfs_item),
3374 &leaf->map_token, &leaf->kaddr,
3375 &leaf->map_start, &leaf->map_len,
3376 KM_USER1);
3377 }
Chris Mason5f39d392007-10-15 16:14:19 -04003378 ioff = btrfs_item_offset(leaf, item);
3379 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003380 }
Chris Mason5f39d392007-10-15 16:14:19 -04003381
Chris Masondb945352007-10-15 16:15:53 -04003382 if (leaf->map_token) {
3383 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3384 leaf->map_token = NULL;
3385 }
3386
Chris Mason6567e832007-04-16 09:22:45 -04003387 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003388 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003389 data_end - data_size, btrfs_leaf_data(leaf) +
3390 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003391
Chris Mason6567e832007-04-16 09:22:45 -04003392 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003393 old_size = btrfs_item_size_nr(leaf, slot);
3394 item = btrfs_item_nr(leaf, slot);
3395 btrfs_set_item_size(leaf, item, old_size + data_size);
3396 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003397
3398 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003399 if (btrfs_leaf_free_space(root, leaf) < 0) {
3400 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003401 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003402 }
Chris Mason6567e832007-04-16 09:22:45 -04003403 return ret;
3404}
3405
Chris Mason74123bd2007-02-02 11:05:29 -05003406/*
Chris Masond352ac62008-09-29 15:18:18 -04003407 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003408 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003409 * Returns the number of keys that were inserted.
3410 */
3411int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3412 struct btrfs_root *root,
3413 struct btrfs_path *path,
3414 struct btrfs_key *cpu_key, u32 *data_size,
3415 int nr)
3416{
3417 struct extent_buffer *leaf;
3418 struct btrfs_item *item;
3419 int ret = 0;
3420 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003421 int i;
3422 u32 nritems;
3423 u32 total_data = 0;
3424 u32 total_size = 0;
3425 unsigned int data_end;
3426 struct btrfs_disk_key disk_key;
3427 struct btrfs_key found_key;
3428
Yan Zheng87b29b22008-12-17 10:21:48 -05003429 for (i = 0; i < nr; i++) {
3430 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3431 BTRFS_LEAF_DATA_SIZE(root)) {
3432 break;
3433 nr = i;
3434 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003435 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003436 total_size += data_size[i] + sizeof(struct btrfs_item);
3437 }
3438 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003439
Josef Bacikf3465ca2008-11-12 14:19:50 -05003440 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3441 if (ret == 0)
3442 return -EEXIST;
3443 if (ret < 0)
3444 goto out;
3445
Josef Bacikf3465ca2008-11-12 14:19:50 -05003446 leaf = path->nodes[0];
3447
3448 nritems = btrfs_header_nritems(leaf);
3449 data_end = leaf_data_end(root, leaf);
3450
3451 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3452 for (i = nr; i >= 0; i--) {
3453 total_data -= data_size[i];
3454 total_size -= data_size[i] + sizeof(struct btrfs_item);
3455 if (total_size < btrfs_leaf_free_space(root, leaf))
3456 break;
3457 }
3458 nr = i;
3459 }
3460
3461 slot = path->slots[0];
3462 BUG_ON(slot < 0);
3463
3464 if (slot != nritems) {
3465 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3466
3467 item = btrfs_item_nr(leaf, slot);
3468 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3469
3470 /* figure out how many keys we can insert in here */
3471 total_data = data_size[0];
3472 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003473 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003474 break;
3475 total_data += data_size[i];
3476 }
3477 nr = i;
3478
3479 if (old_data < data_end) {
3480 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003481 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003482 slot, old_data, data_end);
3483 BUG_ON(1);
3484 }
3485 /*
3486 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3487 */
3488 /* first correct the data pointers */
3489 WARN_ON(leaf->map_token);
3490 for (i = slot; i < nritems; i++) {
3491 u32 ioff;
3492
3493 item = btrfs_item_nr(leaf, i);
3494 if (!leaf->map_token) {
3495 map_extent_buffer(leaf, (unsigned long)item,
3496 sizeof(struct btrfs_item),
3497 &leaf->map_token, &leaf->kaddr,
3498 &leaf->map_start, &leaf->map_len,
3499 KM_USER1);
3500 }
3501
3502 ioff = btrfs_item_offset(leaf, item);
3503 btrfs_set_item_offset(leaf, item, ioff - total_data);
3504 }
3505 if (leaf->map_token) {
3506 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3507 leaf->map_token = NULL;
3508 }
3509
3510 /* shift the items */
3511 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3512 btrfs_item_nr_offset(slot),
3513 (nritems - slot) * sizeof(struct btrfs_item));
3514
3515 /* shift the data */
3516 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3517 data_end - total_data, btrfs_leaf_data(leaf) +
3518 data_end, old_data - data_end);
3519 data_end = old_data;
3520 } else {
3521 /*
3522 * this sucks but it has to be done, if we are inserting at
3523 * the end of the leaf only insert 1 of the items, since we
3524 * have no way of knowing whats on the next leaf and we'd have
3525 * to drop our current locks to figure it out
3526 */
3527 nr = 1;
3528 }
3529
3530 /* setup the item for the new data */
3531 for (i = 0; i < nr; i++) {
3532 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3533 btrfs_set_item_key(leaf, &disk_key, slot + i);
3534 item = btrfs_item_nr(leaf, slot + i);
3535 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3536 data_end -= data_size[i];
3537 btrfs_set_item_size(leaf, item, data_size[i]);
3538 }
3539 btrfs_set_header_nritems(leaf, nritems + nr);
3540 btrfs_mark_buffer_dirty(leaf);
3541
3542 ret = 0;
3543 if (slot == 0) {
3544 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3545 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3546 }
3547
3548 if (btrfs_leaf_free_space(root, leaf) < 0) {
3549 btrfs_print_leaf(root, leaf);
3550 BUG();
3551 }
3552out:
3553 if (!ret)
3554 ret = nr;
3555 return ret;
3556}
3557
3558/*
Chris Mason44871b12009-03-13 10:04:31 -04003559 * this is a helper for btrfs_insert_empty_items, the main goal here is
3560 * to save stack depth by doing the bulk of the work in a function
3561 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003562 */
Chris Mason44871b12009-03-13 10:04:31 -04003563static noinline_for_stack int
3564setup_items_for_insert(struct btrfs_trans_handle *trans,
3565 struct btrfs_root *root, struct btrfs_path *path,
3566 struct btrfs_key *cpu_key, u32 *data_size,
3567 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003568{
Chris Mason5f39d392007-10-15 16:14:19 -04003569 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003570 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003571 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003572 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003573 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003574 int ret;
3575 struct extent_buffer *leaf;
3576 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003577
Chris Mason5f39d392007-10-15 16:14:19 -04003578 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003579 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003580
Chris Mason5f39d392007-10-15 16:14:19 -04003581 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003582 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003583
Chris Masonf25956c2008-09-12 15:32:53 -04003584 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003585 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003586 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003587 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003588 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003589 }
Chris Mason5f39d392007-10-15 16:14:19 -04003590
Chris Masonbe0e5c02007-01-26 15:51:26 -05003591 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003592 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593
Chris Mason5f39d392007-10-15 16:14:19 -04003594 if (old_data < data_end) {
3595 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003596 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003597 slot, old_data, data_end);
3598 BUG_ON(1);
3599 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003600 /*
3601 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3602 */
3603 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003604 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003605 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003606 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003607
Chris Mason5f39d392007-10-15 16:14:19 -04003608 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003609 if (!leaf->map_token) {
3610 map_extent_buffer(leaf, (unsigned long)item,
3611 sizeof(struct btrfs_item),
3612 &leaf->map_token, &leaf->kaddr,
3613 &leaf->map_start, &leaf->map_len,
3614 KM_USER1);
3615 }
3616
Chris Mason5f39d392007-10-15 16:14:19 -04003617 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003618 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003619 }
Chris Masondb945352007-10-15 16:15:53 -04003620 if (leaf->map_token) {
3621 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3622 leaf->map_token = NULL;
3623 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003624
3625 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003626 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003627 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003628 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003629
3630 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003631 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003632 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003633 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003634 data_end = old_data;
3635 }
Chris Mason5f39d392007-10-15 16:14:19 -04003636
Chris Mason62e27492007-03-15 12:56:47 -04003637 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003638 for (i = 0; i < nr; i++) {
3639 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3640 btrfs_set_item_key(leaf, &disk_key, slot + i);
3641 item = btrfs_item_nr(leaf, slot + i);
3642 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3643 data_end -= data_size[i];
3644 btrfs_set_item_size(leaf, item, data_size[i]);
3645 }
Chris Mason44871b12009-03-13 10:04:31 -04003646
Chris Mason9c583092008-01-29 15:15:18 -05003647 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003648
3649 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003650 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003651 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003652 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003653 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003654 }
Chris Masonb9473432009-03-13 11:00:37 -04003655 btrfs_unlock_up_safe(path, 1);
3656 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003657
Chris Mason5f39d392007-10-15 16:14:19 -04003658 if (btrfs_leaf_free_space(root, leaf) < 0) {
3659 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003660 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003661 }
Chris Mason44871b12009-03-13 10:04:31 -04003662 return ret;
3663}
3664
3665/*
3666 * Given a key and some data, insert items into the tree.
3667 * This does all the path init required, making room in the tree if needed.
3668 */
3669int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3670 struct btrfs_root *root,
3671 struct btrfs_path *path,
3672 struct btrfs_key *cpu_key, u32 *data_size,
3673 int nr)
3674{
3675 struct extent_buffer *leaf;
3676 int ret = 0;
3677 int slot;
3678 int i;
3679 u32 total_size = 0;
3680 u32 total_data = 0;
3681
3682 for (i = 0; i < nr; i++)
3683 total_data += data_size[i];
3684
3685 total_size = total_data + (nr * sizeof(struct btrfs_item));
3686 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3687 if (ret == 0)
3688 return -EEXIST;
3689 if (ret < 0)
3690 goto out;
3691
3692 leaf = path->nodes[0];
3693 slot = path->slots[0];
3694 BUG_ON(slot < 0);
3695
3696 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3697 total_data, total_size, nr);
3698
Chris Masoned2ff2c2007-03-01 18:59:40 -05003699out:
Chris Mason62e27492007-03-15 12:56:47 -04003700 return ret;
3701}
3702
3703/*
3704 * Given a key and some data, insert an item into the tree.
3705 * This does all the path init required, making room in the tree if needed.
3706 */
Chris Masone089f052007-03-16 16:20:31 -04003707int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3708 *root, struct btrfs_key *cpu_key, void *data, u32
3709 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003710{
3711 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003712 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003713 struct extent_buffer *leaf;
3714 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003715
Chris Mason2c90e5d2007-04-02 10:50:19 -04003716 path = btrfs_alloc_path();
3717 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003718 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003719 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003720 leaf = path->nodes[0];
3721 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3722 write_extent_buffer(leaf, data, ptr, data_size);
3723 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003724 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003725 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003726 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003727}
3728
Chris Mason74123bd2007-02-02 11:05:29 -05003729/*
Chris Mason5de08d72007-02-24 06:24:44 -05003730 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003731 *
Chris Masond352ac62008-09-29 15:18:18 -04003732 * the tree should have been previously balanced so the deletion does not
3733 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003734 */
Chris Masone089f052007-03-16 16:20:31 -04003735static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3736 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003737{
Chris Mason5f39d392007-10-15 16:14:19 -04003738 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003739 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003740 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003741 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003742
Chris Mason5f39d392007-10-15 16:14:19 -04003743 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003744 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003745 memmove_extent_buffer(parent,
3746 btrfs_node_key_ptr_offset(slot),
3747 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003748 sizeof(struct btrfs_key_ptr) *
3749 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003750 }
Chris Mason7518a232007-03-12 12:01:18 -04003751 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003752 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003753 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003754 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003755 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003756 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003757 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003758 struct btrfs_disk_key disk_key;
3759
3760 btrfs_node_key(parent, &disk_key, 0);
3761 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003762 if (wret)
3763 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003764 }
Chris Masond6025572007-03-30 14:27:56 -04003765 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003766 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003767}
3768
Chris Mason74123bd2007-02-02 11:05:29 -05003769/*
Chris Mason323ac952008-10-01 19:05:46 -04003770 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003771 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003772 *
3773 * This deletes the pointer in path->nodes[1] and frees the leaf
3774 * block extent. zero is returned if it all worked out, < 0 otherwise.
3775 *
3776 * The path must have already been setup for deleting the leaf, including
3777 * all the proper balancing. path->nodes[1] must be locked.
3778 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003779static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3780 struct btrfs_root *root,
3781 struct btrfs_path *path,
3782 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003783{
3784 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003785
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003786 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003787 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3788 if (ret)
3789 return ret;
3790
Chris Mason4d081c42009-02-04 09:31:28 -05003791 /*
3792 * btrfs_free_extent is expensive, we want to make sure we
3793 * aren't holding any locks when we call it
3794 */
3795 btrfs_unlock_up_safe(path, 0);
3796
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003797 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
3798 0, root->root_key.objectid, 0, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003799 return ret;
3800}
3801/*
Chris Mason74123bd2007-02-02 11:05:29 -05003802 * delete the item at the leaf level in path. If that empties
3803 * the leaf, remove it from the tree
3804 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003805int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3806 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003807{
Chris Mason5f39d392007-10-15 16:14:19 -04003808 struct extent_buffer *leaf;
3809 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003810 int last_off;
3811 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003812 int ret = 0;
3813 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003814 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003815 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003816
Chris Mason5f39d392007-10-15 16:14:19 -04003817 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003818 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3819
3820 for (i = 0; i < nr; i++)
3821 dsize += btrfs_item_size_nr(leaf, slot + i);
3822
Chris Mason5f39d392007-10-15 16:14:19 -04003823 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003824
Chris Mason85e21ba2008-01-29 15:11:36 -05003825 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003826 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003827
3828 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003829 data_end + dsize,
3830 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003831 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003832
Chris Mason85e21ba2008-01-29 15:11:36 -05003833 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003834 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003835
Chris Mason5f39d392007-10-15 16:14:19 -04003836 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003837 if (!leaf->map_token) {
3838 map_extent_buffer(leaf, (unsigned long)item,
3839 sizeof(struct btrfs_item),
3840 &leaf->map_token, &leaf->kaddr,
3841 &leaf->map_start, &leaf->map_len,
3842 KM_USER1);
3843 }
Chris Mason5f39d392007-10-15 16:14:19 -04003844 ioff = btrfs_item_offset(leaf, item);
3845 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003846 }
Chris Masondb945352007-10-15 16:15:53 -04003847
3848 if (leaf->map_token) {
3849 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3850 leaf->map_token = NULL;
3851 }
3852
Chris Mason5f39d392007-10-15 16:14:19 -04003853 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003854 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003855 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003856 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003857 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003858 btrfs_set_header_nritems(leaf, nritems - nr);
3859 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003860
Chris Mason74123bd2007-02-02 11:05:29 -05003861 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003862 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003863 if (leaf == root->node) {
3864 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003865 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003866 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003867 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003868 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003869 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003870 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003871 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003872 struct btrfs_disk_key disk_key;
3873
3874 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003875 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003876 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003877 if (wret)
3878 ret = wret;
3879 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003880
Chris Mason74123bd2007-02-02 11:05:29 -05003881 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003882 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003883 /* push_leaf_left fixes the path.
3884 * make sure the path still points to our leaf
3885 * for possible call to del_ptr below
3886 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003887 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003888 extent_buffer_get(leaf);
3889
Chris Masonb9473432009-03-13 11:00:37 -04003890 btrfs_set_path_blocking(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003891 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003892 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003893 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003894
3895 if (path->nodes[0] == leaf &&
3896 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003897 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003898 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003899 ret = wret;
3900 }
Chris Mason5f39d392007-10-15 16:14:19 -04003901
3902 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003903 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003904 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003905 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003906 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003907 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003908 /* if we're still in the path, make sure
3909 * we're dirty. Otherwise, one of the
3910 * push_leaf functions must have already
3911 * dirtied this buffer
3912 */
3913 if (path->nodes[0] == leaf)
3914 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003915 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003916 }
Chris Masond5719762007-03-23 10:01:08 -04003917 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003918 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003919 }
3920 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003921 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003922}
3923
Chris Mason97571fd2007-02-24 13:39:08 -05003924/*
Chris Mason925baed2008-06-25 16:01:30 -04003925 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003926 * returns 0 if it found something or 1 if there are no lesser leaves.
3927 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003928 *
3929 * This may release the path, and so you may lose any locks held at the
3930 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003931 */
3932int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3933{
Chris Mason925baed2008-06-25 16:01:30 -04003934 struct btrfs_key key;
3935 struct btrfs_disk_key found_key;
3936 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003937
Chris Mason925baed2008-06-25 16:01:30 -04003938 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003939
Chris Mason925baed2008-06-25 16:01:30 -04003940 if (key.offset > 0)
3941 key.offset--;
3942 else if (key.type > 0)
3943 key.type--;
3944 else if (key.objectid > 0)
3945 key.objectid--;
3946 else
3947 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003948
Chris Mason925baed2008-06-25 16:01:30 -04003949 btrfs_release_path(root, path);
3950 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3951 if (ret < 0)
3952 return ret;
3953 btrfs_item_key(path->nodes[0], &found_key, 0);
3954 ret = comp_keys(&found_key, &key);
3955 if (ret < 0)
3956 return 0;
3957 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003958}
3959
Chris Mason3f157a22008-06-25 16:01:31 -04003960/*
3961 * A helper function to walk down the tree starting at min_key, and looking
3962 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003963 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003964 *
3965 * This does not cow, but it does stuff the starting key it finds back
3966 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3967 * key and get a writable path.
3968 *
3969 * This does lock as it descends, and path->keep_locks should be set
3970 * to 1 by the caller.
3971 *
3972 * This honors path->lowest_level to prevent descent past a given level
3973 * of the tree.
3974 *
Chris Masond352ac62008-09-29 15:18:18 -04003975 * min_trans indicates the oldest transaction that you are interested
3976 * in walking through. Any nodes or leaves older than min_trans are
3977 * skipped over (without reading them).
3978 *
Chris Mason3f157a22008-06-25 16:01:31 -04003979 * returns zero if something useful was found, < 0 on error and 1 if there
3980 * was nothing in the tree that matched the search criteria.
3981 */
3982int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003983 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003984 struct btrfs_path *path, int cache_only,
3985 u64 min_trans)
3986{
3987 struct extent_buffer *cur;
3988 struct btrfs_key found_key;
3989 int slot;
Yan96524802008-07-24 12:19:49 -04003990 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003991 u32 nritems;
3992 int level;
3993 int ret = 1;
3994
Chris Mason934d3752008-12-08 16:43:10 -05003995 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003996again:
3997 cur = btrfs_lock_root_node(root);
3998 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003999 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004000 path->nodes[level] = cur;
4001 path->locks[level] = 1;
4002
4003 if (btrfs_header_generation(cur) < min_trans) {
4004 ret = 1;
4005 goto out;
4006 }
Chris Masond3977122009-01-05 21:25:51 -05004007 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004008 nritems = btrfs_header_nritems(cur);
4009 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004010 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004011
Chris Mason323ac952008-10-01 19:05:46 -04004012 /* at the lowest level, we're done, setup the path and exit */
4013 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004014 if (slot >= nritems)
4015 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004016 ret = 0;
4017 path->slots[level] = slot;
4018 btrfs_item_key_to_cpu(cur, &found_key, slot);
4019 goto out;
4020 }
Yan96524802008-07-24 12:19:49 -04004021 if (sret && slot > 0)
4022 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004023 /*
4024 * check this node pointer against the cache_only and
4025 * min_trans parameters. If it isn't in cache or is too
4026 * old, skip to the next one.
4027 */
Chris Masond3977122009-01-05 21:25:51 -05004028 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004029 u64 blockptr;
4030 u64 gen;
4031 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004032 struct btrfs_disk_key disk_key;
4033
Chris Mason3f157a22008-06-25 16:01:31 -04004034 blockptr = btrfs_node_blockptr(cur, slot);
4035 gen = btrfs_node_ptr_generation(cur, slot);
4036 if (gen < min_trans) {
4037 slot++;
4038 continue;
4039 }
4040 if (!cache_only)
4041 break;
4042
Chris Masone02119d2008-09-05 16:13:11 -04004043 if (max_key) {
4044 btrfs_node_key(cur, &disk_key, slot);
4045 if (comp_keys(&disk_key, max_key) >= 0) {
4046 ret = 1;
4047 goto out;
4048 }
4049 }
4050
Chris Mason3f157a22008-06-25 16:01:31 -04004051 tmp = btrfs_find_tree_block(root, blockptr,
4052 btrfs_level_size(root, level - 1));
4053
4054 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4055 free_extent_buffer(tmp);
4056 break;
4057 }
4058 if (tmp)
4059 free_extent_buffer(tmp);
4060 slot++;
4061 }
Chris Masone02119d2008-09-05 16:13:11 -04004062find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004063 /*
4064 * we didn't find a candidate key in this node, walk forward
4065 * and find another one
4066 */
4067 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004068 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004069 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004070 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004071 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004072 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004073 btrfs_release_path(root, path);
4074 goto again;
4075 } else {
4076 goto out;
4077 }
4078 }
4079 /* save our key for returning back */
4080 btrfs_node_key_to_cpu(cur, &found_key, slot);
4081 path->slots[level] = slot;
4082 if (level == path->lowest_level) {
4083 ret = 0;
4084 unlock_up(path, level, 1);
4085 goto out;
4086 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004087 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004088 cur = read_node_slot(root, cur, slot);
4089
4090 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004091
Chris Mason3f157a22008-06-25 16:01:31 -04004092 path->locks[level - 1] = 1;
4093 path->nodes[level - 1] = cur;
4094 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004095 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004096 }
4097out:
4098 if (ret == 0)
4099 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004100 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004101 return ret;
4102}
4103
4104/*
4105 * this is similar to btrfs_next_leaf, but does not try to preserve
4106 * and fixup the path. It looks for and returns the next key in the
4107 * tree based on the current path and the cache_only and min_trans
4108 * parameters.
4109 *
4110 * 0 is returned if another key is found, < 0 if there are any errors
4111 * and 1 is returned if there are no higher keys in the tree
4112 *
4113 * path->keep_locks should be set to 1 on the search made before
4114 * calling this function.
4115 */
Chris Masone7a84562008-06-25 16:01:31 -04004116int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004117 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004118 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004119{
Chris Masone7a84562008-06-25 16:01:31 -04004120 int slot;
4121 struct extent_buffer *c;
4122
Chris Mason934d3752008-12-08 16:43:10 -05004123 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004124 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004125 if (!path->nodes[level])
4126 return 1;
4127
4128 slot = path->slots[level] + 1;
4129 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004130next:
Chris Masone7a84562008-06-25 16:01:31 -04004131 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004132 int ret;
4133 int orig_lowest;
4134 struct btrfs_key cur_key;
4135 if (level + 1 >= BTRFS_MAX_LEVEL ||
4136 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004137 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004138
4139 if (path->locks[level + 1]) {
4140 level++;
4141 continue;
4142 }
4143
4144 slot = btrfs_header_nritems(c) - 1;
4145 if (level == 0)
4146 btrfs_item_key_to_cpu(c, &cur_key, slot);
4147 else
4148 btrfs_node_key_to_cpu(c, &cur_key, slot);
4149
4150 orig_lowest = path->lowest_level;
4151 btrfs_release_path(root, path);
4152 path->lowest_level = level;
4153 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4154 0, 0);
4155 path->lowest_level = orig_lowest;
4156 if (ret < 0)
4157 return ret;
4158
4159 c = path->nodes[level];
4160 slot = path->slots[level];
4161 if (ret == 0)
4162 slot++;
4163 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004164 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004165
Chris Masone7a84562008-06-25 16:01:31 -04004166 if (level == 0)
4167 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004168 else {
4169 u64 blockptr = btrfs_node_blockptr(c, slot);
4170 u64 gen = btrfs_node_ptr_generation(c, slot);
4171
4172 if (cache_only) {
4173 struct extent_buffer *cur;
4174 cur = btrfs_find_tree_block(root, blockptr,
4175 btrfs_level_size(root, level - 1));
4176 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4177 slot++;
4178 if (cur)
4179 free_extent_buffer(cur);
4180 goto next;
4181 }
4182 free_extent_buffer(cur);
4183 }
4184 if (gen < min_trans) {
4185 slot++;
4186 goto next;
4187 }
Chris Masone7a84562008-06-25 16:01:31 -04004188 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004189 }
Chris Masone7a84562008-06-25 16:01:31 -04004190 return 0;
4191 }
4192 return 1;
4193}
4194
Chris Mason7bb86312007-12-11 09:25:06 -05004195/*
Chris Mason925baed2008-06-25 16:01:30 -04004196 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004197 * returns 0 if it found something or 1 if there are no greater leaves.
4198 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004199 */
Chris Mason234b63a2007-03-13 10:46:10 -04004200int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004201{
4202 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004203 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004204 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004205 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004206 struct btrfs_key key;
4207 u32 nritems;
4208 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004209 int old_spinning = path->leave_spinning;
4210 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004211
4212 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004213 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004214 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004215
Chris Mason8e73f272009-04-03 10:14:18 -04004216 /*
4217 * we take the blocks in an order that upsets lockdep. Using
4218 * blocking mode is the only way around it.
4219 */
4220#ifdef CONFIG_DEBUG_LOCK_ALLOC
4221 force_blocking = 1;
4222#endif
Chris Mason925baed2008-06-25 16:01:30 -04004223
Chris Mason8e73f272009-04-03 10:14:18 -04004224 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4225again:
4226 level = 1;
4227 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004228 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004229
Chris Masona2135012008-06-25 16:01:30 -04004230 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004231
4232 if (!force_blocking)
4233 path->leave_spinning = 1;
4234
Chris Mason925baed2008-06-25 16:01:30 -04004235 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4236 path->keep_locks = 0;
4237
4238 if (ret < 0)
4239 return ret;
4240
Chris Masona2135012008-06-25 16:01:30 -04004241 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004242 /*
4243 * by releasing the path above we dropped all our locks. A balance
4244 * could have added more items next to the key that used to be
4245 * at the very end of the block. So, check again here and
4246 * advance the path if there are now more items available.
4247 */
Chris Masona2135012008-06-25 16:01:30 -04004248 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004249 if (ret == 0)
4250 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004251 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004252 goto done;
4253 }
Chris Masond97e63b2007-02-20 16:40:44 -05004254
Chris Masond3977122009-01-05 21:25:51 -05004255 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004256 if (!path->nodes[level]) {
4257 ret = 1;
4258 goto done;
4259 }
Chris Mason5f39d392007-10-15 16:14:19 -04004260
Chris Masond97e63b2007-02-20 16:40:44 -05004261 slot = path->slots[level] + 1;
4262 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004263 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004264 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004265 if (level == BTRFS_MAX_LEVEL) {
4266 ret = 1;
4267 goto done;
4268 }
Chris Masond97e63b2007-02-20 16:40:44 -05004269 continue;
4270 }
Chris Mason5f39d392007-10-15 16:14:19 -04004271
Chris Mason925baed2008-06-25 16:01:30 -04004272 if (next) {
4273 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004274 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004275 }
Chris Mason5f39d392007-10-15 16:14:19 -04004276
Chris Mason8e73f272009-04-03 10:14:18 -04004277 next = c;
4278 ret = read_block_for_search(NULL, root, path, &next, level,
4279 slot, &key);
4280 if (ret == -EAGAIN)
4281 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004282
Chris Mason76a05b32009-05-14 13:24:30 -04004283 if (ret < 0) {
4284 btrfs_release_path(root, path);
4285 goto done;
4286 }
4287
Chris Mason5cd57b22008-06-25 16:01:30 -04004288 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004289 ret = btrfs_try_spin_lock(next);
4290 if (!ret) {
4291 btrfs_set_path_blocking(path);
4292 btrfs_tree_lock(next);
4293 if (!force_blocking)
4294 btrfs_clear_path_blocking(path, next);
4295 }
4296 if (force_blocking)
4297 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004298 }
Chris Masond97e63b2007-02-20 16:40:44 -05004299 break;
4300 }
4301 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004302 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004303 level--;
4304 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004305 if (path->locks[level])
4306 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004307
Chris Mason5f39d392007-10-15 16:14:19 -04004308 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004309 path->nodes[level] = next;
4310 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004311 if (!path->skip_locking)
4312 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004313
Chris Masond97e63b2007-02-20 16:40:44 -05004314 if (!level)
4315 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004316
Chris Mason8e73f272009-04-03 10:14:18 -04004317 ret = read_block_for_search(NULL, root, path, &next, level,
4318 0, &key);
4319 if (ret == -EAGAIN)
4320 goto again;
4321
Chris Mason76a05b32009-05-14 13:24:30 -04004322 if (ret < 0) {
4323 btrfs_release_path(root, path);
4324 goto done;
4325 }
4326
Chris Mason5cd57b22008-06-25 16:01:30 -04004327 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004328 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004329 ret = btrfs_try_spin_lock(next);
4330 if (!ret) {
4331 btrfs_set_path_blocking(path);
4332 btrfs_tree_lock(next);
4333 if (!force_blocking)
4334 btrfs_clear_path_blocking(path, next);
4335 }
4336 if (force_blocking)
4337 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004338 }
Chris Masond97e63b2007-02-20 16:40:44 -05004339 }
Chris Mason8e73f272009-04-03 10:14:18 -04004340 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004341done:
4342 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004343 path->leave_spinning = old_spinning;
4344 if (!old_spinning)
4345 btrfs_set_path_blocking(path);
4346
4347 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004348}
Chris Mason0b86a832008-03-24 15:01:56 -04004349
Chris Mason3f157a22008-06-25 16:01:31 -04004350/*
4351 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4352 * searching until it gets past min_objectid or finds an item of 'type'
4353 *
4354 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4355 */
Chris Mason0b86a832008-03-24 15:01:56 -04004356int btrfs_previous_item(struct btrfs_root *root,
4357 struct btrfs_path *path, u64 min_objectid,
4358 int type)
4359{
4360 struct btrfs_key found_key;
4361 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004362 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004363 int ret;
4364
Chris Masond3977122009-01-05 21:25:51 -05004365 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004366 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004367 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004368 ret = btrfs_prev_leaf(root, path);
4369 if (ret != 0)
4370 return ret;
4371 } else {
4372 path->slots[0]--;
4373 }
4374 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004375 nritems = btrfs_header_nritems(leaf);
4376 if (nritems == 0)
4377 return 1;
4378 if (path->slots[0] == nritems)
4379 path->slots[0]--;
4380
Chris Mason0b86a832008-03-24 15:01:56 -04004381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004382 if (found_key.objectid < min_objectid)
4383 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004384 if (found_key.type == type)
4385 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004386 if (found_key.objectid == min_objectid &&
4387 found_key.type < type)
4388 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004389 }
4390 return 1;
4391}