blob: 6bee8e5204fb9c05d14315a862a8ce4168f757a8 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050021#include "ctree.h"
22#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040023#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040024#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040030 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040031 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040032static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040034 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040035static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040039static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40 struct btrfs_path *path, int level, int slot);
Yan, Zhengad48fd752009-11-12 09:33:58 +000041static int setup_items_for_insert(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root, struct btrfs_path *path,
43 struct btrfs_key *cpu_key, u32 *data_size,
44 u32 total_data, u32 total_size, int nr);
45
Chris Masond97e63b2007-02-20 16:40:44 -050046
Chris Mason2c90e5d2007-04-02 10:50:19 -040047struct btrfs_path *btrfs_alloc_path(void)
48{
Chris Masondf24a2b2007-04-04 09:36:31 -040049 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050050 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
51 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040053 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040054}
55
Chris Masonb4ce94d2009-02-04 09:25:08 -050056/*
57 * set all locked nodes in the path to blocking locks. This should
58 * be done before scheduling
59 */
60noinline void btrfs_set_path_blocking(struct btrfs_path *p)
61{
62 int i;
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
64 if (p->nodes[i] && p->locks[i])
65 btrfs_set_lock_blocking(p->nodes[i]);
66 }
67}
68
69/*
70 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050071 *
72 * held is used to keep lockdep happy, when lockdep is enabled
73 * we set held to a blocking lock before we go around and
74 * retake all the spinlocks in the path. You can safely use NULL
75 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050076 */
Chris Mason4008c042009-02-12 14:09:45 -050077noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
78 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050079{
80 int i;
Chris Mason4008c042009-02-12 14:09:45 -050081
82#ifdef CONFIG_DEBUG_LOCK_ALLOC
83 /* lockdep really cares that we take all of these spinlocks
84 * in the right order. If any of the locks in the path are not
85 * currently blocking, it is going to complain. So, make really
86 * really sure by forcing the path to blocking before we clear
87 * the path blocking.
88 */
89 if (held)
90 btrfs_set_lock_blocking(held);
91 btrfs_set_path_blocking(p);
92#endif
93
94 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050095 if (p->nodes[i] && p->locks[i])
96 btrfs_clear_lock_blocking(p->nodes[i]);
97 }
Chris Mason4008c042009-02-12 14:09:45 -050098
99#ifdef CONFIG_DEBUG_LOCK_ALLOC
100 if (held)
101 btrfs_clear_lock_blocking(held);
102#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500103}
104
Chris Masond352ac62008-09-29 15:18:18 -0400105/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400106void btrfs_free_path(struct btrfs_path *p)
107{
Chris Masondf24a2b2007-04-04 09:36:31 -0400108 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400109 kmem_cache_free(btrfs_path_cachep, p);
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
113 * path release drops references on the extent buffers in the path
114 * and it drops any locks held by this path
115 *
116 * It is safe to call this on paths that no locks or extent buffers held.
117 */
Chris Masond3977122009-01-05 21:25:51 -0500118noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500119{
120 int i;
Chris Masona2135012008-06-25 16:01:30 -0400121
Chris Mason234b63a2007-03-13 10:46:10 -0400122 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400123 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500124 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400125 continue;
126 if (p->locks[i]) {
127 btrfs_tree_unlock(p->nodes[i]);
128 p->locks[i] = 0;
129 }
Chris Mason5f39d392007-10-15 16:14:19 -0400130 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400131 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500132 }
133}
134
Chris Masond352ac62008-09-29 15:18:18 -0400135/*
136 * safely gets a reference on the root node of a tree. A lock
137 * is not taken, so a concurrent writer may put a different node
138 * at the root of the tree. See btrfs_lock_root_node for the
139 * looping required.
140 *
141 * The extent buffer returned by this has a reference taken, so
142 * it won't disappear. It may stop being the root of the tree
143 * at any time because there are no locks held.
144 */
Chris Mason925baed2008-06-25 16:01:30 -0400145struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
146{
147 struct extent_buffer *eb;
148 spin_lock(&root->node_lock);
149 eb = root->node;
150 extent_buffer_get(eb);
151 spin_unlock(&root->node_lock);
152 return eb;
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/* loop around taking references on and locking the root node of the
156 * tree until you end up with a lock on the root. A locked buffer
157 * is returned, with a reference held.
158 */
Chris Mason925baed2008-06-25 16:01:30 -0400159struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
160{
161 struct extent_buffer *eb;
162
Chris Masond3977122009-01-05 21:25:51 -0500163 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400164 eb = btrfs_root_node(root);
165 btrfs_tree_lock(eb);
166
167 spin_lock(&root->node_lock);
168 if (eb == root->node) {
169 spin_unlock(&root->node_lock);
170 break;
171 }
172 spin_unlock(&root->node_lock);
173
174 btrfs_tree_unlock(eb);
175 free_extent_buffer(eb);
176 }
177 return eb;
178}
179
Chris Masond352ac62008-09-29 15:18:18 -0400180/* cowonly root (everything not a reference counted cow subvolume), just get
181 * put onto a simple dirty list. transaction.c walks this to make sure they
182 * get properly updated on disk.
183 */
Chris Mason0b86a832008-03-24 15:01:56 -0400184static void add_root_to_dirty_list(struct btrfs_root *root)
185{
186 if (root->track_dirty && list_empty(&root->dirty_list)) {
187 list_add(&root->dirty_list,
188 &root->fs_info->dirty_cowonly_roots);
189 }
190}
191
Chris Masond352ac62008-09-29 15:18:18 -0400192/*
193 * used by snapshot creation to make a copy of a root for a tree with
194 * a given objectid. The buffer with the new root node is returned in
195 * cow_ret, and this func returns zero on success or a negative error code.
196 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500197int btrfs_copy_root(struct btrfs_trans_handle *trans,
198 struct btrfs_root *root,
199 struct extent_buffer *buf,
200 struct extent_buffer **cow_ret, u64 new_root_objectid)
201{
202 struct extent_buffer *cow;
203 u32 nritems;
204 int ret = 0;
205 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400206 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500207
208 WARN_ON(root->ref_cows && trans->transid !=
209 root->fs_info->running_transaction->transid);
210 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
211
212 level = btrfs_header_level(buf);
213 nritems = btrfs_header_nritems(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400214 if (level == 0)
215 btrfs_item_key(buf, &disk_key, 0);
216 else
217 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400218
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400219 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
220 new_root_objectid, &disk_key, level,
221 buf->start, 0);
222 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500223 return PTR_ERR(cow);
224
225 copy_extent_buffer(cow, buf, 0, 0, cow->len);
226 btrfs_set_header_bytenr(cow, cow->start);
227 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400228 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
229 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
230 BTRFS_HEADER_FLAG_RELOC);
231 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
232 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
233 else
234 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500235
Yan Zheng2b820322008-11-17 21:11:30 -0500236 write_extent_buffer(cow, root->fs_info->fsid,
237 (unsigned long)btrfs_header_fsid(cow),
238 BTRFS_FSID_SIZE);
239
Chris Masonbe20aa92007-12-17 20:14:01 -0500240 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400241 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
242 ret = btrfs_inc_ref(trans, root, cow, 1);
243 else
244 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500245
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 if (ret)
247 return ret;
248
249 btrfs_mark_buffer_dirty(cow);
250 *cow_ret = cow;
251 return 0;
252}
253
Chris Masond352ac62008-09-29 15:18:18 -0400254/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 * check if the tree block can be shared by multiple trees
256 */
257int btrfs_block_can_be_shared(struct btrfs_root *root,
258 struct extent_buffer *buf)
259{
260 /*
261 * Tree blocks not in refernece counted trees and tree roots
262 * are never shared. If a block was allocated after the last
263 * snapshot and the block was not allocated by tree relocation,
264 * we know the block is not shared.
265 */
266 if (root->ref_cows &&
267 buf != root->node && buf != root->commit_root &&
268 (btrfs_header_generation(buf) <=
269 btrfs_root_last_snapshot(&root->root_item) ||
270 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
271 return 1;
272#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
273 if (root->ref_cows &&
274 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
275 return 1;
276#endif
277 return 0;
278}
279
280static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root,
282 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400283 struct extent_buffer *cow,
284 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400285{
286 u64 refs;
287 u64 owner;
288 u64 flags;
289 u64 new_flags = 0;
290 int ret;
291
292 /*
293 * Backrefs update rules:
294 *
295 * Always use full backrefs for extent pointers in tree block
296 * allocated by tree relocation.
297 *
298 * If a shared tree block is no longer referenced by its owner
299 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
300 * use full backrefs for extent pointers in tree block.
301 *
302 * If a tree block is been relocating
303 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
304 * use full backrefs for extent pointers in tree block.
305 * The reason for this is some operations (such as drop tree)
306 * are only allowed for blocks use full backrefs.
307 */
308
309 if (btrfs_block_can_be_shared(root, buf)) {
310 ret = btrfs_lookup_extent_info(trans, root, buf->start,
311 buf->len, &refs, &flags);
312 BUG_ON(ret);
313 BUG_ON(refs == 0);
314 } else {
315 refs = 1;
316 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
317 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
318 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
319 else
320 flags = 0;
321 }
322
323 owner = btrfs_header_owner(buf);
324 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
325 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
326
327 if (refs > 1) {
328 if ((owner == root->root_key.objectid ||
329 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
330 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
331 ret = btrfs_inc_ref(trans, root, buf, 1);
332 BUG_ON(ret);
333
334 if (root->root_key.objectid ==
335 BTRFS_TREE_RELOC_OBJECTID) {
336 ret = btrfs_dec_ref(trans, root, buf, 0);
337 BUG_ON(ret);
338 ret = btrfs_inc_ref(trans, root, cow, 1);
339 BUG_ON(ret);
340 }
341 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
342 } else {
343
344 if (root->root_key.objectid ==
345 BTRFS_TREE_RELOC_OBJECTID)
346 ret = btrfs_inc_ref(trans, root, cow, 1);
347 else
348 ret = btrfs_inc_ref(trans, root, cow, 0);
349 BUG_ON(ret);
350 }
351 if (new_flags != 0) {
352 ret = btrfs_set_disk_extent_flags(trans, root,
353 buf->start,
354 buf->len,
355 new_flags, 0);
356 BUG_ON(ret);
357 }
358 } else {
359 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
360 if (root->root_key.objectid ==
361 BTRFS_TREE_RELOC_OBJECTID)
362 ret = btrfs_inc_ref(trans, root, cow, 1);
363 else
364 ret = btrfs_inc_ref(trans, root, cow, 0);
365 BUG_ON(ret);
366 ret = btrfs_dec_ref(trans, root, buf, 1);
367 BUG_ON(ret);
368 }
369 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400370 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400371 }
372 return 0;
373}
374
375/*
Chris Masond3977122009-01-05 21:25:51 -0500376 * does the dirty work in cow of a single block. The parent block (if
377 * supplied) is updated to point to the new cow copy. The new buffer is marked
378 * dirty and returned locked. If you modify the block it needs to be marked
379 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400380 *
381 * search_start -- an allocation hint for the new block
382 *
Chris Masond3977122009-01-05 21:25:51 -0500383 * empty_size -- a hint that you plan on doing more cow. This is the size in
384 * bytes the allocator should try to find free next to the block it returns.
385 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400386 */
Chris Masond3977122009-01-05 21:25:51 -0500387static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400388 struct btrfs_root *root,
389 struct extent_buffer *buf,
390 struct extent_buffer *parent, int parent_slot,
391 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400392 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400393{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400394 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400395 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500396 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400397 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400398 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400399 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400400
Chris Mason925baed2008-06-25 16:01:30 -0400401 if (*cow_ret == buf)
402 unlock_orig = 1;
403
Chris Masonb9447ef2009-03-09 11:45:38 -0400404 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400405
Chris Mason7bb86312007-12-11 09:25:06 -0500406 WARN_ON(root->ref_cows && trans->transid !=
407 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400408 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400409
Chris Mason7bb86312007-12-11 09:25:06 -0500410 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400411
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400412 if (level == 0)
413 btrfs_item_key(buf, &disk_key, 0);
414 else
415 btrfs_node_key(buf, &disk_key, 0);
416
417 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
418 if (parent)
419 parent_start = parent->start;
420 else
421 parent_start = 0;
422 } else
423 parent_start = 0;
424
425 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
426 root->root_key.objectid, &disk_key,
427 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400428 if (IS_ERR(cow))
429 return PTR_ERR(cow);
430
Chris Masonb4ce94d2009-02-04 09:25:08 -0500431 /* cow is set to blocking by btrfs_init_new_buffer */
432
Chris Mason5f39d392007-10-15 16:14:19 -0400433 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400434 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400435 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
437 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
438 BTRFS_HEADER_FLAG_RELOC);
439 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
440 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
441 else
442 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400443
Yan Zheng2b820322008-11-17 21:11:30 -0500444 write_extent_buffer(cow, root->fs_info->fsid,
445 (unsigned long)btrfs_header_fsid(cow),
446 BTRFS_FSID_SIZE);
447
Yan, Zhengf0486c62010-05-16 10:46:25 -0400448 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400449
Chris Mason6702ed42007-08-07 16:15:09 -0400450 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400451 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400452 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
453 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
454 parent_start = buf->start;
455 else
456 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400457
458 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400459 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400460 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400461 spin_unlock(&root->node_lock);
462
Yan, Zhengf0486c62010-05-16 10:46:25 -0400463 btrfs_free_tree_block(trans, root, buf, parent_start,
464 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400465 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400466 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400467 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400468 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
469 parent_start = parent->start;
470 else
471 parent_start = 0;
472
473 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400474 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400475 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500476 btrfs_set_node_ptr_generation(parent, parent_slot,
477 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400478 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400479 btrfs_free_tree_block(trans, root, buf, parent_start,
480 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400481 }
Chris Mason925baed2008-06-25 16:01:30 -0400482 if (unlock_orig)
483 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400484 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400485 btrfs_mark_buffer_dirty(cow);
486 *cow_ret = cow;
487 return 0;
488}
489
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400490static inline int should_cow_block(struct btrfs_trans_handle *trans,
491 struct btrfs_root *root,
492 struct extent_buffer *buf)
493{
494 if (btrfs_header_generation(buf) == trans->transid &&
495 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
496 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
497 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
498 return 0;
499 return 1;
500}
501
Chris Masond352ac62008-09-29 15:18:18 -0400502/*
503 * cows a single block, see __btrfs_cow_block for the real work.
504 * This version of it has extra checks so that a block isn't cow'd more than
505 * once per transaction, as long as it hasn't been written yet
506 */
Chris Masond3977122009-01-05 21:25:51 -0500507noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400508 struct btrfs_root *root, struct extent_buffer *buf,
509 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400510 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500511{
Chris Mason6702ed42007-08-07 16:15:09 -0400512 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400513 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500514
Chris Masonccd467d2007-06-28 15:57:36 -0400515 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500516 printk(KERN_CRIT "trans %llu running %llu\n",
517 (unsigned long long)trans->transid,
518 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400519 root->fs_info->running_transaction->transid);
520 WARN_ON(1);
521 }
522 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500523 printk(KERN_CRIT "trans %llu running %llu\n",
524 (unsigned long long)trans->transid,
525 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400526 WARN_ON(1);
527 }
Chris Masondc17ff82008-01-08 15:46:30 -0500528
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400529 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500530 *cow_ret = buf;
531 return 0;
532 }
Chris Masonc4876852009-02-04 09:24:25 -0500533
Chris Mason0b86a832008-03-24 15:01:56 -0400534 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500535
536 if (parent)
537 btrfs_set_lock_blocking(parent);
538 btrfs_set_lock_blocking(buf);
539
Chris Masonf510cfe2007-10-15 16:14:48 -0400540 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400541 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400542 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400543}
544
Chris Masond352ac62008-09-29 15:18:18 -0400545/*
546 * helper function for defrag to decide if two blocks pointed to by a
547 * node are actually close by
548 */
Chris Mason6b800532007-10-15 16:17:34 -0400549static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400550{
Chris Mason6b800532007-10-15 16:17:34 -0400551 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400552 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400553 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400554 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500555 return 0;
556}
557
Chris Mason081e9572007-11-06 10:26:24 -0500558/*
559 * compare two keys in a memcmp fashion
560 */
561static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
562{
563 struct btrfs_key k1;
564
565 btrfs_disk_key_to_cpu(&k1, disk);
566
Diego Calleja20736ab2009-07-24 11:06:52 -0400567 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500568}
569
Josef Bacikf3465ca2008-11-12 14:19:50 -0500570/*
571 * same as comp_keys only with two btrfs_key's
572 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400573int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500574{
575 if (k1->objectid > k2->objectid)
576 return 1;
577 if (k1->objectid < k2->objectid)
578 return -1;
579 if (k1->type > k2->type)
580 return 1;
581 if (k1->type < k2->type)
582 return -1;
583 if (k1->offset > k2->offset)
584 return 1;
585 if (k1->offset < k2->offset)
586 return -1;
587 return 0;
588}
Chris Mason081e9572007-11-06 10:26:24 -0500589
Chris Masond352ac62008-09-29 15:18:18 -0400590/*
591 * this is used by the defrag code to go through all the
592 * leaves pointed to by a node and reallocate them so that
593 * disk order is close to key order
594 */
Chris Mason6702ed42007-08-07 16:15:09 -0400595int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400596 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400597 int start_slot, int cache_only, u64 *last_ret,
598 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400599{
Chris Mason6b800532007-10-15 16:17:34 -0400600 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400601 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400602 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400603 u64 search_start = *last_ret;
604 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400605 u64 other;
606 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400607 int end_slot;
608 int i;
609 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400610 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400611 int uptodate;
612 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500613 int progress_passed = 0;
614 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400615
Chris Mason5708b952007-10-25 15:43:18 -0400616 parent_level = btrfs_header_level(parent);
617 if (cache_only && parent_level != 1)
618 return 0;
619
Chris Masond3977122009-01-05 21:25:51 -0500620 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400621 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500622 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400623 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400624
Chris Mason6b800532007-10-15 16:17:34 -0400625 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400626 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400627 end_slot = parent_nritems;
628
629 if (parent_nritems == 1)
630 return 0;
631
Chris Masonb4ce94d2009-02-04 09:25:08 -0500632 btrfs_set_lock_blocking(parent);
633
Chris Mason6702ed42007-08-07 16:15:09 -0400634 for (i = start_slot; i < end_slot; i++) {
635 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400636
Chris Mason5708b952007-10-25 15:43:18 -0400637 if (!parent->map_token) {
638 map_extent_buffer(parent,
639 btrfs_node_key_ptr_offset(i),
640 sizeof(struct btrfs_key_ptr),
641 &parent->map_token, &parent->kaddr,
642 &parent->map_start, &parent->map_len,
643 KM_USER1);
644 }
Chris Mason081e9572007-11-06 10:26:24 -0500645 btrfs_node_key(parent, &disk_key, i);
646 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
647 continue;
648
649 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400650 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400651 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400652 if (last_block == 0)
653 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400654
Chris Mason6702ed42007-08-07 16:15:09 -0400655 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400656 other = btrfs_node_blockptr(parent, i - 1);
657 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400658 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400659 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400660 other = btrfs_node_blockptr(parent, i + 1);
661 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400662 }
Chris Masone9d0b132007-08-10 14:06:19 -0400663 if (close) {
664 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400665 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400666 }
Chris Mason5708b952007-10-25 15:43:18 -0400667 if (parent->map_token) {
668 unmap_extent_buffer(parent, parent->map_token,
669 KM_USER1);
670 parent->map_token = NULL;
671 }
Chris Mason6702ed42007-08-07 16:15:09 -0400672
Chris Mason6b800532007-10-15 16:17:34 -0400673 cur = btrfs_find_tree_block(root, blocknr, blocksize);
674 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400675 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400676 else
677 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400678 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400679 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400680 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400681 continue;
682 }
Chris Mason6b800532007-10-15 16:17:34 -0400683 if (!cur) {
684 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400685 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400686 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400687 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400688 }
Chris Mason6702ed42007-08-07 16:15:09 -0400689 }
Chris Masone9d0b132007-08-10 14:06:19 -0400690 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400691 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400692
Chris Masone7a84562008-06-25 16:01:31 -0400693 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500694 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400695 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400696 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400697 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400698 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400699 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400700 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400701 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400702 break;
Yan252c38f2007-08-29 09:11:44 -0400703 }
Chris Masone7a84562008-06-25 16:01:31 -0400704 search_start = cur->start;
705 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400706 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400707 btrfs_tree_unlock(cur);
708 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400709 }
Chris Mason5708b952007-10-25 15:43:18 -0400710 if (parent->map_token) {
711 unmap_extent_buffer(parent, parent->map_token,
712 KM_USER1);
713 parent->map_token = NULL;
714 }
Chris Mason6702ed42007-08-07 16:15:09 -0400715 return err;
716}
717
Chris Mason74123bd2007-02-02 11:05:29 -0500718/*
719 * The leaf data grows from end-to-front in the node.
720 * this returns the address of the start of the last item,
721 * which is the stop of the leaf data stack
722 */
Chris Mason123abc82007-03-14 14:14:43 -0400723static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400724 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500725{
Chris Mason5f39d392007-10-15 16:14:19 -0400726 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500727 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400728 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400729 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500730}
731
Chris Masond352ac62008-09-29 15:18:18 -0400732/*
733 * extra debugging checks to make sure all the items in a key are
734 * well formed and in the proper order
735 */
Chris Mason123abc82007-03-14 14:14:43 -0400736static int check_node(struct btrfs_root *root, struct btrfs_path *path,
737 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500738{
Chris Mason5f39d392007-10-15 16:14:19 -0400739 struct extent_buffer *parent = NULL;
740 struct extent_buffer *node = path->nodes[level];
741 struct btrfs_disk_key parent_key;
742 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500743 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400744 int slot;
745 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400746 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500747
748 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400749 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400750
Chris Mason8d7be552007-05-10 11:24:42 -0400751 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400752 BUG_ON(nritems == 0);
753 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400754 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400755 btrfs_node_key(parent, &parent_key, parent_slot);
756 btrfs_node_key(node, &node_key, 0);
757 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400758 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400759 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400760 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500761 }
Chris Mason123abc82007-03-14 14:14:43 -0400762 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400763 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400764 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
765 btrfs_node_key(node, &node_key, slot);
766 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400767 }
768 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400769 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
770 btrfs_node_key(node, &node_key, slot);
771 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500772 }
773 return 0;
774}
775
Chris Masond352ac62008-09-29 15:18:18 -0400776/*
777 * extra checking to make sure all the items in a leaf are
778 * well formed and in the proper order
779 */
Chris Mason123abc82007-03-14 14:14:43 -0400780static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
781 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500782{
Chris Mason5f39d392007-10-15 16:14:19 -0400783 struct extent_buffer *leaf = path->nodes[level];
784 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500785 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400786 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400787 struct btrfs_disk_key parent_key;
788 struct btrfs_disk_key leaf_key;
789 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400790
Chris Mason5f39d392007-10-15 16:14:19 -0400791 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500792
793 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400794 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400795
796 if (nritems == 0)
797 return 0;
798
799 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400800 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400801 btrfs_node_key(parent, &parent_key, parent_slot);
802 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400803
Chris Mason5f39d392007-10-15 16:14:19 -0400804 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400805 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400806 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400807 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500808 }
Chris Mason5f39d392007-10-15 16:14:19 -0400809 if (slot != 0 && slot < nritems - 1) {
810 btrfs_item_key(leaf, &leaf_key, slot);
811 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
812 if (comp_keys(&leaf_key, &cpukey) <= 0) {
813 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500814 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400815 BUG_ON(1);
816 }
817 if (btrfs_item_offset_nr(leaf, slot - 1) !=
818 btrfs_item_end_nr(leaf, slot)) {
819 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500820 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400821 BUG_ON(1);
822 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500823 }
Chris Mason8d7be552007-05-10 11:24:42 -0400824 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400825 btrfs_item_key(leaf, &leaf_key, slot);
826 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
827 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
828 if (btrfs_item_offset_nr(leaf, slot) !=
829 btrfs_item_end_nr(leaf, slot + 1)) {
830 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500831 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400832 BUG_ON(1);
833 }
Chris Mason8d7be552007-05-10 11:24:42 -0400834 }
Chris Mason5f39d392007-10-15 16:14:19 -0400835 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
836 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500837 return 0;
838}
839
Chris Masond3977122009-01-05 21:25:51 -0500840static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500841 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500842{
Chris Mason85d824c2008-04-10 10:23:19 -0400843 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500844 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400845 return check_leaf(root, path, level);
846 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500847}
848
Chris Mason74123bd2007-02-02 11:05:29 -0500849/*
Chris Mason5f39d392007-10-15 16:14:19 -0400850 * search for key in the extent_buffer. The items start at offset p,
851 * and they are item_size apart. There are 'max' items in p.
852 *
Chris Mason74123bd2007-02-02 11:05:29 -0500853 * the slot in the array is returned via slot, and it points to
854 * the place where you would insert key if it is not found in
855 * the array.
856 *
857 * slot may point to max if the key is bigger than all of the keys
858 */
Chris Masone02119d2008-09-05 16:13:11 -0400859static noinline int generic_bin_search(struct extent_buffer *eb,
860 unsigned long p,
861 int item_size, struct btrfs_key *key,
862 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500863{
864 int low = 0;
865 int high = max;
866 int mid;
867 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400868 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400869 struct btrfs_disk_key unaligned;
870 unsigned long offset;
871 char *map_token = NULL;
872 char *kaddr = NULL;
873 unsigned long map_start = 0;
874 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400875 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500876
Chris Masond3977122009-01-05 21:25:51 -0500877 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500878 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400879 offset = p + mid * item_size;
880
881 if (!map_token || offset < map_start ||
882 (offset + sizeof(struct btrfs_disk_key)) >
883 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400884 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400885 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400886 map_token = NULL;
887 }
Chris Mason934d3752008-12-08 16:43:10 -0500888
889 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400890 sizeof(struct btrfs_disk_key),
891 &map_token, &kaddr,
892 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400893
Chris Mason479965d2007-10-15 16:14:27 -0400894 if (!err) {
895 tmp = (struct btrfs_disk_key *)(kaddr + offset -
896 map_start);
897 } else {
898 read_extent_buffer(eb, &unaligned,
899 offset, sizeof(unaligned));
900 tmp = &unaligned;
901 }
902
Chris Mason5f39d392007-10-15 16:14:19 -0400903 } else {
904 tmp = (struct btrfs_disk_key *)(kaddr + offset -
905 map_start);
906 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500907 ret = comp_keys(tmp, key);
908
909 if (ret < 0)
910 low = mid + 1;
911 else if (ret > 0)
912 high = mid;
913 else {
914 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400915 if (map_token)
916 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500917 return 0;
918 }
919 }
920 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400921 if (map_token)
922 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500923 return 1;
924}
925
Chris Mason97571fd2007-02-24 13:39:08 -0500926/*
927 * simple bin_search frontend that does the right thing for
928 * leaves vs nodes
929 */
Chris Mason5f39d392007-10-15 16:14:19 -0400930static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
931 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500932{
Chris Mason5f39d392007-10-15 16:14:19 -0400933 if (level == 0) {
934 return generic_bin_search(eb,
935 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400936 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400937 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400938 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500939 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400940 return generic_bin_search(eb,
941 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400942 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400943 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400944 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500945 }
946 return -1;
947}
948
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
950 int level, int *slot)
951{
952 return bin_search(eb, key, level, slot);
953}
954
Yan, Zhengf0486c62010-05-16 10:46:25 -0400955static void root_add_used(struct btrfs_root *root, u32 size)
956{
957 spin_lock(&root->accounting_lock);
958 btrfs_set_root_used(&root->root_item,
959 btrfs_root_used(&root->root_item) + size);
960 spin_unlock(&root->accounting_lock);
961}
962
963static void root_sub_used(struct btrfs_root *root, u32 size)
964{
965 spin_lock(&root->accounting_lock);
966 btrfs_set_root_used(&root->root_item,
967 btrfs_root_used(&root->root_item) - size);
968 spin_unlock(&root->accounting_lock);
969}
970
Chris Masond352ac62008-09-29 15:18:18 -0400971/* given a node and slot number, this reads the blocks it points to. The
972 * extent buffer is returned with a reference taken (but unlocked).
973 * NULL is returned on error.
974 */
Chris Masone02119d2008-09-05 16:13:11 -0400975static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400976 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500977{
Chris Masonca7a79a2008-05-12 12:59:19 -0400978 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500979 if (slot < 0)
980 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400981 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500982 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400983
984 BUG_ON(level == 0);
985
Chris Masondb945352007-10-15 16:15:53 -0400986 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400987 btrfs_level_size(root, level - 1),
988 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500989}
990
Chris Masond352ac62008-09-29 15:18:18 -0400991/*
992 * node level balancing, used to make sure nodes are in proper order for
993 * item deletion. We balance from the top down, so we have to make sure
994 * that a deletion won't leave an node completely empty later on.
995 */
Chris Masone02119d2008-09-05 16:13:11 -0400996static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500997 struct btrfs_root *root,
998 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500999{
Chris Mason5f39d392007-10-15 16:14:19 -04001000 struct extent_buffer *right = NULL;
1001 struct extent_buffer *mid;
1002 struct extent_buffer *left = NULL;
1003 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001004 int ret = 0;
1005 int wret;
1006 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001007 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -04001008 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001009 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001010
1011 if (level == 0)
1012 return 0;
1013
Chris Mason5f39d392007-10-15 16:14:19 -04001014 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001015
Chris Mason925baed2008-06-25 16:01:30 -04001016 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -05001017 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1018
Chris Mason1d4f8a02007-03-13 09:28:32 -04001019 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001020
Chris Mason234b63a2007-03-13 10:46:10 -04001021 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001022 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001023 pslot = path->slots[level + 1];
1024
Chris Mason40689472007-03-17 14:29:23 -04001025 /*
1026 * deal with the case where there is only one pointer in the root
1027 * by promoting the node below to a root
1028 */
Chris Mason5f39d392007-10-15 16:14:19 -04001029 if (!parent) {
1030 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001031
Chris Mason5f39d392007-10-15 16:14:19 -04001032 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001033 return 0;
1034
1035 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001036 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001037 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001038 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001039 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001040 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001041 if (ret) {
1042 btrfs_tree_unlock(child);
1043 free_extent_buffer(child);
1044 goto enospc;
1045 }
Yan2f375ab2008-02-01 14:58:07 -05001046
Chris Mason925baed2008-06-25 16:01:30 -04001047 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001048 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001049 spin_unlock(&root->node_lock);
1050
Chris Mason0b86a832008-03-24 15:01:56 -04001051 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001052 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001053
Chris Mason925baed2008-06-25 16:01:30 -04001054 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001055 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001056 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001057 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001058 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001059 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001060
1061 root_sub_used(root, mid->len);
1062 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001063 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001064 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001065 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001066 }
Chris Mason5f39d392007-10-15 16:14:19 -04001067 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001068 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001069 return 0;
1070
Chris Mason5f39d392007-10-15 16:14:19 -04001071 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001072 err_on_enospc = 1;
1073
Chris Mason5f39d392007-10-15 16:14:19 -04001074 left = read_node_slot(root, parent, pslot - 1);
1075 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001076 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001077 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001078 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001079 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001080 if (wret) {
1081 ret = wret;
1082 goto enospc;
1083 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001084 }
Chris Mason5f39d392007-10-15 16:14:19 -04001085 right = read_node_slot(root, parent, pslot + 1);
1086 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001087 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001088 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001089 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001090 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001091 if (wret) {
1092 ret = wret;
1093 goto enospc;
1094 }
1095 }
1096
1097 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001098 if (left) {
1099 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001100 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001101 if (wret < 0)
1102 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001103 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001104 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001105 }
Chris Mason79f95c82007-03-01 15:16:26 -05001106
1107 /*
1108 * then try to empty the right most buffer into the middle
1109 */
Chris Mason5f39d392007-10-15 16:14:19 -04001110 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001111 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001112 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001113 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001114 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001115 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001116 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -04001117 wret = del_ptr(trans, root, path, level + 1, pslot +
1118 1);
Chris Masonbb803952007-03-01 12:04:21 -05001119 if (wret)
1120 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001121 root_sub_used(root, right->len);
1122 btrfs_free_tree_block(trans, root, right, 0, 1);
1123 free_extent_buffer(right);
1124 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001125 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001126 struct btrfs_disk_key right_key;
1127 btrfs_node_key(right, &right_key, 0);
1128 btrfs_set_node_key(parent, &right_key, pslot + 1);
1129 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001130 }
1131 }
Chris Mason5f39d392007-10-15 16:14:19 -04001132 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001133 /*
1134 * we're not allowed to leave a node with one item in the
1135 * tree during a delete. A deletion from lower in the tree
1136 * could try to delete the only pointer in this node.
1137 * So, pull some keys from the left.
1138 * There has to be a left pointer at this point because
1139 * otherwise we would have pulled some pointers from the
1140 * right
1141 */
Chris Mason5f39d392007-10-15 16:14:19 -04001142 BUG_ON(!left);
1143 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001144 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001145 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001146 goto enospc;
1147 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001148 if (wret == 1) {
1149 wret = push_node_left(trans, root, left, mid, 1);
1150 if (wret < 0)
1151 ret = wret;
1152 }
Chris Mason79f95c82007-03-01 15:16:26 -05001153 BUG_ON(wret == 1);
1154 }
Chris Mason5f39d392007-10-15 16:14:19 -04001155 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001156 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001157 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001158 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001159 if (wret)
1160 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001161 root_sub_used(root, mid->len);
1162 btrfs_free_tree_block(trans, root, mid, 0, 1);
1163 free_extent_buffer(mid);
1164 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001165 } else {
1166 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001167 struct btrfs_disk_key mid_key;
1168 btrfs_node_key(mid, &mid_key, 0);
1169 btrfs_set_node_key(parent, &mid_key, pslot);
1170 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001171 }
Chris Masonbb803952007-03-01 12:04:21 -05001172
Chris Mason79f95c82007-03-01 15:16:26 -05001173 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001174 if (left) {
1175 if (btrfs_header_nritems(left) > orig_slot) {
1176 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001177 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001178 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001179 path->slots[level + 1] -= 1;
1180 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001181 if (mid) {
1182 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001183 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001184 }
Chris Masonbb803952007-03-01 12:04:21 -05001185 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001186 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001187 path->slots[level] = orig_slot;
1188 }
1189 }
Chris Mason79f95c82007-03-01 15:16:26 -05001190 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001191 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001192 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001193 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001194 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001195enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001196 if (right) {
1197 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001198 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001199 }
1200 if (left) {
1201 if (path->nodes[level] != left)
1202 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001203 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001204 }
Chris Masonbb803952007-03-01 12:04:21 -05001205 return ret;
1206}
1207
Chris Masond352ac62008-09-29 15:18:18 -04001208/* Node balancing for insertion. Here we only split or push nodes around
1209 * when they are completely full. This is also done top down, so we
1210 * have to be pessimistic.
1211 */
Chris Masond3977122009-01-05 21:25:51 -05001212static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001213 struct btrfs_root *root,
1214 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001215{
Chris Mason5f39d392007-10-15 16:14:19 -04001216 struct extent_buffer *right = NULL;
1217 struct extent_buffer *mid;
1218 struct extent_buffer *left = NULL;
1219 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001220 int ret = 0;
1221 int wret;
1222 int pslot;
1223 int orig_slot = path->slots[level];
1224 u64 orig_ptr;
1225
1226 if (level == 0)
1227 return 1;
1228
Chris Mason5f39d392007-10-15 16:14:19 -04001229 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001230 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001231 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1232
1233 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001234 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001235 pslot = path->slots[level + 1];
1236
Chris Mason5f39d392007-10-15 16:14:19 -04001237 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001238 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001239
Chris Mason5f39d392007-10-15 16:14:19 -04001240 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001241
1242 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001243 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001244 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001245
1246 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001247 btrfs_set_lock_blocking(left);
1248
Chris Mason5f39d392007-10-15 16:14:19 -04001249 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001250 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1251 wret = 1;
1252 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001253 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001254 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001255 if (ret)
1256 wret = 1;
1257 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001258 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001259 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001260 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001261 }
Chris Masone66f7092007-04-20 13:16:02 -04001262 if (wret < 0)
1263 ret = wret;
1264 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001265 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001266 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001267 btrfs_node_key(mid, &disk_key, 0);
1268 btrfs_set_node_key(parent, &disk_key, pslot);
1269 btrfs_mark_buffer_dirty(parent);
1270 if (btrfs_header_nritems(left) > orig_slot) {
1271 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001272 path->slots[level + 1] -= 1;
1273 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001274 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001275 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001276 } else {
1277 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001278 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001279 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001280 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001281 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001282 }
Chris Masone66f7092007-04-20 13:16:02 -04001283 return 0;
1284 }
Chris Mason925baed2008-06-25 16:01:30 -04001285 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001286 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001287 }
Chris Mason925baed2008-06-25 16:01:30 -04001288 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001289
1290 /*
1291 * then try to empty the right most buffer into the middle
1292 */
Chris Mason5f39d392007-10-15 16:14:19 -04001293 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001294 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001295
Chris Mason925baed2008-06-25 16:01:30 -04001296 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001297 btrfs_set_lock_blocking(right);
1298
Chris Mason5f39d392007-10-15 16:14:19 -04001299 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001300 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1301 wret = 1;
1302 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001303 ret = btrfs_cow_block(trans, root, right,
1304 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001305 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001306 if (ret)
1307 wret = 1;
1308 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001309 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001310 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001311 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001312 }
Chris Masone66f7092007-04-20 13:16:02 -04001313 if (wret < 0)
1314 ret = wret;
1315 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001316 struct btrfs_disk_key disk_key;
1317
1318 btrfs_node_key(right, &disk_key, 0);
1319 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1320 btrfs_mark_buffer_dirty(parent);
1321
1322 if (btrfs_header_nritems(mid) <= orig_slot) {
1323 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001324 path->slots[level + 1] += 1;
1325 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001326 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001327 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001328 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001329 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001330 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001331 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001332 }
Chris Masone66f7092007-04-20 13:16:02 -04001333 return 0;
1334 }
Chris Mason925baed2008-06-25 16:01:30 -04001335 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001336 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001337 }
Chris Masone66f7092007-04-20 13:16:02 -04001338 return 1;
1339}
1340
Chris Mason74123bd2007-02-02 11:05:29 -05001341/*
Chris Masond352ac62008-09-29 15:18:18 -04001342 * readahead one full node of leaves, finding things that are close
1343 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001344 */
Chris Masonc8c42862009-04-03 10:14:18 -04001345static void reada_for_search(struct btrfs_root *root,
1346 struct btrfs_path *path,
1347 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001348{
Chris Mason5f39d392007-10-15 16:14:19 -04001349 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001350 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001351 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001352 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001353 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001354 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001355 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001356 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001357 u32 nr;
1358 u32 blocksize;
1359 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001360
Chris Masona6b6e752007-10-15 16:22:39 -04001361 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001362 return;
1363
Chris Mason6702ed42007-08-07 16:15:09 -04001364 if (!path->nodes[level])
1365 return;
1366
Chris Mason5f39d392007-10-15 16:14:19 -04001367 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001368
Chris Mason3c69fae2007-08-07 15:52:22 -04001369 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001370 blocksize = btrfs_level_size(root, level - 1);
1371 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001372 if (eb) {
1373 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001374 return;
1375 }
1376
Chris Masona7175312009-01-22 09:23:10 -05001377 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001378
Chris Mason5f39d392007-10-15 16:14:19 -04001379 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001380 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001381 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001382 if (direction < 0) {
1383 if (nr == 0)
1384 break;
1385 nr--;
1386 } else if (direction > 0) {
1387 nr++;
1388 if (nr >= nritems)
1389 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001390 }
Chris Mason01f46652007-12-21 16:24:26 -05001391 if (path->reada < 0 && objectid) {
1392 btrfs_node_key(node, &disk_key, nr);
1393 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1394 break;
1395 }
Chris Mason6b800532007-10-15 16:17:34 -04001396 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001397 if ((search <= target && target - search <= 65536) ||
1398 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001399 readahead_tree_block(root, search, blocksize,
1400 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001401 nread += blocksize;
1402 }
1403 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001404 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001405 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001406 }
1407}
Chris Mason925baed2008-06-25 16:01:30 -04001408
Chris Masond352ac62008-09-29 15:18:18 -04001409/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001410 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1411 * cache
1412 */
1413static noinline int reada_for_balance(struct btrfs_root *root,
1414 struct btrfs_path *path, int level)
1415{
1416 int slot;
1417 int nritems;
1418 struct extent_buffer *parent;
1419 struct extent_buffer *eb;
1420 u64 gen;
1421 u64 block1 = 0;
1422 u64 block2 = 0;
1423 int ret = 0;
1424 int blocksize;
1425
Chris Mason8c594ea2009-04-20 15:50:10 -04001426 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001427 if (!parent)
1428 return 0;
1429
1430 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001431 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001432 blocksize = btrfs_level_size(root, level);
1433
1434 if (slot > 0) {
1435 block1 = btrfs_node_blockptr(parent, slot - 1);
1436 gen = btrfs_node_ptr_generation(parent, slot - 1);
1437 eb = btrfs_find_tree_block(root, block1, blocksize);
1438 if (eb && btrfs_buffer_uptodate(eb, gen))
1439 block1 = 0;
1440 free_extent_buffer(eb);
1441 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001442 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001443 block2 = btrfs_node_blockptr(parent, slot + 1);
1444 gen = btrfs_node_ptr_generation(parent, slot + 1);
1445 eb = btrfs_find_tree_block(root, block2, blocksize);
1446 if (eb && btrfs_buffer_uptodate(eb, gen))
1447 block2 = 0;
1448 free_extent_buffer(eb);
1449 }
1450 if (block1 || block2) {
1451 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001452
1453 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001454 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001455
1456 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001457 if (block1)
1458 readahead_tree_block(root, block1, blocksize, 0);
1459 if (block2)
1460 readahead_tree_block(root, block2, blocksize, 0);
1461
1462 if (block1) {
1463 eb = read_tree_block(root, block1, blocksize, 0);
1464 free_extent_buffer(eb);
1465 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001466 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001467 eb = read_tree_block(root, block2, blocksize, 0);
1468 free_extent_buffer(eb);
1469 }
1470 }
1471 return ret;
1472}
1473
1474
1475/*
Chris Masond3977122009-01-05 21:25:51 -05001476 * when we walk down the tree, it is usually safe to unlock the higher layers
1477 * in the tree. The exceptions are when our path goes through slot 0, because
1478 * operations on the tree might require changing key pointers higher up in the
1479 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001480 *
Chris Masond3977122009-01-05 21:25:51 -05001481 * callers might also have set path->keep_locks, which tells this code to keep
1482 * the lock if the path points to the last slot in the block. This is part of
1483 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001484 *
Chris Masond3977122009-01-05 21:25:51 -05001485 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1486 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001487 */
Chris Masone02119d2008-09-05 16:13:11 -04001488static noinline void unlock_up(struct btrfs_path *path, int level,
1489 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001490{
1491 int i;
1492 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001493 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001494 struct extent_buffer *t;
1495
1496 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1497 if (!path->nodes[i])
1498 break;
1499 if (!path->locks[i])
1500 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001501 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001502 skip_level = i + 1;
1503 continue;
1504 }
Chris Mason051e1b92008-06-25 16:01:30 -04001505 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001506 u32 nritems;
1507 t = path->nodes[i];
1508 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001509 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001510 skip_level = i + 1;
1511 continue;
1512 }
1513 }
Chris Mason051e1b92008-06-25 16:01:30 -04001514 if (skip_level < i && i >= lowest_unlock)
1515 no_skips = 1;
1516
Chris Mason925baed2008-06-25 16:01:30 -04001517 t = path->nodes[i];
1518 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1519 btrfs_tree_unlock(t);
1520 path->locks[i] = 0;
1521 }
1522 }
1523}
1524
Chris Mason3c69fae2007-08-07 15:52:22 -04001525/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001526 * This releases any locks held in the path starting at level and
1527 * going all the way up to the root.
1528 *
1529 * btrfs_search_slot will keep the lock held on higher nodes in a few
1530 * corner cases, such as COW of the block at slot zero in the node. This
1531 * ignores those rules, and it should only be called when there are no
1532 * more updates to be done higher up in the tree.
1533 */
1534noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1535{
1536 int i;
1537
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001538 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001539 return;
1540
1541 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1542 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001543 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001544 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001545 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001546 btrfs_tree_unlock(path->nodes[i]);
1547 path->locks[i] = 0;
1548 }
1549}
1550
1551/*
Chris Masonc8c42862009-04-03 10:14:18 -04001552 * helper function for btrfs_search_slot. The goal is to find a block
1553 * in cache without setting the path to blocking. If we find the block
1554 * we return zero and the path is unchanged.
1555 *
1556 * If we can't find the block, we set the path blocking and do some
1557 * reada. -EAGAIN is returned and the search must be repeated.
1558 */
1559static int
1560read_block_for_search(struct btrfs_trans_handle *trans,
1561 struct btrfs_root *root, struct btrfs_path *p,
1562 struct extent_buffer **eb_ret, int level, int slot,
1563 struct btrfs_key *key)
1564{
1565 u64 blocknr;
1566 u64 gen;
1567 u32 blocksize;
1568 struct extent_buffer *b = *eb_ret;
1569 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001570 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001571
1572 blocknr = btrfs_node_blockptr(b, slot);
1573 gen = btrfs_node_ptr_generation(b, slot);
1574 blocksize = btrfs_level_size(root, level - 1);
1575
1576 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1577 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason76a05b32009-05-14 13:24:30 -04001578 /*
1579 * we found an up to date block without sleeping, return
1580 * right away
1581 */
Chris Masonc8c42862009-04-03 10:14:18 -04001582 *eb_ret = tmp;
1583 return 0;
1584 }
1585
1586 /*
1587 * reduce lock contention at high levels
1588 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001589 * we read. Don't release the lock on the current
1590 * level because we need to walk this node to figure
1591 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001592 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001593 btrfs_unlock_up_safe(p, level + 1);
1594 btrfs_set_path_blocking(p);
1595
Chris Masonc8c42862009-04-03 10:14:18 -04001596 if (tmp)
1597 free_extent_buffer(tmp);
1598 if (p->reada)
1599 reada_for_search(root, p, level, slot, key->objectid);
1600
Chris Mason8c594ea2009-04-20 15:50:10 -04001601 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001602
1603 ret = -EAGAIN;
Chris Masonc8c42862009-04-03 10:14:18 -04001604 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Mason76a05b32009-05-14 13:24:30 -04001605 if (tmp) {
1606 /*
1607 * If the read above didn't mark this buffer up to date,
1608 * it will never end up being up to date. Set ret to EIO now
1609 * and give up so that our caller doesn't loop forever
1610 * on our EAGAINs.
1611 */
1612 if (!btrfs_buffer_uptodate(tmp, 0))
1613 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001614 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001615 }
1616 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001617}
1618
1619/*
1620 * helper function for btrfs_search_slot. This does all of the checks
1621 * for node-level blocks and does any balancing required based on
1622 * the ins_len.
1623 *
1624 * If no extra work was required, zero is returned. If we had to
1625 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1626 * start over
1627 */
1628static int
1629setup_nodes_for_search(struct btrfs_trans_handle *trans,
1630 struct btrfs_root *root, struct btrfs_path *p,
1631 struct extent_buffer *b, int level, int ins_len)
1632{
1633 int ret;
1634 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1635 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1636 int sret;
1637
1638 sret = reada_for_balance(root, p, level);
1639 if (sret)
1640 goto again;
1641
1642 btrfs_set_path_blocking(p);
1643 sret = split_node(trans, root, p, level);
1644 btrfs_clear_path_blocking(p, NULL);
1645
1646 BUG_ON(sret > 0);
1647 if (sret) {
1648 ret = sret;
1649 goto done;
1650 }
1651 b = p->nodes[level];
1652 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001653 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001654 int sret;
1655
1656 sret = reada_for_balance(root, p, level);
1657 if (sret)
1658 goto again;
1659
1660 btrfs_set_path_blocking(p);
1661 sret = balance_level(trans, root, p, level);
1662 btrfs_clear_path_blocking(p, NULL);
1663
1664 if (sret) {
1665 ret = sret;
1666 goto done;
1667 }
1668 b = p->nodes[level];
1669 if (!b) {
1670 btrfs_release_path(NULL, p);
1671 goto again;
1672 }
1673 BUG_ON(btrfs_header_nritems(b) == 1);
1674 }
1675 return 0;
1676
1677again:
1678 ret = -EAGAIN;
1679done:
1680 return ret;
1681}
1682
1683/*
Chris Mason74123bd2007-02-02 11:05:29 -05001684 * look for key in the tree. path is filled in with nodes along the way
1685 * if key is found, we return zero and you can find the item in the leaf
1686 * level of the path (level 0)
1687 *
1688 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001689 * be inserted, and 1 is returned. If there are other errors during the
1690 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001691 *
1692 * if ins_len > 0, nodes and leaves will be split as we walk down the
1693 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1694 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001695 */
Chris Masone089f052007-03-16 16:20:31 -04001696int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1697 *root, struct btrfs_key *key, struct btrfs_path *p, int
1698 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001699{
Chris Mason5f39d392007-10-15 16:14:19 -04001700 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001701 int slot;
1702 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001703 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001704 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001705 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001706 u8 lowest_level = 0;
1707
Chris Mason6702ed42007-08-07 16:15:09 -04001708 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001709 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001710 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001711
Chris Mason925baed2008-06-25 16:01:30 -04001712 if (ins_len < 0)
1713 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001714
Chris Masonbb803952007-03-01 12:04:21 -05001715again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001716 if (p->search_commit_root) {
1717 b = root->commit_root;
1718 extent_buffer_get(b);
1719 if (!p->skip_locking)
1720 btrfs_tree_lock(b);
1721 } else {
1722 if (p->skip_locking)
1723 b = btrfs_root_node(root);
1724 else
1725 b = btrfs_lock_root_node(root);
1726 }
Chris Mason925baed2008-06-25 16:01:30 -04001727
Chris Masoneb60cea2007-02-02 09:18:22 -05001728 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001729 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001730
1731 /*
1732 * setup the path here so we can release it under lock
1733 * contention with the cow code
1734 */
1735 p->nodes[level] = b;
1736 if (!p->skip_locking)
1737 p->locks[level] = 1;
1738
Chris Mason02217ed2007-03-02 16:08:05 -05001739 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001740 /*
1741 * if we don't really need to cow this block
1742 * then we don't want to set the path blocking,
1743 * so we test it here
1744 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001745 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001746 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001747
Chris Masonb4ce94d2009-02-04 09:25:08 -05001748 btrfs_set_path_blocking(p);
1749
Yan Zheng33c66f42009-07-22 09:59:00 -04001750 err = btrfs_cow_block(trans, root, b,
1751 p->nodes[level + 1],
1752 p->slots[level + 1], &b);
1753 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001754 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001755 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001756 }
Chris Mason02217ed2007-03-02 16:08:05 -05001757 }
Chris Mason65b51a02008-08-01 15:11:20 -04001758cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001759 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001760 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001761 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001762 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001763
Chris Masoneb60cea2007-02-02 09:18:22 -05001764 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001765 if (!p->skip_locking)
1766 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001767
Chris Mason4008c042009-02-12 14:09:45 -05001768 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001769
1770 /*
1771 * we have a lock on b and as long as we aren't changing
1772 * the tree, there is no way to for the items in b to change.
1773 * It is safe to drop the lock on our parent before we
1774 * go through the expensive btree search on b.
1775 *
1776 * If cow is true, then we might be changing slot zero,
1777 * which may require changing the parent. So, we can't
1778 * drop the lock until after we know which slot we're
1779 * operating on.
1780 */
1781 if (!cow)
1782 btrfs_unlock_up_safe(p, level + 1);
1783
Chris Mason123abc82007-03-14 14:14:43 -04001784 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001785 if (ret) {
1786 ret = -1;
1787 goto done;
1788 }
Chris Mason925baed2008-06-25 16:01:30 -04001789
Chris Mason5f39d392007-10-15 16:14:19 -04001790 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001791
Chris Mason5f39d392007-10-15 16:14:19 -04001792 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001793 int dec = 0;
1794 if (ret && slot > 0) {
1795 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001796 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001797 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001798 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001799 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001800 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001801 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001802 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001803 if (err) {
1804 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001805 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001806 }
Chris Masonc8c42862009-04-03 10:14:18 -04001807 b = p->nodes[level];
1808 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001809
Chris Masonf9efa9c2008-06-25 16:14:04 -04001810 unlock_up(p, level, lowest_unlock);
1811
Chris Mason925baed2008-06-25 16:01:30 -04001812 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001813 if (dec)
1814 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001815 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001816 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001817
Yan Zheng33c66f42009-07-22 09:59:00 -04001818 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001819 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001820 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001821 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001822 if (err) {
1823 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001824 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001825 }
Chris Mason76a05b32009-05-14 13:24:30 -04001826
Chris Masonb4ce94d2009-02-04 09:25:08 -05001827 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001828 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001829 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001830
Yan Zheng33c66f42009-07-22 09:59:00 -04001831 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001832 btrfs_set_path_blocking(p);
1833 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001834 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001835 }
1836 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001837 } else {
1838 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001839 if (ins_len > 0 &&
1840 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001841 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001842 err = split_leaf(trans, root, key,
1843 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001844 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001845
Yan Zheng33c66f42009-07-22 09:59:00 -04001846 BUG_ON(err > 0);
1847 if (err) {
1848 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001849 goto done;
1850 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001851 }
Chris Mason459931e2008-12-10 09:10:46 -05001852 if (!p->search_for_split)
1853 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001854 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001855 }
1856 }
Chris Mason65b51a02008-08-01 15:11:20 -04001857 ret = 1;
1858done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001859 /*
1860 * we don't really know what they plan on doing with the path
1861 * from here on, so for now just mark it as blocking
1862 */
Chris Masonb9473432009-03-13 11:00:37 -04001863 if (!p->leave_spinning)
1864 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001865 if (ret < 0)
1866 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001867 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001868}
1869
Chris Mason74123bd2007-02-02 11:05:29 -05001870/*
1871 * adjust the pointers going up the tree, starting at level
1872 * making sure the right key of each node is points to 'key'.
1873 * This is used after shifting pointers to the left, so it stops
1874 * fixing up pointers when a given leaf/node is not in slot 0 of the
1875 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001876 *
1877 * If this fails to write a tree block, it returns -1, but continues
1878 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001879 */
Chris Mason5f39d392007-10-15 16:14:19 -04001880static int fixup_low_keys(struct btrfs_trans_handle *trans,
1881 struct btrfs_root *root, struct btrfs_path *path,
1882 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001883{
1884 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001885 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001886 struct extent_buffer *t;
1887
Chris Mason234b63a2007-03-13 10:46:10 -04001888 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001889 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001890 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001891 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001892 t = path->nodes[i];
1893 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001894 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001895 if (tslot != 0)
1896 break;
1897 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001898 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001899}
1900
Chris Mason74123bd2007-02-02 11:05:29 -05001901/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001902 * update item key.
1903 *
1904 * This function isn't completely safe. It's the caller's responsibility
1905 * that the new key won't break the order
1906 */
1907int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1908 struct btrfs_root *root, struct btrfs_path *path,
1909 struct btrfs_key *new_key)
1910{
1911 struct btrfs_disk_key disk_key;
1912 struct extent_buffer *eb;
1913 int slot;
1914
1915 eb = path->nodes[0];
1916 slot = path->slots[0];
1917 if (slot > 0) {
1918 btrfs_item_key(eb, &disk_key, slot - 1);
1919 if (comp_keys(&disk_key, new_key) >= 0)
1920 return -1;
1921 }
1922 if (slot < btrfs_header_nritems(eb) - 1) {
1923 btrfs_item_key(eb, &disk_key, slot + 1);
1924 if (comp_keys(&disk_key, new_key) <= 0)
1925 return -1;
1926 }
1927
1928 btrfs_cpu_key_to_disk(&disk_key, new_key);
1929 btrfs_set_item_key(eb, &disk_key, slot);
1930 btrfs_mark_buffer_dirty(eb);
1931 if (slot == 0)
1932 fixup_low_keys(trans, root, path, &disk_key, 1);
1933 return 0;
1934}
1935
1936/*
Chris Mason74123bd2007-02-02 11:05:29 -05001937 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001938 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001939 *
1940 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1941 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001942 */
Chris Mason98ed5172008-01-03 10:01:48 -05001943static int push_node_left(struct btrfs_trans_handle *trans,
1944 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001945 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001946{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001947 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001948 int src_nritems;
1949 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001950 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001951
Chris Mason5f39d392007-10-15 16:14:19 -04001952 src_nritems = btrfs_header_nritems(src);
1953 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001954 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001955 WARN_ON(btrfs_header_generation(src) != trans->transid);
1956 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001957
Chris Masonbce4eae2008-04-24 14:42:46 -04001958 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001959 return 1;
1960
Chris Masond3977122009-01-05 21:25:51 -05001961 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001962 return 1;
1963
Chris Masonbce4eae2008-04-24 14:42:46 -04001964 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001965 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001966 if (push_items < src_nritems) {
1967 /* leave at least 8 pointers in the node if
1968 * we aren't going to empty it
1969 */
1970 if (src_nritems - push_items < 8) {
1971 if (push_items <= 8)
1972 return 1;
1973 push_items -= 8;
1974 }
1975 }
1976 } else
1977 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001978
Chris Mason5f39d392007-10-15 16:14:19 -04001979 copy_extent_buffer(dst, src,
1980 btrfs_node_key_ptr_offset(dst_nritems),
1981 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001982 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001983
Chris Masonbb803952007-03-01 12:04:21 -05001984 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001985 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1986 btrfs_node_key_ptr_offset(push_items),
1987 (src_nritems - push_items) *
1988 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001989 }
Chris Mason5f39d392007-10-15 16:14:19 -04001990 btrfs_set_header_nritems(src, src_nritems - push_items);
1991 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1992 btrfs_mark_buffer_dirty(src);
1993 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001994
Chris Masonbb803952007-03-01 12:04:21 -05001995 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001996}
1997
Chris Mason97571fd2007-02-24 13:39:08 -05001998/*
Chris Mason79f95c82007-03-01 15:16:26 -05001999 * try to push data from one node into the next node right in the
2000 * tree.
2001 *
2002 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2003 * error, and > 0 if there was no room in the right hand block.
2004 *
2005 * this will only push up to 1/2 the contents of the left node over
2006 */
Chris Mason5f39d392007-10-15 16:14:19 -04002007static int balance_node_right(struct btrfs_trans_handle *trans,
2008 struct btrfs_root *root,
2009 struct extent_buffer *dst,
2010 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002011{
Chris Mason79f95c82007-03-01 15:16:26 -05002012 int push_items = 0;
2013 int max_push;
2014 int src_nritems;
2015 int dst_nritems;
2016 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002017
Chris Mason7bb86312007-12-11 09:25:06 -05002018 WARN_ON(btrfs_header_generation(src) != trans->transid);
2019 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2020
Chris Mason5f39d392007-10-15 16:14:19 -04002021 src_nritems = btrfs_header_nritems(src);
2022 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002023 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002024 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002025 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002026
Chris Masond3977122009-01-05 21:25:51 -05002027 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002028 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002029
2030 max_push = src_nritems / 2 + 1;
2031 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002032 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002033 return 1;
Yan252c38f2007-08-29 09:11:44 -04002034
Chris Mason79f95c82007-03-01 15:16:26 -05002035 if (max_push < push_items)
2036 push_items = max_push;
2037
Chris Mason5f39d392007-10-15 16:14:19 -04002038 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2039 btrfs_node_key_ptr_offset(0),
2040 (dst_nritems) *
2041 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 copy_extent_buffer(dst, src,
2044 btrfs_node_key_ptr_offset(0),
2045 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002046 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002047
Chris Mason5f39d392007-10-15 16:14:19 -04002048 btrfs_set_header_nritems(src, src_nritems - push_items);
2049 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002050
Chris Mason5f39d392007-10-15 16:14:19 -04002051 btrfs_mark_buffer_dirty(src);
2052 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002053
Chris Mason79f95c82007-03-01 15:16:26 -05002054 return ret;
2055}
2056
2057/*
Chris Mason97571fd2007-02-24 13:39:08 -05002058 * helper function to insert a new root level in the tree.
2059 * A new node is allocated, and a single item is inserted to
2060 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002061 *
2062 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002063 */
Chris Masond3977122009-01-05 21:25:51 -05002064static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002065 struct btrfs_root *root,
2066 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002067{
Chris Mason7bb86312007-12-11 09:25:06 -05002068 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002069 struct extent_buffer *lower;
2070 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002071 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002072 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002073
2074 BUG_ON(path->nodes[level]);
2075 BUG_ON(path->nodes[level-1] != root->node);
2076
Chris Mason7bb86312007-12-11 09:25:06 -05002077 lower = path->nodes[level-1];
2078 if (level == 1)
2079 btrfs_item_key(lower, &lower_key, 0);
2080 else
2081 btrfs_node_key(lower, &lower_key, 0);
2082
Zheng Yan31840ae2008-09-23 13:14:14 -04002083 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002084 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002085 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002086 if (IS_ERR(c))
2087 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002088
Yan, Zhengf0486c62010-05-16 10:46:25 -04002089 root_add_used(root, root->nodesize);
2090
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002091 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002092 btrfs_set_header_nritems(c, 1);
2093 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002094 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002095 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002098
Chris Mason5f39d392007-10-15 16:14:19 -04002099 write_extent_buffer(c, root->fs_info->fsid,
2100 (unsigned long)btrfs_header_fsid(c),
2101 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002102
2103 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2104 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2105 BTRFS_UUID_SIZE);
2106
Chris Mason5f39d392007-10-15 16:14:19 -04002107 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002108 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002109 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002110 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002111
2112 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002113
2114 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002115
Chris Mason925baed2008-06-25 16:01:30 -04002116 spin_lock(&root->node_lock);
2117 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002118 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002119 spin_unlock(&root->node_lock);
2120
2121 /* the super has an extra ref to root->node */
2122 free_extent_buffer(old);
2123
Chris Mason0b86a832008-03-24 15:01:56 -04002124 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002125 extent_buffer_get(c);
2126 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002127 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002128 path->slots[level] = 0;
2129 return 0;
2130}
2131
Chris Mason74123bd2007-02-02 11:05:29 -05002132/*
2133 * worker function to insert a single pointer in a node.
2134 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002135 *
Chris Mason74123bd2007-02-02 11:05:29 -05002136 * slot and level indicate where you want the key to go, and
2137 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002138 *
2139 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002140 */
Chris Masone089f052007-03-16 16:20:31 -04002141static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2142 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002143 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002144{
Chris Mason5f39d392007-10-15 16:14:19 -04002145 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002146 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002147
2148 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002149 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002150 lower = path->nodes[level];
2151 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002152 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002153 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002154 BUG();
2155 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002156 memmove_extent_buffer(lower,
2157 btrfs_node_key_ptr_offset(slot + 1),
2158 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002159 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002160 }
Chris Mason5f39d392007-10-15 16:14:19 -04002161 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002162 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002163 WARN_ON(trans->transid == 0);
2164 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002165 btrfs_set_header_nritems(lower, nritems + 1);
2166 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002167 return 0;
2168}
2169
Chris Mason97571fd2007-02-24 13:39:08 -05002170/*
2171 * split the node at the specified level in path in two.
2172 * The path is corrected to point to the appropriate node after the split
2173 *
2174 * Before splitting this tries to make some room in the node by pushing
2175 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002176 *
2177 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002178 */
Chris Masone02119d2008-09-05 16:13:11 -04002179static noinline int split_node(struct btrfs_trans_handle *trans,
2180 struct btrfs_root *root,
2181 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002182{
Chris Mason5f39d392007-10-15 16:14:19 -04002183 struct extent_buffer *c;
2184 struct extent_buffer *split;
2185 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002186 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002187 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002188 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002189 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002190
Chris Mason5f39d392007-10-15 16:14:19 -04002191 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002192 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002193 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002194 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002195 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002196 if (ret)
2197 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002198 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002199 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002200 c = path->nodes[level];
2201 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002202 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002203 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002204 if (ret < 0)
2205 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002206 }
Chris Masone66f7092007-04-20 13:16:02 -04002207
Chris Mason5f39d392007-10-15 16:14:19 -04002208 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002209 mid = (c_nritems + 1) / 2;
2210 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002211
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002212 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002213 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002214 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002215 if (IS_ERR(split))
2216 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002217
Yan, Zhengf0486c62010-05-16 10:46:25 -04002218 root_add_used(root, root->nodesize);
2219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002220 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002221 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002222 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002223 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002225 btrfs_set_header_owner(split, root->root_key.objectid);
2226 write_extent_buffer(split, root->fs_info->fsid,
2227 (unsigned long)btrfs_header_fsid(split),
2228 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002229 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2230 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2231 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002232
Chris Mason5f39d392007-10-15 16:14:19 -04002233
2234 copy_extent_buffer(split, c,
2235 btrfs_node_key_ptr_offset(0),
2236 btrfs_node_key_ptr_offset(mid),
2237 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2238 btrfs_set_header_nritems(split, c_nritems - mid);
2239 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002240 ret = 0;
2241
Chris Mason5f39d392007-10-15 16:14:19 -04002242 btrfs_mark_buffer_dirty(c);
2243 btrfs_mark_buffer_dirty(split);
2244
Chris Masondb945352007-10-15 16:15:53 -04002245 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002246 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002247 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002248 if (wret)
2249 ret = wret;
2250
Chris Mason5de08d72007-02-24 06:24:44 -05002251 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002252 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002253 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002254 free_extent_buffer(c);
2255 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002256 path->slots[level + 1] += 1;
2257 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002258 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002259 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002260 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002261 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002262}
2263
Chris Mason74123bd2007-02-02 11:05:29 -05002264/*
2265 * how many bytes are required to store the items in a leaf. start
2266 * and nr indicate which items in the leaf to check. This totals up the
2267 * space used both by the item structs and the item data
2268 */
Chris Mason5f39d392007-10-15 16:14:19 -04002269static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002270{
2271 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002272 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002273 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002274
2275 if (!nr)
2276 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002277 data_len = btrfs_item_end_nr(l, start);
2278 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002279 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002280 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002281 return data_len;
2282}
2283
Chris Mason74123bd2007-02-02 11:05:29 -05002284/*
Chris Masond4dbff92007-04-04 14:08:15 -04002285 * The space between the end of the leaf items and
2286 * the start of the leaf data. IOW, how much room
2287 * the leaf has left for both items and data
2288 */
Chris Masond3977122009-01-05 21:25:51 -05002289noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002290 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002291{
Chris Mason5f39d392007-10-15 16:14:19 -04002292 int nritems = btrfs_header_nritems(leaf);
2293 int ret;
2294 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2295 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002296 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2297 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002298 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002299 leaf_space_used(leaf, 0, nritems), nritems);
2300 }
2301 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002302}
2303
Chris Mason44871b12009-03-13 10:04:31 -04002304static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2305 struct btrfs_root *root,
2306 struct btrfs_path *path,
2307 int data_size, int empty,
2308 struct extent_buffer *right,
2309 int free_space, u32 left_nritems)
Chris Mason00ec4c52007-02-24 12:47:20 -05002310{
Chris Mason5f39d392007-10-15 16:14:19 -04002311 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002312 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002313 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002314 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002315 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002316 int push_space = 0;
2317 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002318 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002319 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002320 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002321 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002322 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002323
Chris Mason34a38212007-11-07 13:31:03 -05002324 if (empty)
2325 nr = 0;
2326 else
2327 nr = 1;
2328
Zheng Yan31840ae2008-09-23 13:14:14 -04002329 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002330 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002331
Chris Mason44871b12009-03-13 10:04:31 -04002332 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002333 i = left_nritems - 1;
2334 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002335 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002336
Zheng Yan31840ae2008-09-23 13:14:14 -04002337 if (!empty && push_items > 0) {
2338 if (path->slots[0] > i)
2339 break;
2340 if (path->slots[0] == i) {
2341 int space = btrfs_leaf_free_space(root, left);
2342 if (space + push_space * 2 > free_space)
2343 break;
2344 }
2345 }
2346
Chris Mason00ec4c52007-02-24 12:47:20 -05002347 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002348 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002349
2350 if (!left->map_token) {
2351 map_extent_buffer(left, (unsigned long)item,
2352 sizeof(struct btrfs_item),
2353 &left->map_token, &left->kaddr,
2354 &left->map_start, &left->map_len,
2355 KM_USER1);
2356 }
2357
2358 this_item_size = btrfs_item_size(left, item);
2359 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002360 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002361
Chris Mason00ec4c52007-02-24 12:47:20 -05002362 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002363 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002364 if (i == 0)
2365 break;
2366 i--;
Chris Masondb945352007-10-15 16:15:53 -04002367 }
2368 if (left->map_token) {
2369 unmap_extent_buffer(left, left->map_token, KM_USER1);
2370 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002371 }
Chris Mason5f39d392007-10-15 16:14:19 -04002372
Chris Mason925baed2008-06-25 16:01:30 -04002373 if (push_items == 0)
2374 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002375
Chris Mason34a38212007-11-07 13:31:03 -05002376 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002377 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002378
Chris Mason00ec4c52007-02-24 12:47:20 -05002379 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002380 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002381
Chris Mason5f39d392007-10-15 16:14:19 -04002382 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002383 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002384
Chris Mason00ec4c52007-02-24 12:47:20 -05002385 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002386 data_end = leaf_data_end(root, right);
2387 memmove_extent_buffer(right,
2388 btrfs_leaf_data(right) + data_end - push_space,
2389 btrfs_leaf_data(right) + data_end,
2390 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2391
Chris Mason00ec4c52007-02-24 12:47:20 -05002392 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002393 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002394 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2395 btrfs_leaf_data(left) + leaf_data_end(root, left),
2396 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002397
2398 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2399 btrfs_item_nr_offset(0),
2400 right_nritems * sizeof(struct btrfs_item));
2401
Chris Mason00ec4c52007-02-24 12:47:20 -05002402 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002403 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2404 btrfs_item_nr_offset(left_nritems - push_items),
2405 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002406
2407 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002408 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002409 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002410 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002411 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002412 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002413 if (!right->map_token) {
2414 map_extent_buffer(right, (unsigned long)item,
2415 sizeof(struct btrfs_item),
2416 &right->map_token, &right->kaddr,
2417 &right->map_start, &right->map_len,
2418 KM_USER1);
2419 }
2420 push_space -= btrfs_item_size(right, item);
2421 btrfs_set_item_offset(right, item, push_space);
2422 }
2423
2424 if (right->map_token) {
2425 unmap_extent_buffer(right, right->map_token, KM_USER1);
2426 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002427 }
Chris Mason7518a232007-03-12 12:01:18 -04002428 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002429 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002430
Chris Mason34a38212007-11-07 13:31:03 -05002431 if (left_nritems)
2432 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002433 else
2434 clean_tree_block(trans, root, left);
2435
Chris Mason5f39d392007-10-15 16:14:19 -04002436 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002437
Chris Mason5f39d392007-10-15 16:14:19 -04002438 btrfs_item_key(right, &disk_key, 0);
2439 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002440 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002441
Chris Mason00ec4c52007-02-24 12:47:20 -05002442 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002443 if (path->slots[0] >= left_nritems) {
2444 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002445 if (btrfs_header_nritems(path->nodes[0]) == 0)
2446 clean_tree_block(trans, root, path->nodes[0]);
2447 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002448 free_extent_buffer(path->nodes[0]);
2449 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002450 path->slots[1] += 1;
2451 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002452 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002453 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002454 }
2455 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002456
2457out_unlock:
2458 btrfs_tree_unlock(right);
2459 free_extent_buffer(right);
2460 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002461}
Chris Mason925baed2008-06-25 16:01:30 -04002462
Chris Mason00ec4c52007-02-24 12:47:20 -05002463/*
Chris Mason44871b12009-03-13 10:04:31 -04002464 * push some data in the path leaf to the right, trying to free up at
2465 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2466 *
2467 * returns 1 if the push failed because the other node didn't have enough
2468 * room, 0 if everything worked out and < 0 if there were major errors.
2469 */
2470static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2471 *root, struct btrfs_path *path, int data_size,
2472 int empty)
2473{
2474 struct extent_buffer *left = path->nodes[0];
2475 struct extent_buffer *right;
2476 struct extent_buffer *upper;
2477 int slot;
2478 int free_space;
2479 u32 left_nritems;
2480 int ret;
2481
2482 if (!path->nodes[1])
2483 return 1;
2484
2485 slot = path->slots[1];
2486 upper = path->nodes[1];
2487 if (slot >= btrfs_header_nritems(upper) - 1)
2488 return 1;
2489
2490 btrfs_assert_tree_locked(path->nodes[1]);
2491
2492 right = read_node_slot(root, upper, slot + 1);
2493 btrfs_tree_lock(right);
2494 btrfs_set_lock_blocking(right);
2495
2496 free_space = btrfs_leaf_free_space(root, right);
2497 if (free_space < data_size)
2498 goto out_unlock;
2499
2500 /* cow and double check */
2501 ret = btrfs_cow_block(trans, root, right, upper,
2502 slot + 1, &right);
2503 if (ret)
2504 goto out_unlock;
2505
2506 free_space = btrfs_leaf_free_space(root, right);
2507 if (free_space < data_size)
2508 goto out_unlock;
2509
2510 left_nritems = btrfs_header_nritems(left);
2511 if (left_nritems == 0)
2512 goto out_unlock;
2513
2514 return __push_leaf_right(trans, root, path, data_size, empty,
2515 right, free_space, left_nritems);
2516out_unlock:
2517 btrfs_tree_unlock(right);
2518 free_extent_buffer(right);
2519 return 1;
2520}
2521
2522/*
Chris Mason74123bd2007-02-02 11:05:29 -05002523 * push some data in the path leaf to the left, trying to free up at
2524 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2525 */
Chris Mason44871b12009-03-13 10:04:31 -04002526static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2527 struct btrfs_root *root,
2528 struct btrfs_path *path, int data_size,
2529 int empty, struct extent_buffer *left,
2530 int free_space, int right_nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002531{
Chris Mason5f39d392007-10-15 16:14:19 -04002532 struct btrfs_disk_key disk_key;
2533 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002534 int slot;
2535 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002536 int push_space = 0;
2537 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002538 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002539 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002540 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002541 int ret = 0;
2542 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002543 u32 this_item_size;
2544 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002545
2546 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002547
Chris Mason34a38212007-11-07 13:31:03 -05002548 if (empty)
2549 nr = right_nritems;
2550 else
2551 nr = right_nritems - 1;
2552
2553 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002554 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002555 if (!right->map_token) {
2556 map_extent_buffer(right, (unsigned long)item,
2557 sizeof(struct btrfs_item),
2558 &right->map_token, &right->kaddr,
2559 &right->map_start, &right->map_len,
2560 KM_USER1);
2561 }
2562
Zheng Yan31840ae2008-09-23 13:14:14 -04002563 if (!empty && push_items > 0) {
2564 if (path->slots[0] < i)
2565 break;
2566 if (path->slots[0] == i) {
2567 int space = btrfs_leaf_free_space(root, right);
2568 if (space + push_space * 2 > free_space)
2569 break;
2570 }
2571 }
2572
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002574 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002575
2576 this_item_size = btrfs_item_size(right, item);
2577 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002578 break;
Chris Masondb945352007-10-15 16:15:53 -04002579
Chris Masonbe0e5c02007-01-26 15:51:26 -05002580 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002581 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002582 }
Chris Masondb945352007-10-15 16:15:53 -04002583
2584 if (right->map_token) {
2585 unmap_extent_buffer(right, right->map_token, KM_USER1);
2586 right->map_token = NULL;
2587 }
2588
Chris Masonbe0e5c02007-01-26 15:51:26 -05002589 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002590 ret = 1;
2591 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002592 }
Chris Mason34a38212007-11-07 13:31:03 -05002593 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002594 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002595
Chris Masonbe0e5c02007-01-26 15:51:26 -05002596 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002597 copy_extent_buffer(left, right,
2598 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2599 btrfs_item_nr_offset(0),
2600 push_items * sizeof(struct btrfs_item));
2601
Chris Mason123abc82007-03-14 14:14:43 -04002602 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002603 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002604
2605 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002606 leaf_data_end(root, left) - push_space,
2607 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002608 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002609 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002610 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002611 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002612
Chris Masondb945352007-10-15 16:15:53 -04002613 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002614 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002615 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002616
Chris Mason5f39d392007-10-15 16:14:19 -04002617 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002618 if (!left->map_token) {
2619 map_extent_buffer(left, (unsigned long)item,
2620 sizeof(struct btrfs_item),
2621 &left->map_token, &left->kaddr,
2622 &left->map_start, &left->map_len,
2623 KM_USER1);
2624 }
2625
Chris Mason5f39d392007-10-15 16:14:19 -04002626 ioff = btrfs_item_offset(left, item);
2627 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002628 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002629 }
Chris Mason5f39d392007-10-15 16:14:19 -04002630 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002631 if (left->map_token) {
2632 unmap_extent_buffer(left, left->map_token, KM_USER1);
2633 left->map_token = NULL;
2634 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002635
2636 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002637 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002638 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2639 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002640 WARN_ON(1);
2641 }
Chris Mason5f39d392007-10-15 16:14:19 -04002642
Chris Mason34a38212007-11-07 13:31:03 -05002643 if (push_items < right_nritems) {
2644 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2645 leaf_data_end(root, right);
2646 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2647 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2648 btrfs_leaf_data(right) +
2649 leaf_data_end(root, right), push_space);
2650
2651 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002652 btrfs_item_nr_offset(push_items),
2653 (btrfs_header_nritems(right) - push_items) *
2654 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002655 }
Yaneef1c492007-11-26 10:58:13 -05002656 right_nritems -= push_items;
2657 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002658 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002659 for (i = 0; i < right_nritems; i++) {
2660 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002661
2662 if (!right->map_token) {
2663 map_extent_buffer(right, (unsigned long)item,
2664 sizeof(struct btrfs_item),
2665 &right->map_token, &right->kaddr,
2666 &right->map_start, &right->map_len,
2667 KM_USER1);
2668 }
2669
2670 push_space = push_space - btrfs_item_size(right, item);
2671 btrfs_set_item_offset(right, item, push_space);
2672 }
2673 if (right->map_token) {
2674 unmap_extent_buffer(right, right->map_token, KM_USER1);
2675 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002676 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002677
Chris Mason5f39d392007-10-15 16:14:19 -04002678 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002679 if (right_nritems)
2680 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002681 else
2682 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002683
Chris Mason5f39d392007-10-15 16:14:19 -04002684 btrfs_item_key(right, &disk_key, 0);
2685 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002686 if (wret)
2687 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002688
2689 /* then fixup the leaf pointer in the path */
2690 if (path->slots[0] < push_items) {
2691 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002692 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002693 free_extent_buffer(path->nodes[0]);
2694 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002695 path->slots[1] -= 1;
2696 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002697 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002698 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699 path->slots[0] -= push_items;
2700 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002701 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002702 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002703out:
2704 btrfs_tree_unlock(left);
2705 free_extent_buffer(left);
2706 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002707}
2708
Chris Mason74123bd2007-02-02 11:05:29 -05002709/*
Chris Mason44871b12009-03-13 10:04:31 -04002710 * push some data in the path leaf to the left, trying to free up at
2711 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2712 */
2713static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2714 *root, struct btrfs_path *path, int data_size,
2715 int empty)
2716{
2717 struct extent_buffer *right = path->nodes[0];
2718 struct extent_buffer *left;
2719 int slot;
2720 int free_space;
2721 u32 right_nritems;
2722 int ret = 0;
2723
2724 slot = path->slots[1];
2725 if (slot == 0)
2726 return 1;
2727 if (!path->nodes[1])
2728 return 1;
2729
2730 right_nritems = btrfs_header_nritems(right);
2731 if (right_nritems == 0)
2732 return 1;
2733
2734 btrfs_assert_tree_locked(path->nodes[1]);
2735
2736 left = read_node_slot(root, path->nodes[1], slot - 1);
2737 btrfs_tree_lock(left);
2738 btrfs_set_lock_blocking(left);
2739
2740 free_space = btrfs_leaf_free_space(root, left);
2741 if (free_space < data_size) {
2742 ret = 1;
2743 goto out;
2744 }
2745
2746 /* cow and double check */
2747 ret = btrfs_cow_block(trans, root, left,
2748 path->nodes[1], slot - 1, &left);
2749 if (ret) {
2750 /* we hit -ENOSPC, but it isn't fatal here */
2751 ret = 1;
2752 goto out;
2753 }
2754
2755 free_space = btrfs_leaf_free_space(root, left);
2756 if (free_space < data_size) {
2757 ret = 1;
2758 goto out;
2759 }
2760
2761 return __push_leaf_left(trans, root, path, data_size,
2762 empty, left, free_space, right_nritems);
2763out:
2764 btrfs_tree_unlock(left);
2765 free_extent_buffer(left);
2766 return ret;
2767}
2768
2769/*
Chris Mason74123bd2007-02-02 11:05:29 -05002770 * split the path's leaf in two, making sure there is at least data_size
2771 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002772 *
2773 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002774 */
Chris Mason44871b12009-03-13 10:04:31 -04002775static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002776 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002777 struct btrfs_path *path,
2778 struct extent_buffer *l,
2779 struct extent_buffer *right,
2780 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002781{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002782 int data_copy_size;
2783 int rt_data_off;
2784 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002785 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002786 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002787 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002788
Chris Mason5f39d392007-10-15 16:14:19 -04002789 nritems = nritems - mid;
2790 btrfs_set_header_nritems(right, nritems);
2791 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2792
2793 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2794 btrfs_item_nr_offset(mid),
2795 nritems * sizeof(struct btrfs_item));
2796
2797 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002798 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2799 data_copy_size, btrfs_leaf_data(l) +
2800 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002801
Chris Mason5f39d392007-10-15 16:14:19 -04002802 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2803 btrfs_item_end_nr(l, mid);
2804
2805 for (i = 0; i < nritems; i++) {
2806 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002807 u32 ioff;
2808
2809 if (!right->map_token) {
2810 map_extent_buffer(right, (unsigned long)item,
2811 sizeof(struct btrfs_item),
2812 &right->map_token, &right->kaddr,
2813 &right->map_start, &right->map_len,
2814 KM_USER1);
2815 }
2816
2817 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002818 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002819 }
Chris Mason74123bd2007-02-02 11:05:29 -05002820
Chris Masondb945352007-10-15 16:15:53 -04002821 if (right->map_token) {
2822 unmap_extent_buffer(right, right->map_token, KM_USER1);
2823 right->map_token = NULL;
2824 }
2825
Chris Mason5f39d392007-10-15 16:14:19 -04002826 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002827 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002828 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002829 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2830 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002831 if (wret)
2832 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002833
2834 btrfs_mark_buffer_dirty(right);
2835 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002836 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002837
Chris Masonbe0e5c02007-01-26 15:51:26 -05002838 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002839 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002840 free_extent_buffer(path->nodes[0]);
2841 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002842 path->slots[0] -= mid;
2843 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002844 } else {
2845 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002846 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002847 }
Chris Mason5f39d392007-10-15 16:14:19 -04002848
Chris Masoneb60cea2007-02-02 09:18:22 -05002849 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002850
Chris Mason44871b12009-03-13 10:04:31 -04002851 return ret;
2852}
2853
2854/*
2855 * split the path's leaf in two, making sure there is at least data_size
2856 * available for the resulting leaf level of the path.
2857 *
2858 * returns 0 if all went well and < 0 on failure.
2859 */
2860static noinline int split_leaf(struct btrfs_trans_handle *trans,
2861 struct btrfs_root *root,
2862 struct btrfs_key *ins_key,
2863 struct btrfs_path *path, int data_size,
2864 int extend)
2865{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002866 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002867 struct extent_buffer *l;
2868 u32 nritems;
2869 int mid;
2870 int slot;
2871 struct extent_buffer *right;
2872 int ret = 0;
2873 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002874 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002875 int num_doubles = 0;
2876
Yan, Zhenga5719522009-09-24 09:17:31 -04002877 l = path->nodes[0];
2878 slot = path->slots[0];
2879 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2880 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2881 return -EOVERFLOW;
2882
Chris Mason44871b12009-03-13 10:04:31 -04002883 /* first try to make some room by pushing left and right */
Chris Masonb3612422009-05-13 19:12:15 -04002884 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason44871b12009-03-13 10:04:31 -04002885 wret = push_leaf_right(trans, root, path, data_size, 0);
2886 if (wret < 0)
2887 return wret;
2888 if (wret) {
2889 wret = push_leaf_left(trans, root, path, data_size, 0);
2890 if (wret < 0)
2891 return wret;
2892 }
2893 l = path->nodes[0];
2894
2895 /* did the pushes work? */
2896 if (btrfs_leaf_free_space(root, l) >= data_size)
2897 return 0;
2898 }
2899
2900 if (!path->nodes[1]) {
2901 ret = insert_new_root(trans, root, path, 1);
2902 if (ret)
2903 return ret;
2904 }
2905again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002906 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002907 l = path->nodes[0];
2908 slot = path->slots[0];
2909 nritems = btrfs_header_nritems(l);
2910 mid = (nritems + 1) / 2;
2911
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002912 if (mid <= slot) {
2913 if (nritems == 1 ||
2914 leaf_space_used(l, mid, nritems - mid) + data_size >
2915 BTRFS_LEAF_DATA_SIZE(root)) {
2916 if (slot >= nritems) {
2917 split = 0;
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 } else {
2928 if (leaf_space_used(l, 0, mid) + data_size >
2929 BTRFS_LEAF_DATA_SIZE(root)) {
2930 if (!extend && data_size && slot == 0) {
2931 split = 0;
2932 } else if ((extend || !data_size) && slot == 0) {
2933 mid = 1;
2934 } else {
2935 mid = slot;
2936 if (mid != nritems &&
2937 leaf_space_used(l, mid, nritems - mid) +
2938 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2939 split = 2 ;
2940 }
2941 }
2942 }
2943 }
2944
2945 if (split == 0)
2946 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2947 else
2948 btrfs_item_key(l, &disk_key, mid);
2949
2950 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002951 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002952 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002953 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002954 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002955
2956 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002957
2958 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2959 btrfs_set_header_bytenr(right, right->start);
2960 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002961 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002962 btrfs_set_header_owner(right, root->root_key.objectid);
2963 btrfs_set_header_level(right, 0);
2964 write_extent_buffer(right, root->fs_info->fsid,
2965 (unsigned long)btrfs_header_fsid(right),
2966 BTRFS_FSID_SIZE);
2967
2968 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2969 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2970 BTRFS_UUID_SIZE);
2971
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002972 if (split == 0) {
2973 if (mid <= slot) {
2974 btrfs_set_header_nritems(right, 0);
2975 wret = insert_ptr(trans, root, path,
2976 &disk_key, right->start,
2977 path->slots[1] + 1, 1);
2978 if (wret)
2979 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002980
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002981 btrfs_tree_unlock(path->nodes[0]);
2982 free_extent_buffer(path->nodes[0]);
2983 path->nodes[0] = right;
2984 path->slots[0] = 0;
2985 path->slots[1] += 1;
2986 } else {
2987 btrfs_set_header_nritems(right, 0);
2988 wret = insert_ptr(trans, root, path,
2989 &disk_key,
2990 right->start,
2991 path->slots[1], 1);
2992 if (wret)
2993 ret = wret;
2994 btrfs_tree_unlock(path->nodes[0]);
2995 free_extent_buffer(path->nodes[0]);
2996 path->nodes[0] = right;
2997 path->slots[0] = 0;
2998 if (path->slots[1] == 0) {
2999 wret = fixup_low_keys(trans, root,
3000 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003001 if (wret)
3002 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003003 }
3004 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003005 btrfs_mark_buffer_dirty(right);
3006 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003007 }
3008
3009 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3010 BUG_ON(ret);
3011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003012 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003013 BUG_ON(num_doubles != 0);
3014 num_doubles++;
3015 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003016 }
Chris Mason44871b12009-03-13 10:04:31 -04003017
Chris Masonbe0e5c02007-01-26 15:51:26 -05003018 return ret;
3019}
3020
Yan, Zhengad48fd752009-11-12 09:33:58 +00003021static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3022 struct btrfs_root *root,
3023 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003024{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003025 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003026 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003027 struct btrfs_file_extent_item *fi;
3028 u64 extent_len = 0;
3029 u32 item_size;
3030 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003031
3032 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003033 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3034
3035 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3036 key.type != BTRFS_EXTENT_CSUM_KEY);
3037
3038 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3039 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003040
3041 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003042 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3043 fi = btrfs_item_ptr(leaf, path->slots[0],
3044 struct btrfs_file_extent_item);
3045 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3046 }
Chris Mason459931e2008-12-10 09:10:46 -05003047 btrfs_release_path(root, path);
3048
Chris Mason459931e2008-12-10 09:10:46 -05003049 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003050 path->search_for_split = 1;
3051 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003052 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003053 if (ret < 0)
3054 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003055
Yan, Zhengad48fd752009-11-12 09:33:58 +00003056 ret = -EAGAIN;
3057 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003058 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003059 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3060 goto err;
3061
Chris Mason109f6ae2010-04-02 09:20:18 -04003062 /* the leaf has changed, it now has room. return now */
3063 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3064 goto err;
3065
Yan, Zhengad48fd752009-11-12 09:33:58 +00003066 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3067 fi = btrfs_item_ptr(leaf, path->slots[0],
3068 struct btrfs_file_extent_item);
3069 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3070 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003071 }
3072
Chris Masonb9473432009-03-13 11:00:37 -04003073 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003074 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003075 if (ret)
3076 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003077
Yan, Zhengad48fd752009-11-12 09:33:58 +00003078 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003079 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003080 return 0;
3081err:
3082 path->keep_locks = 0;
3083 return ret;
3084}
3085
3086static noinline int split_item(struct btrfs_trans_handle *trans,
3087 struct btrfs_root *root,
3088 struct btrfs_path *path,
3089 struct btrfs_key *new_key,
3090 unsigned long split_offset)
3091{
3092 struct extent_buffer *leaf;
3093 struct btrfs_item *item;
3094 struct btrfs_item *new_item;
3095 int slot;
3096 char *buf;
3097 u32 nritems;
3098 u32 item_size;
3099 u32 orig_offset;
3100 struct btrfs_disk_key disk_key;
3101
Chris Masonb9473432009-03-13 11:00:37 -04003102 leaf = path->nodes[0];
3103 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3104
Chris Masonb4ce94d2009-02-04 09:25:08 -05003105 btrfs_set_path_blocking(path);
3106
Chris Mason459931e2008-12-10 09:10:46 -05003107 item = btrfs_item_nr(leaf, path->slots[0]);
3108 orig_offset = btrfs_item_offset(leaf, item);
3109 item_size = btrfs_item_size(leaf, item);
3110
Chris Mason459931e2008-12-10 09:10:46 -05003111 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003112 if (!buf)
3113 return -ENOMEM;
3114
Chris Mason459931e2008-12-10 09:10:46 -05003115 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3116 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003117
Chris Mason459931e2008-12-10 09:10:46 -05003118 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003119 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003120 if (slot != nritems) {
3121 /* shift the items */
3122 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003123 btrfs_item_nr_offset(slot),
3124 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003125 }
3126
3127 btrfs_cpu_key_to_disk(&disk_key, new_key);
3128 btrfs_set_item_key(leaf, &disk_key, slot);
3129
3130 new_item = btrfs_item_nr(leaf, slot);
3131
3132 btrfs_set_item_offset(leaf, new_item, orig_offset);
3133 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3134
3135 btrfs_set_item_offset(leaf, item,
3136 orig_offset + item_size - split_offset);
3137 btrfs_set_item_size(leaf, item, split_offset);
3138
3139 btrfs_set_header_nritems(leaf, nritems + 1);
3140
3141 /* write the data for the start of the original item */
3142 write_extent_buffer(leaf, buf,
3143 btrfs_item_ptr_offset(leaf, path->slots[0]),
3144 split_offset);
3145
3146 /* write the data for the new item */
3147 write_extent_buffer(leaf, buf + split_offset,
3148 btrfs_item_ptr_offset(leaf, slot),
3149 item_size - split_offset);
3150 btrfs_mark_buffer_dirty(leaf);
3151
Yan, Zhengad48fd752009-11-12 09:33:58 +00003152 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003153 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003154 return 0;
3155}
3156
3157/*
3158 * This function splits a single item into two items,
3159 * giving 'new_key' to the new item and splitting the
3160 * old one at split_offset (from the start of the item).
3161 *
3162 * The path may be released by this operation. After
3163 * the split, the path is pointing to the old item. The
3164 * new item is going to be in the same node as the old one.
3165 *
3166 * Note, the item being split must be smaller enough to live alone on
3167 * a tree block with room for one extra struct btrfs_item
3168 *
3169 * This allows us to split the item in place, keeping a lock on the
3170 * leaf the entire time.
3171 */
3172int btrfs_split_item(struct btrfs_trans_handle *trans,
3173 struct btrfs_root *root,
3174 struct btrfs_path *path,
3175 struct btrfs_key *new_key,
3176 unsigned long split_offset)
3177{
3178 int ret;
3179 ret = setup_leaf_for_split(trans, root, path,
3180 sizeof(struct btrfs_item));
3181 if (ret)
3182 return ret;
3183
3184 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003185 return ret;
3186}
3187
3188/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003189 * This function duplicate a item, giving 'new_key' to the new item.
3190 * It guarantees both items live in the same tree leaf and the new item
3191 * is contiguous with the original item.
3192 *
3193 * This allows us to split file extent in place, keeping a lock on the
3194 * leaf the entire time.
3195 */
3196int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3197 struct btrfs_root *root,
3198 struct btrfs_path *path,
3199 struct btrfs_key *new_key)
3200{
3201 struct extent_buffer *leaf;
3202 int ret;
3203 u32 item_size;
3204
3205 leaf = path->nodes[0];
3206 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3207 ret = setup_leaf_for_split(trans, root, path,
3208 item_size + sizeof(struct btrfs_item));
3209 if (ret)
3210 return ret;
3211
3212 path->slots[0]++;
3213 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3214 item_size, item_size +
3215 sizeof(struct btrfs_item), 1);
3216 BUG_ON(ret);
3217
3218 leaf = path->nodes[0];
3219 memcpy_extent_buffer(leaf,
3220 btrfs_item_ptr_offset(leaf, path->slots[0]),
3221 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3222 item_size);
3223 return 0;
3224}
3225
3226/*
Chris Masond352ac62008-09-29 15:18:18 -04003227 * make the item pointed to by the path smaller. new_size indicates
3228 * how small to make it, and from_end tells us if we just chop bytes
3229 * off the end of the item or if we shift the item to chop bytes off
3230 * the front.
3231 */
Chris Masonb18c6682007-04-17 13:26:50 -04003232int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3233 struct btrfs_root *root,
3234 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003235 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003236{
3237 int ret = 0;
3238 int slot;
3239 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003240 struct extent_buffer *leaf;
3241 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003242 u32 nritems;
3243 unsigned int data_end;
3244 unsigned int old_data_start;
3245 unsigned int old_size;
3246 unsigned int size_diff;
3247 int i;
3248
3249 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003250 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003251 slot = path->slots[0];
3252
3253 old_size = btrfs_item_size_nr(leaf, slot);
3254 if (old_size == new_size)
3255 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003256
Chris Mason5f39d392007-10-15 16:14:19 -04003257 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003258 data_end = leaf_data_end(root, leaf);
3259
Chris Mason5f39d392007-10-15 16:14:19 -04003260 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003261
Chris Masonb18c6682007-04-17 13:26:50 -04003262 size_diff = old_size - new_size;
3263
3264 BUG_ON(slot < 0);
3265 BUG_ON(slot >= nritems);
3266
3267 /*
3268 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3269 */
3270 /* first correct the data pointers */
3271 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003272 u32 ioff;
3273 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003274
3275 if (!leaf->map_token) {
3276 map_extent_buffer(leaf, (unsigned long)item,
3277 sizeof(struct btrfs_item),
3278 &leaf->map_token, &leaf->kaddr,
3279 &leaf->map_start, &leaf->map_len,
3280 KM_USER1);
3281 }
3282
Chris Mason5f39d392007-10-15 16:14:19 -04003283 ioff = btrfs_item_offset(leaf, item);
3284 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003285 }
Chris Masondb945352007-10-15 16:15:53 -04003286
3287 if (leaf->map_token) {
3288 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3289 leaf->map_token = NULL;
3290 }
3291
Chris Masonb18c6682007-04-17 13:26:50 -04003292 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003293 if (from_end) {
3294 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3295 data_end + size_diff, btrfs_leaf_data(leaf) +
3296 data_end, old_data_start + new_size - data_end);
3297 } else {
3298 struct btrfs_disk_key disk_key;
3299 u64 offset;
3300
3301 btrfs_item_key(leaf, &disk_key, slot);
3302
3303 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3304 unsigned long ptr;
3305 struct btrfs_file_extent_item *fi;
3306
3307 fi = btrfs_item_ptr(leaf, slot,
3308 struct btrfs_file_extent_item);
3309 fi = (struct btrfs_file_extent_item *)(
3310 (unsigned long)fi - size_diff);
3311
3312 if (btrfs_file_extent_type(leaf, fi) ==
3313 BTRFS_FILE_EXTENT_INLINE) {
3314 ptr = btrfs_item_ptr_offset(leaf, slot);
3315 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003316 (unsigned long)fi,
3317 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003318 disk_bytenr));
3319 }
3320 }
3321
3322 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3323 data_end + size_diff, btrfs_leaf_data(leaf) +
3324 data_end, old_data_start - data_end);
3325
3326 offset = btrfs_disk_key_offset(&disk_key);
3327 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3328 btrfs_set_item_key(leaf, &disk_key, slot);
3329 if (slot == 0)
3330 fixup_low_keys(trans, root, path, &disk_key, 1);
3331 }
Chris Mason5f39d392007-10-15 16:14:19 -04003332
3333 item = btrfs_item_nr(leaf, slot);
3334 btrfs_set_item_size(leaf, item, new_size);
3335 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003336
3337 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003338 if (btrfs_leaf_free_space(root, leaf) < 0) {
3339 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003340 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003341 }
Chris Masonb18c6682007-04-17 13:26:50 -04003342 return ret;
3343}
3344
Chris Masond352ac62008-09-29 15:18:18 -04003345/*
3346 * make the item pointed to by the path bigger, data_size is the new size.
3347 */
Chris Mason5f39d392007-10-15 16:14:19 -04003348int btrfs_extend_item(struct btrfs_trans_handle *trans,
3349 struct btrfs_root *root, struct btrfs_path *path,
3350 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003351{
3352 int ret = 0;
3353 int slot;
3354 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003355 struct extent_buffer *leaf;
3356 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003357 u32 nritems;
3358 unsigned int data_end;
3359 unsigned int old_data;
3360 unsigned int old_size;
3361 int i;
3362
3363 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003364 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003365
Chris Mason5f39d392007-10-15 16:14:19 -04003366 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003367 data_end = leaf_data_end(root, leaf);
3368
Chris Mason5f39d392007-10-15 16:14:19 -04003369 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3370 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003371 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003372 }
Chris Mason6567e832007-04-16 09:22:45 -04003373 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003374 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003375
3376 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003377 if (slot >= nritems) {
3378 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003379 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3380 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003381 BUG_ON(1);
3382 }
Chris Mason6567e832007-04-16 09:22:45 -04003383
3384 /*
3385 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3386 */
3387 /* first correct the data pointers */
3388 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003389 u32 ioff;
3390 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003391
3392 if (!leaf->map_token) {
3393 map_extent_buffer(leaf, (unsigned long)item,
3394 sizeof(struct btrfs_item),
3395 &leaf->map_token, &leaf->kaddr,
3396 &leaf->map_start, &leaf->map_len,
3397 KM_USER1);
3398 }
Chris Mason5f39d392007-10-15 16:14:19 -04003399 ioff = btrfs_item_offset(leaf, item);
3400 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003401 }
Chris Mason5f39d392007-10-15 16:14:19 -04003402
Chris Masondb945352007-10-15 16:15:53 -04003403 if (leaf->map_token) {
3404 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3405 leaf->map_token = NULL;
3406 }
3407
Chris Mason6567e832007-04-16 09:22:45 -04003408 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003409 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003410 data_end - data_size, btrfs_leaf_data(leaf) +
3411 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003412
Chris Mason6567e832007-04-16 09:22:45 -04003413 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003414 old_size = btrfs_item_size_nr(leaf, slot);
3415 item = btrfs_item_nr(leaf, slot);
3416 btrfs_set_item_size(leaf, item, old_size + data_size);
3417 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003418
3419 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003420 if (btrfs_leaf_free_space(root, leaf) < 0) {
3421 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003422 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003423 }
Chris Mason6567e832007-04-16 09:22:45 -04003424 return ret;
3425}
3426
Chris Mason74123bd2007-02-02 11:05:29 -05003427/*
Chris Masond352ac62008-09-29 15:18:18 -04003428 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003429 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003430 * Returns the number of keys that were inserted.
3431 */
3432int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3433 struct btrfs_root *root,
3434 struct btrfs_path *path,
3435 struct btrfs_key *cpu_key, u32 *data_size,
3436 int nr)
3437{
3438 struct extent_buffer *leaf;
3439 struct btrfs_item *item;
3440 int ret = 0;
3441 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003442 int i;
3443 u32 nritems;
3444 u32 total_data = 0;
3445 u32 total_size = 0;
3446 unsigned int data_end;
3447 struct btrfs_disk_key disk_key;
3448 struct btrfs_key found_key;
3449
Yan Zheng87b29b22008-12-17 10:21:48 -05003450 for (i = 0; i < nr; i++) {
3451 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3452 BTRFS_LEAF_DATA_SIZE(root)) {
3453 break;
3454 nr = i;
3455 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003456 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003457 total_size += data_size[i] + sizeof(struct btrfs_item);
3458 }
3459 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003460
Josef Bacikf3465ca2008-11-12 14:19:50 -05003461 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3462 if (ret == 0)
3463 return -EEXIST;
3464 if (ret < 0)
3465 goto out;
3466
Josef Bacikf3465ca2008-11-12 14:19:50 -05003467 leaf = path->nodes[0];
3468
3469 nritems = btrfs_header_nritems(leaf);
3470 data_end = leaf_data_end(root, leaf);
3471
3472 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3473 for (i = nr; i >= 0; i--) {
3474 total_data -= data_size[i];
3475 total_size -= data_size[i] + sizeof(struct btrfs_item);
3476 if (total_size < btrfs_leaf_free_space(root, leaf))
3477 break;
3478 }
3479 nr = i;
3480 }
3481
3482 slot = path->slots[0];
3483 BUG_ON(slot < 0);
3484
3485 if (slot != nritems) {
3486 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3487
3488 item = btrfs_item_nr(leaf, slot);
3489 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3490
3491 /* figure out how many keys we can insert in here */
3492 total_data = data_size[0];
3493 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003494 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003495 break;
3496 total_data += data_size[i];
3497 }
3498 nr = i;
3499
3500 if (old_data < data_end) {
3501 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003502 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003503 slot, old_data, data_end);
3504 BUG_ON(1);
3505 }
3506 /*
3507 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3508 */
3509 /* first correct the data pointers */
3510 WARN_ON(leaf->map_token);
3511 for (i = slot; i < nritems; i++) {
3512 u32 ioff;
3513
3514 item = btrfs_item_nr(leaf, i);
3515 if (!leaf->map_token) {
3516 map_extent_buffer(leaf, (unsigned long)item,
3517 sizeof(struct btrfs_item),
3518 &leaf->map_token, &leaf->kaddr,
3519 &leaf->map_start, &leaf->map_len,
3520 KM_USER1);
3521 }
3522
3523 ioff = btrfs_item_offset(leaf, item);
3524 btrfs_set_item_offset(leaf, item, ioff - total_data);
3525 }
3526 if (leaf->map_token) {
3527 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3528 leaf->map_token = NULL;
3529 }
3530
3531 /* shift the items */
3532 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3533 btrfs_item_nr_offset(slot),
3534 (nritems - slot) * sizeof(struct btrfs_item));
3535
3536 /* shift the data */
3537 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3538 data_end - total_data, btrfs_leaf_data(leaf) +
3539 data_end, old_data - data_end);
3540 data_end = old_data;
3541 } else {
3542 /*
3543 * this sucks but it has to be done, if we are inserting at
3544 * the end of the leaf only insert 1 of the items, since we
3545 * have no way of knowing whats on the next leaf and we'd have
3546 * to drop our current locks to figure it out
3547 */
3548 nr = 1;
3549 }
3550
3551 /* setup the item for the new data */
3552 for (i = 0; i < nr; i++) {
3553 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3554 btrfs_set_item_key(leaf, &disk_key, slot + i);
3555 item = btrfs_item_nr(leaf, slot + i);
3556 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3557 data_end -= data_size[i];
3558 btrfs_set_item_size(leaf, item, data_size[i]);
3559 }
3560 btrfs_set_header_nritems(leaf, nritems + nr);
3561 btrfs_mark_buffer_dirty(leaf);
3562
3563 ret = 0;
3564 if (slot == 0) {
3565 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3566 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3567 }
3568
3569 if (btrfs_leaf_free_space(root, leaf) < 0) {
3570 btrfs_print_leaf(root, leaf);
3571 BUG();
3572 }
3573out:
3574 if (!ret)
3575 ret = nr;
3576 return ret;
3577}
3578
3579/*
Chris Mason44871b12009-03-13 10:04:31 -04003580 * this is a helper for btrfs_insert_empty_items, the main goal here is
3581 * to save stack depth by doing the bulk of the work in a function
3582 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003583 */
Chris Mason44871b12009-03-13 10:04:31 -04003584static noinline_for_stack int
3585setup_items_for_insert(struct btrfs_trans_handle *trans,
3586 struct btrfs_root *root, struct btrfs_path *path,
3587 struct btrfs_key *cpu_key, u32 *data_size,
3588 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003589{
Chris Mason5f39d392007-10-15 16:14:19 -04003590 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003591 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003592 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003594 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003595 int ret;
3596 struct extent_buffer *leaf;
3597 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003598
Chris Mason5f39d392007-10-15 16:14:19 -04003599 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003600 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003601
Chris Mason5f39d392007-10-15 16:14:19 -04003602 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003603 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003604
Chris Masonf25956c2008-09-12 15:32:53 -04003605 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003606 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003607 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003608 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003609 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003610 }
Chris Mason5f39d392007-10-15 16:14:19 -04003611
Chris Masonbe0e5c02007-01-26 15:51:26 -05003612 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003613 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003614
Chris Mason5f39d392007-10-15 16:14:19 -04003615 if (old_data < data_end) {
3616 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003617 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003618 slot, old_data, data_end);
3619 BUG_ON(1);
3620 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003621 /*
3622 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3623 */
3624 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003625 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003626 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003627 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003628
Chris Mason5f39d392007-10-15 16:14:19 -04003629 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003630 if (!leaf->map_token) {
3631 map_extent_buffer(leaf, (unsigned long)item,
3632 sizeof(struct btrfs_item),
3633 &leaf->map_token, &leaf->kaddr,
3634 &leaf->map_start, &leaf->map_len,
3635 KM_USER1);
3636 }
3637
Chris Mason5f39d392007-10-15 16:14:19 -04003638 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003639 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003640 }
Chris Masondb945352007-10-15 16:15:53 -04003641 if (leaf->map_token) {
3642 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3643 leaf->map_token = NULL;
3644 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003645
3646 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003647 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003648 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003649 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003650
3651 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003652 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003653 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003654 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003655 data_end = old_data;
3656 }
Chris Mason5f39d392007-10-15 16:14:19 -04003657
Chris Mason62e27492007-03-15 12:56:47 -04003658 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003659 for (i = 0; i < nr; i++) {
3660 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3661 btrfs_set_item_key(leaf, &disk_key, slot + i);
3662 item = btrfs_item_nr(leaf, slot + i);
3663 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3664 data_end -= data_size[i];
3665 btrfs_set_item_size(leaf, item, data_size[i]);
3666 }
Chris Mason44871b12009-03-13 10:04:31 -04003667
Chris Mason9c583092008-01-29 15:15:18 -05003668 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003669
3670 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003671 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003672 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003673 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003674 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003675 }
Chris Masonb9473432009-03-13 11:00:37 -04003676 btrfs_unlock_up_safe(path, 1);
3677 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003678
Chris Mason5f39d392007-10-15 16:14:19 -04003679 if (btrfs_leaf_free_space(root, leaf) < 0) {
3680 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003681 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003682 }
Chris Mason44871b12009-03-13 10:04:31 -04003683 return ret;
3684}
3685
3686/*
3687 * Given a key and some data, insert items into the tree.
3688 * This does all the path init required, making room in the tree if needed.
3689 */
3690int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3691 struct btrfs_root *root,
3692 struct btrfs_path *path,
3693 struct btrfs_key *cpu_key, u32 *data_size,
3694 int nr)
3695{
3696 struct extent_buffer *leaf;
3697 int ret = 0;
3698 int slot;
3699 int i;
3700 u32 total_size = 0;
3701 u32 total_data = 0;
3702
3703 for (i = 0; i < nr; i++)
3704 total_data += data_size[i];
3705
3706 total_size = total_data + (nr * sizeof(struct btrfs_item));
3707 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3708 if (ret == 0)
3709 return -EEXIST;
3710 if (ret < 0)
3711 goto out;
3712
3713 leaf = path->nodes[0];
3714 slot = path->slots[0];
3715 BUG_ON(slot < 0);
3716
3717 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3718 total_data, total_size, nr);
3719
Chris Masoned2ff2c2007-03-01 18:59:40 -05003720out:
Chris Mason62e27492007-03-15 12:56:47 -04003721 return ret;
3722}
3723
3724/*
3725 * Given a key and some data, insert an item into the tree.
3726 * This does all the path init required, making room in the tree if needed.
3727 */
Chris Masone089f052007-03-16 16:20:31 -04003728int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3729 *root, struct btrfs_key *cpu_key, void *data, u32
3730 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003731{
3732 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003733 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003734 struct extent_buffer *leaf;
3735 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003736
Chris Mason2c90e5d2007-04-02 10:50:19 -04003737 path = btrfs_alloc_path();
3738 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003739 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003740 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003741 leaf = path->nodes[0];
3742 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3743 write_extent_buffer(leaf, data, ptr, data_size);
3744 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003745 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003746 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003747 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003748}
3749
Chris Mason74123bd2007-02-02 11:05:29 -05003750/*
Chris Mason5de08d72007-02-24 06:24:44 -05003751 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003752 *
Chris Masond352ac62008-09-29 15:18:18 -04003753 * the tree should have been previously balanced so the deletion does not
3754 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003755 */
Chris Masone089f052007-03-16 16:20:31 -04003756static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3757 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003758{
Chris Mason5f39d392007-10-15 16:14:19 -04003759 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003760 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003761 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003762 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003763
Chris Mason5f39d392007-10-15 16:14:19 -04003764 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003765 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003766 memmove_extent_buffer(parent,
3767 btrfs_node_key_ptr_offset(slot),
3768 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003769 sizeof(struct btrfs_key_ptr) *
3770 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003771 }
Chris Mason7518a232007-03-12 12:01:18 -04003772 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003773 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003774 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003775 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003776 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003777 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003778 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003779 struct btrfs_disk_key disk_key;
3780
3781 btrfs_node_key(parent, &disk_key, 0);
3782 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003783 if (wret)
3784 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003785 }
Chris Masond6025572007-03-30 14:27:56 -04003786 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003787 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003788}
3789
Chris Mason74123bd2007-02-02 11:05:29 -05003790/*
Chris Mason323ac952008-10-01 19:05:46 -04003791 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003792 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003793 *
3794 * This deletes the pointer in path->nodes[1] and frees the leaf
3795 * block extent. zero is returned if it all worked out, < 0 otherwise.
3796 *
3797 * The path must have already been setup for deleting the leaf, including
3798 * all the proper balancing. path->nodes[1] must be locked.
3799 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003800static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3801 struct btrfs_root *root,
3802 struct btrfs_path *path,
3803 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003804{
3805 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003806
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003807 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003808 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3809 if (ret)
3810 return ret;
3811
Chris Mason4d081c42009-02-04 09:31:28 -05003812 /*
3813 * btrfs_free_extent is expensive, we want to make sure we
3814 * aren't holding any locks when we call it
3815 */
3816 btrfs_unlock_up_safe(path, 0);
3817
Yan, Zhengf0486c62010-05-16 10:46:25 -04003818 root_sub_used(root, leaf->len);
3819
3820 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3821 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003822}
3823/*
Chris Mason74123bd2007-02-02 11:05:29 -05003824 * delete the item at the leaf level in path. If that empties
3825 * the leaf, remove it from the tree
3826 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003827int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3828 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003829{
Chris Mason5f39d392007-10-15 16:14:19 -04003830 struct extent_buffer *leaf;
3831 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003832 int last_off;
3833 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003834 int ret = 0;
3835 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003836 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003837 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003838
Chris Mason5f39d392007-10-15 16:14:19 -04003839 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003840 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3841
3842 for (i = 0; i < nr; i++)
3843 dsize += btrfs_item_size_nr(leaf, slot + i);
3844
Chris Mason5f39d392007-10-15 16:14:19 -04003845 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003846
Chris Mason85e21ba2008-01-29 15:11:36 -05003847 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003848 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003849
3850 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003851 data_end + dsize,
3852 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003853 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003854
Chris Mason85e21ba2008-01-29 15:11:36 -05003855 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003856 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003857
Chris Mason5f39d392007-10-15 16:14:19 -04003858 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003859 if (!leaf->map_token) {
3860 map_extent_buffer(leaf, (unsigned long)item,
3861 sizeof(struct btrfs_item),
3862 &leaf->map_token, &leaf->kaddr,
3863 &leaf->map_start, &leaf->map_len,
3864 KM_USER1);
3865 }
Chris Mason5f39d392007-10-15 16:14:19 -04003866 ioff = btrfs_item_offset(leaf, item);
3867 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003868 }
Chris Masondb945352007-10-15 16:15:53 -04003869
3870 if (leaf->map_token) {
3871 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3872 leaf->map_token = NULL;
3873 }
3874
Chris Mason5f39d392007-10-15 16:14:19 -04003875 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003876 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003877 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003878 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003879 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003880 btrfs_set_header_nritems(leaf, nritems - nr);
3881 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003882
Chris Mason74123bd2007-02-02 11:05:29 -05003883 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003884 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003885 if (leaf == root->node) {
3886 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003887 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003888 btrfs_set_path_blocking(path);
3889 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003890 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003891 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003892 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003893 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003894 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003895 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003896 struct btrfs_disk_key disk_key;
3897
3898 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003899 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003900 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003901 if (wret)
3902 ret = wret;
3903 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003904
Chris Mason74123bd2007-02-02 11:05:29 -05003905 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003906 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003907 /* push_leaf_left fixes the path.
3908 * make sure the path still points to our leaf
3909 * for possible call to del_ptr below
3910 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003911 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003912 extent_buffer_get(leaf);
3913
Chris Masonb9473432009-03-13 11:00:37 -04003914 btrfs_set_path_blocking(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003915 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003916 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003917 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003918
3919 if (path->nodes[0] == leaf &&
3920 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003921 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003922 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003923 ret = wret;
3924 }
Chris Mason5f39d392007-10-15 16:14:19 -04003925
3926 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003927 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003928 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003929 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003930 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003931 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003932 /* if we're still in the path, make sure
3933 * we're dirty. Otherwise, one of the
3934 * push_leaf functions must have already
3935 * dirtied this buffer
3936 */
3937 if (path->nodes[0] == leaf)
3938 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003939 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003940 }
Chris Masond5719762007-03-23 10:01:08 -04003941 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003942 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003943 }
3944 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003945 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003946}
3947
Chris Mason97571fd2007-02-24 13:39:08 -05003948/*
Chris Mason925baed2008-06-25 16:01:30 -04003949 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003950 * returns 0 if it found something or 1 if there are no lesser leaves.
3951 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003952 *
3953 * This may release the path, and so you may lose any locks held at the
3954 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003955 */
3956int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3957{
Chris Mason925baed2008-06-25 16:01:30 -04003958 struct btrfs_key key;
3959 struct btrfs_disk_key found_key;
3960 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003961
Chris Mason925baed2008-06-25 16:01:30 -04003962 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003963
Chris Mason925baed2008-06-25 16:01:30 -04003964 if (key.offset > 0)
3965 key.offset--;
3966 else if (key.type > 0)
3967 key.type--;
3968 else if (key.objectid > 0)
3969 key.objectid--;
3970 else
3971 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003972
Chris Mason925baed2008-06-25 16:01:30 -04003973 btrfs_release_path(root, path);
3974 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3975 if (ret < 0)
3976 return ret;
3977 btrfs_item_key(path->nodes[0], &found_key, 0);
3978 ret = comp_keys(&found_key, &key);
3979 if (ret < 0)
3980 return 0;
3981 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003982}
3983
Chris Mason3f157a22008-06-25 16:01:31 -04003984/*
3985 * A helper function to walk down the tree starting at min_key, and looking
3986 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003987 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003988 *
3989 * This does not cow, but it does stuff the starting key it finds back
3990 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3991 * key and get a writable path.
3992 *
3993 * This does lock as it descends, and path->keep_locks should be set
3994 * to 1 by the caller.
3995 *
3996 * This honors path->lowest_level to prevent descent past a given level
3997 * of the tree.
3998 *
Chris Masond352ac62008-09-29 15:18:18 -04003999 * min_trans indicates the oldest transaction that you are interested
4000 * in walking through. Any nodes or leaves older than min_trans are
4001 * skipped over (without reading them).
4002 *
Chris Mason3f157a22008-06-25 16:01:31 -04004003 * returns zero if something useful was found, < 0 on error and 1 if there
4004 * was nothing in the tree that matched the search criteria.
4005 */
4006int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004007 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004008 struct btrfs_path *path, int cache_only,
4009 u64 min_trans)
4010{
4011 struct extent_buffer *cur;
4012 struct btrfs_key found_key;
4013 int slot;
Yan96524802008-07-24 12:19:49 -04004014 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004015 u32 nritems;
4016 int level;
4017 int ret = 1;
4018
Chris Mason934d3752008-12-08 16:43:10 -05004019 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004020again:
4021 cur = btrfs_lock_root_node(root);
4022 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004023 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004024 path->nodes[level] = cur;
4025 path->locks[level] = 1;
4026
4027 if (btrfs_header_generation(cur) < min_trans) {
4028 ret = 1;
4029 goto out;
4030 }
Chris Masond3977122009-01-05 21:25:51 -05004031 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004032 nritems = btrfs_header_nritems(cur);
4033 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004034 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004035
Chris Mason323ac952008-10-01 19:05:46 -04004036 /* at the lowest level, we're done, setup the path and exit */
4037 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004038 if (slot >= nritems)
4039 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004040 ret = 0;
4041 path->slots[level] = slot;
4042 btrfs_item_key_to_cpu(cur, &found_key, slot);
4043 goto out;
4044 }
Yan96524802008-07-24 12:19:49 -04004045 if (sret && slot > 0)
4046 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004047 /*
4048 * check this node pointer against the cache_only and
4049 * min_trans parameters. If it isn't in cache or is too
4050 * old, skip to the next one.
4051 */
Chris Masond3977122009-01-05 21:25:51 -05004052 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004053 u64 blockptr;
4054 u64 gen;
4055 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004056 struct btrfs_disk_key disk_key;
4057
Chris Mason3f157a22008-06-25 16:01:31 -04004058 blockptr = btrfs_node_blockptr(cur, slot);
4059 gen = btrfs_node_ptr_generation(cur, slot);
4060 if (gen < min_trans) {
4061 slot++;
4062 continue;
4063 }
4064 if (!cache_only)
4065 break;
4066
Chris Masone02119d2008-09-05 16:13:11 -04004067 if (max_key) {
4068 btrfs_node_key(cur, &disk_key, slot);
4069 if (comp_keys(&disk_key, max_key) >= 0) {
4070 ret = 1;
4071 goto out;
4072 }
4073 }
4074
Chris Mason3f157a22008-06-25 16:01:31 -04004075 tmp = btrfs_find_tree_block(root, blockptr,
4076 btrfs_level_size(root, level - 1));
4077
4078 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4079 free_extent_buffer(tmp);
4080 break;
4081 }
4082 if (tmp)
4083 free_extent_buffer(tmp);
4084 slot++;
4085 }
Chris Masone02119d2008-09-05 16:13:11 -04004086find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004087 /*
4088 * we didn't find a candidate key in this node, walk forward
4089 * and find another one
4090 */
4091 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004092 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004093 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004094 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004095 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004096 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004097 btrfs_release_path(root, path);
4098 goto again;
4099 } else {
4100 goto out;
4101 }
4102 }
4103 /* save our key for returning back */
4104 btrfs_node_key_to_cpu(cur, &found_key, slot);
4105 path->slots[level] = slot;
4106 if (level == path->lowest_level) {
4107 ret = 0;
4108 unlock_up(path, level, 1);
4109 goto out;
4110 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004111 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004112 cur = read_node_slot(root, cur, slot);
4113
4114 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004115
Chris Mason3f157a22008-06-25 16:01:31 -04004116 path->locks[level - 1] = 1;
4117 path->nodes[level - 1] = cur;
4118 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004119 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004120 }
4121out:
4122 if (ret == 0)
4123 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004124 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004125 return ret;
4126}
4127
4128/*
4129 * this is similar to btrfs_next_leaf, but does not try to preserve
4130 * and fixup the path. It looks for and returns the next key in the
4131 * tree based on the current path and the cache_only and min_trans
4132 * parameters.
4133 *
4134 * 0 is returned if another key is found, < 0 if there are any errors
4135 * and 1 is returned if there are no higher keys in the tree
4136 *
4137 * path->keep_locks should be set to 1 on the search made before
4138 * calling this function.
4139 */
Chris Masone7a84562008-06-25 16:01:31 -04004140int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004141 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004142 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004143{
Chris Masone7a84562008-06-25 16:01:31 -04004144 int slot;
4145 struct extent_buffer *c;
4146
Chris Mason934d3752008-12-08 16:43:10 -05004147 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004148 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004149 if (!path->nodes[level])
4150 return 1;
4151
4152 slot = path->slots[level] + 1;
4153 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004154next:
Chris Masone7a84562008-06-25 16:01:31 -04004155 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004156 int ret;
4157 int orig_lowest;
4158 struct btrfs_key cur_key;
4159 if (level + 1 >= BTRFS_MAX_LEVEL ||
4160 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004161 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004162
4163 if (path->locks[level + 1]) {
4164 level++;
4165 continue;
4166 }
4167
4168 slot = btrfs_header_nritems(c) - 1;
4169 if (level == 0)
4170 btrfs_item_key_to_cpu(c, &cur_key, slot);
4171 else
4172 btrfs_node_key_to_cpu(c, &cur_key, slot);
4173
4174 orig_lowest = path->lowest_level;
4175 btrfs_release_path(root, path);
4176 path->lowest_level = level;
4177 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4178 0, 0);
4179 path->lowest_level = orig_lowest;
4180 if (ret < 0)
4181 return ret;
4182
4183 c = path->nodes[level];
4184 slot = path->slots[level];
4185 if (ret == 0)
4186 slot++;
4187 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004188 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004189
Chris Masone7a84562008-06-25 16:01:31 -04004190 if (level == 0)
4191 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004192 else {
4193 u64 blockptr = btrfs_node_blockptr(c, slot);
4194 u64 gen = btrfs_node_ptr_generation(c, slot);
4195
4196 if (cache_only) {
4197 struct extent_buffer *cur;
4198 cur = btrfs_find_tree_block(root, blockptr,
4199 btrfs_level_size(root, level - 1));
4200 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4201 slot++;
4202 if (cur)
4203 free_extent_buffer(cur);
4204 goto next;
4205 }
4206 free_extent_buffer(cur);
4207 }
4208 if (gen < min_trans) {
4209 slot++;
4210 goto next;
4211 }
Chris Masone7a84562008-06-25 16:01:31 -04004212 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004213 }
Chris Masone7a84562008-06-25 16:01:31 -04004214 return 0;
4215 }
4216 return 1;
4217}
4218
Chris Mason7bb86312007-12-11 09:25:06 -05004219/*
Chris Mason925baed2008-06-25 16:01:30 -04004220 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004221 * returns 0 if it found something or 1 if there are no greater leaves.
4222 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004223 */
Chris Mason234b63a2007-03-13 10:46:10 -04004224int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004225{
4226 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004227 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004228 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004229 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004230 struct btrfs_key key;
4231 u32 nritems;
4232 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004233 int old_spinning = path->leave_spinning;
4234 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004235
4236 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004237 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004238 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004239
Chris Mason8e73f272009-04-03 10:14:18 -04004240 /*
4241 * we take the blocks in an order that upsets lockdep. Using
4242 * blocking mode is the only way around it.
4243 */
4244#ifdef CONFIG_DEBUG_LOCK_ALLOC
4245 force_blocking = 1;
4246#endif
Chris Mason925baed2008-06-25 16:01:30 -04004247
Chris Mason8e73f272009-04-03 10:14:18 -04004248 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4249again:
4250 level = 1;
4251 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004252 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004253
Chris Masona2135012008-06-25 16:01:30 -04004254 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004255
4256 if (!force_blocking)
4257 path->leave_spinning = 1;
4258
Chris Mason925baed2008-06-25 16:01:30 -04004259 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4260 path->keep_locks = 0;
4261
4262 if (ret < 0)
4263 return ret;
4264
Chris Masona2135012008-06-25 16:01:30 -04004265 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004266 /*
4267 * by releasing the path above we dropped all our locks. A balance
4268 * could have added more items next to the key that used to be
4269 * at the very end of the block. So, check again here and
4270 * advance the path if there are now more items available.
4271 */
Chris Masona2135012008-06-25 16:01:30 -04004272 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004273 if (ret == 0)
4274 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004275 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004276 goto done;
4277 }
Chris Masond97e63b2007-02-20 16:40:44 -05004278
Chris Masond3977122009-01-05 21:25:51 -05004279 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004280 if (!path->nodes[level]) {
4281 ret = 1;
4282 goto done;
4283 }
Chris Mason5f39d392007-10-15 16:14:19 -04004284
Chris Masond97e63b2007-02-20 16:40:44 -05004285 slot = path->slots[level] + 1;
4286 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004287 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004288 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004289 if (level == BTRFS_MAX_LEVEL) {
4290 ret = 1;
4291 goto done;
4292 }
Chris Masond97e63b2007-02-20 16:40:44 -05004293 continue;
4294 }
Chris Mason5f39d392007-10-15 16:14:19 -04004295
Chris Mason925baed2008-06-25 16:01:30 -04004296 if (next) {
4297 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004298 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004299 }
Chris Mason5f39d392007-10-15 16:14:19 -04004300
Chris Mason8e73f272009-04-03 10:14:18 -04004301 next = c;
4302 ret = read_block_for_search(NULL, root, path, &next, level,
4303 slot, &key);
4304 if (ret == -EAGAIN)
4305 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004306
Chris Mason76a05b32009-05-14 13:24:30 -04004307 if (ret < 0) {
4308 btrfs_release_path(root, path);
4309 goto done;
4310 }
4311
Chris Mason5cd57b22008-06-25 16:01:30 -04004312 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004313 ret = btrfs_try_spin_lock(next);
4314 if (!ret) {
4315 btrfs_set_path_blocking(path);
4316 btrfs_tree_lock(next);
4317 if (!force_blocking)
4318 btrfs_clear_path_blocking(path, next);
4319 }
4320 if (force_blocking)
4321 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004322 }
Chris Masond97e63b2007-02-20 16:40:44 -05004323 break;
4324 }
4325 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004326 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004327 level--;
4328 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004329 if (path->locks[level])
4330 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004331
Chris Mason5f39d392007-10-15 16:14:19 -04004332 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004333 path->nodes[level] = next;
4334 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004335 if (!path->skip_locking)
4336 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004337
Chris Masond97e63b2007-02-20 16:40:44 -05004338 if (!level)
4339 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004340
Chris Mason8e73f272009-04-03 10:14:18 -04004341 ret = read_block_for_search(NULL, root, path, &next, level,
4342 0, &key);
4343 if (ret == -EAGAIN)
4344 goto again;
4345
Chris Mason76a05b32009-05-14 13:24:30 -04004346 if (ret < 0) {
4347 btrfs_release_path(root, path);
4348 goto done;
4349 }
4350
Chris Mason5cd57b22008-06-25 16:01:30 -04004351 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004352 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004353 ret = btrfs_try_spin_lock(next);
4354 if (!ret) {
4355 btrfs_set_path_blocking(path);
4356 btrfs_tree_lock(next);
4357 if (!force_blocking)
4358 btrfs_clear_path_blocking(path, next);
4359 }
4360 if (force_blocking)
4361 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004362 }
Chris Masond97e63b2007-02-20 16:40:44 -05004363 }
Chris Mason8e73f272009-04-03 10:14:18 -04004364 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004365done:
4366 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004367 path->leave_spinning = old_spinning;
4368 if (!old_spinning)
4369 btrfs_set_path_blocking(path);
4370
4371 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004372}
Chris Mason0b86a832008-03-24 15:01:56 -04004373
Chris Mason3f157a22008-06-25 16:01:31 -04004374/*
4375 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4376 * searching until it gets past min_objectid or finds an item of 'type'
4377 *
4378 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4379 */
Chris Mason0b86a832008-03-24 15:01:56 -04004380int btrfs_previous_item(struct btrfs_root *root,
4381 struct btrfs_path *path, u64 min_objectid,
4382 int type)
4383{
4384 struct btrfs_key found_key;
4385 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004386 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004387 int ret;
4388
Chris Masond3977122009-01-05 21:25:51 -05004389 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004390 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004391 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004392 ret = btrfs_prev_leaf(root, path);
4393 if (ret != 0)
4394 return ret;
4395 } else {
4396 path->slots[0]--;
4397 }
4398 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004399 nritems = btrfs_header_nritems(leaf);
4400 if (nritems == 0)
4401 return 1;
4402 if (path->slots[0] == nritems)
4403 path->slots[0]--;
4404
Chris Mason0b86a832008-03-24 15:01:56 -04004405 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004406 if (found_key.objectid < min_objectid)
4407 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004408 if (found_key.type == type)
4409 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004410 if (found_key.objectid == min_objectid &&
4411 found_key.type < type)
4412 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004413 }
4414 return 1;
4415}