blob: 9ac17159925819a399dc10413d5261dd67bb5584 [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;
Chris Masonbe20aa92007-12-17 20:14:01 -0500203 int ret = 0;
204 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400205 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500206
207 WARN_ON(root->ref_cows && trans->transid !=
208 root->fs_info->running_transaction->transid);
209 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
210
211 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400212 if (level == 0)
213 btrfs_item_key(buf, &disk_key, 0);
214 else
215 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400216
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400217 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
218 new_root_objectid, &disk_key, level,
219 buf->start, 0);
220 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 return PTR_ERR(cow);
222
223 copy_extent_buffer(cow, buf, 0, 0, cow->len);
224 btrfs_set_header_bytenr(cow, cow->start);
225 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400226 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
227 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
228 BTRFS_HEADER_FLAG_RELOC);
229 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
230 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
231 else
232 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500233
Yan Zheng2b820322008-11-17 21:11:30 -0500234 write_extent_buffer(cow, root->fs_info->fsid,
235 (unsigned long)btrfs_header_fsid(cow),
236 BTRFS_FSID_SIZE);
237
Chris Masonbe20aa92007-12-17 20:14:01 -0500238 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400239 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
240 ret = btrfs_inc_ref(trans, root, cow, 1);
241 else
242 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500243
Chris Masonbe20aa92007-12-17 20:14:01 -0500244 if (ret)
245 return ret;
246
247 btrfs_mark_buffer_dirty(cow);
248 *cow_ret = cow;
249 return 0;
250}
251
Chris Masond352ac62008-09-29 15:18:18 -0400252/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400253 * check if the tree block can be shared by multiple trees
254 */
255int btrfs_block_can_be_shared(struct btrfs_root *root,
256 struct extent_buffer *buf)
257{
258 /*
259 * Tree blocks not in refernece counted trees and tree roots
260 * are never shared. If a block was allocated after the last
261 * snapshot and the block was not allocated by tree relocation,
262 * we know the block is not shared.
263 */
264 if (root->ref_cows &&
265 buf != root->node && buf != root->commit_root &&
266 (btrfs_header_generation(buf) <=
267 btrfs_root_last_snapshot(&root->root_item) ||
268 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
269 return 1;
270#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
271 if (root->ref_cows &&
272 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
273 return 1;
274#endif
275 return 0;
276}
277
278static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
279 struct btrfs_root *root,
280 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400281 struct extent_buffer *cow,
282 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400283{
284 u64 refs;
285 u64 owner;
286 u64 flags;
287 u64 new_flags = 0;
288 int ret;
289
290 /*
291 * Backrefs update rules:
292 *
293 * Always use full backrefs for extent pointers in tree block
294 * allocated by tree relocation.
295 *
296 * If a shared tree block is no longer referenced by its owner
297 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
298 * use full backrefs for extent pointers in tree block.
299 *
300 * If a tree block is been relocating
301 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
302 * use full backrefs for extent pointers in tree block.
303 * The reason for this is some operations (such as drop tree)
304 * are only allowed for blocks use full backrefs.
305 */
306
307 if (btrfs_block_can_be_shared(root, buf)) {
308 ret = btrfs_lookup_extent_info(trans, root, buf->start,
309 buf->len, &refs, &flags);
310 BUG_ON(ret);
311 BUG_ON(refs == 0);
312 } else {
313 refs = 1;
314 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
315 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
316 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
317 else
318 flags = 0;
319 }
320
321 owner = btrfs_header_owner(buf);
322 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
323 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
324
325 if (refs > 1) {
326 if ((owner == root->root_key.objectid ||
327 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
328 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
329 ret = btrfs_inc_ref(trans, root, buf, 1);
330 BUG_ON(ret);
331
332 if (root->root_key.objectid ==
333 BTRFS_TREE_RELOC_OBJECTID) {
334 ret = btrfs_dec_ref(trans, root, buf, 0);
335 BUG_ON(ret);
336 ret = btrfs_inc_ref(trans, root, cow, 1);
337 BUG_ON(ret);
338 }
339 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
340 } else {
341
342 if (root->root_key.objectid ==
343 BTRFS_TREE_RELOC_OBJECTID)
344 ret = btrfs_inc_ref(trans, root, cow, 1);
345 else
346 ret = btrfs_inc_ref(trans, root, cow, 0);
347 BUG_ON(ret);
348 }
349 if (new_flags != 0) {
350 ret = btrfs_set_disk_extent_flags(trans, root,
351 buf->start,
352 buf->len,
353 new_flags, 0);
354 BUG_ON(ret);
355 }
356 } else {
357 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
358 if (root->root_key.objectid ==
359 BTRFS_TREE_RELOC_OBJECTID)
360 ret = btrfs_inc_ref(trans, root, cow, 1);
361 else
362 ret = btrfs_inc_ref(trans, root, cow, 0);
363 BUG_ON(ret);
364 ret = btrfs_dec_ref(trans, root, buf, 1);
365 BUG_ON(ret);
366 }
367 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400368 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400369 }
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;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400395 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400396 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400397 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400398
Chris Mason925baed2008-06-25 16:01:30 -0400399 if (*cow_ret == buf)
400 unlock_orig = 1;
401
Chris Masonb9447ef2009-03-09 11:45:38 -0400402 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400403
Chris Mason7bb86312007-12-11 09:25:06 -0500404 WARN_ON(root->ref_cows && trans->transid !=
405 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400406 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400407
Chris Mason7bb86312007-12-11 09:25:06 -0500408 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400409
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400410 if (level == 0)
411 btrfs_item_key(buf, &disk_key, 0);
412 else
413 btrfs_node_key(buf, &disk_key, 0);
414
415 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
416 if (parent)
417 parent_start = parent->start;
418 else
419 parent_start = 0;
420 } else
421 parent_start = 0;
422
423 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
424 root->root_key.objectid, &disk_key,
425 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400426 if (IS_ERR(cow))
427 return PTR_ERR(cow);
428
Chris Masonb4ce94d2009-02-04 09:25:08 -0500429 /* cow is set to blocking by btrfs_init_new_buffer */
430
Chris Mason5f39d392007-10-15 16:14:19 -0400431 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400432 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400433 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400434 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
435 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
436 BTRFS_HEADER_FLAG_RELOC);
437 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
438 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
439 else
440 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400441
Yan Zheng2b820322008-11-17 21:11:30 -0500442 write_extent_buffer(cow, root->fs_info->fsid,
443 (unsigned long)btrfs_header_fsid(cow),
444 BTRFS_FSID_SIZE);
445
Yan, Zhengf0486c62010-05-16 10:46:25 -0400446 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400447
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400448 if (root->ref_cows)
449 btrfs_reloc_cow_block(trans, root, buf, cow);
450
Chris Mason6702ed42007-08-07 16:15:09 -0400451 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400452 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400453 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
454 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
455 parent_start = buf->start;
456 else
457 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400458
459 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400460 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400461 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400462 spin_unlock(&root->node_lock);
463
Yan, Zhengf0486c62010-05-16 10:46:25 -0400464 btrfs_free_tree_block(trans, root, buf, parent_start,
465 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400466 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400467 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400468 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400469 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
470 parent_start = parent->start;
471 else
472 parent_start = 0;
473
474 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400475 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400476 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500477 btrfs_set_node_ptr_generation(parent, parent_slot,
478 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400479 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400480 btrfs_free_tree_block(trans, root, buf, parent_start,
481 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400482 }
Chris Mason925baed2008-06-25 16:01:30 -0400483 if (unlock_orig)
484 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400485 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400486 btrfs_mark_buffer_dirty(cow);
487 *cow_ret = cow;
488 return 0;
489}
490
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400491static inline int should_cow_block(struct btrfs_trans_handle *trans,
492 struct btrfs_root *root,
493 struct extent_buffer *buf)
494{
495 if (btrfs_header_generation(buf) == trans->transid &&
496 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
497 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
498 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
499 return 0;
500 return 1;
501}
502
Chris Masond352ac62008-09-29 15:18:18 -0400503/*
504 * cows a single block, see __btrfs_cow_block for the real work.
505 * This version of it has extra checks so that a block isn't cow'd more than
506 * once per transaction, as long as it hasn't been written yet
507 */
Chris Masond3977122009-01-05 21:25:51 -0500508noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400509 struct btrfs_root *root, struct extent_buffer *buf,
510 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400511 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500512{
Chris Mason6702ed42007-08-07 16:15:09 -0400513 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400514 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500515
Chris Masonccd467d2007-06-28 15:57:36 -0400516 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500517 printk(KERN_CRIT "trans %llu running %llu\n",
518 (unsigned long long)trans->transid,
519 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400520 root->fs_info->running_transaction->transid);
521 WARN_ON(1);
522 }
523 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500524 printk(KERN_CRIT "trans %llu running %llu\n",
525 (unsigned long long)trans->transid,
526 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400527 WARN_ON(1);
528 }
Chris Masondc17ff82008-01-08 15:46:30 -0500529
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400530 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500531 *cow_ret = buf;
532 return 0;
533 }
Chris Masonc4876852009-02-04 09:24:25 -0500534
Chris Mason0b86a832008-03-24 15:01:56 -0400535 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500536
537 if (parent)
538 btrfs_set_lock_blocking(parent);
539 btrfs_set_lock_blocking(buf);
540
Chris Masonf510cfe2007-10-15 16:14:48 -0400541 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400542 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400543 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400544}
545
Chris Masond352ac62008-09-29 15:18:18 -0400546/*
547 * helper function for defrag to decide if two blocks pointed to by a
548 * node are actually close by
549 */
Chris Mason6b800532007-10-15 16:17:34 -0400550static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400551{
Chris Mason6b800532007-10-15 16:17:34 -0400552 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400553 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400554 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400555 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500556 return 0;
557}
558
Chris Mason081e9572007-11-06 10:26:24 -0500559/*
560 * compare two keys in a memcmp fashion
561 */
562static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
563{
564 struct btrfs_key k1;
565
566 btrfs_disk_key_to_cpu(&k1, disk);
567
Diego Calleja20736ab2009-07-24 11:06:52 -0400568 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500569}
570
Josef Bacikf3465ca2008-11-12 14:19:50 -0500571/*
572 * same as comp_keys only with two btrfs_key's
573 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400574int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500575{
576 if (k1->objectid > k2->objectid)
577 return 1;
578 if (k1->objectid < k2->objectid)
579 return -1;
580 if (k1->type > k2->type)
581 return 1;
582 if (k1->type < k2->type)
583 return -1;
584 if (k1->offset > k2->offset)
585 return 1;
586 if (k1->offset < k2->offset)
587 return -1;
588 return 0;
589}
Chris Mason081e9572007-11-06 10:26:24 -0500590
Chris Masond352ac62008-09-29 15:18:18 -0400591/*
592 * this is used by the defrag code to go through all the
593 * leaves pointed to by a node and reallocate them so that
594 * disk order is close to key order
595 */
Chris Mason6702ed42007-08-07 16:15:09 -0400596int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400597 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400598 int start_slot, int cache_only, u64 *last_ret,
599 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400600{
Chris Mason6b800532007-10-15 16:17:34 -0400601 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400602 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400603 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400604 u64 search_start = *last_ret;
605 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400606 u64 other;
607 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400608 int end_slot;
609 int i;
610 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400611 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400612 int uptodate;
613 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500614 int progress_passed = 0;
615 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400616
Chris Mason5708b952007-10-25 15:43:18 -0400617 parent_level = btrfs_header_level(parent);
618 if (cache_only && parent_level != 1)
619 return 0;
620
Chris Masond3977122009-01-05 21:25:51 -0500621 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400622 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500623 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400624 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400625
Chris Mason6b800532007-10-15 16:17:34 -0400626 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400627 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400628 end_slot = parent_nritems;
629
630 if (parent_nritems == 1)
631 return 0;
632
Chris Masonb4ce94d2009-02-04 09:25:08 -0500633 btrfs_set_lock_blocking(parent);
634
Chris Mason6702ed42007-08-07 16:15:09 -0400635 for (i = start_slot; i < end_slot; i++) {
636 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400637
Chris Mason5708b952007-10-25 15:43:18 -0400638 if (!parent->map_token) {
639 map_extent_buffer(parent,
640 btrfs_node_key_ptr_offset(i),
641 sizeof(struct btrfs_key_ptr),
642 &parent->map_token, &parent->kaddr,
643 &parent->map_start, &parent->map_len,
644 KM_USER1);
645 }
Chris Mason081e9572007-11-06 10:26:24 -0500646 btrfs_node_key(parent, &disk_key, i);
647 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
648 continue;
649
650 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400651 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400652 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400653 if (last_block == 0)
654 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400655
Chris Mason6702ed42007-08-07 16:15:09 -0400656 if (i > 0) {
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 Mason0ef3e662008-05-24 14:04:53 -0400660 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400661 other = btrfs_node_blockptr(parent, i + 1);
662 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400663 }
Chris Masone9d0b132007-08-10 14:06:19 -0400664 if (close) {
665 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400666 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400667 }
Chris Mason5708b952007-10-25 15:43:18 -0400668 if (parent->map_token) {
669 unmap_extent_buffer(parent, parent->map_token,
670 KM_USER1);
671 parent->map_token = NULL;
672 }
Chris Mason6702ed42007-08-07 16:15:09 -0400673
Chris Mason6b800532007-10-15 16:17:34 -0400674 cur = btrfs_find_tree_block(root, blocknr, blocksize);
675 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400676 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400677 else
678 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400679 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400680 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400681 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400682 continue;
683 }
Chris Mason6b800532007-10-15 16:17:34 -0400684 if (!cur) {
685 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400686 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400687 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400688 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400689 }
Chris Mason6702ed42007-08-07 16:15:09 -0400690 }
Chris Masone9d0b132007-08-10 14:06:19 -0400691 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400692 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400693
Chris Masone7a84562008-06-25 16:01:31 -0400694 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500695 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400696 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400697 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400698 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400699 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400700 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400701 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400702 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400703 break;
Yan252c38f2007-08-29 09:11:44 -0400704 }
Chris Masone7a84562008-06-25 16:01:31 -0400705 search_start = cur->start;
706 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400707 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400708 btrfs_tree_unlock(cur);
709 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400710 }
Chris Mason5708b952007-10-25 15:43:18 -0400711 if (parent->map_token) {
712 unmap_extent_buffer(parent, parent->map_token,
713 KM_USER1);
714 parent->map_token = NULL;
715 }
Chris Mason6702ed42007-08-07 16:15:09 -0400716 return err;
717}
718
Chris Mason74123bd2007-02-02 11:05:29 -0500719/*
720 * The leaf data grows from end-to-front in the node.
721 * this returns the address of the start of the last item,
722 * which is the stop of the leaf data stack
723 */
Chris Mason123abc82007-03-14 14:14:43 -0400724static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400725 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726{
Chris Mason5f39d392007-10-15 16:14:19 -0400727 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400729 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400730 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500731}
732
Chris Masond352ac62008-09-29 15:18:18 -0400733/*
734 * extra debugging checks to make sure all the items in a key are
735 * well formed and in the proper order
736 */
Chris Mason123abc82007-03-14 14:14:43 -0400737static int check_node(struct btrfs_root *root, struct btrfs_path *path,
738 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500739{
Chris Mason5f39d392007-10-15 16:14:19 -0400740 struct extent_buffer *parent = NULL;
741 struct extent_buffer *node = path->nodes[level];
742 struct btrfs_disk_key parent_key;
743 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500744 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400745 int slot;
746 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400747 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500748
749 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400750 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400751
Chris Mason8d7be552007-05-10 11:24:42 -0400752 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400753 BUG_ON(nritems == 0);
754 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400755 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400756 btrfs_node_key(parent, &parent_key, parent_slot);
757 btrfs_node_key(node, &node_key, 0);
758 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400759 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400760 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400761 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500762 }
Chris Mason123abc82007-03-14 14:14:43 -0400763 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400764 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400765 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
766 btrfs_node_key(node, &node_key, slot);
767 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400768 }
769 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400770 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
771 btrfs_node_key(node, &node_key, slot);
772 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500773 }
774 return 0;
775}
776
Chris Masond352ac62008-09-29 15:18:18 -0400777/*
778 * extra checking to make sure all the items in a leaf are
779 * well formed and in the proper order
780 */
Chris Mason123abc82007-03-14 14:14:43 -0400781static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
782 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500783{
Chris Mason5f39d392007-10-15 16:14:19 -0400784 struct extent_buffer *leaf = path->nodes[level];
785 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500786 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400787 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400788 struct btrfs_disk_key parent_key;
789 struct btrfs_disk_key leaf_key;
790 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400791
Chris Mason5f39d392007-10-15 16:14:19 -0400792 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500793
794 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400795 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400796
797 if (nritems == 0)
798 return 0;
799
800 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400801 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400802 btrfs_node_key(parent, &parent_key, parent_slot);
803 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400804
Chris Mason5f39d392007-10-15 16:14:19 -0400805 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400806 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400807 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400808 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500809 }
Chris Mason5f39d392007-10-15 16:14:19 -0400810 if (slot != 0 && slot < nritems - 1) {
811 btrfs_item_key(leaf, &leaf_key, slot);
812 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
813 if (comp_keys(&leaf_key, &cpukey) <= 0) {
814 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500815 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400816 BUG_ON(1);
817 }
818 if (btrfs_item_offset_nr(leaf, slot - 1) !=
819 btrfs_item_end_nr(leaf, slot)) {
820 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500821 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400822 BUG_ON(1);
823 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500824 }
Chris Mason8d7be552007-05-10 11:24:42 -0400825 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400826 btrfs_item_key(leaf, &leaf_key, slot);
827 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
828 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
829 if (btrfs_item_offset_nr(leaf, slot) !=
830 btrfs_item_end_nr(leaf, slot + 1)) {
831 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500832 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400833 BUG_ON(1);
834 }
Chris Mason8d7be552007-05-10 11:24:42 -0400835 }
Chris Mason5f39d392007-10-15 16:14:19 -0400836 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
837 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500838 return 0;
839}
840
Chris Masond3977122009-01-05 21:25:51 -0500841static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500842 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500843{
Chris Mason85d824c2008-04-10 10:23:19 -0400844 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500845 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400846 return check_leaf(root, path, level);
847 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500848}
849
Chris Mason74123bd2007-02-02 11:05:29 -0500850/*
Chris Mason5f39d392007-10-15 16:14:19 -0400851 * search for key in the extent_buffer. The items start at offset p,
852 * and they are item_size apart. There are 'max' items in p.
853 *
Chris Mason74123bd2007-02-02 11:05:29 -0500854 * the slot in the array is returned via slot, and it points to
855 * the place where you would insert key if it is not found in
856 * the array.
857 *
858 * slot may point to max if the key is bigger than all of the keys
859 */
Chris Masone02119d2008-09-05 16:13:11 -0400860static noinline int generic_bin_search(struct extent_buffer *eb,
861 unsigned long p,
862 int item_size, struct btrfs_key *key,
863 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500864{
865 int low = 0;
866 int high = max;
867 int mid;
868 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400869 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400870 struct btrfs_disk_key unaligned;
871 unsigned long offset;
872 char *map_token = NULL;
873 char *kaddr = NULL;
874 unsigned long map_start = 0;
875 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400876 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500877
Chris Masond3977122009-01-05 21:25:51 -0500878 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500879 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400880 offset = p + mid * item_size;
881
882 if (!map_token || offset < map_start ||
883 (offset + sizeof(struct btrfs_disk_key)) >
884 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400885 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400886 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400887 map_token = NULL;
888 }
Chris Mason934d3752008-12-08 16:43:10 -0500889
890 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400891 sizeof(struct btrfs_disk_key),
892 &map_token, &kaddr,
893 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400894
Chris Mason479965d2007-10-15 16:14:27 -0400895 if (!err) {
896 tmp = (struct btrfs_disk_key *)(kaddr + offset -
897 map_start);
898 } else {
899 read_extent_buffer(eb, &unaligned,
900 offset, sizeof(unaligned));
901 tmp = &unaligned;
902 }
903
Chris Mason5f39d392007-10-15 16:14:19 -0400904 } else {
905 tmp = (struct btrfs_disk_key *)(kaddr + offset -
906 map_start);
907 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500908 ret = comp_keys(tmp, key);
909
910 if (ret < 0)
911 low = mid + 1;
912 else if (ret > 0)
913 high = mid;
914 else {
915 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400916 if (map_token)
917 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500918 return 0;
919 }
920 }
921 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400922 if (map_token)
923 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500924 return 1;
925}
926
Chris Mason97571fd2007-02-24 13:39:08 -0500927/*
928 * simple bin_search frontend that does the right thing for
929 * leaves vs nodes
930 */
Chris Mason5f39d392007-10-15 16:14:19 -0400931static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
932 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500933{
Chris Mason5f39d392007-10-15 16:14:19 -0400934 if (level == 0) {
935 return generic_bin_search(eb,
936 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400937 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400938 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400939 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500940 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400941 return generic_bin_search(eb,
942 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400943 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400944 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400945 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500946 }
947 return -1;
948}
949
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
951 int level, int *slot)
952{
953 return bin_search(eb, key, level, slot);
954}
955
Yan, Zhengf0486c62010-05-16 10:46:25 -0400956static void root_add_used(struct btrfs_root *root, u32 size)
957{
958 spin_lock(&root->accounting_lock);
959 btrfs_set_root_used(&root->root_item,
960 btrfs_root_used(&root->root_item) + size);
961 spin_unlock(&root->accounting_lock);
962}
963
964static void root_sub_used(struct btrfs_root *root, u32 size)
965{
966 spin_lock(&root->accounting_lock);
967 btrfs_set_root_used(&root->root_item,
968 btrfs_root_used(&root->root_item) - size);
969 spin_unlock(&root->accounting_lock);
970}
971
Chris Masond352ac62008-09-29 15:18:18 -0400972/* given a node and slot number, this reads the blocks it points to. The
973 * extent buffer is returned with a reference taken (but unlocked).
974 * NULL is returned on error.
975 */
Chris Masone02119d2008-09-05 16:13:11 -0400976static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400977 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500978{
Chris Masonca7a79a2008-05-12 12:59:19 -0400979 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500980 if (slot < 0)
981 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400982 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500983 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400984
985 BUG_ON(level == 0);
986
Chris Masondb945352007-10-15 16:15:53 -0400987 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400988 btrfs_level_size(root, level - 1),
989 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500990}
991
Chris Masond352ac62008-09-29 15:18:18 -0400992/*
993 * node level balancing, used to make sure nodes are in proper order for
994 * item deletion. We balance from the top down, so we have to make sure
995 * that a deletion won't leave an node completely empty later on.
996 */
Chris Masone02119d2008-09-05 16:13:11 -0400997static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500998 struct btrfs_root *root,
999 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001000{
Chris Mason5f39d392007-10-15 16:14:19 -04001001 struct extent_buffer *right = NULL;
1002 struct extent_buffer *mid;
1003 struct extent_buffer *left = NULL;
1004 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001005 int ret = 0;
1006 int wret;
1007 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001008 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001009 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001010
1011 if (level == 0)
1012 return 0;
1013
Chris Mason5f39d392007-10-15 16:14:19 -04001014 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001015
Chris Mason925baed2008-06-25 16:01:30 -04001016 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -05001017 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1018
Chris Mason1d4f8a02007-03-13 09:28:32 -04001019 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001020
Chris Mason234b63a2007-03-13 10:46:10 -04001021 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001022 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001023 pslot = path->slots[level + 1];
1024
Chris Mason40689472007-03-17 14:29:23 -04001025 /*
1026 * deal with the case where there is only one pointer in the root
1027 * by promoting the node below to a root
1028 */
Chris Mason5f39d392007-10-15 16:14:19 -04001029 if (!parent) {
1030 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001031
Chris Mason5f39d392007-10-15 16:14:19 -04001032 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001033 return 0;
1034
1035 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001036 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001037 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001038 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001039 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001040 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001041 if (ret) {
1042 btrfs_tree_unlock(child);
1043 free_extent_buffer(child);
1044 goto enospc;
1045 }
Yan2f375ab2008-02-01 14:58:07 -05001046
Chris Mason925baed2008-06-25 16:01:30 -04001047 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001048 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001049 spin_unlock(&root->node_lock);
1050
Chris Mason0b86a832008-03-24 15:01:56 -04001051 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001052 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001053
Chris Mason925baed2008-06-25 16:01:30 -04001054 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001055 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001056 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001057 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001058 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001059 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001060
1061 root_sub_used(root, mid->len);
1062 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001063 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001064 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001065 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001066 }
Chris Mason5f39d392007-10-15 16:14:19 -04001067 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001068 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001069 return 0;
1070
Andi Kleen559af822010-10-29 15:14:37 -04001071 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001072
Chris Mason5f39d392007-10-15 16:14:19 -04001073 left = read_node_slot(root, parent, pslot - 1);
1074 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001075 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001076 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001077 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001078 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001079 if (wret) {
1080 ret = wret;
1081 goto enospc;
1082 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001083 }
Chris Mason5f39d392007-10-15 16:14:19 -04001084 right = read_node_slot(root, parent, pslot + 1);
1085 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001086 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001087 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001088 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001089 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001090 if (wret) {
1091 ret = wret;
1092 goto enospc;
1093 }
1094 }
1095
1096 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001097 if (left) {
1098 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001099 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001100 if (wret < 0)
1101 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001102 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001103 }
Chris Mason79f95c82007-03-01 15:16:26 -05001104
1105 /*
1106 * then try to empty the right most buffer into the middle
1107 */
Chris Mason5f39d392007-10-15 16:14:19 -04001108 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001109 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001110 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001111 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001112 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001113 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001114 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -04001115 wret = del_ptr(trans, root, path, level + 1, pslot +
1116 1);
Chris Masonbb803952007-03-01 12:04:21 -05001117 if (wret)
1118 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001119 root_sub_used(root, right->len);
1120 btrfs_free_tree_block(trans, root, right, 0, 1);
1121 free_extent_buffer(right);
1122 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001123 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001124 struct btrfs_disk_key right_key;
1125 btrfs_node_key(right, &right_key, 0);
1126 btrfs_set_node_key(parent, &right_key, pslot + 1);
1127 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001128 }
1129 }
Chris Mason5f39d392007-10-15 16:14:19 -04001130 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001131 /*
1132 * we're not allowed to leave a node with one item in the
1133 * tree during a delete. A deletion from lower in the tree
1134 * could try to delete the only pointer in this node.
1135 * So, pull some keys from the left.
1136 * There has to be a left pointer at this point because
1137 * otherwise we would have pulled some pointers from the
1138 * right
1139 */
Chris Mason5f39d392007-10-15 16:14:19 -04001140 BUG_ON(!left);
1141 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001142 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001143 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001144 goto enospc;
1145 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001146 if (wret == 1) {
1147 wret = push_node_left(trans, root, left, mid, 1);
1148 if (wret < 0)
1149 ret = wret;
1150 }
Chris Mason79f95c82007-03-01 15:16:26 -05001151 BUG_ON(wret == 1);
1152 }
Chris Mason5f39d392007-10-15 16:14:19 -04001153 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001154 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001155 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001156 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001157 if (wret)
1158 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001159 root_sub_used(root, mid->len);
1160 btrfs_free_tree_block(trans, root, mid, 0, 1);
1161 free_extent_buffer(mid);
1162 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001163 } else {
1164 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001165 struct btrfs_disk_key mid_key;
1166 btrfs_node_key(mid, &mid_key, 0);
1167 btrfs_set_node_key(parent, &mid_key, pslot);
1168 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001169 }
Chris Masonbb803952007-03-01 12:04:21 -05001170
Chris Mason79f95c82007-03-01 15:16:26 -05001171 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001172 if (left) {
1173 if (btrfs_header_nritems(left) > orig_slot) {
1174 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001175 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001176 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001177 path->slots[level + 1] -= 1;
1178 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001179 if (mid) {
1180 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001181 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001182 }
Chris Masonbb803952007-03-01 12:04:21 -05001183 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001184 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001185 path->slots[level] = orig_slot;
1186 }
1187 }
Chris Mason79f95c82007-03-01 15:16:26 -05001188 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001189 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001190 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001191 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001192 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001193enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001194 if (right) {
1195 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001196 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001197 }
1198 if (left) {
1199 if (path->nodes[level] != left)
1200 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001201 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001202 }
Chris Masonbb803952007-03-01 12:04:21 -05001203 return ret;
1204}
1205
Chris Masond352ac62008-09-29 15:18:18 -04001206/* Node balancing for insertion. Here we only split or push nodes around
1207 * when they are completely full. This is also done top down, so we
1208 * have to be pessimistic.
1209 */
Chris Masond3977122009-01-05 21:25:51 -05001210static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001211 struct btrfs_root *root,
1212 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001213{
Chris Mason5f39d392007-10-15 16:14:19 -04001214 struct extent_buffer *right = NULL;
1215 struct extent_buffer *mid;
1216 struct extent_buffer *left = NULL;
1217 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001218 int ret = 0;
1219 int wret;
1220 int pslot;
1221 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001222
1223 if (level == 0)
1224 return 1;
1225
Chris Mason5f39d392007-10-15 16:14:19 -04001226 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001227 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001228
1229 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001230 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001231 pslot = path->slots[level + 1];
1232
Chris Mason5f39d392007-10-15 16:14:19 -04001233 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001234 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001235
Chris Mason5f39d392007-10-15 16:14:19 -04001236 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001237
1238 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001239 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001240 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001241
1242 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001243 btrfs_set_lock_blocking(left);
1244
Chris Mason5f39d392007-10-15 16:14:19 -04001245 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001246 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1247 wret = 1;
1248 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001249 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001250 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001251 if (ret)
1252 wret = 1;
1253 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001254 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001255 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001256 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001257 }
Chris Masone66f7092007-04-20 13:16:02 -04001258 if (wret < 0)
1259 ret = wret;
1260 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001261 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001262 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001263 btrfs_node_key(mid, &disk_key, 0);
1264 btrfs_set_node_key(parent, &disk_key, pslot);
1265 btrfs_mark_buffer_dirty(parent);
1266 if (btrfs_header_nritems(left) > orig_slot) {
1267 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001268 path->slots[level + 1] -= 1;
1269 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001270 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001271 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001272 } else {
1273 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001274 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001275 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001276 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001277 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001278 }
Chris Masone66f7092007-04-20 13:16:02 -04001279 return 0;
1280 }
Chris Mason925baed2008-06-25 16:01:30 -04001281 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001282 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001283 }
Chris Mason925baed2008-06-25 16:01:30 -04001284 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001285
1286 /*
1287 * then try to empty the right most buffer into the middle
1288 */
Chris Mason5f39d392007-10-15 16:14:19 -04001289 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001290 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001291
Chris Mason925baed2008-06-25 16:01:30 -04001292 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001293 btrfs_set_lock_blocking(right);
1294
Chris Mason5f39d392007-10-15 16:14:19 -04001295 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001296 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1297 wret = 1;
1298 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001299 ret = btrfs_cow_block(trans, root, right,
1300 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001301 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001302 if (ret)
1303 wret = 1;
1304 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001305 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001306 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001307 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001308 }
Chris Masone66f7092007-04-20 13:16:02 -04001309 if (wret < 0)
1310 ret = wret;
1311 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001312 struct btrfs_disk_key disk_key;
1313
1314 btrfs_node_key(right, &disk_key, 0);
1315 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1316 btrfs_mark_buffer_dirty(parent);
1317
1318 if (btrfs_header_nritems(mid) <= orig_slot) {
1319 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001320 path->slots[level + 1] += 1;
1321 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001322 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001323 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001324 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001325 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001326 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001327 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001328 }
Chris Masone66f7092007-04-20 13:16:02 -04001329 return 0;
1330 }
Chris Mason925baed2008-06-25 16:01:30 -04001331 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001332 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001333 }
Chris Masone66f7092007-04-20 13:16:02 -04001334 return 1;
1335}
1336
Chris Mason74123bd2007-02-02 11:05:29 -05001337/*
Chris Masond352ac62008-09-29 15:18:18 -04001338 * readahead one full node of leaves, finding things that are close
1339 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001340 */
Chris Masonc8c42862009-04-03 10:14:18 -04001341static void reada_for_search(struct btrfs_root *root,
1342 struct btrfs_path *path,
1343 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001344{
Chris Mason5f39d392007-10-15 16:14:19 -04001345 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001346 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001347 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001348 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001349 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001350 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001351 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001352 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001353 u32 nr;
1354 u32 blocksize;
1355 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001356
Chris Masona6b6e752007-10-15 16:22:39 -04001357 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001358 return;
1359
Chris Mason6702ed42007-08-07 16:15:09 -04001360 if (!path->nodes[level])
1361 return;
1362
Chris Mason5f39d392007-10-15 16:14:19 -04001363 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001364
Chris Mason3c69fae2007-08-07 15:52:22 -04001365 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001366 blocksize = btrfs_level_size(root, level - 1);
1367 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001368 if (eb) {
1369 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001370 return;
1371 }
1372
Chris Masona7175312009-01-22 09:23:10 -05001373 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001374
Chris Mason5f39d392007-10-15 16:14:19 -04001375 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001376 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001377 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001378 if (direction < 0) {
1379 if (nr == 0)
1380 break;
1381 nr--;
1382 } else if (direction > 0) {
1383 nr++;
1384 if (nr >= nritems)
1385 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001386 }
Chris Mason01f46652007-12-21 16:24:26 -05001387 if (path->reada < 0 && objectid) {
1388 btrfs_node_key(node, &disk_key, nr);
1389 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1390 break;
1391 }
Chris Mason6b800532007-10-15 16:17:34 -04001392 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001393 if ((search <= target && target - search <= 65536) ||
1394 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001395 readahead_tree_block(root, search, blocksize,
1396 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001397 nread += blocksize;
1398 }
1399 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001400 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001401 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001402 }
1403}
Chris Mason925baed2008-06-25 16:01:30 -04001404
Chris Masond352ac62008-09-29 15:18:18 -04001405/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001406 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1407 * cache
1408 */
1409static noinline int reada_for_balance(struct btrfs_root *root,
1410 struct btrfs_path *path, int level)
1411{
1412 int slot;
1413 int nritems;
1414 struct extent_buffer *parent;
1415 struct extent_buffer *eb;
1416 u64 gen;
1417 u64 block1 = 0;
1418 u64 block2 = 0;
1419 int ret = 0;
1420 int blocksize;
1421
Chris Mason8c594ea2009-04-20 15:50:10 -04001422 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001423 if (!parent)
1424 return 0;
1425
1426 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001427 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001428 blocksize = btrfs_level_size(root, level);
1429
1430 if (slot > 0) {
1431 block1 = btrfs_node_blockptr(parent, slot - 1);
1432 gen = btrfs_node_ptr_generation(parent, slot - 1);
1433 eb = btrfs_find_tree_block(root, block1, blocksize);
1434 if (eb && btrfs_buffer_uptodate(eb, gen))
1435 block1 = 0;
1436 free_extent_buffer(eb);
1437 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001438 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001439 block2 = btrfs_node_blockptr(parent, slot + 1);
1440 gen = btrfs_node_ptr_generation(parent, slot + 1);
1441 eb = btrfs_find_tree_block(root, block2, blocksize);
1442 if (eb && btrfs_buffer_uptodate(eb, gen))
1443 block2 = 0;
1444 free_extent_buffer(eb);
1445 }
1446 if (block1 || block2) {
1447 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001448
1449 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001450 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001451
1452 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001453 if (block1)
1454 readahead_tree_block(root, block1, blocksize, 0);
1455 if (block2)
1456 readahead_tree_block(root, block2, blocksize, 0);
1457
1458 if (block1) {
1459 eb = read_tree_block(root, block1, blocksize, 0);
1460 free_extent_buffer(eb);
1461 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001462 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001463 eb = read_tree_block(root, block2, blocksize, 0);
1464 free_extent_buffer(eb);
1465 }
1466 }
1467 return ret;
1468}
1469
1470
1471/*
Chris Masond3977122009-01-05 21:25:51 -05001472 * when we walk down the tree, it is usually safe to unlock the higher layers
1473 * in the tree. The exceptions are when our path goes through slot 0, because
1474 * operations on the tree might require changing key pointers higher up in the
1475 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001476 *
Chris Masond3977122009-01-05 21:25:51 -05001477 * callers might also have set path->keep_locks, which tells this code to keep
1478 * the lock if the path points to the last slot in the block. This is part of
1479 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001480 *
Chris Masond3977122009-01-05 21:25:51 -05001481 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1482 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001483 */
Chris Masone02119d2008-09-05 16:13:11 -04001484static noinline void unlock_up(struct btrfs_path *path, int level,
1485 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001486{
1487 int i;
1488 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001489 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001490 struct extent_buffer *t;
1491
1492 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1493 if (!path->nodes[i])
1494 break;
1495 if (!path->locks[i])
1496 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001497 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001498 skip_level = i + 1;
1499 continue;
1500 }
Chris Mason051e1b92008-06-25 16:01:30 -04001501 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001502 u32 nritems;
1503 t = path->nodes[i];
1504 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001505 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001506 skip_level = i + 1;
1507 continue;
1508 }
1509 }
Chris Mason051e1b92008-06-25 16:01:30 -04001510 if (skip_level < i && i >= lowest_unlock)
1511 no_skips = 1;
1512
Chris Mason925baed2008-06-25 16:01:30 -04001513 t = path->nodes[i];
1514 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1515 btrfs_tree_unlock(t);
1516 path->locks[i] = 0;
1517 }
1518 }
1519}
1520
Chris Mason3c69fae2007-08-07 15:52:22 -04001521/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001522 * This releases any locks held in the path starting at level and
1523 * going all the way up to the root.
1524 *
1525 * btrfs_search_slot will keep the lock held on higher nodes in a few
1526 * corner cases, such as COW of the block at slot zero in the node. This
1527 * ignores those rules, and it should only be called when there are no
1528 * more updates to be done higher up in the tree.
1529 */
1530noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1531{
1532 int i;
1533
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001534 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001535 return;
1536
1537 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1538 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001539 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001540 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001541 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001542 btrfs_tree_unlock(path->nodes[i]);
1543 path->locks[i] = 0;
1544 }
1545}
1546
1547/*
Chris Masonc8c42862009-04-03 10:14:18 -04001548 * helper function for btrfs_search_slot. The goal is to find a block
1549 * in cache without setting the path to blocking. If we find the block
1550 * we return zero and the path is unchanged.
1551 *
1552 * If we can't find the block, we set the path blocking and do some
1553 * reada. -EAGAIN is returned and the search must be repeated.
1554 */
1555static int
1556read_block_for_search(struct btrfs_trans_handle *trans,
1557 struct btrfs_root *root, struct btrfs_path *p,
1558 struct extent_buffer **eb_ret, int level, int slot,
1559 struct btrfs_key *key)
1560{
1561 u64 blocknr;
1562 u64 gen;
1563 u32 blocksize;
1564 struct extent_buffer *b = *eb_ret;
1565 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001566 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001567
1568 blocknr = btrfs_node_blockptr(b, slot);
1569 gen = btrfs_node_ptr_generation(b, slot);
1570 blocksize = btrfs_level_size(root, level - 1);
1571
1572 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001573 if (tmp) {
1574 if (btrfs_buffer_uptodate(tmp, 0)) {
1575 if (btrfs_buffer_uptodate(tmp, gen)) {
1576 /*
1577 * we found an up to date block without
1578 * sleeping, return
1579 * right away
1580 */
1581 *eb_ret = tmp;
1582 return 0;
1583 }
1584 /* the pages were up to date, but we failed
1585 * the generation number check. Do a full
1586 * read for the generation number that is correct.
1587 * We must do this without dropping locks so
1588 * we can trust our generation number
1589 */
1590 free_extent_buffer(tmp);
1591 tmp = read_tree_block(root, blocknr, blocksize, gen);
1592 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1593 *eb_ret = tmp;
1594 return 0;
1595 }
1596 free_extent_buffer(tmp);
1597 btrfs_release_path(NULL, p);
1598 return -EIO;
1599 }
Chris Masonc8c42862009-04-03 10:14:18 -04001600 }
1601
1602 /*
1603 * reduce lock contention at high levels
1604 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001605 * we read. Don't release the lock on the current
1606 * level because we need to walk this node to figure
1607 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001608 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001609 btrfs_unlock_up_safe(p, level + 1);
1610 btrfs_set_path_blocking(p);
1611
Chris Masoncb449212010-10-24 11:01:27 -04001612 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001613 if (p->reada)
1614 reada_for_search(root, p, level, slot, key->objectid);
1615
Chris Mason8c594ea2009-04-20 15:50:10 -04001616 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001617
1618 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001619 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001620 if (tmp) {
1621 /*
1622 * If the read above didn't mark this buffer up to date,
1623 * it will never end up being up to date. Set ret to EIO now
1624 * and give up so that our caller doesn't loop forever
1625 * on our EAGAINs.
1626 */
1627 if (!btrfs_buffer_uptodate(tmp, 0))
1628 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001629 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001630 }
1631 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001632}
1633
1634/*
1635 * helper function for btrfs_search_slot. This does all of the checks
1636 * for node-level blocks and does any balancing required based on
1637 * the ins_len.
1638 *
1639 * If no extra work was required, zero is returned. If we had to
1640 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1641 * start over
1642 */
1643static int
1644setup_nodes_for_search(struct btrfs_trans_handle *trans,
1645 struct btrfs_root *root, struct btrfs_path *p,
1646 struct extent_buffer *b, int level, int ins_len)
1647{
1648 int ret;
1649 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1650 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1651 int sret;
1652
1653 sret = reada_for_balance(root, p, level);
1654 if (sret)
1655 goto again;
1656
1657 btrfs_set_path_blocking(p);
1658 sret = split_node(trans, root, p, level);
1659 btrfs_clear_path_blocking(p, NULL);
1660
1661 BUG_ON(sret > 0);
1662 if (sret) {
1663 ret = sret;
1664 goto done;
1665 }
1666 b = p->nodes[level];
1667 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001668 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001669 int sret;
1670
1671 sret = reada_for_balance(root, p, level);
1672 if (sret)
1673 goto again;
1674
1675 btrfs_set_path_blocking(p);
1676 sret = balance_level(trans, root, p, level);
1677 btrfs_clear_path_blocking(p, NULL);
1678
1679 if (sret) {
1680 ret = sret;
1681 goto done;
1682 }
1683 b = p->nodes[level];
1684 if (!b) {
1685 btrfs_release_path(NULL, p);
1686 goto again;
1687 }
1688 BUG_ON(btrfs_header_nritems(b) == 1);
1689 }
1690 return 0;
1691
1692again:
1693 ret = -EAGAIN;
1694done:
1695 return ret;
1696}
1697
1698/*
Chris Mason74123bd2007-02-02 11:05:29 -05001699 * look for key in the tree. path is filled in with nodes along the way
1700 * if key is found, we return zero and you can find the item in the leaf
1701 * level of the path (level 0)
1702 *
1703 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001704 * be inserted, and 1 is returned. If there are other errors during the
1705 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001706 *
1707 * if ins_len > 0, nodes and leaves will be split as we walk down the
1708 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1709 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001710 */
Chris Masone089f052007-03-16 16:20:31 -04001711int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1712 *root, struct btrfs_key *key, struct btrfs_path *p, int
1713 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001714{
Chris Mason5f39d392007-10-15 16:14:19 -04001715 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001716 int slot;
1717 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001718 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001719 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001720 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001721 u8 lowest_level = 0;
1722
Chris Mason6702ed42007-08-07 16:15:09 -04001723 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001724 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001725 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001726
Chris Mason925baed2008-06-25 16:01:30 -04001727 if (ins_len < 0)
1728 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001729
Chris Masonbb803952007-03-01 12:04:21 -05001730again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001731 if (p->search_commit_root) {
1732 b = root->commit_root;
1733 extent_buffer_get(b);
1734 if (!p->skip_locking)
1735 btrfs_tree_lock(b);
1736 } else {
1737 if (p->skip_locking)
1738 b = btrfs_root_node(root);
1739 else
1740 b = btrfs_lock_root_node(root);
1741 }
Chris Mason925baed2008-06-25 16:01:30 -04001742
Chris Masoneb60cea2007-02-02 09:18:22 -05001743 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001744 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001745
1746 /*
1747 * setup the path here so we can release it under lock
1748 * contention with the cow code
1749 */
1750 p->nodes[level] = b;
1751 if (!p->skip_locking)
1752 p->locks[level] = 1;
1753
Chris Mason02217ed2007-03-02 16:08:05 -05001754 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001755 /*
1756 * if we don't really need to cow this block
1757 * then we don't want to set the path blocking,
1758 * so we test it here
1759 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001760 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001761 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001762
Chris Masonb4ce94d2009-02-04 09:25:08 -05001763 btrfs_set_path_blocking(p);
1764
Yan Zheng33c66f42009-07-22 09:59:00 -04001765 err = btrfs_cow_block(trans, root, b,
1766 p->nodes[level + 1],
1767 p->slots[level + 1], &b);
1768 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001769 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001770 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001771 }
Chris Mason02217ed2007-03-02 16:08:05 -05001772 }
Chris Mason65b51a02008-08-01 15:11:20 -04001773cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001774 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001775 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001776 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001777 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001778
Chris Masoneb60cea2007-02-02 09:18:22 -05001779 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001780 if (!p->skip_locking)
1781 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001782
Chris Mason4008c042009-02-12 14:09:45 -05001783 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001784
1785 /*
1786 * we have a lock on b and as long as we aren't changing
1787 * the tree, there is no way to for the items in b to change.
1788 * It is safe to drop the lock on our parent before we
1789 * go through the expensive btree search on b.
1790 *
1791 * If cow is true, then we might be changing slot zero,
1792 * which may require changing the parent. So, we can't
1793 * drop the lock until after we know which slot we're
1794 * operating on.
1795 */
1796 if (!cow)
1797 btrfs_unlock_up_safe(p, level + 1);
1798
Chris Mason123abc82007-03-14 14:14:43 -04001799 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001800 if (ret) {
1801 ret = -1;
1802 goto done;
1803 }
Chris Mason925baed2008-06-25 16:01:30 -04001804
Chris Mason5f39d392007-10-15 16:14:19 -04001805 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001806
Chris Mason5f39d392007-10-15 16:14:19 -04001807 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001808 int dec = 0;
1809 if (ret && slot > 0) {
1810 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001811 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001812 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001813 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001814 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001815 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001816 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001817 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001818 if (err) {
1819 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001820 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001821 }
Chris Masonc8c42862009-04-03 10:14:18 -04001822 b = p->nodes[level];
1823 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001824
Chris Masonf9efa9c2008-06-25 16:14:04 -04001825 unlock_up(p, level, lowest_unlock);
1826
Chris Mason925baed2008-06-25 16:01:30 -04001827 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001828 if (dec)
1829 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001830 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001831 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001832
Yan Zheng33c66f42009-07-22 09:59:00 -04001833 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001834 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001835 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001836 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001837 if (err) {
1838 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001839 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001840 }
Chris Mason76a05b32009-05-14 13:24:30 -04001841
Chris Masonb4ce94d2009-02-04 09:25:08 -05001842 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001843 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001844 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001845
Yan Zheng33c66f42009-07-22 09:59:00 -04001846 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001847 btrfs_set_path_blocking(p);
1848 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001849 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001850 }
1851 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001852 } else {
1853 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001854 if (ins_len > 0 &&
1855 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001856 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001857 err = split_leaf(trans, root, key,
1858 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001859 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001860
Yan Zheng33c66f42009-07-22 09:59:00 -04001861 BUG_ON(err > 0);
1862 if (err) {
1863 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001864 goto done;
1865 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001866 }
Chris Mason459931e2008-12-10 09:10:46 -05001867 if (!p->search_for_split)
1868 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001869 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001870 }
1871 }
Chris Mason65b51a02008-08-01 15:11:20 -04001872 ret = 1;
1873done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001874 /*
1875 * we don't really know what they plan on doing with the path
1876 * from here on, so for now just mark it as blocking
1877 */
Chris Masonb9473432009-03-13 11:00:37 -04001878 if (!p->leave_spinning)
1879 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001880 if (ret < 0)
1881 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001882 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001883}
1884
Chris Mason74123bd2007-02-02 11:05:29 -05001885/*
1886 * adjust the pointers going up the tree, starting at level
1887 * making sure the right key of each node is points to 'key'.
1888 * This is used after shifting pointers to the left, so it stops
1889 * fixing up pointers when a given leaf/node is not in slot 0 of the
1890 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001891 *
1892 * If this fails to write a tree block, it returns -1, but continues
1893 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001894 */
Chris Mason5f39d392007-10-15 16:14:19 -04001895static int fixup_low_keys(struct btrfs_trans_handle *trans,
1896 struct btrfs_root *root, struct btrfs_path *path,
1897 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001898{
1899 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001900 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001901 struct extent_buffer *t;
1902
Chris Mason234b63a2007-03-13 10:46:10 -04001903 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001904 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001905 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001906 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001907 t = path->nodes[i];
1908 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001909 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001910 if (tslot != 0)
1911 break;
1912 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001913 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001914}
1915
Chris Mason74123bd2007-02-02 11:05:29 -05001916/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001917 * update item key.
1918 *
1919 * This function isn't completely safe. It's the caller's responsibility
1920 * that the new key won't break the order
1921 */
1922int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1923 struct btrfs_root *root, struct btrfs_path *path,
1924 struct btrfs_key *new_key)
1925{
1926 struct btrfs_disk_key disk_key;
1927 struct extent_buffer *eb;
1928 int slot;
1929
1930 eb = path->nodes[0];
1931 slot = path->slots[0];
1932 if (slot > 0) {
1933 btrfs_item_key(eb, &disk_key, slot - 1);
1934 if (comp_keys(&disk_key, new_key) >= 0)
1935 return -1;
1936 }
1937 if (slot < btrfs_header_nritems(eb) - 1) {
1938 btrfs_item_key(eb, &disk_key, slot + 1);
1939 if (comp_keys(&disk_key, new_key) <= 0)
1940 return -1;
1941 }
1942
1943 btrfs_cpu_key_to_disk(&disk_key, new_key);
1944 btrfs_set_item_key(eb, &disk_key, slot);
1945 btrfs_mark_buffer_dirty(eb);
1946 if (slot == 0)
1947 fixup_low_keys(trans, root, path, &disk_key, 1);
1948 return 0;
1949}
1950
1951/*
Chris Mason74123bd2007-02-02 11:05:29 -05001952 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001953 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001954 *
1955 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1956 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001957 */
Chris Mason98ed5172008-01-03 10:01:48 -05001958static int push_node_left(struct btrfs_trans_handle *trans,
1959 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001960 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001961{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001962 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001963 int src_nritems;
1964 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001965 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001966
Chris Mason5f39d392007-10-15 16:14:19 -04001967 src_nritems = btrfs_header_nritems(src);
1968 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001969 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001970 WARN_ON(btrfs_header_generation(src) != trans->transid);
1971 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001972
Chris Masonbce4eae2008-04-24 14:42:46 -04001973 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001974 return 1;
1975
Chris Masond3977122009-01-05 21:25:51 -05001976 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001977 return 1;
1978
Chris Masonbce4eae2008-04-24 14:42:46 -04001979 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001980 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001981 if (push_items < src_nritems) {
1982 /* leave at least 8 pointers in the node if
1983 * we aren't going to empty it
1984 */
1985 if (src_nritems - push_items < 8) {
1986 if (push_items <= 8)
1987 return 1;
1988 push_items -= 8;
1989 }
1990 }
1991 } else
1992 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001993
Chris Mason5f39d392007-10-15 16:14:19 -04001994 copy_extent_buffer(dst, src,
1995 btrfs_node_key_ptr_offset(dst_nritems),
1996 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001997 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001998
Chris Masonbb803952007-03-01 12:04:21 -05001999 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002000 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2001 btrfs_node_key_ptr_offset(push_items),
2002 (src_nritems - push_items) *
2003 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002004 }
Chris Mason5f39d392007-10-15 16:14:19 -04002005 btrfs_set_header_nritems(src, src_nritems - push_items);
2006 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2007 btrfs_mark_buffer_dirty(src);
2008 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002009
Chris Masonbb803952007-03-01 12:04:21 -05002010 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002011}
2012
Chris Mason97571fd2007-02-24 13:39:08 -05002013/*
Chris Mason79f95c82007-03-01 15:16:26 -05002014 * try to push data from one node into the next node right in the
2015 * tree.
2016 *
2017 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2018 * error, and > 0 if there was no room in the right hand block.
2019 *
2020 * this will only push up to 1/2 the contents of the left node over
2021 */
Chris Mason5f39d392007-10-15 16:14:19 -04002022static int balance_node_right(struct btrfs_trans_handle *trans,
2023 struct btrfs_root *root,
2024 struct extent_buffer *dst,
2025 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002026{
Chris Mason79f95c82007-03-01 15:16:26 -05002027 int push_items = 0;
2028 int max_push;
2029 int src_nritems;
2030 int dst_nritems;
2031 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002032
Chris Mason7bb86312007-12-11 09:25:06 -05002033 WARN_ON(btrfs_header_generation(src) != trans->transid);
2034 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2035
Chris Mason5f39d392007-10-15 16:14:19 -04002036 src_nritems = btrfs_header_nritems(src);
2037 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002038 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002039 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002040 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002041
Chris Masond3977122009-01-05 21:25:51 -05002042 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002043 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002044
2045 max_push = src_nritems / 2 + 1;
2046 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002047 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002048 return 1;
Yan252c38f2007-08-29 09:11:44 -04002049
Chris Mason79f95c82007-03-01 15:16:26 -05002050 if (max_push < push_items)
2051 push_items = max_push;
2052
Chris Mason5f39d392007-10-15 16:14:19 -04002053 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2054 btrfs_node_key_ptr_offset(0),
2055 (dst_nritems) *
2056 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002057
Chris Mason5f39d392007-10-15 16:14:19 -04002058 copy_extent_buffer(dst, src,
2059 btrfs_node_key_ptr_offset(0),
2060 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002061 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002062
Chris Mason5f39d392007-10-15 16:14:19 -04002063 btrfs_set_header_nritems(src, src_nritems - push_items);
2064 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002065
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_mark_buffer_dirty(src);
2067 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002068
Chris Mason79f95c82007-03-01 15:16:26 -05002069 return ret;
2070}
2071
2072/*
Chris Mason97571fd2007-02-24 13:39:08 -05002073 * helper function to insert a new root level in the tree.
2074 * A new node is allocated, and a single item is inserted to
2075 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002076 *
2077 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002078 */
Chris Masond3977122009-01-05 21:25:51 -05002079static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002080 struct btrfs_root *root,
2081 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002082{
Chris Mason7bb86312007-12-11 09:25:06 -05002083 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002084 struct extent_buffer *lower;
2085 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002086 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002087 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002088
2089 BUG_ON(path->nodes[level]);
2090 BUG_ON(path->nodes[level-1] != root->node);
2091
Chris Mason7bb86312007-12-11 09:25:06 -05002092 lower = path->nodes[level-1];
2093 if (level == 1)
2094 btrfs_item_key(lower, &lower_key, 0);
2095 else
2096 btrfs_node_key(lower, &lower_key, 0);
2097
Zheng Yan31840ae2008-09-23 13:14:14 -04002098 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002099 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002100 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002101 if (IS_ERR(c))
2102 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002103
Yan, Zhengf0486c62010-05-16 10:46:25 -04002104 root_add_used(root, root->nodesize);
2105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002106 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002107 btrfs_set_header_nritems(c, 1);
2108 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002109 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002110 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002111 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002112 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002113
Chris Mason5f39d392007-10-15 16:14:19 -04002114 write_extent_buffer(c, root->fs_info->fsid,
2115 (unsigned long)btrfs_header_fsid(c),
2116 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002117
2118 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2119 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2120 BTRFS_UUID_SIZE);
2121
Chris Mason5f39d392007-10-15 16:14:19 -04002122 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002123 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002124 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002125 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002126
2127 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002128
2129 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002130
Chris Mason925baed2008-06-25 16:01:30 -04002131 spin_lock(&root->node_lock);
2132 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002133 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002134 spin_unlock(&root->node_lock);
2135
2136 /* the super has an extra ref to root->node */
2137 free_extent_buffer(old);
2138
Chris Mason0b86a832008-03-24 15:01:56 -04002139 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002140 extent_buffer_get(c);
2141 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002142 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002143 path->slots[level] = 0;
2144 return 0;
2145}
2146
Chris Mason74123bd2007-02-02 11:05:29 -05002147/*
2148 * worker function to insert a single pointer in a node.
2149 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002150 *
Chris Mason74123bd2007-02-02 11:05:29 -05002151 * slot and level indicate where you want the key to go, and
2152 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002153 *
2154 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002155 */
Chris Masone089f052007-03-16 16:20:31 -04002156static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2157 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002158 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002159{
Chris Mason5f39d392007-10-15 16:14:19 -04002160 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002161 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002162
2163 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002164 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002165 lower = path->nodes[level];
2166 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002167 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002168 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002169 BUG();
2170 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002171 memmove_extent_buffer(lower,
2172 btrfs_node_key_ptr_offset(slot + 1),
2173 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002174 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002175 }
Chris Mason5f39d392007-10-15 16:14:19 -04002176 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002177 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002178 WARN_ON(trans->transid == 0);
2179 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002180 btrfs_set_header_nritems(lower, nritems + 1);
2181 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002182 return 0;
2183}
2184
Chris Mason97571fd2007-02-24 13:39:08 -05002185/*
2186 * split the node at the specified level in path in two.
2187 * The path is corrected to point to the appropriate node after the split
2188 *
2189 * Before splitting this tries to make some room in the node by pushing
2190 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002191 *
2192 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002193 */
Chris Masone02119d2008-09-05 16:13:11 -04002194static noinline int split_node(struct btrfs_trans_handle *trans,
2195 struct btrfs_root *root,
2196 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002197{
Chris Mason5f39d392007-10-15 16:14:19 -04002198 struct extent_buffer *c;
2199 struct extent_buffer *split;
2200 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002201 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002202 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002203 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002204 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002205
Chris Mason5f39d392007-10-15 16:14:19 -04002206 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002207 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002208 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002209 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002210 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002211 if (ret)
2212 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002213 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002214 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002215 c = path->nodes[level];
2216 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002217 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002218 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002219 if (ret < 0)
2220 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002221 }
Chris Masone66f7092007-04-20 13:16:02 -04002222
Chris Mason5f39d392007-10-15 16:14:19 -04002223 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002224 mid = (c_nritems + 1) / 2;
2225 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002226
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002227 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002228 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002229 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002230 if (IS_ERR(split))
2231 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002232
Yan, Zhengf0486c62010-05-16 10:46:25 -04002233 root_add_used(root, root->nodesize);
2234
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002235 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002236 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002237 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002238 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002239 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002240 btrfs_set_header_owner(split, root->root_key.objectid);
2241 write_extent_buffer(split, root->fs_info->fsid,
2242 (unsigned long)btrfs_header_fsid(split),
2243 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002244 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2245 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2246 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002247
Chris Mason5f39d392007-10-15 16:14:19 -04002248
2249 copy_extent_buffer(split, c,
2250 btrfs_node_key_ptr_offset(0),
2251 btrfs_node_key_ptr_offset(mid),
2252 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2253 btrfs_set_header_nritems(split, c_nritems - mid);
2254 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002255 ret = 0;
2256
Chris Mason5f39d392007-10-15 16:14:19 -04002257 btrfs_mark_buffer_dirty(c);
2258 btrfs_mark_buffer_dirty(split);
2259
Chris Masondb945352007-10-15 16:15:53 -04002260 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002261 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002262 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002263 if (wret)
2264 ret = wret;
2265
Chris Mason5de08d72007-02-24 06:24:44 -05002266 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002267 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002268 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002269 free_extent_buffer(c);
2270 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002271 path->slots[level + 1] += 1;
2272 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002273 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002274 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002275 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002276 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002277}
2278
Chris Mason74123bd2007-02-02 11:05:29 -05002279/*
2280 * how many bytes are required to store the items in a leaf. start
2281 * and nr indicate which items in the leaf to check. This totals up the
2282 * space used both by the item structs and the item data
2283 */
Chris Mason5f39d392007-10-15 16:14:19 -04002284static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002285{
2286 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002287 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002288 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002289
2290 if (!nr)
2291 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002292 data_len = btrfs_item_end_nr(l, start);
2293 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002294 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002295 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002296 return data_len;
2297}
2298
Chris Mason74123bd2007-02-02 11:05:29 -05002299/*
Chris Masond4dbff92007-04-04 14:08:15 -04002300 * The space between the end of the leaf items and
2301 * the start of the leaf data. IOW, how much room
2302 * the leaf has left for both items and data
2303 */
Chris Masond3977122009-01-05 21:25:51 -05002304noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002305 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002306{
Chris Mason5f39d392007-10-15 16:14:19 -04002307 int nritems = btrfs_header_nritems(leaf);
2308 int ret;
2309 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2310 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002311 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2312 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002313 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002314 leaf_space_used(leaf, 0, nritems), nritems);
2315 }
2316 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002317}
2318
Chris Mason99d8f832010-07-07 10:51:48 -04002319/*
2320 * min slot controls the lowest index we're willing to push to the
2321 * right. We'll push up to and including min_slot, but no lower
2322 */
Chris Mason44871b12009-03-13 10:04:31 -04002323static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2324 struct btrfs_root *root,
2325 struct btrfs_path *path,
2326 int data_size, int empty,
2327 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002328 int free_space, u32 left_nritems,
2329 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002330{
Chris Mason5f39d392007-10-15 16:14:19 -04002331 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002332 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002333 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002334 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002335 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002336 int push_space = 0;
2337 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002338 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002339 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002340 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002341 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002342 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002343
Chris Mason34a38212007-11-07 13:31:03 -05002344 if (empty)
2345 nr = 0;
2346 else
Chris Mason99d8f832010-07-07 10:51:48 -04002347 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002348
Zheng Yan31840ae2008-09-23 13:14:14 -04002349 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002350 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002351
Chris Mason44871b12009-03-13 10:04:31 -04002352 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002353 i = left_nritems - 1;
2354 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002355 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002356
Zheng Yan31840ae2008-09-23 13:14:14 -04002357 if (!empty && push_items > 0) {
2358 if (path->slots[0] > i)
2359 break;
2360 if (path->slots[0] == i) {
2361 int space = btrfs_leaf_free_space(root, left);
2362 if (space + push_space * 2 > free_space)
2363 break;
2364 }
2365 }
2366
Chris Mason00ec4c52007-02-24 12:47:20 -05002367 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002368 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002369
2370 if (!left->map_token) {
2371 map_extent_buffer(left, (unsigned long)item,
2372 sizeof(struct btrfs_item),
2373 &left->map_token, &left->kaddr,
2374 &left->map_start, &left->map_len,
2375 KM_USER1);
2376 }
2377
2378 this_item_size = btrfs_item_size(left, item);
2379 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002380 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002381
Chris Mason00ec4c52007-02-24 12:47:20 -05002382 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002383 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002384 if (i == 0)
2385 break;
2386 i--;
Chris Masondb945352007-10-15 16:15:53 -04002387 }
2388 if (left->map_token) {
2389 unmap_extent_buffer(left, left->map_token, KM_USER1);
2390 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002391 }
Chris Mason5f39d392007-10-15 16:14:19 -04002392
Chris Mason925baed2008-06-25 16:01:30 -04002393 if (push_items == 0)
2394 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002395
Chris Mason34a38212007-11-07 13:31:03 -05002396 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002397 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002398
Chris Mason00ec4c52007-02-24 12:47:20 -05002399 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002400 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002401
Chris Mason5f39d392007-10-15 16:14:19 -04002402 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002403 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002404
Chris Mason00ec4c52007-02-24 12:47:20 -05002405 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002406 data_end = leaf_data_end(root, right);
2407 memmove_extent_buffer(right,
2408 btrfs_leaf_data(right) + data_end - push_space,
2409 btrfs_leaf_data(right) + data_end,
2410 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2411
Chris Mason00ec4c52007-02-24 12:47:20 -05002412 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002413 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002414 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2415 btrfs_leaf_data(left) + leaf_data_end(root, left),
2416 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002417
2418 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2419 btrfs_item_nr_offset(0),
2420 right_nritems * sizeof(struct btrfs_item));
2421
Chris Mason00ec4c52007-02-24 12:47:20 -05002422 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002423 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2424 btrfs_item_nr_offset(left_nritems - push_items),
2425 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002426
2427 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002428 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002429 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002430 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002431 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002432 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002433 if (!right->map_token) {
2434 map_extent_buffer(right, (unsigned long)item,
2435 sizeof(struct btrfs_item),
2436 &right->map_token, &right->kaddr,
2437 &right->map_start, &right->map_len,
2438 KM_USER1);
2439 }
2440 push_space -= btrfs_item_size(right, item);
2441 btrfs_set_item_offset(right, item, push_space);
2442 }
2443
2444 if (right->map_token) {
2445 unmap_extent_buffer(right, right->map_token, KM_USER1);
2446 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002447 }
Chris Mason7518a232007-03-12 12:01:18 -04002448 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002449 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002450
Chris Mason34a38212007-11-07 13:31:03 -05002451 if (left_nritems)
2452 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002453 else
2454 clean_tree_block(trans, root, left);
2455
Chris Mason5f39d392007-10-15 16:14:19 -04002456 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002457
Chris Mason5f39d392007-10-15 16:14:19 -04002458 btrfs_item_key(right, &disk_key, 0);
2459 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002460 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002461
Chris Mason00ec4c52007-02-24 12:47:20 -05002462 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002463 if (path->slots[0] >= left_nritems) {
2464 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002465 if (btrfs_header_nritems(path->nodes[0]) == 0)
2466 clean_tree_block(trans, root, path->nodes[0]);
2467 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002468 free_extent_buffer(path->nodes[0]);
2469 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002470 path->slots[1] += 1;
2471 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002472 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002473 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002474 }
2475 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002476
2477out_unlock:
2478 btrfs_tree_unlock(right);
2479 free_extent_buffer(right);
2480 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002481}
Chris Mason925baed2008-06-25 16:01:30 -04002482
Chris Mason00ec4c52007-02-24 12:47:20 -05002483/*
Chris Mason44871b12009-03-13 10:04:31 -04002484 * push some data in the path leaf to the right, trying to free up at
2485 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2486 *
2487 * returns 1 if the push failed because the other node didn't have enough
2488 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002489 *
2490 * this will push starting from min_slot to the end of the leaf. It won't
2491 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002492 */
2493static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002494 *root, struct btrfs_path *path,
2495 int min_data_size, int data_size,
2496 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002497{
2498 struct extent_buffer *left = path->nodes[0];
2499 struct extent_buffer *right;
2500 struct extent_buffer *upper;
2501 int slot;
2502 int free_space;
2503 u32 left_nritems;
2504 int ret;
2505
2506 if (!path->nodes[1])
2507 return 1;
2508
2509 slot = path->slots[1];
2510 upper = path->nodes[1];
2511 if (slot >= btrfs_header_nritems(upper) - 1)
2512 return 1;
2513
2514 btrfs_assert_tree_locked(path->nodes[1]);
2515
2516 right = read_node_slot(root, upper, slot + 1);
2517 btrfs_tree_lock(right);
2518 btrfs_set_lock_blocking(right);
2519
2520 free_space = btrfs_leaf_free_space(root, right);
2521 if (free_space < data_size)
2522 goto out_unlock;
2523
2524 /* cow and double check */
2525 ret = btrfs_cow_block(trans, root, right, upper,
2526 slot + 1, &right);
2527 if (ret)
2528 goto out_unlock;
2529
2530 free_space = btrfs_leaf_free_space(root, right);
2531 if (free_space < data_size)
2532 goto out_unlock;
2533
2534 left_nritems = btrfs_header_nritems(left);
2535 if (left_nritems == 0)
2536 goto out_unlock;
2537
Chris Mason99d8f832010-07-07 10:51:48 -04002538 return __push_leaf_right(trans, root, path, min_data_size, empty,
2539 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002540out_unlock:
2541 btrfs_tree_unlock(right);
2542 free_extent_buffer(right);
2543 return 1;
2544}
2545
2546/*
Chris Mason74123bd2007-02-02 11:05:29 -05002547 * push some data in the path leaf to the left, trying to free up at
2548 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002549 *
2550 * max_slot can put a limit on how far into the leaf we'll push items. The
2551 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2552 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002553 */
Chris Mason44871b12009-03-13 10:04:31 -04002554static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2555 struct btrfs_root *root,
2556 struct btrfs_path *path, int data_size,
2557 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002558 int free_space, u32 right_nritems,
2559 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002560{
Chris Mason5f39d392007-10-15 16:14:19 -04002561 struct btrfs_disk_key disk_key;
2562 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002564 int push_space = 0;
2565 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002566 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002567 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002568 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002569 int ret = 0;
2570 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002571 u32 this_item_size;
2572 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573
Chris Mason34a38212007-11-07 13:31:03 -05002574 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002575 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002576 else
Chris Mason99d8f832010-07-07 10:51:48 -04002577 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002578
2579 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002580 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002581 if (!right->map_token) {
2582 map_extent_buffer(right, (unsigned long)item,
2583 sizeof(struct btrfs_item),
2584 &right->map_token, &right->kaddr,
2585 &right->map_start, &right->map_len,
2586 KM_USER1);
2587 }
2588
Zheng Yan31840ae2008-09-23 13:14:14 -04002589 if (!empty && push_items > 0) {
2590 if (path->slots[0] < i)
2591 break;
2592 if (path->slots[0] == i) {
2593 int space = btrfs_leaf_free_space(root, right);
2594 if (space + push_space * 2 > free_space)
2595 break;
2596 }
2597 }
2598
Chris Masonbe0e5c02007-01-26 15:51:26 -05002599 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002600 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002601
2602 this_item_size = btrfs_item_size(right, item);
2603 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002604 break;
Chris Masondb945352007-10-15 16:15:53 -04002605
Chris Masonbe0e5c02007-01-26 15:51:26 -05002606 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002607 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002608 }
Chris Masondb945352007-10-15 16:15:53 -04002609
2610 if (right->map_token) {
2611 unmap_extent_buffer(right, right->map_token, KM_USER1);
2612 right->map_token = NULL;
2613 }
2614
Chris Masonbe0e5c02007-01-26 15:51:26 -05002615 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002616 ret = 1;
2617 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002618 }
Chris Mason34a38212007-11-07 13:31:03 -05002619 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002620 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002621
Chris Masonbe0e5c02007-01-26 15:51:26 -05002622 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002623 copy_extent_buffer(left, right,
2624 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2625 btrfs_item_nr_offset(0),
2626 push_items * sizeof(struct btrfs_item));
2627
Chris Mason123abc82007-03-14 14:14:43 -04002628 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002629 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002630
2631 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002632 leaf_data_end(root, left) - push_space,
2633 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002634 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002635 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002636 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002637 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002638
Chris Masondb945352007-10-15 16:15:53 -04002639 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002640 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002641 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002642
Chris Mason5f39d392007-10-15 16:14:19 -04002643 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002644 if (!left->map_token) {
2645 map_extent_buffer(left, (unsigned long)item,
2646 sizeof(struct btrfs_item),
2647 &left->map_token, &left->kaddr,
2648 &left->map_start, &left->map_len,
2649 KM_USER1);
2650 }
2651
Chris Mason5f39d392007-10-15 16:14:19 -04002652 ioff = btrfs_item_offset(left, item);
2653 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002654 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002655 }
Chris Mason5f39d392007-10-15 16:14:19 -04002656 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002657 if (left->map_token) {
2658 unmap_extent_buffer(left, left->map_token, KM_USER1);
2659 left->map_token = NULL;
2660 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002661
2662 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002663 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002664 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2665 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002666 WARN_ON(1);
2667 }
Chris Mason5f39d392007-10-15 16:14:19 -04002668
Chris Mason34a38212007-11-07 13:31:03 -05002669 if (push_items < right_nritems) {
2670 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2671 leaf_data_end(root, right);
2672 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2673 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2674 btrfs_leaf_data(right) +
2675 leaf_data_end(root, right), push_space);
2676
2677 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002678 btrfs_item_nr_offset(push_items),
2679 (btrfs_header_nritems(right) - push_items) *
2680 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002681 }
Yaneef1c492007-11-26 10:58:13 -05002682 right_nritems -= push_items;
2683 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002684 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002685 for (i = 0; i < right_nritems; i++) {
2686 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002687
2688 if (!right->map_token) {
2689 map_extent_buffer(right, (unsigned long)item,
2690 sizeof(struct btrfs_item),
2691 &right->map_token, &right->kaddr,
2692 &right->map_start, &right->map_len,
2693 KM_USER1);
2694 }
2695
2696 push_space = push_space - btrfs_item_size(right, item);
2697 btrfs_set_item_offset(right, item, push_space);
2698 }
2699 if (right->map_token) {
2700 unmap_extent_buffer(right, right->map_token, KM_USER1);
2701 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002702 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002703
Chris Mason5f39d392007-10-15 16:14:19 -04002704 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002705 if (right_nritems)
2706 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002707 else
2708 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002709
Chris Mason5f39d392007-10-15 16:14:19 -04002710 btrfs_item_key(right, &disk_key, 0);
2711 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002712 if (wret)
2713 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002714
2715 /* then fixup the leaf pointer in the path */
2716 if (path->slots[0] < push_items) {
2717 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002718 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002719 free_extent_buffer(path->nodes[0]);
2720 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002721 path->slots[1] -= 1;
2722 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002723 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002724 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002725 path->slots[0] -= push_items;
2726 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002727 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002728 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002729out:
2730 btrfs_tree_unlock(left);
2731 free_extent_buffer(left);
2732 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002733}
2734
Chris Mason74123bd2007-02-02 11:05:29 -05002735/*
Chris Mason44871b12009-03-13 10:04:31 -04002736 * push some data in the path leaf to the left, trying to free up at
2737 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002738 *
2739 * max_slot can put a limit on how far into the leaf we'll push items. The
2740 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2741 * items
Chris Mason44871b12009-03-13 10:04:31 -04002742 */
2743static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002744 *root, struct btrfs_path *path, int min_data_size,
2745 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002746{
2747 struct extent_buffer *right = path->nodes[0];
2748 struct extent_buffer *left;
2749 int slot;
2750 int free_space;
2751 u32 right_nritems;
2752 int ret = 0;
2753
2754 slot = path->slots[1];
2755 if (slot == 0)
2756 return 1;
2757 if (!path->nodes[1])
2758 return 1;
2759
2760 right_nritems = btrfs_header_nritems(right);
2761 if (right_nritems == 0)
2762 return 1;
2763
2764 btrfs_assert_tree_locked(path->nodes[1]);
2765
2766 left = read_node_slot(root, path->nodes[1], slot - 1);
2767 btrfs_tree_lock(left);
2768 btrfs_set_lock_blocking(left);
2769
2770 free_space = btrfs_leaf_free_space(root, left);
2771 if (free_space < data_size) {
2772 ret = 1;
2773 goto out;
2774 }
2775
2776 /* cow and double check */
2777 ret = btrfs_cow_block(trans, root, left,
2778 path->nodes[1], slot - 1, &left);
2779 if (ret) {
2780 /* we hit -ENOSPC, but it isn't fatal here */
2781 ret = 1;
2782 goto out;
2783 }
2784
2785 free_space = btrfs_leaf_free_space(root, left);
2786 if (free_space < data_size) {
2787 ret = 1;
2788 goto out;
2789 }
2790
Chris Mason99d8f832010-07-07 10:51:48 -04002791 return __push_leaf_left(trans, root, path, min_data_size,
2792 empty, left, free_space, right_nritems,
2793 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002794out:
2795 btrfs_tree_unlock(left);
2796 free_extent_buffer(left);
2797 return ret;
2798}
2799
2800/*
Chris Mason74123bd2007-02-02 11:05:29 -05002801 * split the path's leaf in two, making sure there is at least data_size
2802 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002803 *
2804 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002805 */
Chris Mason44871b12009-03-13 10:04:31 -04002806static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002807 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002808 struct btrfs_path *path,
2809 struct extent_buffer *l,
2810 struct extent_buffer *right,
2811 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002812{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002813 int data_copy_size;
2814 int rt_data_off;
2815 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002816 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002817 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002818 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002819
Chris Mason5f39d392007-10-15 16:14:19 -04002820 nritems = nritems - mid;
2821 btrfs_set_header_nritems(right, nritems);
2822 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2823
2824 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2825 btrfs_item_nr_offset(mid),
2826 nritems * sizeof(struct btrfs_item));
2827
2828 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002829 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2830 data_copy_size, btrfs_leaf_data(l) +
2831 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002832
Chris Mason5f39d392007-10-15 16:14:19 -04002833 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2834 btrfs_item_end_nr(l, mid);
2835
2836 for (i = 0; i < nritems; i++) {
2837 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002838 u32 ioff;
2839
2840 if (!right->map_token) {
2841 map_extent_buffer(right, (unsigned long)item,
2842 sizeof(struct btrfs_item),
2843 &right->map_token, &right->kaddr,
2844 &right->map_start, &right->map_len,
2845 KM_USER1);
2846 }
2847
2848 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002849 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002850 }
Chris Mason74123bd2007-02-02 11:05:29 -05002851
Chris Masondb945352007-10-15 16:15:53 -04002852 if (right->map_token) {
2853 unmap_extent_buffer(right, right->map_token, KM_USER1);
2854 right->map_token = NULL;
2855 }
2856
Chris Mason5f39d392007-10-15 16:14:19 -04002857 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002858 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002859 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002860 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2861 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002862 if (wret)
2863 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002864
2865 btrfs_mark_buffer_dirty(right);
2866 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002867 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002868
Chris Masonbe0e5c02007-01-26 15:51:26 -05002869 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002870 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002871 free_extent_buffer(path->nodes[0]);
2872 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002873 path->slots[0] -= mid;
2874 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002875 } else {
2876 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002877 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002878 }
Chris Mason5f39d392007-10-15 16:14:19 -04002879
Chris Masoneb60cea2007-02-02 09:18:22 -05002880 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002881
Chris Mason44871b12009-03-13 10:04:31 -04002882 return ret;
2883}
2884
2885/*
Chris Mason99d8f832010-07-07 10:51:48 -04002886 * double splits happen when we need to insert a big item in the middle
2887 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2888 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2889 * A B C
2890 *
2891 * We avoid this by trying to push the items on either side of our target
2892 * into the adjacent leaves. If all goes well we can avoid the double split
2893 * completely.
2894 */
2895static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2896 struct btrfs_root *root,
2897 struct btrfs_path *path,
2898 int data_size)
2899{
2900 int ret;
2901 int progress = 0;
2902 int slot;
2903 u32 nritems;
2904
2905 slot = path->slots[0];
2906
2907 /*
2908 * try to push all the items after our slot into the
2909 * right leaf
2910 */
2911 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2912 if (ret < 0)
2913 return ret;
2914
2915 if (ret == 0)
2916 progress++;
2917
2918 nritems = btrfs_header_nritems(path->nodes[0]);
2919 /*
2920 * our goal is to get our slot at the start or end of a leaf. If
2921 * we've done so we're done
2922 */
2923 if (path->slots[0] == 0 || path->slots[0] == nritems)
2924 return 0;
2925
2926 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2927 return 0;
2928
2929 /* try to push all the items before our slot into the next leaf */
2930 slot = path->slots[0];
2931 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2932 if (ret < 0)
2933 return ret;
2934
2935 if (ret == 0)
2936 progress++;
2937
2938 if (progress)
2939 return 0;
2940 return 1;
2941}
2942
2943/*
Chris Mason44871b12009-03-13 10:04:31 -04002944 * split the path's leaf in two, making sure there is at least data_size
2945 * available for the resulting leaf level of the path.
2946 *
2947 * returns 0 if all went well and < 0 on failure.
2948 */
2949static noinline int split_leaf(struct btrfs_trans_handle *trans,
2950 struct btrfs_root *root,
2951 struct btrfs_key *ins_key,
2952 struct btrfs_path *path, int data_size,
2953 int extend)
2954{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002955 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002956 struct extent_buffer *l;
2957 u32 nritems;
2958 int mid;
2959 int slot;
2960 struct extent_buffer *right;
2961 int ret = 0;
2962 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002963 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002964 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002965 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002966
Yan, Zhenga5719522009-09-24 09:17:31 -04002967 l = path->nodes[0];
2968 slot = path->slots[0];
2969 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2970 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2971 return -EOVERFLOW;
2972
Chris Mason44871b12009-03-13 10:04:31 -04002973 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002974 if (data_size) {
2975 wret = push_leaf_right(trans, root, path, data_size,
2976 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002977 if (wret < 0)
2978 return wret;
2979 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002980 wret = push_leaf_left(trans, root, path, data_size,
2981 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002982 if (wret < 0)
2983 return wret;
2984 }
2985 l = path->nodes[0];
2986
2987 /* did the pushes work? */
2988 if (btrfs_leaf_free_space(root, l) >= data_size)
2989 return 0;
2990 }
2991
2992 if (!path->nodes[1]) {
2993 ret = insert_new_root(trans, root, path, 1);
2994 if (ret)
2995 return ret;
2996 }
2997again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002998 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002999 l = path->nodes[0];
3000 slot = path->slots[0];
3001 nritems = btrfs_header_nritems(l);
3002 mid = (nritems + 1) / 2;
3003
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003004 if (mid <= slot) {
3005 if (nritems == 1 ||
3006 leaf_space_used(l, mid, nritems - mid) + data_size >
3007 BTRFS_LEAF_DATA_SIZE(root)) {
3008 if (slot >= nritems) {
3009 split = 0;
3010 } else {
3011 mid = slot;
3012 if (mid != nritems &&
3013 leaf_space_used(l, mid, nritems - mid) +
3014 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003015 if (data_size && !tried_avoid_double)
3016 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003017 split = 2;
3018 }
3019 }
3020 }
3021 } else {
3022 if (leaf_space_used(l, 0, mid) + data_size >
3023 BTRFS_LEAF_DATA_SIZE(root)) {
3024 if (!extend && data_size && slot == 0) {
3025 split = 0;
3026 } else if ((extend || !data_size) && slot == 0) {
3027 mid = 1;
3028 } else {
3029 mid = slot;
3030 if (mid != nritems &&
3031 leaf_space_used(l, mid, nritems - mid) +
3032 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003033 if (data_size && !tried_avoid_double)
3034 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035 split = 2 ;
3036 }
3037 }
3038 }
3039 }
3040
3041 if (split == 0)
3042 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3043 else
3044 btrfs_item_key(l, &disk_key, mid);
3045
3046 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003047 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003048 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003049 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003050 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003051
3052 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003053
3054 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3055 btrfs_set_header_bytenr(right, right->start);
3056 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003057 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003058 btrfs_set_header_owner(right, root->root_key.objectid);
3059 btrfs_set_header_level(right, 0);
3060 write_extent_buffer(right, root->fs_info->fsid,
3061 (unsigned long)btrfs_header_fsid(right),
3062 BTRFS_FSID_SIZE);
3063
3064 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3065 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3066 BTRFS_UUID_SIZE);
3067
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003068 if (split == 0) {
3069 if (mid <= slot) {
3070 btrfs_set_header_nritems(right, 0);
3071 wret = insert_ptr(trans, root, path,
3072 &disk_key, right->start,
3073 path->slots[1] + 1, 1);
3074 if (wret)
3075 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003076
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003077 btrfs_tree_unlock(path->nodes[0]);
3078 free_extent_buffer(path->nodes[0]);
3079 path->nodes[0] = right;
3080 path->slots[0] = 0;
3081 path->slots[1] += 1;
3082 } else {
3083 btrfs_set_header_nritems(right, 0);
3084 wret = insert_ptr(trans, root, path,
3085 &disk_key,
3086 right->start,
3087 path->slots[1], 1);
3088 if (wret)
3089 ret = wret;
3090 btrfs_tree_unlock(path->nodes[0]);
3091 free_extent_buffer(path->nodes[0]);
3092 path->nodes[0] = right;
3093 path->slots[0] = 0;
3094 if (path->slots[1] == 0) {
3095 wret = fixup_low_keys(trans, root,
3096 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003097 if (wret)
3098 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003099 }
3100 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003101 btrfs_mark_buffer_dirty(right);
3102 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003103 }
3104
3105 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3106 BUG_ON(ret);
3107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003108 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003109 BUG_ON(num_doubles != 0);
3110 num_doubles++;
3111 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003112 }
Chris Mason44871b12009-03-13 10:04:31 -04003113
Chris Masonbe0e5c02007-01-26 15:51:26 -05003114 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04003115
3116push_for_double:
3117 push_for_double_split(trans, root, path, data_size);
3118 tried_avoid_double = 1;
3119 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3120 return 0;
3121 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003122}
3123
Yan, Zhengad48fd752009-11-12 09:33:58 +00003124static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3125 struct btrfs_root *root,
3126 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003127{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003128 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003129 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003130 struct btrfs_file_extent_item *fi;
3131 u64 extent_len = 0;
3132 u32 item_size;
3133 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003134
3135 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003136 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3137
3138 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3139 key.type != BTRFS_EXTENT_CSUM_KEY);
3140
3141 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3142 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003143
3144 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003145 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3146 fi = btrfs_item_ptr(leaf, path->slots[0],
3147 struct btrfs_file_extent_item);
3148 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3149 }
Chris Mason459931e2008-12-10 09:10:46 -05003150 btrfs_release_path(root, path);
3151
Chris Mason459931e2008-12-10 09:10:46 -05003152 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003153 path->search_for_split = 1;
3154 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003155 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003156 if (ret < 0)
3157 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003158
Yan, Zhengad48fd752009-11-12 09:33:58 +00003159 ret = -EAGAIN;
3160 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003161 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003162 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3163 goto err;
3164
Chris Mason109f6ae2010-04-02 09:20:18 -04003165 /* the leaf has changed, it now has room. return now */
3166 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3167 goto err;
3168
Yan, Zhengad48fd752009-11-12 09:33:58 +00003169 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3170 fi = btrfs_item_ptr(leaf, path->slots[0],
3171 struct btrfs_file_extent_item);
3172 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3173 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003174 }
3175
Chris Masonb9473432009-03-13 11:00:37 -04003176 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003177 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003178 if (ret)
3179 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003180
Yan, Zhengad48fd752009-11-12 09:33:58 +00003181 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003182 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003183 return 0;
3184err:
3185 path->keep_locks = 0;
3186 return ret;
3187}
3188
3189static noinline int split_item(struct btrfs_trans_handle *trans,
3190 struct btrfs_root *root,
3191 struct btrfs_path *path,
3192 struct btrfs_key *new_key,
3193 unsigned long split_offset)
3194{
3195 struct extent_buffer *leaf;
3196 struct btrfs_item *item;
3197 struct btrfs_item *new_item;
3198 int slot;
3199 char *buf;
3200 u32 nritems;
3201 u32 item_size;
3202 u32 orig_offset;
3203 struct btrfs_disk_key disk_key;
3204
Chris Masonb9473432009-03-13 11:00:37 -04003205 leaf = path->nodes[0];
3206 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3207
Chris Masonb4ce94d2009-02-04 09:25:08 -05003208 btrfs_set_path_blocking(path);
3209
Chris Mason459931e2008-12-10 09:10:46 -05003210 item = btrfs_item_nr(leaf, path->slots[0]);
3211 orig_offset = btrfs_item_offset(leaf, item);
3212 item_size = btrfs_item_size(leaf, item);
3213
Chris Mason459931e2008-12-10 09:10:46 -05003214 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003215 if (!buf)
3216 return -ENOMEM;
3217
Chris Mason459931e2008-12-10 09:10:46 -05003218 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3219 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003220
Chris Mason459931e2008-12-10 09:10:46 -05003221 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003222 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003223 if (slot != nritems) {
3224 /* shift the items */
3225 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003226 btrfs_item_nr_offset(slot),
3227 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003228 }
3229
3230 btrfs_cpu_key_to_disk(&disk_key, new_key);
3231 btrfs_set_item_key(leaf, &disk_key, slot);
3232
3233 new_item = btrfs_item_nr(leaf, slot);
3234
3235 btrfs_set_item_offset(leaf, new_item, orig_offset);
3236 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3237
3238 btrfs_set_item_offset(leaf, item,
3239 orig_offset + item_size - split_offset);
3240 btrfs_set_item_size(leaf, item, split_offset);
3241
3242 btrfs_set_header_nritems(leaf, nritems + 1);
3243
3244 /* write the data for the start of the original item */
3245 write_extent_buffer(leaf, buf,
3246 btrfs_item_ptr_offset(leaf, path->slots[0]),
3247 split_offset);
3248
3249 /* write the data for the new item */
3250 write_extent_buffer(leaf, buf + split_offset,
3251 btrfs_item_ptr_offset(leaf, slot),
3252 item_size - split_offset);
3253 btrfs_mark_buffer_dirty(leaf);
3254
Yan, Zhengad48fd752009-11-12 09:33:58 +00003255 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003256 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003257 return 0;
3258}
3259
3260/*
3261 * This function splits a single item into two items,
3262 * giving 'new_key' to the new item and splitting the
3263 * old one at split_offset (from the start of the item).
3264 *
3265 * The path may be released by this operation. After
3266 * the split, the path is pointing to the old item. The
3267 * new item is going to be in the same node as the old one.
3268 *
3269 * Note, the item being split must be smaller enough to live alone on
3270 * a tree block with room for one extra struct btrfs_item
3271 *
3272 * This allows us to split the item in place, keeping a lock on the
3273 * leaf the entire time.
3274 */
3275int btrfs_split_item(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *root,
3277 struct btrfs_path *path,
3278 struct btrfs_key *new_key,
3279 unsigned long split_offset)
3280{
3281 int ret;
3282 ret = setup_leaf_for_split(trans, root, path,
3283 sizeof(struct btrfs_item));
3284 if (ret)
3285 return ret;
3286
3287 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003288 return ret;
3289}
3290
3291/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003292 * This function duplicate a item, giving 'new_key' to the new item.
3293 * It guarantees both items live in the same tree leaf and the new item
3294 * is contiguous with the original item.
3295 *
3296 * This allows us to split file extent in place, keeping a lock on the
3297 * leaf the entire time.
3298 */
3299int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3300 struct btrfs_root *root,
3301 struct btrfs_path *path,
3302 struct btrfs_key *new_key)
3303{
3304 struct extent_buffer *leaf;
3305 int ret;
3306 u32 item_size;
3307
3308 leaf = path->nodes[0];
3309 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3310 ret = setup_leaf_for_split(trans, root, path,
3311 item_size + sizeof(struct btrfs_item));
3312 if (ret)
3313 return ret;
3314
3315 path->slots[0]++;
3316 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3317 item_size, item_size +
3318 sizeof(struct btrfs_item), 1);
3319 BUG_ON(ret);
3320
3321 leaf = path->nodes[0];
3322 memcpy_extent_buffer(leaf,
3323 btrfs_item_ptr_offset(leaf, path->slots[0]),
3324 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3325 item_size);
3326 return 0;
3327}
3328
3329/*
Chris Masond352ac62008-09-29 15:18:18 -04003330 * make the item pointed to by the path smaller. new_size indicates
3331 * how small to make it, and from_end tells us if we just chop bytes
3332 * off the end of the item or if we shift the item to chop bytes off
3333 * the front.
3334 */
Chris Masonb18c6682007-04-17 13:26:50 -04003335int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3336 struct btrfs_root *root,
3337 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003338 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003339{
3340 int ret = 0;
3341 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003342 struct extent_buffer *leaf;
3343 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003344 u32 nritems;
3345 unsigned int data_end;
3346 unsigned int old_data_start;
3347 unsigned int old_size;
3348 unsigned int size_diff;
3349 int i;
3350
Chris Mason5f39d392007-10-15 16:14:19 -04003351 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003352 slot = path->slots[0];
3353
3354 old_size = btrfs_item_size_nr(leaf, slot);
3355 if (old_size == new_size)
3356 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003357
Chris Mason5f39d392007-10-15 16:14:19 -04003358 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003359 data_end = leaf_data_end(root, leaf);
3360
Chris Mason5f39d392007-10-15 16:14:19 -04003361 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003362
Chris Masonb18c6682007-04-17 13:26:50 -04003363 size_diff = old_size - new_size;
3364
3365 BUG_ON(slot < 0);
3366 BUG_ON(slot >= nritems);
3367
3368 /*
3369 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3370 */
3371 /* first correct the data pointers */
3372 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003373 u32 ioff;
3374 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003375
3376 if (!leaf->map_token) {
3377 map_extent_buffer(leaf, (unsigned long)item,
3378 sizeof(struct btrfs_item),
3379 &leaf->map_token, &leaf->kaddr,
3380 &leaf->map_start, &leaf->map_len,
3381 KM_USER1);
3382 }
3383
Chris Mason5f39d392007-10-15 16:14:19 -04003384 ioff = btrfs_item_offset(leaf, item);
3385 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003386 }
Chris Masondb945352007-10-15 16:15:53 -04003387
3388 if (leaf->map_token) {
3389 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3390 leaf->map_token = NULL;
3391 }
3392
Chris Masonb18c6682007-04-17 13:26:50 -04003393 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003394 if (from_end) {
3395 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3396 data_end + size_diff, btrfs_leaf_data(leaf) +
3397 data_end, old_data_start + new_size - data_end);
3398 } else {
3399 struct btrfs_disk_key disk_key;
3400 u64 offset;
3401
3402 btrfs_item_key(leaf, &disk_key, slot);
3403
3404 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3405 unsigned long ptr;
3406 struct btrfs_file_extent_item *fi;
3407
3408 fi = btrfs_item_ptr(leaf, slot,
3409 struct btrfs_file_extent_item);
3410 fi = (struct btrfs_file_extent_item *)(
3411 (unsigned long)fi - size_diff);
3412
3413 if (btrfs_file_extent_type(leaf, fi) ==
3414 BTRFS_FILE_EXTENT_INLINE) {
3415 ptr = btrfs_item_ptr_offset(leaf, slot);
3416 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003417 (unsigned long)fi,
3418 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003419 disk_bytenr));
3420 }
3421 }
3422
3423 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3424 data_end + size_diff, btrfs_leaf_data(leaf) +
3425 data_end, old_data_start - data_end);
3426
3427 offset = btrfs_disk_key_offset(&disk_key);
3428 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3429 btrfs_set_item_key(leaf, &disk_key, slot);
3430 if (slot == 0)
3431 fixup_low_keys(trans, root, path, &disk_key, 1);
3432 }
Chris Mason5f39d392007-10-15 16:14:19 -04003433
3434 item = btrfs_item_nr(leaf, slot);
3435 btrfs_set_item_size(leaf, item, new_size);
3436 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003437
3438 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003439 if (btrfs_leaf_free_space(root, leaf) < 0) {
3440 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003441 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003442 }
Chris Masonb18c6682007-04-17 13:26:50 -04003443 return ret;
3444}
3445
Chris Masond352ac62008-09-29 15:18:18 -04003446/*
3447 * make the item pointed to by the path bigger, data_size is the new size.
3448 */
Chris Mason5f39d392007-10-15 16:14:19 -04003449int btrfs_extend_item(struct btrfs_trans_handle *trans,
3450 struct btrfs_root *root, struct btrfs_path *path,
3451 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003452{
3453 int ret = 0;
3454 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003455 struct extent_buffer *leaf;
3456 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003457 u32 nritems;
3458 unsigned int data_end;
3459 unsigned int old_data;
3460 unsigned int old_size;
3461 int i;
3462
Chris Mason5f39d392007-10-15 16:14:19 -04003463 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003464
Chris Mason5f39d392007-10-15 16:14:19 -04003465 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003466 data_end = leaf_data_end(root, leaf);
3467
Chris Mason5f39d392007-10-15 16:14:19 -04003468 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3469 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003470 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003471 }
Chris Mason6567e832007-04-16 09:22:45 -04003472 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003473 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003474
3475 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003476 if (slot >= nritems) {
3477 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003478 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3479 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003480 BUG_ON(1);
3481 }
Chris Mason6567e832007-04-16 09:22:45 -04003482
3483 /*
3484 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3485 */
3486 /* first correct the data pointers */
3487 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003488 u32 ioff;
3489 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003490
3491 if (!leaf->map_token) {
3492 map_extent_buffer(leaf, (unsigned long)item,
3493 sizeof(struct btrfs_item),
3494 &leaf->map_token, &leaf->kaddr,
3495 &leaf->map_start, &leaf->map_len,
3496 KM_USER1);
3497 }
Chris Mason5f39d392007-10-15 16:14:19 -04003498 ioff = btrfs_item_offset(leaf, item);
3499 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003500 }
Chris Mason5f39d392007-10-15 16:14:19 -04003501
Chris Masondb945352007-10-15 16:15:53 -04003502 if (leaf->map_token) {
3503 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3504 leaf->map_token = NULL;
3505 }
3506
Chris Mason6567e832007-04-16 09:22:45 -04003507 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003508 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003509 data_end - data_size, btrfs_leaf_data(leaf) +
3510 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003511
Chris Mason6567e832007-04-16 09:22:45 -04003512 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003513 old_size = btrfs_item_size_nr(leaf, slot);
3514 item = btrfs_item_nr(leaf, slot);
3515 btrfs_set_item_size(leaf, item, old_size + data_size);
3516 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003517
3518 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003519 if (btrfs_leaf_free_space(root, leaf) < 0) {
3520 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003521 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003522 }
Chris Mason6567e832007-04-16 09:22:45 -04003523 return ret;
3524}
3525
Chris Mason74123bd2007-02-02 11:05:29 -05003526/*
Chris Masond352ac62008-09-29 15:18:18 -04003527 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003528 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003529 * Returns the number of keys that were inserted.
3530 */
3531int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3532 struct btrfs_root *root,
3533 struct btrfs_path *path,
3534 struct btrfs_key *cpu_key, u32 *data_size,
3535 int nr)
3536{
3537 struct extent_buffer *leaf;
3538 struct btrfs_item *item;
3539 int ret = 0;
3540 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003541 int i;
3542 u32 nritems;
3543 u32 total_data = 0;
3544 u32 total_size = 0;
3545 unsigned int data_end;
3546 struct btrfs_disk_key disk_key;
3547 struct btrfs_key found_key;
3548
Yan Zheng87b29b22008-12-17 10:21:48 -05003549 for (i = 0; i < nr; i++) {
3550 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3551 BTRFS_LEAF_DATA_SIZE(root)) {
3552 break;
3553 nr = i;
3554 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003555 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003556 total_size += data_size[i] + sizeof(struct btrfs_item);
3557 }
3558 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003559
Josef Bacikf3465ca2008-11-12 14:19:50 -05003560 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3561 if (ret == 0)
3562 return -EEXIST;
3563 if (ret < 0)
3564 goto out;
3565
Josef Bacikf3465ca2008-11-12 14:19:50 -05003566 leaf = path->nodes[0];
3567
3568 nritems = btrfs_header_nritems(leaf);
3569 data_end = leaf_data_end(root, leaf);
3570
3571 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3572 for (i = nr; i >= 0; i--) {
3573 total_data -= data_size[i];
3574 total_size -= data_size[i] + sizeof(struct btrfs_item);
3575 if (total_size < btrfs_leaf_free_space(root, leaf))
3576 break;
3577 }
3578 nr = i;
3579 }
3580
3581 slot = path->slots[0];
3582 BUG_ON(slot < 0);
3583
3584 if (slot != nritems) {
3585 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3586
3587 item = btrfs_item_nr(leaf, slot);
3588 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3589
3590 /* figure out how many keys we can insert in here */
3591 total_data = data_size[0];
3592 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003593 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003594 break;
3595 total_data += data_size[i];
3596 }
3597 nr = i;
3598
3599 if (old_data < data_end) {
3600 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003601 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003602 slot, old_data, data_end);
3603 BUG_ON(1);
3604 }
3605 /*
3606 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3607 */
3608 /* first correct the data pointers */
3609 WARN_ON(leaf->map_token);
3610 for (i = slot; i < nritems; i++) {
3611 u32 ioff;
3612
3613 item = btrfs_item_nr(leaf, i);
3614 if (!leaf->map_token) {
3615 map_extent_buffer(leaf, (unsigned long)item,
3616 sizeof(struct btrfs_item),
3617 &leaf->map_token, &leaf->kaddr,
3618 &leaf->map_start, &leaf->map_len,
3619 KM_USER1);
3620 }
3621
3622 ioff = btrfs_item_offset(leaf, item);
3623 btrfs_set_item_offset(leaf, item, ioff - total_data);
3624 }
3625 if (leaf->map_token) {
3626 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3627 leaf->map_token = NULL;
3628 }
3629
3630 /* shift the items */
3631 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3632 btrfs_item_nr_offset(slot),
3633 (nritems - slot) * sizeof(struct btrfs_item));
3634
3635 /* shift the data */
3636 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3637 data_end - total_data, btrfs_leaf_data(leaf) +
3638 data_end, old_data - data_end);
3639 data_end = old_data;
3640 } else {
3641 /*
3642 * this sucks but it has to be done, if we are inserting at
3643 * the end of the leaf only insert 1 of the items, since we
3644 * have no way of knowing whats on the next leaf and we'd have
3645 * to drop our current locks to figure it out
3646 */
3647 nr = 1;
3648 }
3649
3650 /* setup the item for the new data */
3651 for (i = 0; i < nr; i++) {
3652 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3653 btrfs_set_item_key(leaf, &disk_key, slot + i);
3654 item = btrfs_item_nr(leaf, slot + i);
3655 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3656 data_end -= data_size[i];
3657 btrfs_set_item_size(leaf, item, data_size[i]);
3658 }
3659 btrfs_set_header_nritems(leaf, nritems + nr);
3660 btrfs_mark_buffer_dirty(leaf);
3661
3662 ret = 0;
3663 if (slot == 0) {
3664 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3665 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3666 }
3667
3668 if (btrfs_leaf_free_space(root, leaf) < 0) {
3669 btrfs_print_leaf(root, leaf);
3670 BUG();
3671 }
3672out:
3673 if (!ret)
3674 ret = nr;
3675 return ret;
3676}
3677
3678/*
Chris Mason44871b12009-03-13 10:04:31 -04003679 * this is a helper for btrfs_insert_empty_items, the main goal here is
3680 * to save stack depth by doing the bulk of the work in a function
3681 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003682 */
Chris Mason44871b12009-03-13 10:04:31 -04003683static noinline_for_stack int
3684setup_items_for_insert(struct btrfs_trans_handle *trans,
3685 struct btrfs_root *root, struct btrfs_path *path,
3686 struct btrfs_key *cpu_key, u32 *data_size,
3687 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003688{
Chris Mason5f39d392007-10-15 16:14:19 -04003689 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003690 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003691 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003692 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003693 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003694 int ret;
3695 struct extent_buffer *leaf;
3696 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003697
Chris Mason5f39d392007-10-15 16:14:19 -04003698 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003699 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003700
Chris Mason5f39d392007-10-15 16:14:19 -04003701 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003702 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003703
Chris Masonf25956c2008-09-12 15:32:53 -04003704 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003705 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003706 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003707 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003708 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003709 }
Chris Mason5f39d392007-10-15 16:14:19 -04003710
Chris Masonbe0e5c02007-01-26 15:51:26 -05003711 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003712 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003713
Chris Mason5f39d392007-10-15 16:14:19 -04003714 if (old_data < data_end) {
3715 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003716 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003717 slot, old_data, data_end);
3718 BUG_ON(1);
3719 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003720 /*
3721 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3722 */
3723 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003724 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003725 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003726 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003727
Chris Mason5f39d392007-10-15 16:14:19 -04003728 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003729 if (!leaf->map_token) {
3730 map_extent_buffer(leaf, (unsigned long)item,
3731 sizeof(struct btrfs_item),
3732 &leaf->map_token, &leaf->kaddr,
3733 &leaf->map_start, &leaf->map_len,
3734 KM_USER1);
3735 }
3736
Chris Mason5f39d392007-10-15 16:14:19 -04003737 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003738 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003739 }
Chris Masondb945352007-10-15 16:15:53 -04003740 if (leaf->map_token) {
3741 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3742 leaf->map_token = NULL;
3743 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003744
3745 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003746 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003747 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003748 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003749
3750 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003751 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003752 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003753 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003754 data_end = old_data;
3755 }
Chris Mason5f39d392007-10-15 16:14:19 -04003756
Chris Mason62e27492007-03-15 12:56:47 -04003757 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003758 for (i = 0; i < nr; i++) {
3759 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3760 btrfs_set_item_key(leaf, &disk_key, slot + i);
3761 item = btrfs_item_nr(leaf, slot + i);
3762 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3763 data_end -= data_size[i];
3764 btrfs_set_item_size(leaf, item, data_size[i]);
3765 }
Chris Mason44871b12009-03-13 10:04:31 -04003766
Chris Mason9c583092008-01-29 15:15:18 -05003767 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003768
3769 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003770 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003771 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003772 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003773 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003774 }
Chris Masonb9473432009-03-13 11:00:37 -04003775 btrfs_unlock_up_safe(path, 1);
3776 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003777
Chris Mason5f39d392007-10-15 16:14:19 -04003778 if (btrfs_leaf_free_space(root, leaf) < 0) {
3779 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003780 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003781 }
Chris Mason44871b12009-03-13 10:04:31 -04003782 return ret;
3783}
3784
3785/*
3786 * Given a key and some data, insert items into the tree.
3787 * This does all the path init required, making room in the tree if needed.
3788 */
3789int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3790 struct btrfs_root *root,
3791 struct btrfs_path *path,
3792 struct btrfs_key *cpu_key, u32 *data_size,
3793 int nr)
3794{
Chris Mason44871b12009-03-13 10:04:31 -04003795 int ret = 0;
3796 int slot;
3797 int i;
3798 u32 total_size = 0;
3799 u32 total_data = 0;
3800
3801 for (i = 0; i < nr; i++)
3802 total_data += data_size[i];
3803
3804 total_size = total_data + (nr * sizeof(struct btrfs_item));
3805 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3806 if (ret == 0)
3807 return -EEXIST;
3808 if (ret < 0)
3809 goto out;
3810
Chris Mason44871b12009-03-13 10:04:31 -04003811 slot = path->slots[0];
3812 BUG_ON(slot < 0);
3813
3814 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3815 total_data, total_size, nr);
3816
Chris Masoned2ff2c2007-03-01 18:59:40 -05003817out:
Chris Mason62e27492007-03-15 12:56:47 -04003818 return ret;
3819}
3820
3821/*
3822 * Given a key and some data, insert an item into the tree.
3823 * This does all the path init required, making room in the tree if needed.
3824 */
Chris Masone089f052007-03-16 16:20:31 -04003825int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3826 *root, struct btrfs_key *cpu_key, void *data, u32
3827 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003828{
3829 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003830 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003831 struct extent_buffer *leaf;
3832 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003833
Chris Mason2c90e5d2007-04-02 10:50:19 -04003834 path = btrfs_alloc_path();
3835 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003836 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003837 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003838 leaf = path->nodes[0];
3839 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3840 write_extent_buffer(leaf, data, ptr, data_size);
3841 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003842 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003843 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003844 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003845}
3846
Chris Mason74123bd2007-02-02 11:05:29 -05003847/*
Chris Mason5de08d72007-02-24 06:24:44 -05003848 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003849 *
Chris Masond352ac62008-09-29 15:18:18 -04003850 * the tree should have been previously balanced so the deletion does not
3851 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003852 */
Chris Masone089f052007-03-16 16:20:31 -04003853static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3854 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855{
Chris Mason5f39d392007-10-15 16:14:19 -04003856 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003857 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003858 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003859 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003860
Chris Mason5f39d392007-10-15 16:14:19 -04003861 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003862 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003863 memmove_extent_buffer(parent,
3864 btrfs_node_key_ptr_offset(slot),
3865 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003866 sizeof(struct btrfs_key_ptr) *
3867 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003868 }
Chris Mason7518a232007-03-12 12:01:18 -04003869 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003870 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003871 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003872 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003873 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003874 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003875 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003876 struct btrfs_disk_key disk_key;
3877
3878 btrfs_node_key(parent, &disk_key, 0);
3879 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003880 if (wret)
3881 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003882 }
Chris Masond6025572007-03-30 14:27:56 -04003883 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003884 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003885}
3886
Chris Mason74123bd2007-02-02 11:05:29 -05003887/*
Chris Mason323ac952008-10-01 19:05:46 -04003888 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003889 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003890 *
3891 * This deletes the pointer in path->nodes[1] and frees the leaf
3892 * block extent. zero is returned if it all worked out, < 0 otherwise.
3893 *
3894 * The path must have already been setup for deleting the leaf, including
3895 * all the proper balancing. path->nodes[1] must be locked.
3896 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003897static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3898 struct btrfs_root *root,
3899 struct btrfs_path *path,
3900 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003901{
3902 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003903
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003904 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003905 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3906 if (ret)
3907 return ret;
3908
Chris Mason4d081c42009-02-04 09:31:28 -05003909 /*
3910 * btrfs_free_extent is expensive, we want to make sure we
3911 * aren't holding any locks when we call it
3912 */
3913 btrfs_unlock_up_safe(path, 0);
3914
Yan, Zhengf0486c62010-05-16 10:46:25 -04003915 root_sub_used(root, leaf->len);
3916
3917 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3918 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003919}
3920/*
Chris Mason74123bd2007-02-02 11:05:29 -05003921 * delete the item at the leaf level in path. If that empties
3922 * the leaf, remove it from the tree
3923 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003924int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3925 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003926{
Chris Mason5f39d392007-10-15 16:14:19 -04003927 struct extent_buffer *leaf;
3928 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003929 int last_off;
3930 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003931 int ret = 0;
3932 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003933 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003934 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003935
Chris Mason5f39d392007-10-15 16:14:19 -04003936 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003937 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3938
3939 for (i = 0; i < nr; i++)
3940 dsize += btrfs_item_size_nr(leaf, slot + i);
3941
Chris Mason5f39d392007-10-15 16:14:19 -04003942 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003943
Chris Mason85e21ba2008-01-29 15:11:36 -05003944 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003945 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003946
3947 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003948 data_end + dsize,
3949 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003950 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003951
Chris Mason85e21ba2008-01-29 15:11:36 -05003952 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003953 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003954
Chris Mason5f39d392007-10-15 16:14:19 -04003955 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003956 if (!leaf->map_token) {
3957 map_extent_buffer(leaf, (unsigned long)item,
3958 sizeof(struct btrfs_item),
3959 &leaf->map_token, &leaf->kaddr,
3960 &leaf->map_start, &leaf->map_len,
3961 KM_USER1);
3962 }
Chris Mason5f39d392007-10-15 16:14:19 -04003963 ioff = btrfs_item_offset(leaf, item);
3964 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003965 }
Chris Masondb945352007-10-15 16:15:53 -04003966
3967 if (leaf->map_token) {
3968 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3969 leaf->map_token = NULL;
3970 }
3971
Chris Mason5f39d392007-10-15 16:14:19 -04003972 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003973 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003974 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003975 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003976 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003977 btrfs_set_header_nritems(leaf, nritems - nr);
3978 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003979
Chris Mason74123bd2007-02-02 11:05:29 -05003980 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003981 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003982 if (leaf == root->node) {
3983 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003984 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003985 btrfs_set_path_blocking(path);
3986 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003987 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003988 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003989 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003990 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003991 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003992 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003993 struct btrfs_disk_key disk_key;
3994
3995 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003996 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003997 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003998 if (wret)
3999 ret = wret;
4000 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004001
Chris Mason74123bd2007-02-02 11:05:29 -05004002 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004003 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004004 /* push_leaf_left fixes the path.
4005 * make sure the path still points to our leaf
4006 * for possible call to del_ptr below
4007 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004008 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004009 extent_buffer_get(leaf);
4010
Chris Masonb9473432009-03-13 11:00:37 -04004011 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004012 wret = push_leaf_left(trans, root, path, 1, 1,
4013 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004014 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004015 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004016
4017 if (path->nodes[0] == leaf &&
4018 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004019 wret = push_leaf_right(trans, root, path, 1,
4020 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004021 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004022 ret = wret;
4023 }
Chris Mason5f39d392007-10-15 16:14:19 -04004024
4025 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004026 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004027 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004028 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004029 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05004030 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004031 /* if we're still in the path, make sure
4032 * we're dirty. Otherwise, one of the
4033 * push_leaf functions must have already
4034 * dirtied this buffer
4035 */
4036 if (path->nodes[0] == leaf)
4037 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004038 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004039 }
Chris Masond5719762007-03-23 10:01:08 -04004040 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004041 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004042 }
4043 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004044 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004045}
4046
Chris Mason97571fd2007-02-24 13:39:08 -05004047/*
Chris Mason925baed2008-06-25 16:01:30 -04004048 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004049 * returns 0 if it found something or 1 if there are no lesser leaves.
4050 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004051 *
4052 * This may release the path, and so you may lose any locks held at the
4053 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004054 */
4055int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4056{
Chris Mason925baed2008-06-25 16:01:30 -04004057 struct btrfs_key key;
4058 struct btrfs_disk_key found_key;
4059 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004060
Chris Mason925baed2008-06-25 16:01:30 -04004061 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004062
Chris Mason925baed2008-06-25 16:01:30 -04004063 if (key.offset > 0)
4064 key.offset--;
4065 else if (key.type > 0)
4066 key.type--;
4067 else if (key.objectid > 0)
4068 key.objectid--;
4069 else
4070 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004071
Chris Mason925baed2008-06-25 16:01:30 -04004072 btrfs_release_path(root, path);
4073 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4074 if (ret < 0)
4075 return ret;
4076 btrfs_item_key(path->nodes[0], &found_key, 0);
4077 ret = comp_keys(&found_key, &key);
4078 if (ret < 0)
4079 return 0;
4080 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004081}
4082
Chris Mason3f157a22008-06-25 16:01:31 -04004083/*
4084 * A helper function to walk down the tree starting at min_key, and looking
4085 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004086 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004087 *
4088 * This does not cow, but it does stuff the starting key it finds back
4089 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4090 * key and get a writable path.
4091 *
4092 * This does lock as it descends, and path->keep_locks should be set
4093 * to 1 by the caller.
4094 *
4095 * This honors path->lowest_level to prevent descent past a given level
4096 * of the tree.
4097 *
Chris Masond352ac62008-09-29 15:18:18 -04004098 * min_trans indicates the oldest transaction that you are interested
4099 * in walking through. Any nodes or leaves older than min_trans are
4100 * skipped over (without reading them).
4101 *
Chris Mason3f157a22008-06-25 16:01:31 -04004102 * returns zero if something useful was found, < 0 on error and 1 if there
4103 * was nothing in the tree that matched the search criteria.
4104 */
4105int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004106 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004107 struct btrfs_path *path, int cache_only,
4108 u64 min_trans)
4109{
4110 struct extent_buffer *cur;
4111 struct btrfs_key found_key;
4112 int slot;
Yan96524802008-07-24 12:19:49 -04004113 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004114 u32 nritems;
4115 int level;
4116 int ret = 1;
4117
Chris Mason934d3752008-12-08 16:43:10 -05004118 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004119again:
4120 cur = btrfs_lock_root_node(root);
4121 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004122 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004123 path->nodes[level] = cur;
4124 path->locks[level] = 1;
4125
4126 if (btrfs_header_generation(cur) < min_trans) {
4127 ret = 1;
4128 goto out;
4129 }
Chris Masond3977122009-01-05 21:25:51 -05004130 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004131 nritems = btrfs_header_nritems(cur);
4132 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004133 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004134
Chris Mason323ac952008-10-01 19:05:46 -04004135 /* at the lowest level, we're done, setup the path and exit */
4136 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004137 if (slot >= nritems)
4138 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004139 ret = 0;
4140 path->slots[level] = slot;
4141 btrfs_item_key_to_cpu(cur, &found_key, slot);
4142 goto out;
4143 }
Yan96524802008-07-24 12:19:49 -04004144 if (sret && slot > 0)
4145 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004146 /*
4147 * check this node pointer against the cache_only and
4148 * min_trans parameters. If it isn't in cache or is too
4149 * old, skip to the next one.
4150 */
Chris Masond3977122009-01-05 21:25:51 -05004151 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004152 u64 blockptr;
4153 u64 gen;
4154 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004155 struct btrfs_disk_key disk_key;
4156
Chris Mason3f157a22008-06-25 16:01:31 -04004157 blockptr = btrfs_node_blockptr(cur, slot);
4158 gen = btrfs_node_ptr_generation(cur, slot);
4159 if (gen < min_trans) {
4160 slot++;
4161 continue;
4162 }
4163 if (!cache_only)
4164 break;
4165
Chris Masone02119d2008-09-05 16:13:11 -04004166 if (max_key) {
4167 btrfs_node_key(cur, &disk_key, slot);
4168 if (comp_keys(&disk_key, max_key) >= 0) {
4169 ret = 1;
4170 goto out;
4171 }
4172 }
4173
Chris Mason3f157a22008-06-25 16:01:31 -04004174 tmp = btrfs_find_tree_block(root, blockptr,
4175 btrfs_level_size(root, level - 1));
4176
4177 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4178 free_extent_buffer(tmp);
4179 break;
4180 }
4181 if (tmp)
4182 free_extent_buffer(tmp);
4183 slot++;
4184 }
Chris Masone02119d2008-09-05 16:13:11 -04004185find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004186 /*
4187 * we didn't find a candidate key in this node, walk forward
4188 * and find another one
4189 */
4190 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004191 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004192 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004193 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004194 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004195 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004196 btrfs_release_path(root, path);
4197 goto again;
4198 } else {
4199 goto out;
4200 }
4201 }
4202 /* save our key for returning back */
4203 btrfs_node_key_to_cpu(cur, &found_key, slot);
4204 path->slots[level] = slot;
4205 if (level == path->lowest_level) {
4206 ret = 0;
4207 unlock_up(path, level, 1);
4208 goto out;
4209 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004210 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004211 cur = read_node_slot(root, cur, slot);
4212
4213 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004214
Chris Mason3f157a22008-06-25 16:01:31 -04004215 path->locks[level - 1] = 1;
4216 path->nodes[level - 1] = cur;
4217 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004218 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004219 }
4220out:
4221 if (ret == 0)
4222 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004223 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004224 return ret;
4225}
4226
4227/*
4228 * this is similar to btrfs_next_leaf, but does not try to preserve
4229 * and fixup the path. It looks for and returns the next key in the
4230 * tree based on the current path and the cache_only and min_trans
4231 * parameters.
4232 *
4233 * 0 is returned if another key is found, < 0 if there are any errors
4234 * and 1 is returned if there are no higher keys in the tree
4235 *
4236 * path->keep_locks should be set to 1 on the search made before
4237 * calling this function.
4238 */
Chris Masone7a84562008-06-25 16:01:31 -04004239int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004240 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004241 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004242{
Chris Masone7a84562008-06-25 16:01:31 -04004243 int slot;
4244 struct extent_buffer *c;
4245
Chris Mason934d3752008-12-08 16:43:10 -05004246 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004247 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004248 if (!path->nodes[level])
4249 return 1;
4250
4251 slot = path->slots[level] + 1;
4252 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004253next:
Chris Masone7a84562008-06-25 16:01:31 -04004254 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004255 int ret;
4256 int orig_lowest;
4257 struct btrfs_key cur_key;
4258 if (level + 1 >= BTRFS_MAX_LEVEL ||
4259 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004260 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004261
4262 if (path->locks[level + 1]) {
4263 level++;
4264 continue;
4265 }
4266
4267 slot = btrfs_header_nritems(c) - 1;
4268 if (level == 0)
4269 btrfs_item_key_to_cpu(c, &cur_key, slot);
4270 else
4271 btrfs_node_key_to_cpu(c, &cur_key, slot);
4272
4273 orig_lowest = path->lowest_level;
4274 btrfs_release_path(root, path);
4275 path->lowest_level = level;
4276 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4277 0, 0);
4278 path->lowest_level = orig_lowest;
4279 if (ret < 0)
4280 return ret;
4281
4282 c = path->nodes[level];
4283 slot = path->slots[level];
4284 if (ret == 0)
4285 slot++;
4286 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004287 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004288
Chris Masone7a84562008-06-25 16:01:31 -04004289 if (level == 0)
4290 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004291 else {
4292 u64 blockptr = btrfs_node_blockptr(c, slot);
4293 u64 gen = btrfs_node_ptr_generation(c, slot);
4294
4295 if (cache_only) {
4296 struct extent_buffer *cur;
4297 cur = btrfs_find_tree_block(root, blockptr,
4298 btrfs_level_size(root, level - 1));
4299 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4300 slot++;
4301 if (cur)
4302 free_extent_buffer(cur);
4303 goto next;
4304 }
4305 free_extent_buffer(cur);
4306 }
4307 if (gen < min_trans) {
4308 slot++;
4309 goto next;
4310 }
Chris Masone7a84562008-06-25 16:01:31 -04004311 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004312 }
Chris Masone7a84562008-06-25 16:01:31 -04004313 return 0;
4314 }
4315 return 1;
4316}
4317
Chris Mason7bb86312007-12-11 09:25:06 -05004318/*
Chris Mason925baed2008-06-25 16:01:30 -04004319 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004320 * returns 0 if it found something or 1 if there are no greater leaves.
4321 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004322 */
Chris Mason234b63a2007-03-13 10:46:10 -04004323int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004324{
4325 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004326 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004327 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004328 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004329 struct btrfs_key key;
4330 u32 nritems;
4331 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004332 int old_spinning = path->leave_spinning;
4333 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004334
4335 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004336 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004337 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004338
Chris Mason8e73f272009-04-03 10:14:18 -04004339 /*
4340 * we take the blocks in an order that upsets lockdep. Using
4341 * blocking mode is the only way around it.
4342 */
4343#ifdef CONFIG_DEBUG_LOCK_ALLOC
4344 force_blocking = 1;
4345#endif
Chris Mason925baed2008-06-25 16:01:30 -04004346
Chris Mason8e73f272009-04-03 10:14:18 -04004347 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4348again:
4349 level = 1;
4350 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004351 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004352
Chris Masona2135012008-06-25 16:01:30 -04004353 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004354
4355 if (!force_blocking)
4356 path->leave_spinning = 1;
4357
Chris Mason925baed2008-06-25 16:01:30 -04004358 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4359 path->keep_locks = 0;
4360
4361 if (ret < 0)
4362 return ret;
4363
Chris Masona2135012008-06-25 16:01:30 -04004364 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004365 /*
4366 * by releasing the path above we dropped all our locks. A balance
4367 * could have added more items next to the key that used to be
4368 * at the very end of the block. So, check again here and
4369 * advance the path if there are now more items available.
4370 */
Chris Masona2135012008-06-25 16:01:30 -04004371 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004372 if (ret == 0)
4373 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004374 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004375 goto done;
4376 }
Chris Masond97e63b2007-02-20 16:40:44 -05004377
Chris Masond3977122009-01-05 21:25:51 -05004378 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004379 if (!path->nodes[level]) {
4380 ret = 1;
4381 goto done;
4382 }
Chris Mason5f39d392007-10-15 16:14:19 -04004383
Chris Masond97e63b2007-02-20 16:40:44 -05004384 slot = path->slots[level] + 1;
4385 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004386 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004387 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004388 if (level == BTRFS_MAX_LEVEL) {
4389 ret = 1;
4390 goto done;
4391 }
Chris Masond97e63b2007-02-20 16:40:44 -05004392 continue;
4393 }
Chris Mason5f39d392007-10-15 16:14:19 -04004394
Chris Mason925baed2008-06-25 16:01:30 -04004395 if (next) {
4396 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004397 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004398 }
Chris Mason5f39d392007-10-15 16:14:19 -04004399
Chris Mason8e73f272009-04-03 10:14:18 -04004400 next = c;
4401 ret = read_block_for_search(NULL, root, path, &next, level,
4402 slot, &key);
4403 if (ret == -EAGAIN)
4404 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004405
Chris Mason76a05b32009-05-14 13:24:30 -04004406 if (ret < 0) {
4407 btrfs_release_path(root, path);
4408 goto done;
4409 }
4410
Chris Mason5cd57b22008-06-25 16:01:30 -04004411 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004412 ret = btrfs_try_spin_lock(next);
4413 if (!ret) {
4414 btrfs_set_path_blocking(path);
4415 btrfs_tree_lock(next);
4416 if (!force_blocking)
4417 btrfs_clear_path_blocking(path, next);
4418 }
4419 if (force_blocking)
4420 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004421 }
Chris Masond97e63b2007-02-20 16:40:44 -05004422 break;
4423 }
4424 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004425 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004426 level--;
4427 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004428 if (path->locks[level])
4429 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004430
Chris Mason5f39d392007-10-15 16:14:19 -04004431 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004432 path->nodes[level] = next;
4433 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004434 if (!path->skip_locking)
4435 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004436
Chris Masond97e63b2007-02-20 16:40:44 -05004437 if (!level)
4438 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004439
Chris Mason8e73f272009-04-03 10:14:18 -04004440 ret = read_block_for_search(NULL, root, path, &next, level,
4441 0, &key);
4442 if (ret == -EAGAIN)
4443 goto again;
4444
Chris Mason76a05b32009-05-14 13:24:30 -04004445 if (ret < 0) {
4446 btrfs_release_path(root, path);
4447 goto done;
4448 }
4449
Chris Mason5cd57b22008-06-25 16:01:30 -04004450 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004451 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004452 ret = btrfs_try_spin_lock(next);
4453 if (!ret) {
4454 btrfs_set_path_blocking(path);
4455 btrfs_tree_lock(next);
4456 if (!force_blocking)
4457 btrfs_clear_path_blocking(path, next);
4458 }
4459 if (force_blocking)
4460 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004461 }
Chris Masond97e63b2007-02-20 16:40:44 -05004462 }
Chris Mason8e73f272009-04-03 10:14:18 -04004463 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004464done:
4465 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004466 path->leave_spinning = old_spinning;
4467 if (!old_spinning)
4468 btrfs_set_path_blocking(path);
4469
4470 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004471}
Chris Mason0b86a832008-03-24 15:01:56 -04004472
Chris Mason3f157a22008-06-25 16:01:31 -04004473/*
4474 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4475 * searching until it gets past min_objectid or finds an item of 'type'
4476 *
4477 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4478 */
Chris Mason0b86a832008-03-24 15:01:56 -04004479int btrfs_previous_item(struct btrfs_root *root,
4480 struct btrfs_path *path, u64 min_objectid,
4481 int type)
4482{
4483 struct btrfs_key found_key;
4484 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004485 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004486 int ret;
4487
Chris Masond3977122009-01-05 21:25:51 -05004488 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004489 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004490 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004491 ret = btrfs_prev_leaf(root, path);
4492 if (ret != 0)
4493 return ret;
4494 } else {
4495 path->slots[0]--;
4496 }
4497 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004498 nritems = btrfs_header_nritems(leaf);
4499 if (nritems == 0)
4500 return 1;
4501 if (path->slots[0] == nritems)
4502 path->slots[0]--;
4503
Chris Mason0b86a832008-03-24 15:01:56 -04004504 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004505 if (found_key.objectid < min_objectid)
4506 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004507 if (found_key.type == type)
4508 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004509 if (found_key.objectid == min_objectid &&
4510 found_key.type < type)
4511 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004512 }
4513 return 1;
4514}