blob: 8e29260cd0bbaffed28bf445319083d197b521a7 [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,
283 struct extent_buffer *cow)
284{
285 u64 refs;
286 u64 owner;
287 u64 flags;
288 u64 new_flags = 0;
289 int ret;
290
291 /*
292 * Backrefs update rules:
293 *
294 * Always use full backrefs for extent pointers in tree block
295 * allocated by tree relocation.
296 *
297 * If a shared tree block is no longer referenced by its owner
298 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
299 * use full backrefs for extent pointers in tree block.
300 *
301 * If a tree block is been relocating
302 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
303 * use full backrefs for extent pointers in tree block.
304 * The reason for this is some operations (such as drop tree)
305 * are only allowed for blocks use full backrefs.
306 */
307
308 if (btrfs_block_can_be_shared(root, buf)) {
309 ret = btrfs_lookup_extent_info(trans, root, buf->start,
310 buf->len, &refs, &flags);
311 BUG_ON(ret);
312 BUG_ON(refs == 0);
313 } else {
314 refs = 1;
315 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
316 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
317 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
318 else
319 flags = 0;
320 }
321
322 owner = btrfs_header_owner(buf);
323 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
324 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
325
326 if (refs > 1) {
327 if ((owner == root->root_key.objectid ||
328 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
329 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
330 ret = btrfs_inc_ref(trans, root, buf, 1);
331 BUG_ON(ret);
332
333 if (root->root_key.objectid ==
334 BTRFS_TREE_RELOC_OBJECTID) {
335 ret = btrfs_dec_ref(trans, root, buf, 0);
336 BUG_ON(ret);
337 ret = btrfs_inc_ref(trans, root, cow, 1);
338 BUG_ON(ret);
339 }
340 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
341 } else {
342
343 if (root->root_key.objectid ==
344 BTRFS_TREE_RELOC_OBJECTID)
345 ret = btrfs_inc_ref(trans, root, cow, 1);
346 else
347 ret = btrfs_inc_ref(trans, root, cow, 0);
348 BUG_ON(ret);
349 }
350 if (new_flags != 0) {
351 ret = btrfs_set_disk_extent_flags(trans, root,
352 buf->start,
353 buf->len,
354 new_flags, 0);
355 BUG_ON(ret);
356 }
357 } else {
358 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
359 if (root->root_key.objectid ==
360 BTRFS_TREE_RELOC_OBJECTID)
361 ret = btrfs_inc_ref(trans, root, cow, 1);
362 else
363 ret = btrfs_inc_ref(trans, root, cow, 0);
364 BUG_ON(ret);
365 ret = btrfs_dec_ref(trans, root, buf, 1);
366 BUG_ON(ret);
367 }
368 clean_tree_block(trans, root, buf);
369 }
370 return 0;
371}
372
373/*
Chris Masond3977122009-01-05 21:25:51 -0500374 * does the dirty work in cow of a single block. The parent block (if
375 * supplied) is updated to point to the new cow copy. The new buffer is marked
376 * dirty and returned locked. If you modify the block it needs to be marked
377 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400378 *
379 * search_start -- an allocation hint for the new block
380 *
Chris Masond3977122009-01-05 21:25:51 -0500381 * empty_size -- a hint that you plan on doing more cow. This is the size in
382 * bytes the allocator should try to find free next to the block it returns.
383 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400384 */
Chris Masond3977122009-01-05 21:25:51 -0500385static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400386 struct btrfs_root *root,
387 struct extent_buffer *buf,
388 struct extent_buffer *parent, int parent_slot,
389 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400390 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400391{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400392 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400393 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500394 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400395 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400396 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400397
Chris Mason925baed2008-06-25 16:01:30 -0400398 if (*cow_ret == buf)
399 unlock_orig = 1;
400
Chris Masonb9447ef2009-03-09 11:45:38 -0400401 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400402
Chris Mason7bb86312007-12-11 09:25:06 -0500403 WARN_ON(root->ref_cows && trans->transid !=
404 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400405 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400406
Chris Mason7bb86312007-12-11 09:25:06 -0500407 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400408
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400409 if (level == 0)
410 btrfs_item_key(buf, &disk_key, 0);
411 else
412 btrfs_node_key(buf, &disk_key, 0);
413
414 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
415 if (parent)
416 parent_start = parent->start;
417 else
418 parent_start = 0;
419 } else
420 parent_start = 0;
421
422 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
423 root->root_key.objectid, &disk_key,
424 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400425 if (IS_ERR(cow))
426 return PTR_ERR(cow);
427
Chris Masonb4ce94d2009-02-04 09:25:08 -0500428 /* cow is set to blocking by btrfs_init_new_buffer */
429
Chris Mason5f39d392007-10-15 16:14:19 -0400430 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400431 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400432 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400433 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
434 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
435 BTRFS_HEADER_FLAG_RELOC);
436 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
437 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
438 else
439 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400440
Yan Zheng2b820322008-11-17 21:11:30 -0500441 write_extent_buffer(cow, root->fs_info->fsid,
442 (unsigned long)btrfs_header_fsid(cow),
443 BTRFS_FSID_SIZE);
444
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400445 update_ref_for_cow(trans, root, buf, cow);
Zheng Yan1a40e232008-09-26 10:09:34 -0400446
Chris Mason6702ed42007-08-07 16:15:09 -0400447 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400448 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400449 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
450 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
451 parent_start = buf->start;
452 else
453 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400454
455 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400456 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400457 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400458 spin_unlock(&root->node_lock);
459
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000460 btrfs_free_tree_block(trans, root, buf->start, buf->len,
461 parent_start, root->root_key.objectid, level);
Chris Mason5f39d392007-10-15 16:14:19 -0400462 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400463 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400464 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400465 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
466 parent_start = parent->start;
467 else
468 parent_start = 0;
469
470 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400471 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400472 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500473 btrfs_set_node_ptr_generation(parent, parent_slot,
474 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400475 btrfs_mark_buffer_dirty(parent);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000476 btrfs_free_tree_block(trans, root, buf->start, buf->len,
477 parent_start, root->root_key.objectid, level);
Chris Mason6702ed42007-08-07 16:15:09 -0400478 }
Chris Mason925baed2008-06-25 16:01:30 -0400479 if (unlock_orig)
480 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400481 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400482 btrfs_mark_buffer_dirty(cow);
483 *cow_ret = cow;
484 return 0;
485}
486
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400487static inline int should_cow_block(struct btrfs_trans_handle *trans,
488 struct btrfs_root *root,
489 struct extent_buffer *buf)
490{
491 if (btrfs_header_generation(buf) == trans->transid &&
492 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
493 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
494 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
495 return 0;
496 return 1;
497}
498
Chris Masond352ac62008-09-29 15:18:18 -0400499/*
500 * cows a single block, see __btrfs_cow_block for the real work.
501 * This version of it has extra checks so that a block isn't cow'd more than
502 * once per transaction, as long as it hasn't been written yet
503 */
Chris Masond3977122009-01-05 21:25:51 -0500504noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400505 struct btrfs_root *root, struct extent_buffer *buf,
506 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400507 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500508{
Chris Mason6702ed42007-08-07 16:15:09 -0400509 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400510 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500511
Chris Masonccd467d2007-06-28 15:57:36 -0400512 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500513 printk(KERN_CRIT "trans %llu running %llu\n",
514 (unsigned long long)trans->transid,
515 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400516 root->fs_info->running_transaction->transid);
517 WARN_ON(1);
518 }
519 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500520 printk(KERN_CRIT "trans %llu running %llu\n",
521 (unsigned long long)trans->transid,
522 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400523 WARN_ON(1);
524 }
Chris Masondc17ff82008-01-08 15:46:30 -0500525
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400526 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500527 *cow_ret = buf;
528 return 0;
529 }
Chris Masonc4876852009-02-04 09:24:25 -0500530
Chris Mason0b86a832008-03-24 15:01:56 -0400531 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500532
533 if (parent)
534 btrfs_set_lock_blocking(parent);
535 btrfs_set_lock_blocking(buf);
536
Chris Masonf510cfe2007-10-15 16:14:48 -0400537 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400538 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400539 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400540}
541
Chris Masond352ac62008-09-29 15:18:18 -0400542/*
543 * helper function for defrag to decide if two blocks pointed to by a
544 * node are actually close by
545 */
Chris Mason6b800532007-10-15 16:17:34 -0400546static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400547{
Chris Mason6b800532007-10-15 16:17:34 -0400548 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400549 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400550 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400551 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500552 return 0;
553}
554
Chris Mason081e9572007-11-06 10:26:24 -0500555/*
556 * compare two keys in a memcmp fashion
557 */
558static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
559{
560 struct btrfs_key k1;
561
562 btrfs_disk_key_to_cpu(&k1, disk);
563
Diego Calleja20736ab2009-07-24 11:06:52 -0400564 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500565}
566
Josef Bacikf3465ca2008-11-12 14:19:50 -0500567/*
568 * same as comp_keys only with two btrfs_key's
569 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400570int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500571{
572 if (k1->objectid > k2->objectid)
573 return 1;
574 if (k1->objectid < k2->objectid)
575 return -1;
576 if (k1->type > k2->type)
577 return 1;
578 if (k1->type < k2->type)
579 return -1;
580 if (k1->offset > k2->offset)
581 return 1;
582 if (k1->offset < k2->offset)
583 return -1;
584 return 0;
585}
Chris Mason081e9572007-11-06 10:26:24 -0500586
Chris Masond352ac62008-09-29 15:18:18 -0400587/*
588 * this is used by the defrag code to go through all the
589 * leaves pointed to by a node and reallocate them so that
590 * disk order is close to key order
591 */
Chris Mason6702ed42007-08-07 16:15:09 -0400592int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400593 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400594 int start_slot, int cache_only, u64 *last_ret,
595 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400596{
Chris Mason6b800532007-10-15 16:17:34 -0400597 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400598 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400599 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400600 u64 search_start = *last_ret;
601 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400602 u64 other;
603 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400604 int end_slot;
605 int i;
606 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400607 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400608 int uptodate;
609 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500610 int progress_passed = 0;
611 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400612
Chris Mason5708b952007-10-25 15:43:18 -0400613 parent_level = btrfs_header_level(parent);
614 if (cache_only && parent_level != 1)
615 return 0;
616
Chris Masond3977122009-01-05 21:25:51 -0500617 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400618 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500619 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400620 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400621
Chris Mason6b800532007-10-15 16:17:34 -0400622 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400623 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400624 end_slot = parent_nritems;
625
626 if (parent_nritems == 1)
627 return 0;
628
Chris Masonb4ce94d2009-02-04 09:25:08 -0500629 btrfs_set_lock_blocking(parent);
630
Chris Mason6702ed42007-08-07 16:15:09 -0400631 for (i = start_slot; i < end_slot; i++) {
632 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400633
Chris Mason5708b952007-10-25 15:43:18 -0400634 if (!parent->map_token) {
635 map_extent_buffer(parent,
636 btrfs_node_key_ptr_offset(i),
637 sizeof(struct btrfs_key_ptr),
638 &parent->map_token, &parent->kaddr,
639 &parent->map_start, &parent->map_len,
640 KM_USER1);
641 }
Chris Mason081e9572007-11-06 10:26:24 -0500642 btrfs_node_key(parent, &disk_key, i);
643 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
644 continue;
645
646 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400647 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400648 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400649 if (last_block == 0)
650 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400651
Chris Mason6702ed42007-08-07 16:15:09 -0400652 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400653 other = btrfs_node_blockptr(parent, i - 1);
654 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400655 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400656 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400657 other = btrfs_node_blockptr(parent, i + 1);
658 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400659 }
Chris Masone9d0b132007-08-10 14:06:19 -0400660 if (close) {
661 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400662 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400663 }
Chris Mason5708b952007-10-25 15:43:18 -0400664 if (parent->map_token) {
665 unmap_extent_buffer(parent, parent->map_token,
666 KM_USER1);
667 parent->map_token = NULL;
668 }
Chris Mason6702ed42007-08-07 16:15:09 -0400669
Chris Mason6b800532007-10-15 16:17:34 -0400670 cur = btrfs_find_tree_block(root, blocknr, blocksize);
671 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400672 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400673 else
674 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400675 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400676 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400677 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400678 continue;
679 }
Chris Mason6b800532007-10-15 16:17:34 -0400680 if (!cur) {
681 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400682 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400683 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400684 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400685 }
Chris Mason6702ed42007-08-07 16:15:09 -0400686 }
Chris Masone9d0b132007-08-10 14:06:19 -0400687 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400688 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400689
Chris Masone7a84562008-06-25 16:01:31 -0400690 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500691 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400692 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400693 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400694 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400695 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400696 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400697 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400698 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400699 break;
Yan252c38f2007-08-29 09:11:44 -0400700 }
Chris Masone7a84562008-06-25 16:01:31 -0400701 search_start = cur->start;
702 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400703 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400704 btrfs_tree_unlock(cur);
705 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400706 }
Chris Mason5708b952007-10-25 15:43:18 -0400707 if (parent->map_token) {
708 unmap_extent_buffer(parent, parent->map_token,
709 KM_USER1);
710 parent->map_token = NULL;
711 }
Chris Mason6702ed42007-08-07 16:15:09 -0400712 return err;
713}
714
Chris Mason74123bd2007-02-02 11:05:29 -0500715/*
716 * The leaf data grows from end-to-front in the node.
717 * this returns the address of the start of the last item,
718 * which is the stop of the leaf data stack
719 */
Chris Mason123abc82007-03-14 14:14:43 -0400720static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400721 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500722{
Chris Mason5f39d392007-10-15 16:14:19 -0400723 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500724 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400725 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400726 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500727}
728
Chris Masond352ac62008-09-29 15:18:18 -0400729/*
730 * extra debugging checks to make sure all the items in a key are
731 * well formed and in the proper order
732 */
Chris Mason123abc82007-03-14 14:14:43 -0400733static int check_node(struct btrfs_root *root, struct btrfs_path *path,
734 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500735{
Chris Mason5f39d392007-10-15 16:14:19 -0400736 struct extent_buffer *parent = NULL;
737 struct extent_buffer *node = path->nodes[level];
738 struct btrfs_disk_key parent_key;
739 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500740 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400741 int slot;
742 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400743 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500744
745 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400746 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400747
Chris Mason8d7be552007-05-10 11:24:42 -0400748 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400749 BUG_ON(nritems == 0);
750 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400751 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400752 btrfs_node_key(parent, &parent_key, parent_slot);
753 btrfs_node_key(node, &node_key, 0);
754 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400755 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400756 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400757 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500758 }
Chris Mason123abc82007-03-14 14:14:43 -0400759 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400760 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400761 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
762 btrfs_node_key(node, &node_key, slot);
763 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400764 }
765 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400766 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
767 btrfs_node_key(node, &node_key, slot);
768 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500769 }
770 return 0;
771}
772
Chris Masond352ac62008-09-29 15:18:18 -0400773/*
774 * extra checking to make sure all the items in a leaf are
775 * well formed and in the proper order
776 */
Chris Mason123abc82007-03-14 14:14:43 -0400777static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
778 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500779{
Chris Mason5f39d392007-10-15 16:14:19 -0400780 struct extent_buffer *leaf = path->nodes[level];
781 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500782 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400783 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400784 struct btrfs_disk_key parent_key;
785 struct btrfs_disk_key leaf_key;
786 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400787
Chris Mason5f39d392007-10-15 16:14:19 -0400788 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500789
790 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400791 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400792
793 if (nritems == 0)
794 return 0;
795
796 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400797 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400798 btrfs_node_key(parent, &parent_key, parent_slot);
799 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400800
Chris Mason5f39d392007-10-15 16:14:19 -0400801 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400802 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400803 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400804 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500805 }
Chris Mason5f39d392007-10-15 16:14:19 -0400806 if (slot != 0 && slot < nritems - 1) {
807 btrfs_item_key(leaf, &leaf_key, slot);
808 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
809 if (comp_keys(&leaf_key, &cpukey) <= 0) {
810 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500811 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400812 BUG_ON(1);
813 }
814 if (btrfs_item_offset_nr(leaf, slot - 1) !=
815 btrfs_item_end_nr(leaf, slot)) {
816 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500817 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400818 BUG_ON(1);
819 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500820 }
Chris Mason8d7be552007-05-10 11:24:42 -0400821 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400822 btrfs_item_key(leaf, &leaf_key, slot);
823 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
824 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
825 if (btrfs_item_offset_nr(leaf, slot) !=
826 btrfs_item_end_nr(leaf, slot + 1)) {
827 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500828 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400829 BUG_ON(1);
830 }
Chris Mason8d7be552007-05-10 11:24:42 -0400831 }
Chris Mason5f39d392007-10-15 16:14:19 -0400832 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
833 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500834 return 0;
835}
836
Chris Masond3977122009-01-05 21:25:51 -0500837static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500838 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500839{
Chris Mason85d824c2008-04-10 10:23:19 -0400840 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500841 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400842 return check_leaf(root, path, level);
843 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500844}
845
Chris Mason74123bd2007-02-02 11:05:29 -0500846/*
Chris Mason5f39d392007-10-15 16:14:19 -0400847 * search for key in the extent_buffer. The items start at offset p,
848 * and they are item_size apart. There are 'max' items in p.
849 *
Chris Mason74123bd2007-02-02 11:05:29 -0500850 * the slot in the array is returned via slot, and it points to
851 * the place where you would insert key if it is not found in
852 * the array.
853 *
854 * slot may point to max if the key is bigger than all of the keys
855 */
Chris Masone02119d2008-09-05 16:13:11 -0400856static noinline int generic_bin_search(struct extent_buffer *eb,
857 unsigned long p,
858 int item_size, struct btrfs_key *key,
859 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500860{
861 int low = 0;
862 int high = max;
863 int mid;
864 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400865 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400866 struct btrfs_disk_key unaligned;
867 unsigned long offset;
868 char *map_token = NULL;
869 char *kaddr = NULL;
870 unsigned long map_start = 0;
871 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400872 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500873
Chris Masond3977122009-01-05 21:25:51 -0500874 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500875 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400876 offset = p + mid * item_size;
877
878 if (!map_token || offset < map_start ||
879 (offset + sizeof(struct btrfs_disk_key)) >
880 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400881 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400882 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400883 map_token = NULL;
884 }
Chris Mason934d3752008-12-08 16:43:10 -0500885
886 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400887 sizeof(struct btrfs_disk_key),
888 &map_token, &kaddr,
889 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400890
Chris Mason479965d2007-10-15 16:14:27 -0400891 if (!err) {
892 tmp = (struct btrfs_disk_key *)(kaddr + offset -
893 map_start);
894 } else {
895 read_extent_buffer(eb, &unaligned,
896 offset, sizeof(unaligned));
897 tmp = &unaligned;
898 }
899
Chris Mason5f39d392007-10-15 16:14:19 -0400900 } else {
901 tmp = (struct btrfs_disk_key *)(kaddr + offset -
902 map_start);
903 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500904 ret = comp_keys(tmp, key);
905
906 if (ret < 0)
907 low = mid + 1;
908 else if (ret > 0)
909 high = mid;
910 else {
911 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400912 if (map_token)
913 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500914 return 0;
915 }
916 }
917 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400918 if (map_token)
919 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500920 return 1;
921}
922
Chris Mason97571fd2007-02-24 13:39:08 -0500923/*
924 * simple bin_search frontend that does the right thing for
925 * leaves vs nodes
926 */
Chris Mason5f39d392007-10-15 16:14:19 -0400927static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
928 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500929{
Chris Mason5f39d392007-10-15 16:14:19 -0400930 if (level == 0) {
931 return generic_bin_search(eb,
932 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400933 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400934 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400935 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500936 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400937 return generic_bin_search(eb,
938 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400939 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400940 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400941 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500942 }
943 return -1;
944}
945
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
947 int level, int *slot)
948{
949 return bin_search(eb, key, level, slot);
950}
951
Chris Masond352ac62008-09-29 15:18:18 -0400952/* given a node and slot number, this reads the blocks it points to. The
953 * extent buffer is returned with a reference taken (but unlocked).
954 * NULL is returned on error.
955 */
Chris Masone02119d2008-09-05 16:13:11 -0400956static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400957 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500958{
Chris Masonca7a79a2008-05-12 12:59:19 -0400959 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500960 if (slot < 0)
961 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400962 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500963 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400964
965 BUG_ON(level == 0);
966
Chris Masondb945352007-10-15 16:15:53 -0400967 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400968 btrfs_level_size(root, level - 1),
969 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500970}
971
Chris Masond352ac62008-09-29 15:18:18 -0400972/*
973 * node level balancing, used to make sure nodes are in proper order for
974 * item deletion. We balance from the top down, so we have to make sure
975 * that a deletion won't leave an node completely empty later on.
976 */
Chris Masone02119d2008-09-05 16:13:11 -0400977static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500978 struct btrfs_root *root,
979 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500980{
Chris Mason5f39d392007-10-15 16:14:19 -0400981 struct extent_buffer *right = NULL;
982 struct extent_buffer *mid;
983 struct extent_buffer *left = NULL;
984 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500985 int ret = 0;
986 int wret;
987 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500988 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400989 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500990 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500991
992 if (level == 0)
993 return 0;
994
Chris Mason5f39d392007-10-15 16:14:19 -0400995 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500996
Chris Mason925baed2008-06-25 16:01:30 -0400997 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500998 WARN_ON(btrfs_header_generation(mid) != trans->transid);
999
Chris Mason1d4f8a02007-03-13 09:28:32 -04001000 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001001
Chris Mason234b63a2007-03-13 10:46:10 -04001002 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001003 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001004 pslot = path->slots[level + 1];
1005
Chris Mason40689472007-03-17 14:29:23 -04001006 /*
1007 * deal with the case where there is only one pointer in the root
1008 * by promoting the node below to a root
1009 */
Chris Mason5f39d392007-10-15 16:14:19 -04001010 if (!parent) {
1011 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001012
Chris Mason5f39d392007-10-15 16:14:19 -04001013 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001014 return 0;
1015
1016 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001017 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001018 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001019 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001020 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001021 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan2f375ab2008-02-01 14:58:07 -05001022 BUG_ON(ret);
1023
Chris Mason925baed2008-06-25 16:01:30 -04001024 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001025 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001026 spin_unlock(&root->node_lock);
1027
Chris Mason0b86a832008-03-24 15:01:56 -04001028 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001029 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001030
Chris Mason925baed2008-06-25 16:01:30 -04001031 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001032 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001033 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001034 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001035 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001036 free_extent_buffer(mid);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00001037 ret = btrfs_free_tree_block(trans, root, mid->start, mid->len,
1038 0, root->root_key.objectid, level);
Chris Masonbb803952007-03-01 12:04:21 -05001039 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001040 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -04001041 return ret;
Chris Masonbb803952007-03-01 12:04:21 -05001042 }
Chris Mason5f39d392007-10-15 16:14:19 -04001043 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001044 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001045 return 0;
1046
Chris Mason5f39d392007-10-15 16:14:19 -04001047 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001048 err_on_enospc = 1;
1049
Chris Mason5f39d392007-10-15 16:14:19 -04001050 left = read_node_slot(root, parent, pslot - 1);
1051 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001052 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001053 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001054 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001055 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001056 if (wret) {
1057 ret = wret;
1058 goto enospc;
1059 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001060 }
Chris Mason5f39d392007-10-15 16:14:19 -04001061 right = read_node_slot(root, parent, pslot + 1);
1062 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001063 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001064 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001065 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001066 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001067 if (wret) {
1068 ret = wret;
1069 goto enospc;
1070 }
1071 }
1072
1073 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001074 if (left) {
1075 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001076 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001077 if (wret < 0)
1078 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001079 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001080 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001081 }
Chris Mason79f95c82007-03-01 15:16:26 -05001082
1083 /*
1084 * then try to empty the right most buffer into the middle
1085 */
Chris Mason5f39d392007-10-15 16:14:19 -04001086 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001087 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001088 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001089 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001090 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001091 u64 bytenr = right->start;
1092 u32 blocksize = right->len;
1093
Chris Mason5f39d392007-10-15 16:14:19 -04001094 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001095 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001096 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001097 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001098 wret = del_ptr(trans, root, path, level + 1, pslot +
1099 1);
Chris Masonbb803952007-03-01 12:04:21 -05001100 if (wret)
1101 ret = wret;
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00001102 wret = btrfs_free_tree_block(trans, root,
1103 bytenr, blocksize, 0,
1104 root->root_key.objectid,
1105 level);
Chris Masonbb803952007-03-01 12:04:21 -05001106 if (wret)
1107 ret = wret;
1108 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001109 struct btrfs_disk_key right_key;
1110 btrfs_node_key(right, &right_key, 0);
1111 btrfs_set_node_key(parent, &right_key, pslot + 1);
1112 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001113 }
1114 }
Chris Mason5f39d392007-10-15 16:14:19 -04001115 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001116 /*
1117 * we're not allowed to leave a node with one item in the
1118 * tree during a delete. A deletion from lower in the tree
1119 * could try to delete the only pointer in this node.
1120 * So, pull some keys from the left.
1121 * There has to be a left pointer at this point because
1122 * otherwise we would have pulled some pointers from the
1123 * right
1124 */
Chris Mason5f39d392007-10-15 16:14:19 -04001125 BUG_ON(!left);
1126 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001127 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001128 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001129 goto enospc;
1130 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001131 if (wret == 1) {
1132 wret = push_node_left(trans, root, left, mid, 1);
1133 if (wret < 0)
1134 ret = wret;
1135 }
Chris Mason79f95c82007-03-01 15:16:26 -05001136 BUG_ON(wret == 1);
1137 }
Chris Mason5f39d392007-10-15 16:14:19 -04001138 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001139 /* we've managed to empty the middle node, drop it */
Chris Masondb945352007-10-15 16:15:53 -04001140 u64 bytenr = mid->start;
1141 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001142
Chris Mason5f39d392007-10-15 16:14:19 -04001143 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001144 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001145 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001146 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001147 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001148 if (wret)
1149 ret = wret;
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00001150 wret = btrfs_free_tree_block(trans, root, bytenr, blocksize,
1151 0, root->root_key.objectid, level);
Chris Masonbb803952007-03-01 12:04:21 -05001152 if (wret)
1153 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001154 } else {
1155 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001156 struct btrfs_disk_key mid_key;
1157 btrfs_node_key(mid, &mid_key, 0);
1158 btrfs_set_node_key(parent, &mid_key, pslot);
1159 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001160 }
Chris Masonbb803952007-03-01 12:04:21 -05001161
Chris Mason79f95c82007-03-01 15:16:26 -05001162 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001163 if (left) {
1164 if (btrfs_header_nritems(left) > orig_slot) {
1165 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001166 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001167 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001168 path->slots[level + 1] -= 1;
1169 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001170 if (mid) {
1171 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001172 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001173 }
Chris Masonbb803952007-03-01 12:04:21 -05001174 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001175 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001176 path->slots[level] = orig_slot;
1177 }
1178 }
Chris Mason79f95c82007-03-01 15:16:26 -05001179 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001180 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001181 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001182 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001183 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001184enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001185 if (right) {
1186 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001187 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001188 }
1189 if (left) {
1190 if (path->nodes[level] != left)
1191 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001192 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001193 }
Chris Masonbb803952007-03-01 12:04:21 -05001194 return ret;
1195}
1196
Chris Masond352ac62008-09-29 15:18:18 -04001197/* Node balancing for insertion. Here we only split or push nodes around
1198 * when they are completely full. This is also done top down, so we
1199 * have to be pessimistic.
1200 */
Chris Masond3977122009-01-05 21:25:51 -05001201static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001202 struct btrfs_root *root,
1203 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001204{
Chris Mason5f39d392007-10-15 16:14:19 -04001205 struct extent_buffer *right = NULL;
1206 struct extent_buffer *mid;
1207 struct extent_buffer *left = NULL;
1208 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001209 int ret = 0;
1210 int wret;
1211 int pslot;
1212 int orig_slot = path->slots[level];
1213 u64 orig_ptr;
1214
1215 if (level == 0)
1216 return 1;
1217
Chris Mason5f39d392007-10-15 16:14:19 -04001218 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001219 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001220 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1221
1222 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001223 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001224 pslot = path->slots[level + 1];
1225
Chris Mason5f39d392007-10-15 16:14:19 -04001226 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001227 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001228
Chris Mason5f39d392007-10-15 16:14:19 -04001229 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001230
1231 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001232 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001233 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001234
1235 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001236 btrfs_set_lock_blocking(left);
1237
Chris Mason5f39d392007-10-15 16:14:19 -04001238 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001239 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1240 wret = 1;
1241 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001242 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001243 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001244 if (ret)
1245 wret = 1;
1246 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001247 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001248 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001249 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001250 }
Chris Masone66f7092007-04-20 13:16:02 -04001251 if (wret < 0)
1252 ret = wret;
1253 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001254 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001255 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001256 btrfs_node_key(mid, &disk_key, 0);
1257 btrfs_set_node_key(parent, &disk_key, pslot);
1258 btrfs_mark_buffer_dirty(parent);
1259 if (btrfs_header_nritems(left) > orig_slot) {
1260 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001261 path->slots[level + 1] -= 1;
1262 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001263 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001264 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001265 } else {
1266 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001267 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001268 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001269 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001270 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001271 }
Chris Masone66f7092007-04-20 13:16:02 -04001272 return 0;
1273 }
Chris Mason925baed2008-06-25 16:01:30 -04001274 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001275 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001276 }
Chris Mason925baed2008-06-25 16:01:30 -04001277 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001278
1279 /*
1280 * then try to empty the right most buffer into the middle
1281 */
Chris Mason5f39d392007-10-15 16:14:19 -04001282 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001283 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001284
Chris Mason925baed2008-06-25 16:01:30 -04001285 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001286 btrfs_set_lock_blocking(right);
1287
Chris Mason5f39d392007-10-15 16:14:19 -04001288 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001289 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1290 wret = 1;
1291 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001292 ret = btrfs_cow_block(trans, root, right,
1293 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001294 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001295 if (ret)
1296 wret = 1;
1297 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001298 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001299 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001300 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001301 }
Chris Masone66f7092007-04-20 13:16:02 -04001302 if (wret < 0)
1303 ret = wret;
1304 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001305 struct btrfs_disk_key disk_key;
1306
1307 btrfs_node_key(right, &disk_key, 0);
1308 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1309 btrfs_mark_buffer_dirty(parent);
1310
1311 if (btrfs_header_nritems(mid) <= orig_slot) {
1312 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001313 path->slots[level + 1] += 1;
1314 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001315 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001316 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001317 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001318 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001319 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001320 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001321 }
Chris Masone66f7092007-04-20 13:16:02 -04001322 return 0;
1323 }
Chris Mason925baed2008-06-25 16:01:30 -04001324 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001325 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001326 }
Chris Masone66f7092007-04-20 13:16:02 -04001327 return 1;
1328}
1329
Chris Mason74123bd2007-02-02 11:05:29 -05001330/*
Chris Masond352ac62008-09-29 15:18:18 -04001331 * readahead one full node of leaves, finding things that are close
1332 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001333 */
Chris Masonc8c42862009-04-03 10:14:18 -04001334static void reada_for_search(struct btrfs_root *root,
1335 struct btrfs_path *path,
1336 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001337{
Chris Mason5f39d392007-10-15 16:14:19 -04001338 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001339 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001340 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001341 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001342 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001343 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001344 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001345 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001346 u32 nr;
1347 u32 blocksize;
1348 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001349
Chris Masona6b6e752007-10-15 16:22:39 -04001350 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001351 return;
1352
Chris Mason6702ed42007-08-07 16:15:09 -04001353 if (!path->nodes[level])
1354 return;
1355
Chris Mason5f39d392007-10-15 16:14:19 -04001356 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001357
Chris Mason3c69fae2007-08-07 15:52:22 -04001358 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001359 blocksize = btrfs_level_size(root, level - 1);
1360 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001361 if (eb) {
1362 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001363 return;
1364 }
1365
Chris Masona7175312009-01-22 09:23:10 -05001366 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001367
Chris Mason5f39d392007-10-15 16:14:19 -04001368 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001369 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001370 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001371 if (direction < 0) {
1372 if (nr == 0)
1373 break;
1374 nr--;
1375 } else if (direction > 0) {
1376 nr++;
1377 if (nr >= nritems)
1378 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001379 }
Chris Mason01f46652007-12-21 16:24:26 -05001380 if (path->reada < 0 && objectid) {
1381 btrfs_node_key(node, &disk_key, nr);
1382 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1383 break;
1384 }
Chris Mason6b800532007-10-15 16:17:34 -04001385 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001386 if ((search <= target && target - search <= 65536) ||
1387 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001388 readahead_tree_block(root, search, blocksize,
1389 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001390 nread += blocksize;
1391 }
1392 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001393 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001394 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001395 }
1396}
Chris Mason925baed2008-06-25 16:01:30 -04001397
Chris Masond352ac62008-09-29 15:18:18 -04001398/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001399 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1400 * cache
1401 */
1402static noinline int reada_for_balance(struct btrfs_root *root,
1403 struct btrfs_path *path, int level)
1404{
1405 int slot;
1406 int nritems;
1407 struct extent_buffer *parent;
1408 struct extent_buffer *eb;
1409 u64 gen;
1410 u64 block1 = 0;
1411 u64 block2 = 0;
1412 int ret = 0;
1413 int blocksize;
1414
Chris Mason8c594ea2009-04-20 15:50:10 -04001415 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001416 if (!parent)
1417 return 0;
1418
1419 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001420 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001421 blocksize = btrfs_level_size(root, level);
1422
1423 if (slot > 0) {
1424 block1 = btrfs_node_blockptr(parent, slot - 1);
1425 gen = btrfs_node_ptr_generation(parent, slot - 1);
1426 eb = btrfs_find_tree_block(root, block1, blocksize);
1427 if (eb && btrfs_buffer_uptodate(eb, gen))
1428 block1 = 0;
1429 free_extent_buffer(eb);
1430 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001431 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001432 block2 = btrfs_node_blockptr(parent, slot + 1);
1433 gen = btrfs_node_ptr_generation(parent, slot + 1);
1434 eb = btrfs_find_tree_block(root, block2, blocksize);
1435 if (eb && btrfs_buffer_uptodate(eb, gen))
1436 block2 = 0;
1437 free_extent_buffer(eb);
1438 }
1439 if (block1 || block2) {
1440 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001441
1442 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001443 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001444
1445 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001446 if (block1)
1447 readahead_tree_block(root, block1, blocksize, 0);
1448 if (block2)
1449 readahead_tree_block(root, block2, blocksize, 0);
1450
1451 if (block1) {
1452 eb = read_tree_block(root, block1, blocksize, 0);
1453 free_extent_buffer(eb);
1454 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001455 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001456 eb = read_tree_block(root, block2, blocksize, 0);
1457 free_extent_buffer(eb);
1458 }
1459 }
1460 return ret;
1461}
1462
1463
1464/*
Chris Masond3977122009-01-05 21:25:51 -05001465 * when we walk down the tree, it is usually safe to unlock the higher layers
1466 * in the tree. The exceptions are when our path goes through slot 0, because
1467 * operations on the tree might require changing key pointers higher up in the
1468 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001469 *
Chris Masond3977122009-01-05 21:25:51 -05001470 * callers might also have set path->keep_locks, which tells this code to keep
1471 * the lock if the path points to the last slot in the block. This is part of
1472 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001473 *
Chris Masond3977122009-01-05 21:25:51 -05001474 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1475 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001476 */
Chris Masone02119d2008-09-05 16:13:11 -04001477static noinline void unlock_up(struct btrfs_path *path, int level,
1478 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001479{
1480 int i;
1481 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001482 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001483 struct extent_buffer *t;
1484
1485 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1486 if (!path->nodes[i])
1487 break;
1488 if (!path->locks[i])
1489 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001490 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001491 skip_level = i + 1;
1492 continue;
1493 }
Chris Mason051e1b92008-06-25 16:01:30 -04001494 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001495 u32 nritems;
1496 t = path->nodes[i];
1497 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001498 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001499 skip_level = i + 1;
1500 continue;
1501 }
1502 }
Chris Mason051e1b92008-06-25 16:01:30 -04001503 if (skip_level < i && i >= lowest_unlock)
1504 no_skips = 1;
1505
Chris Mason925baed2008-06-25 16:01:30 -04001506 t = path->nodes[i];
1507 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1508 btrfs_tree_unlock(t);
1509 path->locks[i] = 0;
1510 }
1511 }
1512}
1513
Chris Mason3c69fae2007-08-07 15:52:22 -04001514/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001515 * This releases any locks held in the path starting at level and
1516 * going all the way up to the root.
1517 *
1518 * btrfs_search_slot will keep the lock held on higher nodes in a few
1519 * corner cases, such as COW of the block at slot zero in the node. This
1520 * ignores those rules, and it should only be called when there are no
1521 * more updates to be done higher up in the tree.
1522 */
1523noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1524{
1525 int i;
1526
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001527 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001528 return;
1529
1530 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1531 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001532 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001533 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001534 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001535 btrfs_tree_unlock(path->nodes[i]);
1536 path->locks[i] = 0;
1537 }
1538}
1539
1540/*
Chris Masonc8c42862009-04-03 10:14:18 -04001541 * helper function for btrfs_search_slot. The goal is to find a block
1542 * in cache without setting the path to blocking. If we find the block
1543 * we return zero and the path is unchanged.
1544 *
1545 * If we can't find the block, we set the path blocking and do some
1546 * reada. -EAGAIN is returned and the search must be repeated.
1547 */
1548static int
1549read_block_for_search(struct btrfs_trans_handle *trans,
1550 struct btrfs_root *root, struct btrfs_path *p,
1551 struct extent_buffer **eb_ret, int level, int slot,
1552 struct btrfs_key *key)
1553{
1554 u64 blocknr;
1555 u64 gen;
1556 u32 blocksize;
1557 struct extent_buffer *b = *eb_ret;
1558 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001559 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001560
1561 blocknr = btrfs_node_blockptr(b, slot);
1562 gen = btrfs_node_ptr_generation(b, slot);
1563 blocksize = btrfs_level_size(root, level - 1);
1564
1565 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1566 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason76a05b32009-05-14 13:24:30 -04001567 /*
1568 * we found an up to date block without sleeping, return
1569 * right away
1570 */
Chris Masonc8c42862009-04-03 10:14:18 -04001571 *eb_ret = tmp;
1572 return 0;
1573 }
1574
1575 /*
1576 * reduce lock contention at high levels
1577 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001578 * we read. Don't release the lock on the current
1579 * level because we need to walk this node to figure
1580 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001581 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001582 btrfs_unlock_up_safe(p, level + 1);
1583 btrfs_set_path_blocking(p);
1584
Chris Masonc8c42862009-04-03 10:14:18 -04001585 if (tmp)
1586 free_extent_buffer(tmp);
1587 if (p->reada)
1588 reada_for_search(root, p, level, slot, key->objectid);
1589
Chris Mason8c594ea2009-04-20 15:50:10 -04001590 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001591
1592 ret = -EAGAIN;
Chris Masonc8c42862009-04-03 10:14:18 -04001593 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Mason76a05b32009-05-14 13:24:30 -04001594 if (tmp) {
1595 /*
1596 * If the read above didn't mark this buffer up to date,
1597 * it will never end up being up to date. Set ret to EIO now
1598 * and give up so that our caller doesn't loop forever
1599 * on our EAGAINs.
1600 */
1601 if (!btrfs_buffer_uptodate(tmp, 0))
1602 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001603 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001604 }
1605 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001606}
1607
1608/*
1609 * helper function for btrfs_search_slot. This does all of the checks
1610 * for node-level blocks and does any balancing required based on
1611 * the ins_len.
1612 *
1613 * If no extra work was required, zero is returned. If we had to
1614 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1615 * start over
1616 */
1617static int
1618setup_nodes_for_search(struct btrfs_trans_handle *trans,
1619 struct btrfs_root *root, struct btrfs_path *p,
1620 struct extent_buffer *b, int level, int ins_len)
1621{
1622 int ret;
1623 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1624 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1625 int sret;
1626
1627 sret = reada_for_balance(root, p, level);
1628 if (sret)
1629 goto again;
1630
1631 btrfs_set_path_blocking(p);
1632 sret = split_node(trans, root, p, level);
1633 btrfs_clear_path_blocking(p, NULL);
1634
1635 BUG_ON(sret > 0);
1636 if (sret) {
1637 ret = sret;
1638 goto done;
1639 }
1640 b = p->nodes[level];
1641 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001642 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001643 int sret;
1644
1645 sret = reada_for_balance(root, p, level);
1646 if (sret)
1647 goto again;
1648
1649 btrfs_set_path_blocking(p);
1650 sret = balance_level(trans, root, p, level);
1651 btrfs_clear_path_blocking(p, NULL);
1652
1653 if (sret) {
1654 ret = sret;
1655 goto done;
1656 }
1657 b = p->nodes[level];
1658 if (!b) {
1659 btrfs_release_path(NULL, p);
1660 goto again;
1661 }
1662 BUG_ON(btrfs_header_nritems(b) == 1);
1663 }
1664 return 0;
1665
1666again:
1667 ret = -EAGAIN;
1668done:
1669 return ret;
1670}
1671
1672/*
Chris Mason74123bd2007-02-02 11:05:29 -05001673 * look for key in the tree. path is filled in with nodes along the way
1674 * if key is found, we return zero and you can find the item in the leaf
1675 * level of the path (level 0)
1676 *
1677 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001678 * be inserted, and 1 is returned. If there are other errors during the
1679 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001680 *
1681 * if ins_len > 0, nodes and leaves will be split as we walk down the
1682 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1683 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001684 */
Chris Masone089f052007-03-16 16:20:31 -04001685int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1686 *root, struct btrfs_key *key, struct btrfs_path *p, int
1687 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001688{
Chris Mason5f39d392007-10-15 16:14:19 -04001689 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001690 int slot;
1691 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001692 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001693 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001694 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001695 u8 lowest_level = 0;
1696
Chris Mason6702ed42007-08-07 16:15:09 -04001697 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001698 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001699 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001700
Chris Mason925baed2008-06-25 16:01:30 -04001701 if (ins_len < 0)
1702 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001703
Chris Masonbb803952007-03-01 12:04:21 -05001704again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001705 if (p->search_commit_root) {
1706 b = root->commit_root;
1707 extent_buffer_get(b);
1708 if (!p->skip_locking)
1709 btrfs_tree_lock(b);
1710 } else {
1711 if (p->skip_locking)
1712 b = btrfs_root_node(root);
1713 else
1714 b = btrfs_lock_root_node(root);
1715 }
Chris Mason925baed2008-06-25 16:01:30 -04001716
Chris Masoneb60cea2007-02-02 09:18:22 -05001717 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001718 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001719
1720 /*
1721 * setup the path here so we can release it under lock
1722 * contention with the cow code
1723 */
1724 p->nodes[level] = b;
1725 if (!p->skip_locking)
1726 p->locks[level] = 1;
1727
Chris Mason02217ed2007-03-02 16:08:05 -05001728 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001729 /*
1730 * if we don't really need to cow this block
1731 * then we don't want to set the path blocking,
1732 * so we test it here
1733 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001734 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001735 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001736
Chris Masonb4ce94d2009-02-04 09:25:08 -05001737 btrfs_set_path_blocking(p);
1738
Yan Zheng33c66f42009-07-22 09:59:00 -04001739 err = btrfs_cow_block(trans, root, b,
1740 p->nodes[level + 1],
1741 p->slots[level + 1], &b);
1742 if (err) {
Chris Mason5f39d392007-10-15 16:14:19 -04001743 free_extent_buffer(b);
Yan Zheng33c66f42009-07-22 09:59:00 -04001744 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001745 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001746 }
Chris Mason02217ed2007-03-02 16:08:05 -05001747 }
Chris Mason65b51a02008-08-01 15:11:20 -04001748cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001749 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001750 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001751 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001752 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001753
Chris Masoneb60cea2007-02-02 09:18:22 -05001754 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001755 if (!p->skip_locking)
1756 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001757
Chris Mason4008c042009-02-12 14:09:45 -05001758 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001759
1760 /*
1761 * we have a lock on b and as long as we aren't changing
1762 * the tree, there is no way to for the items in b to change.
1763 * It is safe to drop the lock on our parent before we
1764 * go through the expensive btree search on b.
1765 *
1766 * If cow is true, then we might be changing slot zero,
1767 * which may require changing the parent. So, we can't
1768 * drop the lock until after we know which slot we're
1769 * operating on.
1770 */
1771 if (!cow)
1772 btrfs_unlock_up_safe(p, level + 1);
1773
Chris Mason123abc82007-03-14 14:14:43 -04001774 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001775 if (ret) {
1776 ret = -1;
1777 goto done;
1778 }
Chris Mason925baed2008-06-25 16:01:30 -04001779
Chris Mason5f39d392007-10-15 16:14:19 -04001780 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001781
Chris Mason5f39d392007-10-15 16:14:19 -04001782 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001783 int dec = 0;
1784 if (ret && slot > 0) {
1785 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001786 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001787 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001788 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001789 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001790 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001791 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001792 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001793 if (err) {
1794 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001795 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001796 }
Chris Masonc8c42862009-04-03 10:14:18 -04001797 b = p->nodes[level];
1798 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001799
Chris Masonf9efa9c2008-06-25 16:14:04 -04001800 unlock_up(p, level, lowest_unlock);
1801
Chris Mason925baed2008-06-25 16:01:30 -04001802 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001803 if (dec)
1804 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001805 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001806 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001807
Yan Zheng33c66f42009-07-22 09:59:00 -04001808 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001809 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001810 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001811 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001812 if (err) {
1813 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001814 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001815 }
Chris Mason76a05b32009-05-14 13:24:30 -04001816
Chris Masonb4ce94d2009-02-04 09:25:08 -05001817 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001818 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001819 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001820
Yan Zheng33c66f42009-07-22 09:59:00 -04001821 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001822 btrfs_set_path_blocking(p);
1823 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001824 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001825 }
1826 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001827 } else {
1828 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001829 if (ins_len > 0 &&
1830 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001831 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001832 err = split_leaf(trans, root, key,
1833 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001834 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001835
Yan Zheng33c66f42009-07-22 09:59:00 -04001836 BUG_ON(err > 0);
1837 if (err) {
1838 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001839 goto done;
1840 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001841 }
Chris Mason459931e2008-12-10 09:10:46 -05001842 if (!p->search_for_split)
1843 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001844 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001845 }
1846 }
Chris Mason65b51a02008-08-01 15:11:20 -04001847 ret = 1;
1848done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001849 /*
1850 * we don't really know what they plan on doing with the path
1851 * from here on, so for now just mark it as blocking
1852 */
Chris Masonb9473432009-03-13 11:00:37 -04001853 if (!p->leave_spinning)
1854 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001855 if (ret < 0)
1856 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001857 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001858}
1859
Chris Mason74123bd2007-02-02 11:05:29 -05001860/*
1861 * adjust the pointers going up the tree, starting at level
1862 * making sure the right key of each node is points to 'key'.
1863 * This is used after shifting pointers to the left, so it stops
1864 * fixing up pointers when a given leaf/node is not in slot 0 of the
1865 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001866 *
1867 * If this fails to write a tree block, it returns -1, but continues
1868 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001869 */
Chris Mason5f39d392007-10-15 16:14:19 -04001870static int fixup_low_keys(struct btrfs_trans_handle *trans,
1871 struct btrfs_root *root, struct btrfs_path *path,
1872 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001873{
1874 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001875 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001876 struct extent_buffer *t;
1877
Chris Mason234b63a2007-03-13 10:46:10 -04001878 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001879 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001880 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001881 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001882 t = path->nodes[i];
1883 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001884 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001885 if (tslot != 0)
1886 break;
1887 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001888 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001889}
1890
Chris Mason74123bd2007-02-02 11:05:29 -05001891/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001892 * update item key.
1893 *
1894 * This function isn't completely safe. It's the caller's responsibility
1895 * that the new key won't break the order
1896 */
1897int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1898 struct btrfs_root *root, struct btrfs_path *path,
1899 struct btrfs_key *new_key)
1900{
1901 struct btrfs_disk_key disk_key;
1902 struct extent_buffer *eb;
1903 int slot;
1904
1905 eb = path->nodes[0];
1906 slot = path->slots[0];
1907 if (slot > 0) {
1908 btrfs_item_key(eb, &disk_key, slot - 1);
1909 if (comp_keys(&disk_key, new_key) >= 0)
1910 return -1;
1911 }
1912 if (slot < btrfs_header_nritems(eb) - 1) {
1913 btrfs_item_key(eb, &disk_key, slot + 1);
1914 if (comp_keys(&disk_key, new_key) <= 0)
1915 return -1;
1916 }
1917
1918 btrfs_cpu_key_to_disk(&disk_key, new_key);
1919 btrfs_set_item_key(eb, &disk_key, slot);
1920 btrfs_mark_buffer_dirty(eb);
1921 if (slot == 0)
1922 fixup_low_keys(trans, root, path, &disk_key, 1);
1923 return 0;
1924}
1925
1926/*
Chris Mason74123bd2007-02-02 11:05:29 -05001927 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001928 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001929 *
1930 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1931 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001932 */
Chris Mason98ed5172008-01-03 10:01:48 -05001933static int push_node_left(struct btrfs_trans_handle *trans,
1934 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001935 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001936{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001937 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001938 int src_nritems;
1939 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001940 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001941
Chris Mason5f39d392007-10-15 16:14:19 -04001942 src_nritems = btrfs_header_nritems(src);
1943 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001944 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001945 WARN_ON(btrfs_header_generation(src) != trans->transid);
1946 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001947
Chris Masonbce4eae2008-04-24 14:42:46 -04001948 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001949 return 1;
1950
Chris Masond3977122009-01-05 21:25:51 -05001951 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001952 return 1;
1953
Chris Masonbce4eae2008-04-24 14:42:46 -04001954 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001955 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001956 if (push_items < src_nritems) {
1957 /* leave at least 8 pointers in the node if
1958 * we aren't going to empty it
1959 */
1960 if (src_nritems - push_items < 8) {
1961 if (push_items <= 8)
1962 return 1;
1963 push_items -= 8;
1964 }
1965 }
1966 } else
1967 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001968
Chris Mason5f39d392007-10-15 16:14:19 -04001969 copy_extent_buffer(dst, src,
1970 btrfs_node_key_ptr_offset(dst_nritems),
1971 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001972 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001973
Chris Masonbb803952007-03-01 12:04:21 -05001974 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001975 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1976 btrfs_node_key_ptr_offset(push_items),
1977 (src_nritems - push_items) *
1978 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001979 }
Chris Mason5f39d392007-10-15 16:14:19 -04001980 btrfs_set_header_nritems(src, src_nritems - push_items);
1981 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1982 btrfs_mark_buffer_dirty(src);
1983 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001984
Chris Masonbb803952007-03-01 12:04:21 -05001985 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001986}
1987
Chris Mason97571fd2007-02-24 13:39:08 -05001988/*
Chris Mason79f95c82007-03-01 15:16:26 -05001989 * try to push data from one node into the next node right in the
1990 * tree.
1991 *
1992 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1993 * error, and > 0 if there was no room in the right hand block.
1994 *
1995 * this will only push up to 1/2 the contents of the left node over
1996 */
Chris Mason5f39d392007-10-15 16:14:19 -04001997static int balance_node_right(struct btrfs_trans_handle *trans,
1998 struct btrfs_root *root,
1999 struct extent_buffer *dst,
2000 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002001{
Chris Mason79f95c82007-03-01 15:16:26 -05002002 int push_items = 0;
2003 int max_push;
2004 int src_nritems;
2005 int dst_nritems;
2006 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002007
Chris Mason7bb86312007-12-11 09:25:06 -05002008 WARN_ON(btrfs_header_generation(src) != trans->transid);
2009 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2010
Chris Mason5f39d392007-10-15 16:14:19 -04002011 src_nritems = btrfs_header_nritems(src);
2012 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002013 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002014 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002015 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002016
Chris Masond3977122009-01-05 21:25:51 -05002017 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002018 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002019
2020 max_push = src_nritems / 2 + 1;
2021 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002022 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002023 return 1;
Yan252c38f2007-08-29 09:11:44 -04002024
Chris Mason79f95c82007-03-01 15:16:26 -05002025 if (max_push < push_items)
2026 push_items = max_push;
2027
Chris Mason5f39d392007-10-15 16:14:19 -04002028 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2029 btrfs_node_key_ptr_offset(0),
2030 (dst_nritems) *
2031 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002032
Chris Mason5f39d392007-10-15 16:14:19 -04002033 copy_extent_buffer(dst, src,
2034 btrfs_node_key_ptr_offset(0),
2035 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002036 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002037
Chris Mason5f39d392007-10-15 16:14:19 -04002038 btrfs_set_header_nritems(src, src_nritems - push_items);
2039 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002040
Chris Mason5f39d392007-10-15 16:14:19 -04002041 btrfs_mark_buffer_dirty(src);
2042 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002043
Chris Mason79f95c82007-03-01 15:16:26 -05002044 return ret;
2045}
2046
2047/*
Chris Mason97571fd2007-02-24 13:39:08 -05002048 * helper function to insert a new root level in the tree.
2049 * A new node is allocated, and a single item is inserted to
2050 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002051 *
2052 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002053 */
Chris Masond3977122009-01-05 21:25:51 -05002054static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002055 struct btrfs_root *root,
2056 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002057{
Chris Mason7bb86312007-12-11 09:25:06 -05002058 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002059 struct extent_buffer *lower;
2060 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002061 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002062 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002063
2064 BUG_ON(path->nodes[level]);
2065 BUG_ON(path->nodes[level-1] != root->node);
2066
Chris Mason7bb86312007-12-11 09:25:06 -05002067 lower = path->nodes[level-1];
2068 if (level == 1)
2069 btrfs_item_key(lower, &lower_key, 0);
2070 else
2071 btrfs_node_key(lower, &lower_key, 0);
2072
Zheng Yan31840ae2008-09-23 13:14:14 -04002073 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002074 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002075 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002076 if (IS_ERR(c))
2077 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002078
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002079 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002080 btrfs_set_header_nritems(c, 1);
2081 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002082 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002083 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002084 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002085 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002086
Chris Mason5f39d392007-10-15 16:14:19 -04002087 write_extent_buffer(c, root->fs_info->fsid,
2088 (unsigned long)btrfs_header_fsid(c),
2089 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002090
2091 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2092 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2093 BTRFS_UUID_SIZE);
2094
Chris Mason5f39d392007-10-15 16:14:19 -04002095 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002096 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002097 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002098 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002099
2100 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002101
2102 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002103
Chris Mason925baed2008-06-25 16:01:30 -04002104 spin_lock(&root->node_lock);
2105 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002106 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002107 spin_unlock(&root->node_lock);
2108
2109 /* the super has an extra ref to root->node */
2110 free_extent_buffer(old);
2111
Chris Mason0b86a832008-03-24 15:01:56 -04002112 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002113 extent_buffer_get(c);
2114 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002115 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002116 path->slots[level] = 0;
2117 return 0;
2118}
2119
Chris Mason74123bd2007-02-02 11:05:29 -05002120/*
2121 * worker function to insert a single pointer in a node.
2122 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002123 *
Chris Mason74123bd2007-02-02 11:05:29 -05002124 * slot and level indicate where you want the key to go, and
2125 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002126 *
2127 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002128 */
Chris Masone089f052007-03-16 16:20:31 -04002129static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2130 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002131 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002132{
Chris Mason5f39d392007-10-15 16:14:19 -04002133 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002134 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002135
2136 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002137 lower = path->nodes[level];
2138 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002139 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002140 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002141 BUG();
2142 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002143 memmove_extent_buffer(lower,
2144 btrfs_node_key_ptr_offset(slot + 1),
2145 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002146 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002147 }
Chris Mason5f39d392007-10-15 16:14:19 -04002148 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002149 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002150 WARN_ON(trans->transid == 0);
2151 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002152 btrfs_set_header_nritems(lower, nritems + 1);
2153 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002154 return 0;
2155}
2156
Chris Mason97571fd2007-02-24 13:39:08 -05002157/*
2158 * split the node at the specified level in path in two.
2159 * The path is corrected to point to the appropriate node after the split
2160 *
2161 * Before splitting this tries to make some room in the node by pushing
2162 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002163 *
2164 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002165 */
Chris Masone02119d2008-09-05 16:13:11 -04002166static noinline int split_node(struct btrfs_trans_handle *trans,
2167 struct btrfs_root *root,
2168 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169{
Chris Mason5f39d392007-10-15 16:14:19 -04002170 struct extent_buffer *c;
2171 struct extent_buffer *split;
2172 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002173 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002174 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002175 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002176 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002177
Chris Mason5f39d392007-10-15 16:14:19 -04002178 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002179 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002180 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002181 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002182 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002183 if (ret)
2184 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002185 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002186 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002187 c = path->nodes[level];
2188 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002189 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002190 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002191 if (ret < 0)
2192 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002193 }
Chris Masone66f7092007-04-20 13:16:02 -04002194
Chris Mason5f39d392007-10-15 16:14:19 -04002195 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002196 mid = (c_nritems + 1) / 2;
2197 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002198
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002199 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002200 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002202 if (IS_ERR(split))
2203 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002204
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002205 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002206 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002207 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002208 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002209 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002210 btrfs_set_header_owner(split, root->root_key.objectid);
2211 write_extent_buffer(split, root->fs_info->fsid,
2212 (unsigned long)btrfs_header_fsid(split),
2213 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002214 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2215 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2216 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002217
Chris Mason5f39d392007-10-15 16:14:19 -04002218
2219 copy_extent_buffer(split, c,
2220 btrfs_node_key_ptr_offset(0),
2221 btrfs_node_key_ptr_offset(mid),
2222 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2223 btrfs_set_header_nritems(split, c_nritems - mid);
2224 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002225 ret = 0;
2226
Chris Mason5f39d392007-10-15 16:14:19 -04002227 btrfs_mark_buffer_dirty(c);
2228 btrfs_mark_buffer_dirty(split);
2229
Chris Masondb945352007-10-15 16:15:53 -04002230 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002231 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002232 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002233 if (wret)
2234 ret = wret;
2235
Chris Mason5de08d72007-02-24 06:24:44 -05002236 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002237 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002238 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002239 free_extent_buffer(c);
2240 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002241 path->slots[level + 1] += 1;
2242 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002243 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002244 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002245 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002246 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002247}
2248
Chris Mason74123bd2007-02-02 11:05:29 -05002249/*
2250 * how many bytes are required to store the items in a leaf. start
2251 * and nr indicate which items in the leaf to check. This totals up the
2252 * space used both by the item structs and the item data
2253 */
Chris Mason5f39d392007-10-15 16:14:19 -04002254static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002255{
2256 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002257 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002258 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002259
2260 if (!nr)
2261 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002262 data_len = btrfs_item_end_nr(l, start);
2263 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002264 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002265 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002266 return data_len;
2267}
2268
Chris Mason74123bd2007-02-02 11:05:29 -05002269/*
Chris Masond4dbff92007-04-04 14:08:15 -04002270 * The space between the end of the leaf items and
2271 * the start of the leaf data. IOW, how much room
2272 * the leaf has left for both items and data
2273 */
Chris Masond3977122009-01-05 21:25:51 -05002274noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002275 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002276{
Chris Mason5f39d392007-10-15 16:14:19 -04002277 int nritems = btrfs_header_nritems(leaf);
2278 int ret;
2279 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2280 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002281 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2282 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002283 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002284 leaf_space_used(leaf, 0, nritems), nritems);
2285 }
2286 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002287}
2288
Chris Mason44871b12009-03-13 10:04:31 -04002289static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2290 struct btrfs_root *root,
2291 struct btrfs_path *path,
2292 int data_size, int empty,
2293 struct extent_buffer *right,
2294 int free_space, u32 left_nritems)
Chris Mason00ec4c52007-02-24 12:47:20 -05002295{
Chris Mason5f39d392007-10-15 16:14:19 -04002296 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002297 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002298 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002299 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002300 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002301 int push_space = 0;
2302 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002303 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002304 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002305 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002306 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002307 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002308
Chris Mason34a38212007-11-07 13:31:03 -05002309 if (empty)
2310 nr = 0;
2311 else
2312 nr = 1;
2313
Zheng Yan31840ae2008-09-23 13:14:14 -04002314 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002315 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002316
Chris Mason44871b12009-03-13 10:04:31 -04002317 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002318 i = left_nritems - 1;
2319 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002320 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002321
Zheng Yan31840ae2008-09-23 13:14:14 -04002322 if (!empty && push_items > 0) {
2323 if (path->slots[0] > i)
2324 break;
2325 if (path->slots[0] == i) {
2326 int space = btrfs_leaf_free_space(root, left);
2327 if (space + push_space * 2 > free_space)
2328 break;
2329 }
2330 }
2331
Chris Mason00ec4c52007-02-24 12:47:20 -05002332 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002333 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002334
2335 if (!left->map_token) {
2336 map_extent_buffer(left, (unsigned long)item,
2337 sizeof(struct btrfs_item),
2338 &left->map_token, &left->kaddr,
2339 &left->map_start, &left->map_len,
2340 KM_USER1);
2341 }
2342
2343 this_item_size = btrfs_item_size(left, item);
2344 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002345 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002346
Chris Mason00ec4c52007-02-24 12:47:20 -05002347 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002348 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002349 if (i == 0)
2350 break;
2351 i--;
Chris Masondb945352007-10-15 16:15:53 -04002352 }
2353 if (left->map_token) {
2354 unmap_extent_buffer(left, left->map_token, KM_USER1);
2355 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002356 }
Chris Mason5f39d392007-10-15 16:14:19 -04002357
Chris Mason925baed2008-06-25 16:01:30 -04002358 if (push_items == 0)
2359 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002360
Chris Mason34a38212007-11-07 13:31:03 -05002361 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002362 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002363
Chris Mason00ec4c52007-02-24 12:47:20 -05002364 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002365 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002366
Chris Mason5f39d392007-10-15 16:14:19 -04002367 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002368 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002369
Chris Mason00ec4c52007-02-24 12:47:20 -05002370 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002371 data_end = leaf_data_end(root, right);
2372 memmove_extent_buffer(right,
2373 btrfs_leaf_data(right) + data_end - push_space,
2374 btrfs_leaf_data(right) + data_end,
2375 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2376
Chris Mason00ec4c52007-02-24 12:47:20 -05002377 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002378 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002379 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2380 btrfs_leaf_data(left) + leaf_data_end(root, left),
2381 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002382
2383 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2384 btrfs_item_nr_offset(0),
2385 right_nritems * sizeof(struct btrfs_item));
2386
Chris Mason00ec4c52007-02-24 12:47:20 -05002387 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002388 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2389 btrfs_item_nr_offset(left_nritems - push_items),
2390 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002391
2392 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002393 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002394 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002395 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002396 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002397 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002398 if (!right->map_token) {
2399 map_extent_buffer(right, (unsigned long)item,
2400 sizeof(struct btrfs_item),
2401 &right->map_token, &right->kaddr,
2402 &right->map_start, &right->map_len,
2403 KM_USER1);
2404 }
2405 push_space -= btrfs_item_size(right, item);
2406 btrfs_set_item_offset(right, item, push_space);
2407 }
2408
2409 if (right->map_token) {
2410 unmap_extent_buffer(right, right->map_token, KM_USER1);
2411 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002412 }
Chris Mason7518a232007-03-12 12:01:18 -04002413 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002414 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002415
Chris Mason34a38212007-11-07 13:31:03 -05002416 if (left_nritems)
2417 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002418 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002419
Chris Mason5f39d392007-10-15 16:14:19 -04002420 btrfs_item_key(right, &disk_key, 0);
2421 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002422 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002423
Chris Mason00ec4c52007-02-24 12:47:20 -05002424 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002425 if (path->slots[0] >= left_nritems) {
2426 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002427 if (btrfs_header_nritems(path->nodes[0]) == 0)
2428 clean_tree_block(trans, root, path->nodes[0]);
2429 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002430 free_extent_buffer(path->nodes[0]);
2431 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002432 path->slots[1] += 1;
2433 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002434 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002435 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002436 }
2437 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002438
2439out_unlock:
2440 btrfs_tree_unlock(right);
2441 free_extent_buffer(right);
2442 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002443}
Chris Mason925baed2008-06-25 16:01:30 -04002444
Chris Mason00ec4c52007-02-24 12:47:20 -05002445/*
Chris Mason44871b12009-03-13 10:04:31 -04002446 * push some data in the path leaf to the right, trying to free up at
2447 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2448 *
2449 * returns 1 if the push failed because the other node didn't have enough
2450 * room, 0 if everything worked out and < 0 if there were major errors.
2451 */
2452static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2453 *root, struct btrfs_path *path, int data_size,
2454 int empty)
2455{
2456 struct extent_buffer *left = path->nodes[0];
2457 struct extent_buffer *right;
2458 struct extent_buffer *upper;
2459 int slot;
2460 int free_space;
2461 u32 left_nritems;
2462 int ret;
2463
2464 if (!path->nodes[1])
2465 return 1;
2466
2467 slot = path->slots[1];
2468 upper = path->nodes[1];
2469 if (slot >= btrfs_header_nritems(upper) - 1)
2470 return 1;
2471
2472 btrfs_assert_tree_locked(path->nodes[1]);
2473
2474 right = read_node_slot(root, upper, slot + 1);
2475 btrfs_tree_lock(right);
2476 btrfs_set_lock_blocking(right);
2477
2478 free_space = btrfs_leaf_free_space(root, right);
2479 if (free_space < data_size)
2480 goto out_unlock;
2481
2482 /* cow and double check */
2483 ret = btrfs_cow_block(trans, root, right, upper,
2484 slot + 1, &right);
2485 if (ret)
2486 goto out_unlock;
2487
2488 free_space = btrfs_leaf_free_space(root, right);
2489 if (free_space < data_size)
2490 goto out_unlock;
2491
2492 left_nritems = btrfs_header_nritems(left);
2493 if (left_nritems == 0)
2494 goto out_unlock;
2495
2496 return __push_leaf_right(trans, root, path, data_size, empty,
2497 right, free_space, left_nritems);
2498out_unlock:
2499 btrfs_tree_unlock(right);
2500 free_extent_buffer(right);
2501 return 1;
2502}
2503
2504/*
Chris Mason74123bd2007-02-02 11:05:29 -05002505 * push some data in the path leaf to the left, trying to free up at
2506 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2507 */
Chris Mason44871b12009-03-13 10:04:31 -04002508static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2509 struct btrfs_root *root,
2510 struct btrfs_path *path, int data_size,
2511 int empty, struct extent_buffer *left,
2512 int free_space, int right_nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002513{
Chris Mason5f39d392007-10-15 16:14:19 -04002514 struct btrfs_disk_key disk_key;
2515 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002516 int slot;
2517 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002518 int push_space = 0;
2519 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002520 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002521 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002522 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002523 int ret = 0;
2524 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002525 u32 this_item_size;
2526 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002527
2528 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002529
Chris Mason34a38212007-11-07 13:31:03 -05002530 if (empty)
2531 nr = right_nritems;
2532 else
2533 nr = right_nritems - 1;
2534
2535 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002536 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002537 if (!right->map_token) {
2538 map_extent_buffer(right, (unsigned long)item,
2539 sizeof(struct btrfs_item),
2540 &right->map_token, &right->kaddr,
2541 &right->map_start, &right->map_len,
2542 KM_USER1);
2543 }
2544
Zheng Yan31840ae2008-09-23 13:14:14 -04002545 if (!empty && push_items > 0) {
2546 if (path->slots[0] < i)
2547 break;
2548 if (path->slots[0] == i) {
2549 int space = btrfs_leaf_free_space(root, right);
2550 if (space + push_space * 2 > free_space)
2551 break;
2552 }
2553 }
2554
Chris Masonbe0e5c02007-01-26 15:51:26 -05002555 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002556 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002557
2558 this_item_size = btrfs_item_size(right, item);
2559 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002560 break;
Chris Masondb945352007-10-15 16:15:53 -04002561
Chris Masonbe0e5c02007-01-26 15:51:26 -05002562 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002563 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002564 }
Chris Masondb945352007-10-15 16:15:53 -04002565
2566 if (right->map_token) {
2567 unmap_extent_buffer(right, right->map_token, KM_USER1);
2568 right->map_token = NULL;
2569 }
2570
Chris Masonbe0e5c02007-01-26 15:51:26 -05002571 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002572 ret = 1;
2573 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002574 }
Chris Mason34a38212007-11-07 13:31:03 -05002575 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002576 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002577
Chris Masonbe0e5c02007-01-26 15:51:26 -05002578 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002579 copy_extent_buffer(left, right,
2580 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2581 btrfs_item_nr_offset(0),
2582 push_items * sizeof(struct btrfs_item));
2583
Chris Mason123abc82007-03-14 14:14:43 -04002584 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002585 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002586
2587 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002588 leaf_data_end(root, left) - push_space,
2589 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002590 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002591 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002592 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002593 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002594
Chris Masondb945352007-10-15 16:15:53 -04002595 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002596 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002597 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002598
Chris Mason5f39d392007-10-15 16:14:19 -04002599 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002600 if (!left->map_token) {
2601 map_extent_buffer(left, (unsigned long)item,
2602 sizeof(struct btrfs_item),
2603 &left->map_token, &left->kaddr,
2604 &left->map_start, &left->map_len,
2605 KM_USER1);
2606 }
2607
Chris Mason5f39d392007-10-15 16:14:19 -04002608 ioff = btrfs_item_offset(left, item);
2609 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002610 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002611 }
Chris Mason5f39d392007-10-15 16:14:19 -04002612 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002613 if (left->map_token) {
2614 unmap_extent_buffer(left, left->map_token, KM_USER1);
2615 left->map_token = NULL;
2616 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002617
2618 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002619 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002620 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2621 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002622 WARN_ON(1);
2623 }
Chris Mason5f39d392007-10-15 16:14:19 -04002624
Chris Mason34a38212007-11-07 13:31:03 -05002625 if (push_items < right_nritems) {
2626 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2627 leaf_data_end(root, right);
2628 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2629 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2630 btrfs_leaf_data(right) +
2631 leaf_data_end(root, right), push_space);
2632
2633 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002634 btrfs_item_nr_offset(push_items),
2635 (btrfs_header_nritems(right) - push_items) *
2636 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002637 }
Yaneef1c492007-11-26 10:58:13 -05002638 right_nritems -= push_items;
2639 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002640 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002641 for (i = 0; i < right_nritems; i++) {
2642 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002643
2644 if (!right->map_token) {
2645 map_extent_buffer(right, (unsigned long)item,
2646 sizeof(struct btrfs_item),
2647 &right->map_token, &right->kaddr,
2648 &right->map_start, &right->map_len,
2649 KM_USER1);
2650 }
2651
2652 push_space = push_space - btrfs_item_size(right, item);
2653 btrfs_set_item_offset(right, item, push_space);
2654 }
2655 if (right->map_token) {
2656 unmap_extent_buffer(right, right->map_token, KM_USER1);
2657 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002658 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002659
Chris Mason5f39d392007-10-15 16:14:19 -04002660 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002661 if (right_nritems)
2662 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002663
Chris Mason5f39d392007-10-15 16:14:19 -04002664 btrfs_item_key(right, &disk_key, 0);
2665 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002666 if (wret)
2667 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002668
2669 /* then fixup the leaf pointer in the path */
2670 if (path->slots[0] < push_items) {
2671 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002672 if (btrfs_header_nritems(path->nodes[0]) == 0)
2673 clean_tree_block(trans, root, path->nodes[0]);
2674 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002675 free_extent_buffer(path->nodes[0]);
2676 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002677 path->slots[1] -= 1;
2678 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002679 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002680 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002681 path->slots[0] -= push_items;
2682 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002683 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002684 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002685out:
2686 btrfs_tree_unlock(left);
2687 free_extent_buffer(left);
2688 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689}
2690
Chris Mason74123bd2007-02-02 11:05:29 -05002691/*
Chris Mason44871b12009-03-13 10:04:31 -04002692 * push some data in the path leaf to the left, trying to free up at
2693 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2694 */
2695static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2696 *root, struct btrfs_path *path, int data_size,
2697 int empty)
2698{
2699 struct extent_buffer *right = path->nodes[0];
2700 struct extent_buffer *left;
2701 int slot;
2702 int free_space;
2703 u32 right_nritems;
2704 int ret = 0;
2705
2706 slot = path->slots[1];
2707 if (slot == 0)
2708 return 1;
2709 if (!path->nodes[1])
2710 return 1;
2711
2712 right_nritems = btrfs_header_nritems(right);
2713 if (right_nritems == 0)
2714 return 1;
2715
2716 btrfs_assert_tree_locked(path->nodes[1]);
2717
2718 left = read_node_slot(root, path->nodes[1], slot - 1);
2719 btrfs_tree_lock(left);
2720 btrfs_set_lock_blocking(left);
2721
2722 free_space = btrfs_leaf_free_space(root, left);
2723 if (free_space < data_size) {
2724 ret = 1;
2725 goto out;
2726 }
2727
2728 /* cow and double check */
2729 ret = btrfs_cow_block(trans, root, left,
2730 path->nodes[1], slot - 1, &left);
2731 if (ret) {
2732 /* we hit -ENOSPC, but it isn't fatal here */
2733 ret = 1;
2734 goto out;
2735 }
2736
2737 free_space = btrfs_leaf_free_space(root, left);
2738 if (free_space < data_size) {
2739 ret = 1;
2740 goto out;
2741 }
2742
2743 return __push_leaf_left(trans, root, path, data_size,
2744 empty, left, free_space, right_nritems);
2745out:
2746 btrfs_tree_unlock(left);
2747 free_extent_buffer(left);
2748 return ret;
2749}
2750
2751/*
Chris Mason74123bd2007-02-02 11:05:29 -05002752 * split the path's leaf in two, making sure there is at least data_size
2753 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002754 *
2755 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002756 */
Chris Mason44871b12009-03-13 10:04:31 -04002757static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002758 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002759 struct btrfs_path *path,
2760 struct extent_buffer *l,
2761 struct extent_buffer *right,
2762 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002763{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002764 int data_copy_size;
2765 int rt_data_off;
2766 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002767 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002768 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002769 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002770
Chris Mason5f39d392007-10-15 16:14:19 -04002771 nritems = nritems - mid;
2772 btrfs_set_header_nritems(right, nritems);
2773 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2774
2775 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2776 btrfs_item_nr_offset(mid),
2777 nritems * sizeof(struct btrfs_item));
2778
2779 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002780 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2781 data_copy_size, btrfs_leaf_data(l) +
2782 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002783
Chris Mason5f39d392007-10-15 16:14:19 -04002784 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2785 btrfs_item_end_nr(l, mid);
2786
2787 for (i = 0; i < nritems; i++) {
2788 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002789 u32 ioff;
2790
2791 if (!right->map_token) {
2792 map_extent_buffer(right, (unsigned long)item,
2793 sizeof(struct btrfs_item),
2794 &right->map_token, &right->kaddr,
2795 &right->map_start, &right->map_len,
2796 KM_USER1);
2797 }
2798
2799 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002800 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002801 }
Chris Mason74123bd2007-02-02 11:05:29 -05002802
Chris Masondb945352007-10-15 16:15:53 -04002803 if (right->map_token) {
2804 unmap_extent_buffer(right, right->map_token, KM_USER1);
2805 right->map_token = NULL;
2806 }
2807
Chris Mason5f39d392007-10-15 16:14:19 -04002808 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002809 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002810 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002811 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2812 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002813 if (wret)
2814 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002815
2816 btrfs_mark_buffer_dirty(right);
2817 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002818 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002819
Chris Masonbe0e5c02007-01-26 15:51:26 -05002820 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002821 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002822 free_extent_buffer(path->nodes[0]);
2823 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002824 path->slots[0] -= mid;
2825 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002826 } else {
2827 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002828 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002829 }
Chris Mason5f39d392007-10-15 16:14:19 -04002830
Chris Masoneb60cea2007-02-02 09:18:22 -05002831 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002832
Chris Mason44871b12009-03-13 10:04:31 -04002833 return ret;
2834}
2835
2836/*
2837 * split the path's leaf in two, making sure there is at least data_size
2838 * available for the resulting leaf level of the path.
2839 *
2840 * returns 0 if all went well and < 0 on failure.
2841 */
2842static noinline int split_leaf(struct btrfs_trans_handle *trans,
2843 struct btrfs_root *root,
2844 struct btrfs_key *ins_key,
2845 struct btrfs_path *path, int data_size,
2846 int extend)
2847{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002848 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002849 struct extent_buffer *l;
2850 u32 nritems;
2851 int mid;
2852 int slot;
2853 struct extent_buffer *right;
2854 int ret = 0;
2855 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002856 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002857 int num_doubles = 0;
2858
Yan, Zhenga5719522009-09-24 09:17:31 -04002859 l = path->nodes[0];
2860 slot = path->slots[0];
2861 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2862 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2863 return -EOVERFLOW;
2864
Chris Mason44871b12009-03-13 10:04:31 -04002865 /* first try to make some room by pushing left and right */
Chris Masonb3612422009-05-13 19:12:15 -04002866 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason44871b12009-03-13 10:04:31 -04002867 wret = push_leaf_right(trans, root, path, data_size, 0);
2868 if (wret < 0)
2869 return wret;
2870 if (wret) {
2871 wret = push_leaf_left(trans, root, path, data_size, 0);
2872 if (wret < 0)
2873 return wret;
2874 }
2875 l = path->nodes[0];
2876
2877 /* did the pushes work? */
2878 if (btrfs_leaf_free_space(root, l) >= data_size)
2879 return 0;
2880 }
2881
2882 if (!path->nodes[1]) {
2883 ret = insert_new_root(trans, root, path, 1);
2884 if (ret)
2885 return ret;
2886 }
2887again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002888 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002889 l = path->nodes[0];
2890 slot = path->slots[0];
2891 nritems = btrfs_header_nritems(l);
2892 mid = (nritems + 1) / 2;
2893
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002894 if (mid <= slot) {
2895 if (nritems == 1 ||
2896 leaf_space_used(l, mid, nritems - mid) + data_size >
2897 BTRFS_LEAF_DATA_SIZE(root)) {
2898 if (slot >= nritems) {
2899 split = 0;
2900 } else {
2901 mid = slot;
2902 if (mid != nritems &&
2903 leaf_space_used(l, mid, nritems - mid) +
2904 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2905 split = 2;
2906 }
2907 }
2908 }
2909 } else {
2910 if (leaf_space_used(l, 0, mid) + data_size >
2911 BTRFS_LEAF_DATA_SIZE(root)) {
2912 if (!extend && data_size && slot == 0) {
2913 split = 0;
2914 } else if ((extend || !data_size) && slot == 0) {
2915 mid = 1;
2916 } else {
2917 mid = slot;
2918 if (mid != nritems &&
2919 leaf_space_used(l, mid, nritems - mid) +
2920 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2921 split = 2 ;
2922 }
2923 }
2924 }
2925 }
2926
2927 if (split == 0)
2928 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2929 else
2930 btrfs_item_key(l, &disk_key, mid);
2931
2932 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002933 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002934 &disk_key, 0, l->start, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002935 if (IS_ERR(right)) {
2936 BUG_ON(1);
2937 return PTR_ERR(right);
2938 }
2939
2940 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2941 btrfs_set_header_bytenr(right, right->start);
2942 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002943 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002944 btrfs_set_header_owner(right, root->root_key.objectid);
2945 btrfs_set_header_level(right, 0);
2946 write_extent_buffer(right, root->fs_info->fsid,
2947 (unsigned long)btrfs_header_fsid(right),
2948 BTRFS_FSID_SIZE);
2949
2950 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2951 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2952 BTRFS_UUID_SIZE);
2953
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002954 if (split == 0) {
2955 if (mid <= slot) {
2956 btrfs_set_header_nritems(right, 0);
2957 wret = insert_ptr(trans, root, path,
2958 &disk_key, right->start,
2959 path->slots[1] + 1, 1);
2960 if (wret)
2961 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002962
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002963 btrfs_tree_unlock(path->nodes[0]);
2964 free_extent_buffer(path->nodes[0]);
2965 path->nodes[0] = right;
2966 path->slots[0] = 0;
2967 path->slots[1] += 1;
2968 } else {
2969 btrfs_set_header_nritems(right, 0);
2970 wret = insert_ptr(trans, root, path,
2971 &disk_key,
2972 right->start,
2973 path->slots[1], 1);
2974 if (wret)
2975 ret = wret;
2976 btrfs_tree_unlock(path->nodes[0]);
2977 free_extent_buffer(path->nodes[0]);
2978 path->nodes[0] = right;
2979 path->slots[0] = 0;
2980 if (path->slots[1] == 0) {
2981 wret = fixup_low_keys(trans, root,
2982 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002983 if (wret)
2984 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002985 }
2986 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002987 btrfs_mark_buffer_dirty(right);
2988 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002989 }
2990
2991 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2992 BUG_ON(ret);
2993
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002994 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002995 BUG_ON(num_doubles != 0);
2996 num_doubles++;
2997 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002998 }
Chris Mason44871b12009-03-13 10:04:31 -04002999
Chris Masonbe0e5c02007-01-26 15:51:26 -05003000 return ret;
3001}
3002
Yan, Zhengad48fd752009-11-12 09:33:58 +00003003static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3004 struct btrfs_root *root,
3005 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003006{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003007 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003008 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003009 struct btrfs_file_extent_item *fi;
3010 u64 extent_len = 0;
3011 u32 item_size;
3012 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003013
3014 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003015 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3016
3017 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3018 key.type != BTRFS_EXTENT_CSUM_KEY);
3019
3020 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3021 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003022
3023 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003024 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3025 fi = btrfs_item_ptr(leaf, path->slots[0],
3026 struct btrfs_file_extent_item);
3027 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3028 }
Chris Mason459931e2008-12-10 09:10:46 -05003029 btrfs_release_path(root, path);
3030
Chris Mason459931e2008-12-10 09:10:46 -05003031 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003032 path->search_for_split = 1;
3033 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003034 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003035 if (ret < 0)
3036 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003037
Yan, Zhengad48fd752009-11-12 09:33:58 +00003038 ret = -EAGAIN;
3039 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003040 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003041 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3042 goto err;
3043
3044 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3045 fi = btrfs_item_ptr(leaf, path->slots[0],
3046 struct btrfs_file_extent_item);
3047 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3048 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003049 }
3050
Chris Masonb9473432009-03-13 11:00:37 -04003051 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003052 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003053 BUG_ON(ret);
3054
Yan, Zhengad48fd752009-11-12 09:33:58 +00003055 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003056 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003057 return 0;
3058err:
3059 path->keep_locks = 0;
3060 return ret;
3061}
3062
3063static noinline int split_item(struct btrfs_trans_handle *trans,
3064 struct btrfs_root *root,
3065 struct btrfs_path *path,
3066 struct btrfs_key *new_key,
3067 unsigned long split_offset)
3068{
3069 struct extent_buffer *leaf;
3070 struct btrfs_item *item;
3071 struct btrfs_item *new_item;
3072 int slot;
3073 char *buf;
3074 u32 nritems;
3075 u32 item_size;
3076 u32 orig_offset;
3077 struct btrfs_disk_key disk_key;
3078
Chris Masonb9473432009-03-13 11:00:37 -04003079 leaf = path->nodes[0];
3080 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3081
Chris Masonb4ce94d2009-02-04 09:25:08 -05003082 btrfs_set_path_blocking(path);
3083
Chris Mason459931e2008-12-10 09:10:46 -05003084 item = btrfs_item_nr(leaf, path->slots[0]);
3085 orig_offset = btrfs_item_offset(leaf, item);
3086 item_size = btrfs_item_size(leaf, item);
3087
Chris Mason459931e2008-12-10 09:10:46 -05003088 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003089 if (!buf)
3090 return -ENOMEM;
3091
Chris Mason459931e2008-12-10 09:10:46 -05003092 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3093 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003094
Chris Mason459931e2008-12-10 09:10:46 -05003095 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003096 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003097 if (slot != nritems) {
3098 /* shift the items */
3099 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003100 btrfs_item_nr_offset(slot),
3101 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003102 }
3103
3104 btrfs_cpu_key_to_disk(&disk_key, new_key);
3105 btrfs_set_item_key(leaf, &disk_key, slot);
3106
3107 new_item = btrfs_item_nr(leaf, slot);
3108
3109 btrfs_set_item_offset(leaf, new_item, orig_offset);
3110 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3111
3112 btrfs_set_item_offset(leaf, item,
3113 orig_offset + item_size - split_offset);
3114 btrfs_set_item_size(leaf, item, split_offset);
3115
3116 btrfs_set_header_nritems(leaf, nritems + 1);
3117
3118 /* write the data for the start of the original item */
3119 write_extent_buffer(leaf, buf,
3120 btrfs_item_ptr_offset(leaf, path->slots[0]),
3121 split_offset);
3122
3123 /* write the data for the new item */
3124 write_extent_buffer(leaf, buf + split_offset,
3125 btrfs_item_ptr_offset(leaf, slot),
3126 item_size - split_offset);
3127 btrfs_mark_buffer_dirty(leaf);
3128
Yan, Zhengad48fd752009-11-12 09:33:58 +00003129 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003130 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003131 return 0;
3132}
3133
3134/*
3135 * This function splits a single item into two items,
3136 * giving 'new_key' to the new item and splitting the
3137 * old one at split_offset (from the start of the item).
3138 *
3139 * The path may be released by this operation. After
3140 * the split, the path is pointing to the old item. The
3141 * new item is going to be in the same node as the old one.
3142 *
3143 * Note, the item being split must be smaller enough to live alone on
3144 * a tree block with room for one extra struct btrfs_item
3145 *
3146 * This allows us to split the item in place, keeping a lock on the
3147 * leaf the entire time.
3148 */
3149int btrfs_split_item(struct btrfs_trans_handle *trans,
3150 struct btrfs_root *root,
3151 struct btrfs_path *path,
3152 struct btrfs_key *new_key,
3153 unsigned long split_offset)
3154{
3155 int ret;
3156 ret = setup_leaf_for_split(trans, root, path,
3157 sizeof(struct btrfs_item));
3158 if (ret)
3159 return ret;
3160
3161 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003162 return ret;
3163}
3164
3165/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003166 * This function duplicate a item, giving 'new_key' to the new item.
3167 * It guarantees both items live in the same tree leaf and the new item
3168 * is contiguous with the original item.
3169 *
3170 * This allows us to split file extent in place, keeping a lock on the
3171 * leaf the entire time.
3172 */
3173int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root,
3175 struct btrfs_path *path,
3176 struct btrfs_key *new_key)
3177{
3178 struct extent_buffer *leaf;
3179 int ret;
3180 u32 item_size;
3181
3182 leaf = path->nodes[0];
3183 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3184 ret = setup_leaf_for_split(trans, root, path,
3185 item_size + sizeof(struct btrfs_item));
3186 if (ret)
3187 return ret;
3188
3189 path->slots[0]++;
3190 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3191 item_size, item_size +
3192 sizeof(struct btrfs_item), 1);
3193 BUG_ON(ret);
3194
3195 leaf = path->nodes[0];
3196 memcpy_extent_buffer(leaf,
3197 btrfs_item_ptr_offset(leaf, path->slots[0]),
3198 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3199 item_size);
3200 return 0;
3201}
3202
3203/*
Chris Masond352ac62008-09-29 15:18:18 -04003204 * make the item pointed to by the path smaller. new_size indicates
3205 * how small to make it, and from_end tells us if we just chop bytes
3206 * off the end of the item or if we shift the item to chop bytes off
3207 * the front.
3208 */
Chris Masonb18c6682007-04-17 13:26:50 -04003209int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3210 struct btrfs_root *root,
3211 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003212 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003213{
3214 int ret = 0;
3215 int slot;
3216 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003217 struct extent_buffer *leaf;
3218 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003219 u32 nritems;
3220 unsigned int data_end;
3221 unsigned int old_data_start;
3222 unsigned int old_size;
3223 unsigned int size_diff;
3224 int i;
3225
3226 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003227 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003228 slot = path->slots[0];
3229
3230 old_size = btrfs_item_size_nr(leaf, slot);
3231 if (old_size == new_size)
3232 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003233
Chris Mason5f39d392007-10-15 16:14:19 -04003234 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003235 data_end = leaf_data_end(root, leaf);
3236
Chris Mason5f39d392007-10-15 16:14:19 -04003237 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003238
Chris Masonb18c6682007-04-17 13:26:50 -04003239 size_diff = old_size - new_size;
3240
3241 BUG_ON(slot < 0);
3242 BUG_ON(slot >= nritems);
3243
3244 /*
3245 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3246 */
3247 /* first correct the data pointers */
3248 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003249 u32 ioff;
3250 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003251
3252 if (!leaf->map_token) {
3253 map_extent_buffer(leaf, (unsigned long)item,
3254 sizeof(struct btrfs_item),
3255 &leaf->map_token, &leaf->kaddr,
3256 &leaf->map_start, &leaf->map_len,
3257 KM_USER1);
3258 }
3259
Chris Mason5f39d392007-10-15 16:14:19 -04003260 ioff = btrfs_item_offset(leaf, item);
3261 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003262 }
Chris Masondb945352007-10-15 16:15:53 -04003263
3264 if (leaf->map_token) {
3265 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3266 leaf->map_token = NULL;
3267 }
3268
Chris Masonb18c6682007-04-17 13:26:50 -04003269 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003270 if (from_end) {
3271 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3272 data_end + size_diff, btrfs_leaf_data(leaf) +
3273 data_end, old_data_start + new_size - data_end);
3274 } else {
3275 struct btrfs_disk_key disk_key;
3276 u64 offset;
3277
3278 btrfs_item_key(leaf, &disk_key, slot);
3279
3280 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3281 unsigned long ptr;
3282 struct btrfs_file_extent_item *fi;
3283
3284 fi = btrfs_item_ptr(leaf, slot,
3285 struct btrfs_file_extent_item);
3286 fi = (struct btrfs_file_extent_item *)(
3287 (unsigned long)fi - size_diff);
3288
3289 if (btrfs_file_extent_type(leaf, fi) ==
3290 BTRFS_FILE_EXTENT_INLINE) {
3291 ptr = btrfs_item_ptr_offset(leaf, slot);
3292 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003293 (unsigned long)fi,
3294 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003295 disk_bytenr));
3296 }
3297 }
3298
3299 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3300 data_end + size_diff, btrfs_leaf_data(leaf) +
3301 data_end, old_data_start - data_end);
3302
3303 offset = btrfs_disk_key_offset(&disk_key);
3304 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3305 btrfs_set_item_key(leaf, &disk_key, slot);
3306 if (slot == 0)
3307 fixup_low_keys(trans, root, path, &disk_key, 1);
3308 }
Chris Mason5f39d392007-10-15 16:14:19 -04003309
3310 item = btrfs_item_nr(leaf, slot);
3311 btrfs_set_item_size(leaf, item, new_size);
3312 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003313
3314 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003315 if (btrfs_leaf_free_space(root, leaf) < 0) {
3316 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003317 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003318 }
Chris Masonb18c6682007-04-17 13:26:50 -04003319 return ret;
3320}
3321
Chris Masond352ac62008-09-29 15:18:18 -04003322/*
3323 * make the item pointed to by the path bigger, data_size is the new size.
3324 */
Chris Mason5f39d392007-10-15 16:14:19 -04003325int btrfs_extend_item(struct btrfs_trans_handle *trans,
3326 struct btrfs_root *root, struct btrfs_path *path,
3327 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003328{
3329 int ret = 0;
3330 int slot;
3331 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003332 struct extent_buffer *leaf;
3333 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003334 u32 nritems;
3335 unsigned int data_end;
3336 unsigned int old_data;
3337 unsigned int old_size;
3338 int i;
3339
3340 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003341 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003342
Chris Mason5f39d392007-10-15 16:14:19 -04003343 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003344 data_end = leaf_data_end(root, leaf);
3345
Chris Mason5f39d392007-10-15 16:14:19 -04003346 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3347 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003348 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003349 }
Chris Mason6567e832007-04-16 09:22:45 -04003350 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003351 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003352
3353 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003354 if (slot >= nritems) {
3355 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003356 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3357 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003358 BUG_ON(1);
3359 }
Chris Mason6567e832007-04-16 09:22:45 -04003360
3361 /*
3362 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3363 */
3364 /* first correct the data pointers */
3365 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003366 u32 ioff;
3367 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003368
3369 if (!leaf->map_token) {
3370 map_extent_buffer(leaf, (unsigned long)item,
3371 sizeof(struct btrfs_item),
3372 &leaf->map_token, &leaf->kaddr,
3373 &leaf->map_start, &leaf->map_len,
3374 KM_USER1);
3375 }
Chris Mason5f39d392007-10-15 16:14:19 -04003376 ioff = btrfs_item_offset(leaf, item);
3377 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003378 }
Chris Mason5f39d392007-10-15 16:14:19 -04003379
Chris Masondb945352007-10-15 16:15:53 -04003380 if (leaf->map_token) {
3381 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3382 leaf->map_token = NULL;
3383 }
3384
Chris Mason6567e832007-04-16 09:22:45 -04003385 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003386 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003387 data_end - data_size, btrfs_leaf_data(leaf) +
3388 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003389
Chris Mason6567e832007-04-16 09:22:45 -04003390 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003391 old_size = btrfs_item_size_nr(leaf, slot);
3392 item = btrfs_item_nr(leaf, slot);
3393 btrfs_set_item_size(leaf, item, old_size + data_size);
3394 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003395
3396 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003397 if (btrfs_leaf_free_space(root, leaf) < 0) {
3398 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003399 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003400 }
Chris Mason6567e832007-04-16 09:22:45 -04003401 return ret;
3402}
3403
Chris Mason74123bd2007-02-02 11:05:29 -05003404/*
Chris Masond352ac62008-09-29 15:18:18 -04003405 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003406 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003407 * Returns the number of keys that were inserted.
3408 */
3409int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3410 struct btrfs_root *root,
3411 struct btrfs_path *path,
3412 struct btrfs_key *cpu_key, u32 *data_size,
3413 int nr)
3414{
3415 struct extent_buffer *leaf;
3416 struct btrfs_item *item;
3417 int ret = 0;
3418 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003419 int i;
3420 u32 nritems;
3421 u32 total_data = 0;
3422 u32 total_size = 0;
3423 unsigned int data_end;
3424 struct btrfs_disk_key disk_key;
3425 struct btrfs_key found_key;
3426
Yan Zheng87b29b22008-12-17 10:21:48 -05003427 for (i = 0; i < nr; i++) {
3428 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3429 BTRFS_LEAF_DATA_SIZE(root)) {
3430 break;
3431 nr = i;
3432 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003433 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003434 total_size += data_size[i] + sizeof(struct btrfs_item);
3435 }
3436 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003437
Josef Bacikf3465ca2008-11-12 14:19:50 -05003438 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3439 if (ret == 0)
3440 return -EEXIST;
3441 if (ret < 0)
3442 goto out;
3443
Josef Bacikf3465ca2008-11-12 14:19:50 -05003444 leaf = path->nodes[0];
3445
3446 nritems = btrfs_header_nritems(leaf);
3447 data_end = leaf_data_end(root, leaf);
3448
3449 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3450 for (i = nr; i >= 0; i--) {
3451 total_data -= data_size[i];
3452 total_size -= data_size[i] + sizeof(struct btrfs_item);
3453 if (total_size < btrfs_leaf_free_space(root, leaf))
3454 break;
3455 }
3456 nr = i;
3457 }
3458
3459 slot = path->slots[0];
3460 BUG_ON(slot < 0);
3461
3462 if (slot != nritems) {
3463 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3464
3465 item = btrfs_item_nr(leaf, slot);
3466 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3467
3468 /* figure out how many keys we can insert in here */
3469 total_data = data_size[0];
3470 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003471 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003472 break;
3473 total_data += data_size[i];
3474 }
3475 nr = i;
3476
3477 if (old_data < data_end) {
3478 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003479 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003480 slot, old_data, data_end);
3481 BUG_ON(1);
3482 }
3483 /*
3484 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3485 */
3486 /* first correct the data pointers */
3487 WARN_ON(leaf->map_token);
3488 for (i = slot; i < nritems; i++) {
3489 u32 ioff;
3490
3491 item = btrfs_item_nr(leaf, i);
3492 if (!leaf->map_token) {
3493 map_extent_buffer(leaf, (unsigned long)item,
3494 sizeof(struct btrfs_item),
3495 &leaf->map_token, &leaf->kaddr,
3496 &leaf->map_start, &leaf->map_len,
3497 KM_USER1);
3498 }
3499
3500 ioff = btrfs_item_offset(leaf, item);
3501 btrfs_set_item_offset(leaf, item, ioff - total_data);
3502 }
3503 if (leaf->map_token) {
3504 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3505 leaf->map_token = NULL;
3506 }
3507
3508 /* shift the items */
3509 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3510 btrfs_item_nr_offset(slot),
3511 (nritems - slot) * sizeof(struct btrfs_item));
3512
3513 /* shift the data */
3514 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3515 data_end - total_data, btrfs_leaf_data(leaf) +
3516 data_end, old_data - data_end);
3517 data_end = old_data;
3518 } else {
3519 /*
3520 * this sucks but it has to be done, if we are inserting at
3521 * the end of the leaf only insert 1 of the items, since we
3522 * have no way of knowing whats on the next leaf and we'd have
3523 * to drop our current locks to figure it out
3524 */
3525 nr = 1;
3526 }
3527
3528 /* setup the item for the new data */
3529 for (i = 0; i < nr; i++) {
3530 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3531 btrfs_set_item_key(leaf, &disk_key, slot + i);
3532 item = btrfs_item_nr(leaf, slot + i);
3533 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3534 data_end -= data_size[i];
3535 btrfs_set_item_size(leaf, item, data_size[i]);
3536 }
3537 btrfs_set_header_nritems(leaf, nritems + nr);
3538 btrfs_mark_buffer_dirty(leaf);
3539
3540 ret = 0;
3541 if (slot == 0) {
3542 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3543 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3544 }
3545
3546 if (btrfs_leaf_free_space(root, leaf) < 0) {
3547 btrfs_print_leaf(root, leaf);
3548 BUG();
3549 }
3550out:
3551 if (!ret)
3552 ret = nr;
3553 return ret;
3554}
3555
3556/*
Chris Mason44871b12009-03-13 10:04:31 -04003557 * this is a helper for btrfs_insert_empty_items, the main goal here is
3558 * to save stack depth by doing the bulk of the work in a function
3559 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003560 */
Chris Mason44871b12009-03-13 10:04:31 -04003561static noinline_for_stack int
3562setup_items_for_insert(struct btrfs_trans_handle *trans,
3563 struct btrfs_root *root, struct btrfs_path *path,
3564 struct btrfs_key *cpu_key, u32 *data_size,
3565 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003566{
Chris Mason5f39d392007-10-15 16:14:19 -04003567 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003568 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003569 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003570 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003571 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003572 int ret;
3573 struct extent_buffer *leaf;
3574 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003575
Chris Mason5f39d392007-10-15 16:14:19 -04003576 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003577 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003578
Chris Mason5f39d392007-10-15 16:14:19 -04003579 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003580 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003581
Chris Masonf25956c2008-09-12 15:32:53 -04003582 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003583 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003584 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003585 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003586 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003587 }
Chris Mason5f39d392007-10-15 16:14:19 -04003588
Chris Masonbe0e5c02007-01-26 15:51:26 -05003589 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003590 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003591
Chris Mason5f39d392007-10-15 16:14:19 -04003592 if (old_data < data_end) {
3593 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003594 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003595 slot, old_data, data_end);
3596 BUG_ON(1);
3597 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003598 /*
3599 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3600 */
3601 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003602 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003603 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003604 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003605
Chris Mason5f39d392007-10-15 16:14:19 -04003606 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003607 if (!leaf->map_token) {
3608 map_extent_buffer(leaf, (unsigned long)item,
3609 sizeof(struct btrfs_item),
3610 &leaf->map_token, &leaf->kaddr,
3611 &leaf->map_start, &leaf->map_len,
3612 KM_USER1);
3613 }
3614
Chris Mason5f39d392007-10-15 16:14:19 -04003615 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003616 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003617 }
Chris Masondb945352007-10-15 16:15:53 -04003618 if (leaf->map_token) {
3619 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3620 leaf->map_token = NULL;
3621 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003622
3623 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003624 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003625 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003626 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003627
3628 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003629 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003630 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003631 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632 data_end = old_data;
3633 }
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Chris Mason62e27492007-03-15 12:56:47 -04003635 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003636 for (i = 0; i < nr; i++) {
3637 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3638 btrfs_set_item_key(leaf, &disk_key, slot + i);
3639 item = btrfs_item_nr(leaf, slot + i);
3640 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3641 data_end -= data_size[i];
3642 btrfs_set_item_size(leaf, item, data_size[i]);
3643 }
Chris Mason44871b12009-03-13 10:04:31 -04003644
Chris Mason9c583092008-01-29 15:15:18 -05003645 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003646
3647 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003648 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003649 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003650 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003651 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003652 }
Chris Masonb9473432009-03-13 11:00:37 -04003653 btrfs_unlock_up_safe(path, 1);
3654 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003655
Chris Mason5f39d392007-10-15 16:14:19 -04003656 if (btrfs_leaf_free_space(root, leaf) < 0) {
3657 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003658 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003659 }
Chris Mason44871b12009-03-13 10:04:31 -04003660 return ret;
3661}
3662
3663/*
3664 * Given a key and some data, insert items into the tree.
3665 * This does all the path init required, making room in the tree if needed.
3666 */
3667int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3668 struct btrfs_root *root,
3669 struct btrfs_path *path,
3670 struct btrfs_key *cpu_key, u32 *data_size,
3671 int nr)
3672{
3673 struct extent_buffer *leaf;
3674 int ret = 0;
3675 int slot;
3676 int i;
3677 u32 total_size = 0;
3678 u32 total_data = 0;
3679
3680 for (i = 0; i < nr; i++)
3681 total_data += data_size[i];
3682
3683 total_size = total_data + (nr * sizeof(struct btrfs_item));
3684 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3685 if (ret == 0)
3686 return -EEXIST;
3687 if (ret < 0)
3688 goto out;
3689
3690 leaf = path->nodes[0];
3691 slot = path->slots[0];
3692 BUG_ON(slot < 0);
3693
3694 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3695 total_data, total_size, nr);
3696
Chris Masoned2ff2c2007-03-01 18:59:40 -05003697out:
Chris Mason62e27492007-03-15 12:56:47 -04003698 return ret;
3699}
3700
3701/*
3702 * Given a key and some data, insert an item into the tree.
3703 * This does all the path init required, making room in the tree if needed.
3704 */
Chris Masone089f052007-03-16 16:20:31 -04003705int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3706 *root, struct btrfs_key *cpu_key, void *data, u32
3707 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003708{
3709 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003710 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003711 struct extent_buffer *leaf;
3712 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003713
Chris Mason2c90e5d2007-04-02 10:50:19 -04003714 path = btrfs_alloc_path();
3715 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003716 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003717 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003718 leaf = path->nodes[0];
3719 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3720 write_extent_buffer(leaf, data, ptr, data_size);
3721 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003722 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003723 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003724 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003725}
3726
Chris Mason74123bd2007-02-02 11:05:29 -05003727/*
Chris Mason5de08d72007-02-24 06:24:44 -05003728 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003729 *
Chris Masond352ac62008-09-29 15:18:18 -04003730 * the tree should have been previously balanced so the deletion does not
3731 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003732 */
Chris Masone089f052007-03-16 16:20:31 -04003733static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3734 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003735{
Chris Mason5f39d392007-10-15 16:14:19 -04003736 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003737 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003738 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003739 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003740
Chris Mason5f39d392007-10-15 16:14:19 -04003741 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003742 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003743 memmove_extent_buffer(parent,
3744 btrfs_node_key_ptr_offset(slot),
3745 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003746 sizeof(struct btrfs_key_ptr) *
3747 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003748 }
Chris Mason7518a232007-03-12 12:01:18 -04003749 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003750 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003751 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003752 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003753 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003754 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003755 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003756 struct btrfs_disk_key disk_key;
3757
3758 btrfs_node_key(parent, &disk_key, 0);
3759 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003760 if (wret)
3761 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003762 }
Chris Masond6025572007-03-30 14:27:56 -04003763 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003764 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003765}
3766
Chris Mason74123bd2007-02-02 11:05:29 -05003767/*
Chris Mason323ac952008-10-01 19:05:46 -04003768 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003769 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003770 *
3771 * This deletes the pointer in path->nodes[1] and frees the leaf
3772 * block extent. zero is returned if it all worked out, < 0 otherwise.
3773 *
3774 * The path must have already been setup for deleting the leaf, including
3775 * all the proper balancing. path->nodes[1] must be locked.
3776 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003777static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3778 struct btrfs_root *root,
3779 struct btrfs_path *path,
3780 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003781{
3782 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003783
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003784 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003785 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3786 if (ret)
3787 return ret;
3788
Chris Mason4d081c42009-02-04 09:31:28 -05003789 /*
3790 * btrfs_free_extent is expensive, we want to make sure we
3791 * aren't holding any locks when we call it
3792 */
3793 btrfs_unlock_up_safe(path, 0);
3794
Yan, Zheng86b9f2e2009-11-12 09:36:50 +00003795 ret = btrfs_free_tree_block(trans, root, leaf->start, leaf->len,
3796 0, root->root_key.objectid, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003797 return ret;
3798}
3799/*
Chris Mason74123bd2007-02-02 11:05:29 -05003800 * delete the item at the leaf level in path. If that empties
3801 * the leaf, remove it from the tree
3802 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003803int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3804 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003805{
Chris Mason5f39d392007-10-15 16:14:19 -04003806 struct extent_buffer *leaf;
3807 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003808 int last_off;
3809 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003810 int ret = 0;
3811 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003812 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003813 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003814
Chris Mason5f39d392007-10-15 16:14:19 -04003815 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003816 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3817
3818 for (i = 0; i < nr; i++)
3819 dsize += btrfs_item_size_nr(leaf, slot + i);
3820
Chris Mason5f39d392007-10-15 16:14:19 -04003821 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003822
Chris Mason85e21ba2008-01-29 15:11:36 -05003823 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003824 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003825
3826 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003827 data_end + dsize,
3828 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003829 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003830
Chris Mason85e21ba2008-01-29 15:11:36 -05003831 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003832 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003833
Chris Mason5f39d392007-10-15 16:14:19 -04003834 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003835 if (!leaf->map_token) {
3836 map_extent_buffer(leaf, (unsigned long)item,
3837 sizeof(struct btrfs_item),
3838 &leaf->map_token, &leaf->kaddr,
3839 &leaf->map_start, &leaf->map_len,
3840 KM_USER1);
3841 }
Chris Mason5f39d392007-10-15 16:14:19 -04003842 ioff = btrfs_item_offset(leaf, item);
3843 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003844 }
Chris Masondb945352007-10-15 16:15:53 -04003845
3846 if (leaf->map_token) {
3847 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3848 leaf->map_token = NULL;
3849 }
3850
Chris Mason5f39d392007-10-15 16:14:19 -04003851 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003852 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003853 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003854 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003856 btrfs_set_header_nritems(leaf, nritems - nr);
3857 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003858
Chris Mason74123bd2007-02-02 11:05:29 -05003859 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003860 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003861 if (leaf == root->node) {
3862 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003863 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003864 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003865 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003866 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003867 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003868 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003869 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003870 struct btrfs_disk_key disk_key;
3871
3872 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003873 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003874 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003875 if (wret)
3876 ret = wret;
3877 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003878
Chris Mason74123bd2007-02-02 11:05:29 -05003879 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003880 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003881 /* push_leaf_left fixes the path.
3882 * make sure the path still points to our leaf
3883 * for possible call to del_ptr below
3884 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003885 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003886 extent_buffer_get(leaf);
3887
Chris Masonb9473432009-03-13 11:00:37 -04003888 btrfs_set_path_blocking(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003889 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003890 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003891 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003892
3893 if (path->nodes[0] == leaf &&
3894 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003895 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003896 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003897 ret = wret;
3898 }
Chris Mason5f39d392007-10-15 16:14:19 -04003899
3900 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003901 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003902 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003903 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003904 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003905 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003906 /* if we're still in the path, make sure
3907 * we're dirty. Otherwise, one of the
3908 * push_leaf functions must have already
3909 * dirtied this buffer
3910 */
3911 if (path->nodes[0] == leaf)
3912 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003913 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003914 }
Chris Masond5719762007-03-23 10:01:08 -04003915 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003916 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003917 }
3918 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003919 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003920}
3921
Chris Mason97571fd2007-02-24 13:39:08 -05003922/*
Chris Mason925baed2008-06-25 16:01:30 -04003923 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003924 * returns 0 if it found something or 1 if there are no lesser leaves.
3925 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003926 *
3927 * This may release the path, and so you may lose any locks held at the
3928 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003929 */
3930int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3931{
Chris Mason925baed2008-06-25 16:01:30 -04003932 struct btrfs_key key;
3933 struct btrfs_disk_key found_key;
3934 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003935
Chris Mason925baed2008-06-25 16:01:30 -04003936 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003937
Chris Mason925baed2008-06-25 16:01:30 -04003938 if (key.offset > 0)
3939 key.offset--;
3940 else if (key.type > 0)
3941 key.type--;
3942 else if (key.objectid > 0)
3943 key.objectid--;
3944 else
3945 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003946
Chris Mason925baed2008-06-25 16:01:30 -04003947 btrfs_release_path(root, path);
3948 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3949 if (ret < 0)
3950 return ret;
3951 btrfs_item_key(path->nodes[0], &found_key, 0);
3952 ret = comp_keys(&found_key, &key);
3953 if (ret < 0)
3954 return 0;
3955 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003956}
3957
Chris Mason3f157a22008-06-25 16:01:31 -04003958/*
3959 * A helper function to walk down the tree starting at min_key, and looking
3960 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003961 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003962 *
3963 * This does not cow, but it does stuff the starting key it finds back
3964 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3965 * key and get a writable path.
3966 *
3967 * This does lock as it descends, and path->keep_locks should be set
3968 * to 1 by the caller.
3969 *
3970 * This honors path->lowest_level to prevent descent past a given level
3971 * of the tree.
3972 *
Chris Masond352ac62008-09-29 15:18:18 -04003973 * min_trans indicates the oldest transaction that you are interested
3974 * in walking through. Any nodes or leaves older than min_trans are
3975 * skipped over (without reading them).
3976 *
Chris Mason3f157a22008-06-25 16:01:31 -04003977 * returns zero if something useful was found, < 0 on error and 1 if there
3978 * was nothing in the tree that matched the search criteria.
3979 */
3980int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003981 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003982 struct btrfs_path *path, int cache_only,
3983 u64 min_trans)
3984{
3985 struct extent_buffer *cur;
3986 struct btrfs_key found_key;
3987 int slot;
Yan96524802008-07-24 12:19:49 -04003988 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003989 u32 nritems;
3990 int level;
3991 int ret = 1;
3992
Chris Mason934d3752008-12-08 16:43:10 -05003993 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003994again:
3995 cur = btrfs_lock_root_node(root);
3996 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003997 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003998 path->nodes[level] = cur;
3999 path->locks[level] = 1;
4000
4001 if (btrfs_header_generation(cur) < min_trans) {
4002 ret = 1;
4003 goto out;
4004 }
Chris Masond3977122009-01-05 21:25:51 -05004005 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004006 nritems = btrfs_header_nritems(cur);
4007 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004008 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004009
Chris Mason323ac952008-10-01 19:05:46 -04004010 /* at the lowest level, we're done, setup the path and exit */
4011 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004012 if (slot >= nritems)
4013 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004014 ret = 0;
4015 path->slots[level] = slot;
4016 btrfs_item_key_to_cpu(cur, &found_key, slot);
4017 goto out;
4018 }
Yan96524802008-07-24 12:19:49 -04004019 if (sret && slot > 0)
4020 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004021 /*
4022 * check this node pointer against the cache_only and
4023 * min_trans parameters. If it isn't in cache or is too
4024 * old, skip to the next one.
4025 */
Chris Masond3977122009-01-05 21:25:51 -05004026 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004027 u64 blockptr;
4028 u64 gen;
4029 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004030 struct btrfs_disk_key disk_key;
4031
Chris Mason3f157a22008-06-25 16:01:31 -04004032 blockptr = btrfs_node_blockptr(cur, slot);
4033 gen = btrfs_node_ptr_generation(cur, slot);
4034 if (gen < min_trans) {
4035 slot++;
4036 continue;
4037 }
4038 if (!cache_only)
4039 break;
4040
Chris Masone02119d2008-09-05 16:13:11 -04004041 if (max_key) {
4042 btrfs_node_key(cur, &disk_key, slot);
4043 if (comp_keys(&disk_key, max_key) >= 0) {
4044 ret = 1;
4045 goto out;
4046 }
4047 }
4048
Chris Mason3f157a22008-06-25 16:01:31 -04004049 tmp = btrfs_find_tree_block(root, blockptr,
4050 btrfs_level_size(root, level - 1));
4051
4052 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4053 free_extent_buffer(tmp);
4054 break;
4055 }
4056 if (tmp)
4057 free_extent_buffer(tmp);
4058 slot++;
4059 }
Chris Masone02119d2008-09-05 16:13:11 -04004060find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004061 /*
4062 * we didn't find a candidate key in this node, walk forward
4063 * and find another one
4064 */
4065 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004066 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004067 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004068 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004069 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004070 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004071 btrfs_release_path(root, path);
4072 goto again;
4073 } else {
4074 goto out;
4075 }
4076 }
4077 /* save our key for returning back */
4078 btrfs_node_key_to_cpu(cur, &found_key, slot);
4079 path->slots[level] = slot;
4080 if (level == path->lowest_level) {
4081 ret = 0;
4082 unlock_up(path, level, 1);
4083 goto out;
4084 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004085 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004086 cur = read_node_slot(root, cur, slot);
4087
4088 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004089
Chris Mason3f157a22008-06-25 16:01:31 -04004090 path->locks[level - 1] = 1;
4091 path->nodes[level - 1] = cur;
4092 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004093 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004094 }
4095out:
4096 if (ret == 0)
4097 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004098 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004099 return ret;
4100}
4101
4102/*
4103 * this is similar to btrfs_next_leaf, but does not try to preserve
4104 * and fixup the path. It looks for and returns the next key in the
4105 * tree based on the current path and the cache_only and min_trans
4106 * parameters.
4107 *
4108 * 0 is returned if another key is found, < 0 if there are any errors
4109 * and 1 is returned if there are no higher keys in the tree
4110 *
4111 * path->keep_locks should be set to 1 on the search made before
4112 * calling this function.
4113 */
Chris Masone7a84562008-06-25 16:01:31 -04004114int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004115 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004116 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004117{
Chris Masone7a84562008-06-25 16:01:31 -04004118 int slot;
4119 struct extent_buffer *c;
4120
Chris Mason934d3752008-12-08 16:43:10 -05004121 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004122 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004123 if (!path->nodes[level])
4124 return 1;
4125
4126 slot = path->slots[level] + 1;
4127 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004128next:
Chris Masone7a84562008-06-25 16:01:31 -04004129 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004130 int ret;
4131 int orig_lowest;
4132 struct btrfs_key cur_key;
4133 if (level + 1 >= BTRFS_MAX_LEVEL ||
4134 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004135 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004136
4137 if (path->locks[level + 1]) {
4138 level++;
4139 continue;
4140 }
4141
4142 slot = btrfs_header_nritems(c) - 1;
4143 if (level == 0)
4144 btrfs_item_key_to_cpu(c, &cur_key, slot);
4145 else
4146 btrfs_node_key_to_cpu(c, &cur_key, slot);
4147
4148 orig_lowest = path->lowest_level;
4149 btrfs_release_path(root, path);
4150 path->lowest_level = level;
4151 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4152 0, 0);
4153 path->lowest_level = orig_lowest;
4154 if (ret < 0)
4155 return ret;
4156
4157 c = path->nodes[level];
4158 slot = path->slots[level];
4159 if (ret == 0)
4160 slot++;
4161 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004162 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004163
Chris Masone7a84562008-06-25 16:01:31 -04004164 if (level == 0)
4165 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004166 else {
4167 u64 blockptr = btrfs_node_blockptr(c, slot);
4168 u64 gen = btrfs_node_ptr_generation(c, slot);
4169
4170 if (cache_only) {
4171 struct extent_buffer *cur;
4172 cur = btrfs_find_tree_block(root, blockptr,
4173 btrfs_level_size(root, level - 1));
4174 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4175 slot++;
4176 if (cur)
4177 free_extent_buffer(cur);
4178 goto next;
4179 }
4180 free_extent_buffer(cur);
4181 }
4182 if (gen < min_trans) {
4183 slot++;
4184 goto next;
4185 }
Chris Masone7a84562008-06-25 16:01:31 -04004186 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004187 }
Chris Masone7a84562008-06-25 16:01:31 -04004188 return 0;
4189 }
4190 return 1;
4191}
4192
Chris Mason7bb86312007-12-11 09:25:06 -05004193/*
Chris Mason925baed2008-06-25 16:01:30 -04004194 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004195 * returns 0 if it found something or 1 if there are no greater leaves.
4196 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004197 */
Chris Mason234b63a2007-03-13 10:46:10 -04004198int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004199{
4200 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004201 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004202 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004203 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004204 struct btrfs_key key;
4205 u32 nritems;
4206 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004207 int old_spinning = path->leave_spinning;
4208 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004209
4210 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004211 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004212 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004213
Chris Mason8e73f272009-04-03 10:14:18 -04004214 /*
4215 * we take the blocks in an order that upsets lockdep. Using
4216 * blocking mode is the only way around it.
4217 */
4218#ifdef CONFIG_DEBUG_LOCK_ALLOC
4219 force_blocking = 1;
4220#endif
Chris Mason925baed2008-06-25 16:01:30 -04004221
Chris Mason8e73f272009-04-03 10:14:18 -04004222 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4223again:
4224 level = 1;
4225 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004226 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004227
Chris Masona2135012008-06-25 16:01:30 -04004228 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004229
4230 if (!force_blocking)
4231 path->leave_spinning = 1;
4232
Chris Mason925baed2008-06-25 16:01:30 -04004233 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4234 path->keep_locks = 0;
4235
4236 if (ret < 0)
4237 return ret;
4238
Chris Masona2135012008-06-25 16:01:30 -04004239 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004240 /*
4241 * by releasing the path above we dropped all our locks. A balance
4242 * could have added more items next to the key that used to be
4243 * at the very end of the block. So, check again here and
4244 * advance the path if there are now more items available.
4245 */
Chris Masona2135012008-06-25 16:01:30 -04004246 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004247 if (ret == 0)
4248 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004249 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004250 goto done;
4251 }
Chris Masond97e63b2007-02-20 16:40:44 -05004252
Chris Masond3977122009-01-05 21:25:51 -05004253 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004254 if (!path->nodes[level]) {
4255 ret = 1;
4256 goto done;
4257 }
Chris Mason5f39d392007-10-15 16:14:19 -04004258
Chris Masond97e63b2007-02-20 16:40:44 -05004259 slot = path->slots[level] + 1;
4260 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004261 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004262 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004263 if (level == BTRFS_MAX_LEVEL) {
4264 ret = 1;
4265 goto done;
4266 }
Chris Masond97e63b2007-02-20 16:40:44 -05004267 continue;
4268 }
Chris Mason5f39d392007-10-15 16:14:19 -04004269
Chris Mason925baed2008-06-25 16:01:30 -04004270 if (next) {
4271 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004272 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004273 }
Chris Mason5f39d392007-10-15 16:14:19 -04004274
Chris Mason8e73f272009-04-03 10:14:18 -04004275 next = c;
4276 ret = read_block_for_search(NULL, root, path, &next, level,
4277 slot, &key);
4278 if (ret == -EAGAIN)
4279 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004280
Chris Mason76a05b32009-05-14 13:24:30 -04004281 if (ret < 0) {
4282 btrfs_release_path(root, path);
4283 goto done;
4284 }
4285
Chris Mason5cd57b22008-06-25 16:01:30 -04004286 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004287 ret = btrfs_try_spin_lock(next);
4288 if (!ret) {
4289 btrfs_set_path_blocking(path);
4290 btrfs_tree_lock(next);
4291 if (!force_blocking)
4292 btrfs_clear_path_blocking(path, next);
4293 }
4294 if (force_blocking)
4295 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004296 }
Chris Masond97e63b2007-02-20 16:40:44 -05004297 break;
4298 }
4299 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004300 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004301 level--;
4302 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004303 if (path->locks[level])
4304 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004305
Chris Mason5f39d392007-10-15 16:14:19 -04004306 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004307 path->nodes[level] = next;
4308 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004309 if (!path->skip_locking)
4310 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004311
Chris Masond97e63b2007-02-20 16:40:44 -05004312 if (!level)
4313 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004314
Chris Mason8e73f272009-04-03 10:14:18 -04004315 ret = read_block_for_search(NULL, root, path, &next, level,
4316 0, &key);
4317 if (ret == -EAGAIN)
4318 goto again;
4319
Chris Mason76a05b32009-05-14 13:24:30 -04004320 if (ret < 0) {
4321 btrfs_release_path(root, path);
4322 goto done;
4323 }
4324
Chris Mason5cd57b22008-06-25 16:01:30 -04004325 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004326 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004327 ret = btrfs_try_spin_lock(next);
4328 if (!ret) {
4329 btrfs_set_path_blocking(path);
4330 btrfs_tree_lock(next);
4331 if (!force_blocking)
4332 btrfs_clear_path_blocking(path, next);
4333 }
4334 if (force_blocking)
4335 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004336 }
Chris Masond97e63b2007-02-20 16:40:44 -05004337 }
Chris Mason8e73f272009-04-03 10:14:18 -04004338 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004339done:
4340 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004341 path->leave_spinning = old_spinning;
4342 if (!old_spinning)
4343 btrfs_set_path_blocking(path);
4344
4345 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004346}
Chris Mason0b86a832008-03-24 15:01:56 -04004347
Chris Mason3f157a22008-06-25 16:01:31 -04004348/*
4349 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4350 * searching until it gets past min_objectid or finds an item of 'type'
4351 *
4352 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4353 */
Chris Mason0b86a832008-03-24 15:01:56 -04004354int btrfs_previous_item(struct btrfs_root *root,
4355 struct btrfs_path *path, u64 min_objectid,
4356 int type)
4357{
4358 struct btrfs_key found_key;
4359 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004360 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004361 int ret;
4362
Chris Masond3977122009-01-05 21:25:51 -05004363 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004364 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004365 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004366 ret = btrfs_prev_leaf(root, path);
4367 if (ret != 0)
4368 return ret;
4369 } else {
4370 path->slots[0]--;
4371 }
4372 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004373 nritems = btrfs_header_nritems(leaf);
4374 if (nritems == 0)
4375 return 1;
4376 if (path->slots[0] == nritems)
4377 path->slots[0]--;
4378
Chris Mason0b86a832008-03-24 15:01:56 -04004379 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004380 if (found_key.objectid < min_objectid)
4381 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004382 if (found_key.type == type)
4383 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004384 if (found_key.objectid == min_objectid &&
4385 found_key.type < type)
4386 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004387 }
4388 return 1;
4389}