blob: 465b5d7d6b48d9b7d8a630f4fa4ffd0714323f30 [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{
Jesper Juhlff175d52010-12-25 21:22:30 +0000108 if (!p)
109 return;
Chris Masondf24a2b2007-04-04 09:36:31 -0400110 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400111 kmem_cache_free(btrfs_path_cachep, p);
112}
113
Chris Masond352ac62008-09-29 15:18:18 -0400114/*
115 * path release drops references on the extent buffers in the path
116 * and it drops any locks held by this path
117 *
118 * It is safe to call this on paths that no locks or extent buffers held.
119 */
Chris Masond3977122009-01-05 21:25:51 -0500120noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500121{
122 int i;
Chris Masona2135012008-06-25 16:01:30 -0400123
Chris Mason234b63a2007-03-13 10:46:10 -0400124 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400125 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500126 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400127 continue;
128 if (p->locks[i]) {
129 btrfs_tree_unlock(p->nodes[i]);
130 p->locks[i] = 0;
131 }
Chris Mason5f39d392007-10-15 16:14:19 -0400132 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400133 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 }
135}
136
Chris Masond352ac62008-09-29 15:18:18 -0400137/*
138 * safely gets a reference on the root node of a tree. A lock
139 * is not taken, so a concurrent writer may put a different node
140 * at the root of the tree. See btrfs_lock_root_node for the
141 * looping required.
142 *
143 * The extent buffer returned by this has a reference taken, so
144 * it won't disappear. It may stop being the root of the tree
145 * at any time because there are no locks held.
146 */
Chris Mason925baed2008-06-25 16:01:30 -0400147struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
148{
149 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400150
151 rcu_read_lock();
152 eb = rcu_dereference(root->node);
Chris Mason925baed2008-06-25 16:01:30 -0400153 extent_buffer_get(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400154 rcu_read_unlock();
Chris Mason925baed2008-06-25 16:01:30 -0400155 return eb;
156}
157
Chris Masond352ac62008-09-29 15:18:18 -0400158/* loop around taking references on and locking the root node of the
159 * tree until you end up with a lock on the root. A locked buffer
160 * is returned, with a reference held.
161 */
Chris Mason925baed2008-06-25 16:01:30 -0400162struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
163{
164 struct extent_buffer *eb;
165
Chris Masond3977122009-01-05 21:25:51 -0500166 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400167 eb = btrfs_root_node(root);
168 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400169 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400170 break;
Chris Mason925baed2008-06-25 16:01:30 -0400171 btrfs_tree_unlock(eb);
172 free_extent_buffer(eb);
173 }
174 return eb;
175}
176
Chris Masond352ac62008-09-29 15:18:18 -0400177/* cowonly root (everything not a reference counted cow subvolume), just get
178 * put onto a simple dirty list. transaction.c walks this to make sure they
179 * get properly updated on disk.
180 */
Chris Mason0b86a832008-03-24 15:01:56 -0400181static void add_root_to_dirty_list(struct btrfs_root *root)
182{
183 if (root->track_dirty && list_empty(&root->dirty_list)) {
184 list_add(&root->dirty_list,
185 &root->fs_info->dirty_cowonly_roots);
186 }
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/*
190 * used by snapshot creation to make a copy of a root for a tree with
191 * a given objectid. The buffer with the new root node is returned in
192 * cow_ret, and this func returns zero on success or a negative error code.
193 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500194int btrfs_copy_root(struct btrfs_trans_handle *trans,
195 struct btrfs_root *root,
196 struct extent_buffer *buf,
197 struct extent_buffer **cow_ret, u64 new_root_objectid)
198{
199 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500200 int ret = 0;
201 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400202 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500203
204 WARN_ON(root->ref_cows && trans->transid !=
205 root->fs_info->running_transaction->transid);
206 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
207
208 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400209 if (level == 0)
210 btrfs_item_key(buf, &disk_key, 0);
211 else
212 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400213
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400214 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
215 new_root_objectid, &disk_key, level,
216 buf->start, 0);
217 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500218 return PTR_ERR(cow);
219
220 copy_extent_buffer(cow, buf, 0, 0, cow->len);
221 btrfs_set_header_bytenr(cow, cow->start);
222 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400223 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
224 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
225 BTRFS_HEADER_FLAG_RELOC);
226 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
227 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
228 else
229 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500230
Yan Zheng2b820322008-11-17 21:11:30 -0500231 write_extent_buffer(cow, root->fs_info->fsid,
232 (unsigned long)btrfs_header_fsid(cow),
233 BTRFS_FSID_SIZE);
234
Chris Masonbe20aa92007-12-17 20:14:01 -0500235 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400236 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
237 ret = btrfs_inc_ref(trans, root, cow, 1);
238 else
239 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500240
Chris Masonbe20aa92007-12-17 20:14:01 -0500241 if (ret)
242 return ret;
243
244 btrfs_mark_buffer_dirty(cow);
245 *cow_ret = cow;
246 return 0;
247}
248
Chris Masond352ac62008-09-29 15:18:18 -0400249/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400250 * check if the tree block can be shared by multiple trees
251 */
252int btrfs_block_can_be_shared(struct btrfs_root *root,
253 struct extent_buffer *buf)
254{
255 /*
256 * Tree blocks not in refernece counted trees and tree roots
257 * are never shared. If a block was allocated after the last
258 * snapshot and the block was not allocated by tree relocation,
259 * we know the block is not shared.
260 */
261 if (root->ref_cows &&
262 buf != root->node && buf != root->commit_root &&
263 (btrfs_header_generation(buf) <=
264 btrfs_root_last_snapshot(&root->root_item) ||
265 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
266 return 1;
267#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
268 if (root->ref_cows &&
269 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
270 return 1;
271#endif
272 return 0;
273}
274
275static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
276 struct btrfs_root *root,
277 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400278 struct extent_buffer *cow,
279 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400280{
281 u64 refs;
282 u64 owner;
283 u64 flags;
284 u64 new_flags = 0;
285 int ret;
286
287 /*
288 * Backrefs update rules:
289 *
290 * Always use full backrefs for extent pointers in tree block
291 * allocated by tree relocation.
292 *
293 * If a shared tree block is no longer referenced by its owner
294 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
295 * use full backrefs for extent pointers in tree block.
296 *
297 * If a tree block is been relocating
298 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
299 * use full backrefs for extent pointers in tree block.
300 * The reason for this is some operations (such as drop tree)
301 * are only allowed for blocks use full backrefs.
302 */
303
304 if (btrfs_block_can_be_shared(root, buf)) {
305 ret = btrfs_lookup_extent_info(trans, root, buf->start,
306 buf->len, &refs, &flags);
307 BUG_ON(ret);
308 BUG_ON(refs == 0);
309 } else {
310 refs = 1;
311 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
312 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
313 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
314 else
315 flags = 0;
316 }
317
318 owner = btrfs_header_owner(buf);
319 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
320 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
321
322 if (refs > 1) {
323 if ((owner == root->root_key.objectid ||
324 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
325 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
326 ret = btrfs_inc_ref(trans, root, buf, 1);
327 BUG_ON(ret);
328
329 if (root->root_key.objectid ==
330 BTRFS_TREE_RELOC_OBJECTID) {
331 ret = btrfs_dec_ref(trans, root, buf, 0);
332 BUG_ON(ret);
333 ret = btrfs_inc_ref(trans, root, cow, 1);
334 BUG_ON(ret);
335 }
336 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
337 } else {
338
339 if (root->root_key.objectid ==
340 BTRFS_TREE_RELOC_OBJECTID)
341 ret = btrfs_inc_ref(trans, root, cow, 1);
342 else
343 ret = btrfs_inc_ref(trans, root, cow, 0);
344 BUG_ON(ret);
345 }
346 if (new_flags != 0) {
347 ret = btrfs_set_disk_extent_flags(trans, root,
348 buf->start,
349 buf->len,
350 new_flags, 0);
351 BUG_ON(ret);
352 }
353 } else {
354 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
355 if (root->root_key.objectid ==
356 BTRFS_TREE_RELOC_OBJECTID)
357 ret = btrfs_inc_ref(trans, root, cow, 1);
358 else
359 ret = btrfs_inc_ref(trans, root, cow, 0);
360 BUG_ON(ret);
361 ret = btrfs_dec_ref(trans, root, buf, 1);
362 BUG_ON(ret);
363 }
364 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400365 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400366 }
367 return 0;
368}
369
370/*
Chris Masond3977122009-01-05 21:25:51 -0500371 * does the dirty work in cow of a single block. The parent block (if
372 * supplied) is updated to point to the new cow copy. The new buffer is marked
373 * dirty and returned locked. If you modify the block it needs to be marked
374 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400375 *
376 * search_start -- an allocation hint for the new block
377 *
Chris Masond3977122009-01-05 21:25:51 -0500378 * empty_size -- a hint that you plan on doing more cow. This is the size in
379 * bytes the allocator should try to find free next to the block it returns.
380 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400381 */
Chris Masond3977122009-01-05 21:25:51 -0500382static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400383 struct btrfs_root *root,
384 struct extent_buffer *buf,
385 struct extent_buffer *parent, int parent_slot,
386 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400387 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400388{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400389 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400390 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500391 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400392 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400393 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400394 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400395
Chris Mason925baed2008-06-25 16:01:30 -0400396 if (*cow_ret == buf)
397 unlock_orig = 1;
398
Chris Masonb9447ef82009-03-09 11:45:38 -0400399 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400400
Chris Mason7bb86312007-12-11 09:25:06 -0500401 WARN_ON(root->ref_cows && trans->transid !=
402 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400403 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400404
Chris Mason7bb86312007-12-11 09:25:06 -0500405 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400406
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400407 if (level == 0)
408 btrfs_item_key(buf, &disk_key, 0);
409 else
410 btrfs_node_key(buf, &disk_key, 0);
411
412 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
413 if (parent)
414 parent_start = parent->start;
415 else
416 parent_start = 0;
417 } else
418 parent_start = 0;
419
420 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
421 root->root_key.objectid, &disk_key,
422 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400423 if (IS_ERR(cow))
424 return PTR_ERR(cow);
425
Chris Masonb4ce94d2009-02-04 09:25:08 -0500426 /* cow is set to blocking by btrfs_init_new_buffer */
427
Chris Mason5f39d392007-10-15 16:14:19 -0400428 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400429 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400430 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400431 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
432 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
433 BTRFS_HEADER_FLAG_RELOC);
434 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
435 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
436 else
437 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400438
Yan Zheng2b820322008-11-17 21:11:30 -0500439 write_extent_buffer(cow, root->fs_info->fsid,
440 (unsigned long)btrfs_header_fsid(cow),
441 BTRFS_FSID_SIZE);
442
Yan, Zhengf0486c62010-05-16 10:46:25 -0400443 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400444
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400445 if (root->ref_cows)
446 btrfs_reloc_cow_block(trans, root, buf, cow);
447
Chris Mason6702ed42007-08-07 16:15:09 -0400448 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400449 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
451 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
452 parent_start = buf->start;
453 else
454 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400455
Chris Mason5f39d392007-10-15 16:14:19 -0400456 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400457 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400458
Yan, Zhengf0486c62010-05-16 10:46:25 -0400459 btrfs_free_tree_block(trans, root, buf, parent_start,
460 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400461 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400462 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400463 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400464 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
465 parent_start = parent->start;
466 else
467 parent_start = 0;
468
469 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400470 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400471 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500472 btrfs_set_node_ptr_generation(parent, parent_slot,
473 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400474 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400475 btrfs_free_tree_block(trans, root, buf, parent_start,
476 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400477 }
Chris Mason925baed2008-06-25 16:01:30 -0400478 if (unlock_orig)
479 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400480 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400481 btrfs_mark_buffer_dirty(cow);
482 *cow_ret = cow;
483 return 0;
484}
485
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400486static inline int should_cow_block(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root,
488 struct extent_buffer *buf)
489{
490 if (btrfs_header_generation(buf) == trans->transid &&
491 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
492 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
493 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
494 return 0;
495 return 1;
496}
497
Chris Masond352ac62008-09-29 15:18:18 -0400498/*
499 * cows a single block, see __btrfs_cow_block for the real work.
500 * This version of it has extra checks so that a block isn't cow'd more than
501 * once per transaction, as long as it hasn't been written yet
502 */
Chris Masond3977122009-01-05 21:25:51 -0500503noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400504 struct btrfs_root *root, struct extent_buffer *buf,
505 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400506 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500507{
Chris Mason6702ed42007-08-07 16:15:09 -0400508 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400509 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500510
Chris Masonccd467d2007-06-28 15:57:36 -0400511 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500512 printk(KERN_CRIT "trans %llu running %llu\n",
513 (unsigned long long)trans->transid,
514 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400515 root->fs_info->running_transaction->transid);
516 WARN_ON(1);
517 }
518 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500519 printk(KERN_CRIT "trans %llu running %llu\n",
520 (unsigned long long)trans->transid,
521 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400522 WARN_ON(1);
523 }
Chris Masondc17ff82008-01-08 15:46:30 -0500524
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400525 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500526 *cow_ret = buf;
527 return 0;
528 }
Chris Masonc4876852009-02-04 09:24:25 -0500529
Chris Mason0b86a832008-03-24 15:01:56 -0400530 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500531
532 if (parent)
533 btrfs_set_lock_blocking(parent);
534 btrfs_set_lock_blocking(buf);
535
Chris Masonf510cfe2007-10-15 16:14:48 -0400536 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400537 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000538
539 trace_btrfs_cow_block(root, buf, *cow_ret);
540
Chris Masonf510cfe2007-10-15 16:14:48 -0400541 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400542}
543
Chris Masond352ac62008-09-29 15:18:18 -0400544/*
545 * helper function for defrag to decide if two blocks pointed to by a
546 * node are actually close by
547 */
Chris Mason6b800532007-10-15 16:17:34 -0400548static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400549{
Chris Mason6b800532007-10-15 16:17:34 -0400550 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400551 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400552 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400553 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500554 return 0;
555}
556
Chris Mason081e9572007-11-06 10:26:24 -0500557/*
558 * compare two keys in a memcmp fashion
559 */
560static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
561{
562 struct btrfs_key k1;
563
564 btrfs_disk_key_to_cpu(&k1, disk);
565
Diego Calleja20736ab2009-07-24 11:06:52 -0400566 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500567}
568
Josef Bacikf3465ca2008-11-12 14:19:50 -0500569/*
570 * same as comp_keys only with two btrfs_key's
571 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400572int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500573{
574 if (k1->objectid > k2->objectid)
575 return 1;
576 if (k1->objectid < k2->objectid)
577 return -1;
578 if (k1->type > k2->type)
579 return 1;
580 if (k1->type < k2->type)
581 return -1;
582 if (k1->offset > k2->offset)
583 return 1;
584 if (k1->offset < k2->offset)
585 return -1;
586 return 0;
587}
Chris Mason081e9572007-11-06 10:26:24 -0500588
Chris Masond352ac62008-09-29 15:18:18 -0400589/*
590 * this is used by the defrag code to go through all the
591 * leaves pointed to by a node and reallocate them so that
592 * disk order is close to key order
593 */
Chris Mason6702ed42007-08-07 16:15:09 -0400594int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400595 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400596 int start_slot, int cache_only, u64 *last_ret,
597 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400598{
Chris Mason6b800532007-10-15 16:17:34 -0400599 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400600 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400601 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400602 u64 search_start = *last_ret;
603 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400604 u64 other;
605 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400606 int end_slot;
607 int i;
608 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400609 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400610 int uptodate;
611 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500612 int progress_passed = 0;
613 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400614
Chris Mason5708b952007-10-25 15:43:18 -0400615 parent_level = btrfs_header_level(parent);
616 if (cache_only && parent_level != 1)
617 return 0;
618
Chris Masond3977122009-01-05 21:25:51 -0500619 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400620 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500621 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400622 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400623
Chris Mason6b800532007-10-15 16:17:34 -0400624 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400625 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400626 end_slot = parent_nritems;
627
628 if (parent_nritems == 1)
629 return 0;
630
Chris Masonb4ce94d2009-02-04 09:25:08 -0500631 btrfs_set_lock_blocking(parent);
632
Chris Mason6702ed42007-08-07 16:15:09 -0400633 for (i = start_slot; i < end_slot; i++) {
634 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400635
Chris Mason5708b952007-10-25 15:43:18 -0400636 if (!parent->map_token) {
637 map_extent_buffer(parent,
638 btrfs_node_key_ptr_offset(i),
639 sizeof(struct btrfs_key_ptr),
640 &parent->map_token, &parent->kaddr,
641 &parent->map_start, &parent->map_len,
642 KM_USER1);
643 }
Chris Mason081e9572007-11-06 10:26:24 -0500644 btrfs_node_key(parent, &disk_key, i);
645 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
646 continue;
647
648 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400649 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400650 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400651 if (last_block == 0)
652 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400653
Chris Mason6702ed42007-08-07 16:15:09 -0400654 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400655 other = btrfs_node_blockptr(parent, i - 1);
656 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400657 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400658 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400659 other = btrfs_node_blockptr(parent, i + 1);
660 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400661 }
Chris Masone9d0b132007-08-10 14:06:19 -0400662 if (close) {
663 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400664 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400665 }
Chris Mason5708b952007-10-25 15:43:18 -0400666 if (parent->map_token) {
667 unmap_extent_buffer(parent, parent->map_token,
668 KM_USER1);
669 parent->map_token = NULL;
670 }
Chris Mason6702ed42007-08-07 16:15:09 -0400671
Chris Mason6b800532007-10-15 16:17:34 -0400672 cur = btrfs_find_tree_block(root, blocknr, blocksize);
673 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400674 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400675 else
676 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400677 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400678 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400679 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400680 continue;
681 }
Chris Mason6b800532007-10-15 16:17:34 -0400682 if (!cur) {
683 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400684 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400685 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400686 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400687 }
Chris Mason6702ed42007-08-07 16:15:09 -0400688 }
Chris Masone9d0b132007-08-10 14:06:19 -0400689 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400690 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400691
Chris Masone7a84562008-06-25 16:01:31 -0400692 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500693 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400694 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400695 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400696 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400697 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400698 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400699 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400700 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400701 break;
Yan252c38f2007-08-29 09:11:44 -0400702 }
Chris Masone7a84562008-06-25 16:01:31 -0400703 search_start = cur->start;
704 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400705 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400706 btrfs_tree_unlock(cur);
707 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400708 }
Chris Mason5708b952007-10-25 15:43:18 -0400709 if (parent->map_token) {
710 unmap_extent_buffer(parent, parent->map_token,
711 KM_USER1);
712 parent->map_token = NULL;
713 }
Chris Mason6702ed42007-08-07 16:15:09 -0400714 return err;
715}
716
Chris Mason74123bd2007-02-02 11:05:29 -0500717/*
718 * The leaf data grows from end-to-front in the node.
719 * this returns the address of the start of the last item,
720 * which is the stop of the leaf data stack
721 */
Chris Mason123abc82007-03-14 14:14:43 -0400722static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400723 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500724{
Chris Mason5f39d392007-10-15 16:14:19 -0400725 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400727 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400728 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500729}
730
Chris Masonaa5d6be2007-02-28 16:35:06 -0500731
Chris Mason74123bd2007-02-02 11:05:29 -0500732/*
Chris Mason5f39d392007-10-15 16:14:19 -0400733 * search for key in the extent_buffer. The items start at offset p,
734 * and they are item_size apart. There are 'max' items in p.
735 *
Chris Mason74123bd2007-02-02 11:05:29 -0500736 * the slot in the array is returned via slot, and it points to
737 * the place where you would insert key if it is not found in
738 * the array.
739 *
740 * slot may point to max if the key is bigger than all of the keys
741 */
Chris Masone02119d2008-09-05 16:13:11 -0400742static noinline int generic_bin_search(struct extent_buffer *eb,
743 unsigned long p,
744 int item_size, struct btrfs_key *key,
745 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500746{
747 int low = 0;
748 int high = max;
749 int mid;
750 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400751 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400752 struct btrfs_disk_key unaligned;
753 unsigned long offset;
754 char *map_token = NULL;
755 char *kaddr = NULL;
756 unsigned long map_start = 0;
757 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400758 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500759
Chris Masond3977122009-01-05 21:25:51 -0500760 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500761 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400762 offset = p + mid * item_size;
763
764 if (!map_token || offset < map_start ||
765 (offset + sizeof(struct btrfs_disk_key)) >
766 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400767 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400768 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400769 map_token = NULL;
770 }
Chris Mason934d3752008-12-08 16:43:10 -0500771
772 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400773 sizeof(struct btrfs_disk_key),
774 &map_token, &kaddr,
775 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400776
Chris Mason479965d2007-10-15 16:14:27 -0400777 if (!err) {
778 tmp = (struct btrfs_disk_key *)(kaddr + offset -
779 map_start);
780 } else {
781 read_extent_buffer(eb, &unaligned,
782 offset, sizeof(unaligned));
783 tmp = &unaligned;
784 }
785
Chris Mason5f39d392007-10-15 16:14:19 -0400786 } else {
787 tmp = (struct btrfs_disk_key *)(kaddr + offset -
788 map_start);
789 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500790 ret = comp_keys(tmp, key);
791
792 if (ret < 0)
793 low = mid + 1;
794 else if (ret > 0)
795 high = mid;
796 else {
797 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400798 if (map_token)
799 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500800 return 0;
801 }
802 }
803 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400804 if (map_token)
805 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500806 return 1;
807}
808
Chris Mason97571fd2007-02-24 13:39:08 -0500809/*
810 * simple bin_search frontend that does the right thing for
811 * leaves vs nodes
812 */
Chris Mason5f39d392007-10-15 16:14:19 -0400813static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
814 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500815{
Chris Mason5f39d392007-10-15 16:14:19 -0400816 if (level == 0) {
817 return generic_bin_search(eb,
818 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400819 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400820 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400821 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500822 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400823 return generic_bin_search(eb,
824 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400825 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400826 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400827 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500828 }
829 return -1;
830}
831
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400832int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
833 int level, int *slot)
834{
835 return bin_search(eb, key, level, slot);
836}
837
Yan, Zhengf0486c62010-05-16 10:46:25 -0400838static void root_add_used(struct btrfs_root *root, u32 size)
839{
840 spin_lock(&root->accounting_lock);
841 btrfs_set_root_used(&root->root_item,
842 btrfs_root_used(&root->root_item) + size);
843 spin_unlock(&root->accounting_lock);
844}
845
846static void root_sub_used(struct btrfs_root *root, u32 size)
847{
848 spin_lock(&root->accounting_lock);
849 btrfs_set_root_used(&root->root_item,
850 btrfs_root_used(&root->root_item) - size);
851 spin_unlock(&root->accounting_lock);
852}
853
Chris Masond352ac62008-09-29 15:18:18 -0400854/* given a node and slot number, this reads the blocks it points to. The
855 * extent buffer is returned with a reference taken (but unlocked).
856 * NULL is returned on error.
857 */
Chris Masone02119d2008-09-05 16:13:11 -0400858static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400859 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500860{
Chris Masonca7a79a2008-05-12 12:59:19 -0400861 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500862 if (slot < 0)
863 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400864 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500865 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400866
867 BUG_ON(level == 0);
868
Chris Masondb945352007-10-15 16:15:53 -0400869 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400870 btrfs_level_size(root, level - 1),
871 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500872}
873
Chris Masond352ac62008-09-29 15:18:18 -0400874/*
875 * node level balancing, used to make sure nodes are in proper order for
876 * item deletion. We balance from the top down, so we have to make sure
877 * that a deletion won't leave an node completely empty later on.
878 */
Chris Masone02119d2008-09-05 16:13:11 -0400879static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500880 struct btrfs_root *root,
881 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500882{
Chris Mason5f39d392007-10-15 16:14:19 -0400883 struct extent_buffer *right = NULL;
884 struct extent_buffer *mid;
885 struct extent_buffer *left = NULL;
886 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500887 int ret = 0;
888 int wret;
889 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500890 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500891 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500892
893 if (level == 0)
894 return 0;
895
Chris Mason5f39d392007-10-15 16:14:19 -0400896 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500897
Chris Mason925baed2008-06-25 16:01:30 -0400898 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500899 WARN_ON(btrfs_header_generation(mid) != trans->transid);
900
Chris Mason1d4f8a02007-03-13 09:28:32 -0400901 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500902
Chris Mason234b63a2007-03-13 10:46:10 -0400903 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400904 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500905 pslot = path->slots[level + 1];
906
Chris Mason40689472007-03-17 14:29:23 -0400907 /*
908 * deal with the case where there is only one pointer in the root
909 * by promoting the node below to a root
910 */
Chris Mason5f39d392007-10-15 16:14:19 -0400911 if (!parent) {
912 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500913
Chris Mason5f39d392007-10-15 16:14:19 -0400914 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500915 return 0;
916
917 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400918 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500919 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400920 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500921 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400922 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400923 if (ret) {
924 btrfs_tree_unlock(child);
925 free_extent_buffer(child);
926 goto enospc;
927 }
Yan2f375ab2008-02-01 14:58:07 -0500928
Chris Mason240f62c2011-03-23 14:54:42 -0400929 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400930
Chris Mason0b86a832008-03-24 15:01:56 -0400931 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400932 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500933
Chris Mason925baed2008-06-25 16:01:30 -0400934 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500935 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400936 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400937 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500938 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400939 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400940
941 root_sub_used(root, mid->len);
942 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500943 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400944 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400945 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500946 }
Chris Mason5f39d392007-10-15 16:14:19 -0400947 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400948 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500949 return 0;
950
Andi Kleen559af822010-10-29 15:14:37 -0400951 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400952
Chris Mason5f39d392007-10-15 16:14:19 -0400953 left = read_node_slot(root, parent, pslot - 1);
954 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400955 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500956 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400957 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400958 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400959 if (wret) {
960 ret = wret;
961 goto enospc;
962 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400963 }
Chris Mason5f39d392007-10-15 16:14:19 -0400964 right = read_node_slot(root, parent, pslot + 1);
965 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400966 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500967 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400968 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400969 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400970 if (wret) {
971 ret = wret;
972 goto enospc;
973 }
974 }
975
976 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400977 if (left) {
978 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400979 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500980 if (wret < 0)
981 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -0400982 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500983 }
Chris Mason79f95c82007-03-01 15:16:26 -0500984
985 /*
986 * then try to empty the right most buffer into the middle
987 */
Chris Mason5f39d392007-10-15 16:14:19 -0400988 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400989 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400990 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500991 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400992 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400993 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400994 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -0400995 wret = del_ptr(trans, root, path, level + 1, pslot +
996 1);
Chris Masonbb803952007-03-01 12:04:21 -0500997 if (wret)
998 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400999 root_sub_used(root, right->len);
1000 btrfs_free_tree_block(trans, root, right, 0, 1);
1001 free_extent_buffer(right);
1002 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001003 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001004 struct btrfs_disk_key right_key;
1005 btrfs_node_key(right, &right_key, 0);
1006 btrfs_set_node_key(parent, &right_key, pslot + 1);
1007 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001008 }
1009 }
Chris Mason5f39d392007-10-15 16:14:19 -04001010 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001011 /*
1012 * we're not allowed to leave a node with one item in the
1013 * tree during a delete. A deletion from lower in the tree
1014 * could try to delete the only pointer in this node.
1015 * So, pull some keys from the left.
1016 * There has to be a left pointer at this point because
1017 * otherwise we would have pulled some pointers from the
1018 * right
1019 */
Chris Mason5f39d392007-10-15 16:14:19 -04001020 BUG_ON(!left);
1021 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001022 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001023 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001024 goto enospc;
1025 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001026 if (wret == 1) {
1027 wret = push_node_left(trans, root, left, mid, 1);
1028 if (wret < 0)
1029 ret = wret;
1030 }
Chris Mason79f95c82007-03-01 15:16:26 -05001031 BUG_ON(wret == 1);
1032 }
Chris Mason5f39d392007-10-15 16:14:19 -04001033 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001034 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001035 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001036 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001037 if (wret)
1038 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001039 root_sub_used(root, mid->len);
1040 btrfs_free_tree_block(trans, root, mid, 0, 1);
1041 free_extent_buffer(mid);
1042 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001043 } else {
1044 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001045 struct btrfs_disk_key mid_key;
1046 btrfs_node_key(mid, &mid_key, 0);
1047 btrfs_set_node_key(parent, &mid_key, pslot);
1048 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001049 }
Chris Masonbb803952007-03-01 12:04:21 -05001050
Chris Mason79f95c82007-03-01 15:16:26 -05001051 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001052 if (left) {
1053 if (btrfs_header_nritems(left) > orig_slot) {
1054 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001055 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001056 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001057 path->slots[level + 1] -= 1;
1058 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001059 if (mid) {
1060 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001061 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001062 }
Chris Masonbb803952007-03-01 12:04:21 -05001063 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001064 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001065 path->slots[level] = orig_slot;
1066 }
1067 }
Chris Mason79f95c82007-03-01 15:16:26 -05001068 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001069 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001070 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001071 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001072enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001073 if (right) {
1074 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001075 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001076 }
1077 if (left) {
1078 if (path->nodes[level] != left)
1079 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001080 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001081 }
Chris Masonbb803952007-03-01 12:04:21 -05001082 return ret;
1083}
1084
Chris Masond352ac62008-09-29 15:18:18 -04001085/* Node balancing for insertion. Here we only split or push nodes around
1086 * when they are completely full. This is also done top down, so we
1087 * have to be pessimistic.
1088 */
Chris Masond3977122009-01-05 21:25:51 -05001089static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001090 struct btrfs_root *root,
1091 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001092{
Chris Mason5f39d392007-10-15 16:14:19 -04001093 struct extent_buffer *right = NULL;
1094 struct extent_buffer *mid;
1095 struct extent_buffer *left = NULL;
1096 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001097 int ret = 0;
1098 int wret;
1099 int pslot;
1100 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001101
1102 if (level == 0)
1103 return 1;
1104
Chris Mason5f39d392007-10-15 16:14:19 -04001105 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001106 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001107
1108 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001109 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001110 pslot = path->slots[level + 1];
1111
Chris Mason5f39d392007-10-15 16:14:19 -04001112 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001113 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001114
Chris Mason5f39d392007-10-15 16:14:19 -04001115 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001116
1117 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001118 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001119 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001120
1121 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001122 btrfs_set_lock_blocking(left);
1123
Chris Mason5f39d392007-10-15 16:14:19 -04001124 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001125 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1126 wret = 1;
1127 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001128 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001129 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001130 if (ret)
1131 wret = 1;
1132 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001133 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001134 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001135 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001136 }
Chris Masone66f7092007-04-20 13:16:02 -04001137 if (wret < 0)
1138 ret = wret;
1139 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001140 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001141 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001142 btrfs_node_key(mid, &disk_key, 0);
1143 btrfs_set_node_key(parent, &disk_key, pslot);
1144 btrfs_mark_buffer_dirty(parent);
1145 if (btrfs_header_nritems(left) > orig_slot) {
1146 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001147 path->slots[level + 1] -= 1;
1148 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001149 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001150 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001151 } else {
1152 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001153 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001154 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001155 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001156 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001157 }
Chris Masone66f7092007-04-20 13:16:02 -04001158 return 0;
1159 }
Chris Mason925baed2008-06-25 16:01:30 -04001160 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001161 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001162 }
Chris Mason925baed2008-06-25 16:01:30 -04001163 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001164
1165 /*
1166 * then try to empty the right most buffer into the middle
1167 */
Chris Mason5f39d392007-10-15 16:14:19 -04001168 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001169 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001170
Chris Mason925baed2008-06-25 16:01:30 -04001171 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001172 btrfs_set_lock_blocking(right);
1173
Chris Mason5f39d392007-10-15 16:14:19 -04001174 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001175 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1176 wret = 1;
1177 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001178 ret = btrfs_cow_block(trans, root, right,
1179 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001180 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 if (ret)
1182 wret = 1;
1183 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001184 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001185 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001186 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001187 }
Chris Masone66f7092007-04-20 13:16:02 -04001188 if (wret < 0)
1189 ret = wret;
1190 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001191 struct btrfs_disk_key disk_key;
1192
1193 btrfs_node_key(right, &disk_key, 0);
1194 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1195 btrfs_mark_buffer_dirty(parent);
1196
1197 if (btrfs_header_nritems(mid) <= orig_slot) {
1198 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001199 path->slots[level + 1] += 1;
1200 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001201 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001202 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001203 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001204 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001205 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001206 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001207 }
Chris Masone66f7092007-04-20 13:16:02 -04001208 return 0;
1209 }
Chris Mason925baed2008-06-25 16:01:30 -04001210 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001211 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001212 }
Chris Masone66f7092007-04-20 13:16:02 -04001213 return 1;
1214}
1215
Chris Mason74123bd2007-02-02 11:05:29 -05001216/*
Chris Masond352ac62008-09-29 15:18:18 -04001217 * readahead one full node of leaves, finding things that are close
1218 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001219 */
Chris Masonc8c42862009-04-03 10:14:18 -04001220static void reada_for_search(struct btrfs_root *root,
1221 struct btrfs_path *path,
1222 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001223{
Chris Mason5f39d392007-10-15 16:14:19 -04001224 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001225 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001226 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001227 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001228 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001229 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001230 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001231 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001232 u32 nr;
1233 u32 blocksize;
1234 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001235
Chris Masona6b6e752007-10-15 16:22:39 -04001236 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001237 return;
1238
Chris Mason6702ed42007-08-07 16:15:09 -04001239 if (!path->nodes[level])
1240 return;
1241
Chris Mason5f39d392007-10-15 16:14:19 -04001242 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001243
Chris Mason3c69fae2007-08-07 15:52:22 -04001244 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001245 blocksize = btrfs_level_size(root, level - 1);
1246 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001247 if (eb) {
1248 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001249 return;
1250 }
1251
Chris Masona7175312009-01-22 09:23:10 -05001252 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001253
Chris Mason5f39d392007-10-15 16:14:19 -04001254 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001255 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001256 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001257 if (direction < 0) {
1258 if (nr == 0)
1259 break;
1260 nr--;
1261 } else if (direction > 0) {
1262 nr++;
1263 if (nr >= nritems)
1264 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001265 }
Chris Mason01f46652007-12-21 16:24:26 -05001266 if (path->reada < 0 && objectid) {
1267 btrfs_node_key(node, &disk_key, nr);
1268 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1269 break;
1270 }
Chris Mason6b800532007-10-15 16:17:34 -04001271 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001272 if ((search <= target && target - search <= 65536) ||
1273 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001274 readahead_tree_block(root, search, blocksize,
1275 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001276 nread += blocksize;
1277 }
1278 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001279 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001280 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001281 }
1282}
Chris Mason925baed2008-06-25 16:01:30 -04001283
Chris Masond352ac62008-09-29 15:18:18 -04001284/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001285 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1286 * cache
1287 */
1288static noinline int reada_for_balance(struct btrfs_root *root,
1289 struct btrfs_path *path, int level)
1290{
1291 int slot;
1292 int nritems;
1293 struct extent_buffer *parent;
1294 struct extent_buffer *eb;
1295 u64 gen;
1296 u64 block1 = 0;
1297 u64 block2 = 0;
1298 int ret = 0;
1299 int blocksize;
1300
Chris Mason8c594ea2009-04-20 15:50:10 -04001301 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001302 if (!parent)
1303 return 0;
1304
1305 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001306 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001307 blocksize = btrfs_level_size(root, level);
1308
1309 if (slot > 0) {
1310 block1 = btrfs_node_blockptr(parent, slot - 1);
1311 gen = btrfs_node_ptr_generation(parent, slot - 1);
1312 eb = btrfs_find_tree_block(root, block1, blocksize);
1313 if (eb && btrfs_buffer_uptodate(eb, gen))
1314 block1 = 0;
1315 free_extent_buffer(eb);
1316 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001317 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001318 block2 = btrfs_node_blockptr(parent, slot + 1);
1319 gen = btrfs_node_ptr_generation(parent, slot + 1);
1320 eb = btrfs_find_tree_block(root, block2, blocksize);
1321 if (eb && btrfs_buffer_uptodate(eb, gen))
1322 block2 = 0;
1323 free_extent_buffer(eb);
1324 }
1325 if (block1 || block2) {
1326 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001327
1328 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001329 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001330
1331 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001332 if (block1)
1333 readahead_tree_block(root, block1, blocksize, 0);
1334 if (block2)
1335 readahead_tree_block(root, block2, blocksize, 0);
1336
1337 if (block1) {
1338 eb = read_tree_block(root, block1, blocksize, 0);
1339 free_extent_buffer(eb);
1340 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001341 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001342 eb = read_tree_block(root, block2, blocksize, 0);
1343 free_extent_buffer(eb);
1344 }
1345 }
1346 return ret;
1347}
1348
1349
1350/*
Chris Masond3977122009-01-05 21:25:51 -05001351 * when we walk down the tree, it is usually safe to unlock the higher layers
1352 * in the tree. The exceptions are when our path goes through slot 0, because
1353 * operations on the tree might require changing key pointers higher up in the
1354 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001355 *
Chris Masond3977122009-01-05 21:25:51 -05001356 * callers might also have set path->keep_locks, which tells this code to keep
1357 * the lock if the path points to the last slot in the block. This is part of
1358 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001359 *
Chris Masond3977122009-01-05 21:25:51 -05001360 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1361 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001362 */
Chris Masone02119d2008-09-05 16:13:11 -04001363static noinline void unlock_up(struct btrfs_path *path, int level,
1364 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001365{
1366 int i;
1367 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001368 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001369 struct extent_buffer *t;
1370
1371 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1372 if (!path->nodes[i])
1373 break;
1374 if (!path->locks[i])
1375 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001376 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001377 skip_level = i + 1;
1378 continue;
1379 }
Chris Mason051e1b92008-06-25 16:01:30 -04001380 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001381 u32 nritems;
1382 t = path->nodes[i];
1383 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001384 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001385 skip_level = i + 1;
1386 continue;
1387 }
1388 }
Chris Mason051e1b92008-06-25 16:01:30 -04001389 if (skip_level < i && i >= lowest_unlock)
1390 no_skips = 1;
1391
Chris Mason925baed2008-06-25 16:01:30 -04001392 t = path->nodes[i];
1393 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1394 btrfs_tree_unlock(t);
1395 path->locks[i] = 0;
1396 }
1397 }
1398}
1399
Chris Mason3c69fae2007-08-07 15:52:22 -04001400/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001401 * This releases any locks held in the path starting at level and
1402 * going all the way up to the root.
1403 *
1404 * btrfs_search_slot will keep the lock held on higher nodes in a few
1405 * corner cases, such as COW of the block at slot zero in the node. This
1406 * ignores those rules, and it should only be called when there are no
1407 * more updates to be done higher up in the tree.
1408 */
1409noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1410{
1411 int i;
1412
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001413 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001414 return;
1415
1416 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1417 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001418 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001419 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001420 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001421 btrfs_tree_unlock(path->nodes[i]);
1422 path->locks[i] = 0;
1423 }
1424}
1425
1426/*
Chris Masonc8c42862009-04-03 10:14:18 -04001427 * helper function for btrfs_search_slot. The goal is to find a block
1428 * in cache without setting the path to blocking. If we find the block
1429 * we return zero and the path is unchanged.
1430 *
1431 * If we can't find the block, we set the path blocking and do some
1432 * reada. -EAGAIN is returned and the search must be repeated.
1433 */
1434static int
1435read_block_for_search(struct btrfs_trans_handle *trans,
1436 struct btrfs_root *root, struct btrfs_path *p,
1437 struct extent_buffer **eb_ret, int level, int slot,
1438 struct btrfs_key *key)
1439{
1440 u64 blocknr;
1441 u64 gen;
1442 u32 blocksize;
1443 struct extent_buffer *b = *eb_ret;
1444 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001445 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001446
1447 blocknr = btrfs_node_blockptr(b, slot);
1448 gen = btrfs_node_ptr_generation(b, slot);
1449 blocksize = btrfs_level_size(root, level - 1);
1450
1451 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001452 if (tmp) {
1453 if (btrfs_buffer_uptodate(tmp, 0)) {
1454 if (btrfs_buffer_uptodate(tmp, gen)) {
1455 /*
1456 * we found an up to date block without
1457 * sleeping, return
1458 * right away
1459 */
1460 *eb_ret = tmp;
1461 return 0;
1462 }
1463 /* the pages were up to date, but we failed
1464 * the generation number check. Do a full
1465 * read for the generation number that is correct.
1466 * We must do this without dropping locks so
1467 * we can trust our generation number
1468 */
1469 free_extent_buffer(tmp);
1470 tmp = read_tree_block(root, blocknr, blocksize, gen);
1471 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1472 *eb_ret = tmp;
1473 return 0;
1474 }
1475 free_extent_buffer(tmp);
1476 btrfs_release_path(NULL, p);
1477 return -EIO;
1478 }
Chris Masonc8c42862009-04-03 10:14:18 -04001479 }
1480
1481 /*
1482 * reduce lock contention at high levels
1483 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001484 * we read. Don't release the lock on the current
1485 * level because we need to walk this node to figure
1486 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001487 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001488 btrfs_unlock_up_safe(p, level + 1);
1489 btrfs_set_path_blocking(p);
1490
Chris Masoncb449212010-10-24 11:01:27 -04001491 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001492 if (p->reada)
1493 reada_for_search(root, p, level, slot, key->objectid);
1494
Chris Mason8c594ea2009-04-20 15:50:10 -04001495 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001496
1497 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001498 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001499 if (tmp) {
1500 /*
1501 * If the read above didn't mark this buffer up to date,
1502 * it will never end up being up to date. Set ret to EIO now
1503 * and give up so that our caller doesn't loop forever
1504 * on our EAGAINs.
1505 */
1506 if (!btrfs_buffer_uptodate(tmp, 0))
1507 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001508 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001509 }
1510 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001511}
1512
1513/*
1514 * helper function for btrfs_search_slot. This does all of the checks
1515 * for node-level blocks and does any balancing required based on
1516 * the ins_len.
1517 *
1518 * If no extra work was required, zero is returned. If we had to
1519 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1520 * start over
1521 */
1522static int
1523setup_nodes_for_search(struct btrfs_trans_handle *trans,
1524 struct btrfs_root *root, struct btrfs_path *p,
1525 struct extent_buffer *b, int level, int ins_len)
1526{
1527 int ret;
1528 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1529 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1530 int sret;
1531
1532 sret = reada_for_balance(root, p, level);
1533 if (sret)
1534 goto again;
1535
1536 btrfs_set_path_blocking(p);
1537 sret = split_node(trans, root, p, level);
1538 btrfs_clear_path_blocking(p, NULL);
1539
1540 BUG_ON(sret > 0);
1541 if (sret) {
1542 ret = sret;
1543 goto done;
1544 }
1545 b = p->nodes[level];
1546 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001547 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001548 int sret;
1549
1550 sret = reada_for_balance(root, p, level);
1551 if (sret)
1552 goto again;
1553
1554 btrfs_set_path_blocking(p);
1555 sret = balance_level(trans, root, p, level);
1556 btrfs_clear_path_blocking(p, NULL);
1557
1558 if (sret) {
1559 ret = sret;
1560 goto done;
1561 }
1562 b = p->nodes[level];
1563 if (!b) {
1564 btrfs_release_path(NULL, p);
1565 goto again;
1566 }
1567 BUG_ON(btrfs_header_nritems(b) == 1);
1568 }
1569 return 0;
1570
1571again:
1572 ret = -EAGAIN;
1573done:
1574 return ret;
1575}
1576
1577/*
Chris Mason74123bd2007-02-02 11:05:29 -05001578 * look for key in the tree. path is filled in with nodes along the way
1579 * if key is found, we return zero and you can find the item in the leaf
1580 * level of the path (level 0)
1581 *
1582 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001583 * be inserted, and 1 is returned. If there are other errors during the
1584 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001585 *
1586 * if ins_len > 0, nodes and leaves will be split as we walk down the
1587 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1588 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001589 */
Chris Masone089f052007-03-16 16:20:31 -04001590int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1591 *root, struct btrfs_key *key, struct btrfs_path *p, int
1592 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001593{
Chris Mason5f39d392007-10-15 16:14:19 -04001594 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001595 int slot;
1596 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001597 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001598 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001599 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001600 u8 lowest_level = 0;
1601
Chris Mason6702ed42007-08-07 16:15:09 -04001602 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001603 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001604 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001605
Chris Mason925baed2008-06-25 16:01:30 -04001606 if (ins_len < 0)
1607 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001608
Chris Masonbb803952007-03-01 12:04:21 -05001609again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001610 if (p->search_commit_root) {
1611 b = root->commit_root;
1612 extent_buffer_get(b);
1613 if (!p->skip_locking)
1614 btrfs_tree_lock(b);
1615 } else {
1616 if (p->skip_locking)
1617 b = btrfs_root_node(root);
1618 else
1619 b = btrfs_lock_root_node(root);
1620 }
Chris Mason925baed2008-06-25 16:01:30 -04001621
Chris Masoneb60cea2007-02-02 09:18:22 -05001622 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001623 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001624
1625 /*
1626 * setup the path here so we can release it under lock
1627 * contention with the cow code
1628 */
1629 p->nodes[level] = b;
1630 if (!p->skip_locking)
1631 p->locks[level] = 1;
1632
Chris Mason02217ed2007-03-02 16:08:05 -05001633 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001634 /*
1635 * if we don't really need to cow this block
1636 * then we don't want to set the path blocking,
1637 * so we test it here
1638 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001639 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001640 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001641
Chris Masonb4ce94d2009-02-04 09:25:08 -05001642 btrfs_set_path_blocking(p);
1643
Yan Zheng33c66f42009-07-22 09:59:00 -04001644 err = btrfs_cow_block(trans, root, b,
1645 p->nodes[level + 1],
1646 p->slots[level + 1], &b);
1647 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001648 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001649 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001650 }
Chris Mason02217ed2007-03-02 16:08:05 -05001651 }
Chris Mason65b51a02008-08-01 15:11:20 -04001652cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001653 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001654 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001655 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001656 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001657
Chris Masoneb60cea2007-02-02 09:18:22 -05001658 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001659 if (!p->skip_locking)
1660 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001661
Chris Mason4008c042009-02-12 14:09:45 -05001662 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001663
1664 /*
1665 * we have a lock on b and as long as we aren't changing
1666 * the tree, there is no way to for the items in b to change.
1667 * It is safe to drop the lock on our parent before we
1668 * go through the expensive btree search on b.
1669 *
1670 * If cow is true, then we might be changing slot zero,
1671 * which may require changing the parent. So, we can't
1672 * drop the lock until after we know which slot we're
1673 * operating on.
1674 */
1675 if (!cow)
1676 btrfs_unlock_up_safe(p, level + 1);
1677
Chris Mason5f39d392007-10-15 16:14:19 -04001678 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001679
Chris Mason5f39d392007-10-15 16:14:19 -04001680 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001681 int dec = 0;
1682 if (ret && slot > 0) {
1683 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001684 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001685 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001686 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001687 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001688 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001689 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001690 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001691 if (err) {
1692 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001693 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001694 }
Chris Masonc8c42862009-04-03 10:14:18 -04001695 b = p->nodes[level];
1696 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001697
Chris Masonf9efa9c2008-06-25 16:14:04 -04001698 unlock_up(p, level, lowest_unlock);
1699
Chris Mason925baed2008-06-25 16:01:30 -04001700 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001701 if (dec)
1702 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001703 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001704 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001705
Yan Zheng33c66f42009-07-22 09:59:00 -04001706 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001707 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001708 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001709 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001710 if (err) {
1711 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001712 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001713 }
Chris Mason76a05b32009-05-14 13:24:30 -04001714
Chris Masonb4ce94d2009-02-04 09:25:08 -05001715 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001716 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001717 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001718
Yan Zheng33c66f42009-07-22 09:59:00 -04001719 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001720 btrfs_set_path_blocking(p);
1721 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001722 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001723 }
1724 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001725 } else {
1726 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001727 if (ins_len > 0 &&
1728 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001729 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001730 err = split_leaf(trans, root, key,
1731 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001732 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001733
Yan Zheng33c66f42009-07-22 09:59:00 -04001734 BUG_ON(err > 0);
1735 if (err) {
1736 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001737 goto done;
1738 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001739 }
Chris Mason459931e2008-12-10 09:10:46 -05001740 if (!p->search_for_split)
1741 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001742 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001743 }
1744 }
Chris Mason65b51a02008-08-01 15:11:20 -04001745 ret = 1;
1746done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001747 /*
1748 * we don't really know what they plan on doing with the path
1749 * from here on, so for now just mark it as blocking
1750 */
Chris Masonb9473432009-03-13 11:00:37 -04001751 if (!p->leave_spinning)
1752 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001753 if (ret < 0)
1754 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001755 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001756}
1757
Chris Mason74123bd2007-02-02 11:05:29 -05001758/*
1759 * adjust the pointers going up the tree, starting at level
1760 * making sure the right key of each node is points to 'key'.
1761 * This is used after shifting pointers to the left, so it stops
1762 * fixing up pointers when a given leaf/node is not in slot 0 of the
1763 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001764 *
1765 * If this fails to write a tree block, it returns -1, but continues
1766 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001767 */
Chris Mason5f39d392007-10-15 16:14:19 -04001768static int fixup_low_keys(struct btrfs_trans_handle *trans,
1769 struct btrfs_root *root, struct btrfs_path *path,
1770 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001771{
1772 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001773 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001774 struct extent_buffer *t;
1775
Chris Mason234b63a2007-03-13 10:46:10 -04001776 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001777 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001778 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001779 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001780 t = path->nodes[i];
1781 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001782 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001783 if (tslot != 0)
1784 break;
1785 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001786 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001787}
1788
Chris Mason74123bd2007-02-02 11:05:29 -05001789/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001790 * update item key.
1791 *
1792 * This function isn't completely safe. It's the caller's responsibility
1793 * that the new key won't break the order
1794 */
1795int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1796 struct btrfs_root *root, struct btrfs_path *path,
1797 struct btrfs_key *new_key)
1798{
1799 struct btrfs_disk_key disk_key;
1800 struct extent_buffer *eb;
1801 int slot;
1802
1803 eb = path->nodes[0];
1804 slot = path->slots[0];
1805 if (slot > 0) {
1806 btrfs_item_key(eb, &disk_key, slot - 1);
1807 if (comp_keys(&disk_key, new_key) >= 0)
1808 return -1;
1809 }
1810 if (slot < btrfs_header_nritems(eb) - 1) {
1811 btrfs_item_key(eb, &disk_key, slot + 1);
1812 if (comp_keys(&disk_key, new_key) <= 0)
1813 return -1;
1814 }
1815
1816 btrfs_cpu_key_to_disk(&disk_key, new_key);
1817 btrfs_set_item_key(eb, &disk_key, slot);
1818 btrfs_mark_buffer_dirty(eb);
1819 if (slot == 0)
1820 fixup_low_keys(trans, root, path, &disk_key, 1);
1821 return 0;
1822}
1823
1824/*
Chris Mason74123bd2007-02-02 11:05:29 -05001825 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001826 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001827 *
1828 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1829 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001830 */
Chris Mason98ed5172008-01-03 10:01:48 -05001831static int push_node_left(struct btrfs_trans_handle *trans,
1832 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001833 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001834{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001835 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001836 int src_nritems;
1837 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001838 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001839
Chris Mason5f39d392007-10-15 16:14:19 -04001840 src_nritems = btrfs_header_nritems(src);
1841 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001842 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001843 WARN_ON(btrfs_header_generation(src) != trans->transid);
1844 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001845
Chris Masonbce4eae2008-04-24 14:42:46 -04001846 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001847 return 1;
1848
Chris Masond3977122009-01-05 21:25:51 -05001849 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001850 return 1;
1851
Chris Masonbce4eae2008-04-24 14:42:46 -04001852 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001853 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001854 if (push_items < src_nritems) {
1855 /* leave at least 8 pointers in the node if
1856 * we aren't going to empty it
1857 */
1858 if (src_nritems - push_items < 8) {
1859 if (push_items <= 8)
1860 return 1;
1861 push_items -= 8;
1862 }
1863 }
1864 } else
1865 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001866
Chris Mason5f39d392007-10-15 16:14:19 -04001867 copy_extent_buffer(dst, src,
1868 btrfs_node_key_ptr_offset(dst_nritems),
1869 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001870 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001871
Chris Masonbb803952007-03-01 12:04:21 -05001872 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001873 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1874 btrfs_node_key_ptr_offset(push_items),
1875 (src_nritems - push_items) *
1876 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001877 }
Chris Mason5f39d392007-10-15 16:14:19 -04001878 btrfs_set_header_nritems(src, src_nritems - push_items);
1879 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1880 btrfs_mark_buffer_dirty(src);
1881 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001882
Chris Masonbb803952007-03-01 12:04:21 -05001883 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001884}
1885
Chris Mason97571fd2007-02-24 13:39:08 -05001886/*
Chris Mason79f95c82007-03-01 15:16:26 -05001887 * try to push data from one node into the next node right in the
1888 * tree.
1889 *
1890 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1891 * error, and > 0 if there was no room in the right hand block.
1892 *
1893 * this will only push up to 1/2 the contents of the left node over
1894 */
Chris Mason5f39d392007-10-15 16:14:19 -04001895static int balance_node_right(struct btrfs_trans_handle *trans,
1896 struct btrfs_root *root,
1897 struct extent_buffer *dst,
1898 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001899{
Chris Mason79f95c82007-03-01 15:16:26 -05001900 int push_items = 0;
1901 int max_push;
1902 int src_nritems;
1903 int dst_nritems;
1904 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001905
Chris Mason7bb86312007-12-11 09:25:06 -05001906 WARN_ON(btrfs_header_generation(src) != trans->transid);
1907 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1908
Chris Mason5f39d392007-10-15 16:14:19 -04001909 src_nritems = btrfs_header_nritems(src);
1910 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001911 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001912 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001913 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001914
Chris Masond3977122009-01-05 21:25:51 -05001915 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001916 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001917
1918 max_push = src_nritems / 2 + 1;
1919 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001920 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001921 return 1;
Yan252c38f2007-08-29 09:11:44 -04001922
Chris Mason79f95c82007-03-01 15:16:26 -05001923 if (max_push < push_items)
1924 push_items = max_push;
1925
Chris Mason5f39d392007-10-15 16:14:19 -04001926 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1927 btrfs_node_key_ptr_offset(0),
1928 (dst_nritems) *
1929 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001930
Chris Mason5f39d392007-10-15 16:14:19 -04001931 copy_extent_buffer(dst, src,
1932 btrfs_node_key_ptr_offset(0),
1933 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001934 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001935
Chris Mason5f39d392007-10-15 16:14:19 -04001936 btrfs_set_header_nritems(src, src_nritems - push_items);
1937 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001938
Chris Mason5f39d392007-10-15 16:14:19 -04001939 btrfs_mark_buffer_dirty(src);
1940 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001941
Chris Mason79f95c82007-03-01 15:16:26 -05001942 return ret;
1943}
1944
1945/*
Chris Mason97571fd2007-02-24 13:39:08 -05001946 * helper function to insert a new root level in the tree.
1947 * A new node is allocated, and a single item is inserted to
1948 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001949 *
1950 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001951 */
Chris Masond3977122009-01-05 21:25:51 -05001952static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001953 struct btrfs_root *root,
1954 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001955{
Chris Mason7bb86312007-12-11 09:25:06 -05001956 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001957 struct extent_buffer *lower;
1958 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001959 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001960 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001961
1962 BUG_ON(path->nodes[level]);
1963 BUG_ON(path->nodes[level-1] != root->node);
1964
Chris Mason7bb86312007-12-11 09:25:06 -05001965 lower = path->nodes[level-1];
1966 if (level == 1)
1967 btrfs_item_key(lower, &lower_key, 0);
1968 else
1969 btrfs_node_key(lower, &lower_key, 0);
1970
Zheng Yan31840ae2008-09-23 13:14:14 -04001971 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001972 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001973 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001974 if (IS_ERR(c))
1975 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001976
Yan, Zhengf0486c62010-05-16 10:46:25 -04001977 root_add_used(root, root->nodesize);
1978
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001979 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04001980 btrfs_set_header_nritems(c, 1);
1981 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001982 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001983 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001984 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04001985 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001986
Chris Mason5f39d392007-10-15 16:14:19 -04001987 write_extent_buffer(c, root->fs_info->fsid,
1988 (unsigned long)btrfs_header_fsid(c),
1989 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001990
1991 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1992 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1993 BTRFS_UUID_SIZE);
1994
Chris Mason5f39d392007-10-15 16:14:19 -04001995 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001996 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001997 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001998 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001999
2000 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002001
2002 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002003
Chris Mason925baed2008-06-25 16:01:30 -04002004 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002005 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002006
2007 /* the super has an extra ref to root->node */
2008 free_extent_buffer(old);
2009
Chris Mason0b86a832008-03-24 15:01:56 -04002010 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002011 extent_buffer_get(c);
2012 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002013 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002014 path->slots[level] = 0;
2015 return 0;
2016}
2017
Chris Mason74123bd2007-02-02 11:05:29 -05002018/*
2019 * worker function to insert a single pointer in a node.
2020 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002021 *
Chris Mason74123bd2007-02-02 11:05:29 -05002022 * slot and level indicate where you want the key to go, and
2023 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002024 *
2025 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002026 */
Chris Masone089f052007-03-16 16:20:31 -04002027static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2028 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002029 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002030{
Chris Mason5f39d392007-10-15 16:14:19 -04002031 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002032 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002033
2034 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002035 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002036 lower = path->nodes[level];
2037 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002038 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002039 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002040 BUG();
2041 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002042 memmove_extent_buffer(lower,
2043 btrfs_node_key_ptr_offset(slot + 1),
2044 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002045 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002046 }
Chris Mason5f39d392007-10-15 16:14:19 -04002047 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002048 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002049 WARN_ON(trans->transid == 0);
2050 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 btrfs_set_header_nritems(lower, nritems + 1);
2052 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002053 return 0;
2054}
2055
Chris Mason97571fd2007-02-24 13:39:08 -05002056/*
2057 * split the node at the specified level in path in two.
2058 * The path is corrected to point to the appropriate node after the split
2059 *
2060 * Before splitting this tries to make some room in the node by pushing
2061 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002062 *
2063 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002064 */
Chris Masone02119d2008-09-05 16:13:11 -04002065static noinline int split_node(struct btrfs_trans_handle *trans,
2066 struct btrfs_root *root,
2067 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002068{
Chris Mason5f39d392007-10-15 16:14:19 -04002069 struct extent_buffer *c;
2070 struct extent_buffer *split;
2071 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002072 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002073 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002074 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002075 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002076
Chris Mason5f39d392007-10-15 16:14:19 -04002077 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002078 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002079 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002080 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002081 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002082 if (ret)
2083 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002084 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002085 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002086 c = path->nodes[level];
2087 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002088 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002089 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002090 if (ret < 0)
2091 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002092 }
Chris Masone66f7092007-04-20 13:16:02 -04002093
Chris Mason5f39d392007-10-15 16:14:19 -04002094 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002095 mid = (c_nritems + 1) / 2;
2096 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002097
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002098 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002099 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002101 if (IS_ERR(split))
2102 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -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(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002107 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002108 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002109 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002110 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002111 btrfs_set_header_owner(split, root->root_key.objectid);
2112 write_extent_buffer(split, root->fs_info->fsid,
2113 (unsigned long)btrfs_header_fsid(split),
2114 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002115 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2116 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2117 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002118
Chris Mason5f39d392007-10-15 16:14:19 -04002119
2120 copy_extent_buffer(split, c,
2121 btrfs_node_key_ptr_offset(0),
2122 btrfs_node_key_ptr_offset(mid),
2123 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2124 btrfs_set_header_nritems(split, c_nritems - mid);
2125 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002126 ret = 0;
2127
Chris Mason5f39d392007-10-15 16:14:19 -04002128 btrfs_mark_buffer_dirty(c);
2129 btrfs_mark_buffer_dirty(split);
2130
Chris Masondb945352007-10-15 16:15:53 -04002131 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002132 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002133 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002134 if (wret)
2135 ret = wret;
2136
Chris Mason5de08d72007-02-24 06:24:44 -05002137 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002138 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002139 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002140 free_extent_buffer(c);
2141 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002142 path->slots[level + 1] += 1;
2143 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002144 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002145 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002146 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002147 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002148}
2149
Chris Mason74123bd2007-02-02 11:05:29 -05002150/*
2151 * how many bytes are required to store the items in a leaf. start
2152 * and nr indicate which items in the leaf to check. This totals up the
2153 * space used both by the item structs and the item data
2154 */
Chris Mason5f39d392007-10-15 16:14:19 -04002155static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002156{
2157 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002158 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002159 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002160
2161 if (!nr)
2162 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002163 data_len = btrfs_item_end_nr(l, start);
2164 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002165 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002166 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002167 return data_len;
2168}
2169
Chris Mason74123bd2007-02-02 11:05:29 -05002170/*
Chris Masond4dbff92007-04-04 14:08:15 -04002171 * The space between the end of the leaf items and
2172 * the start of the leaf data. IOW, how much room
2173 * the leaf has left for both items and data
2174 */
Chris Masond3977122009-01-05 21:25:51 -05002175noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002176 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002177{
Chris Mason5f39d392007-10-15 16:14:19 -04002178 int nritems = btrfs_header_nritems(leaf);
2179 int ret;
2180 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2181 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002182 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2183 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002184 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002185 leaf_space_used(leaf, 0, nritems), nritems);
2186 }
2187 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002188}
2189
Chris Mason99d8f832010-07-07 10:51:48 -04002190/*
2191 * min slot controls the lowest index we're willing to push to the
2192 * right. We'll push up to and including min_slot, but no lower
2193 */
Chris Mason44871b12009-03-13 10:04:31 -04002194static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2195 struct btrfs_root *root,
2196 struct btrfs_path *path,
2197 int data_size, int empty,
2198 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002199 int free_space, u32 left_nritems,
2200 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002201{
Chris Mason5f39d392007-10-15 16:14:19 -04002202 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002203 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002204 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002205 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002206 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002207 int push_space = 0;
2208 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002209 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002210 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002211 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002212 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002213 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002214
Chris Mason34a38212007-11-07 13:31:03 -05002215 if (empty)
2216 nr = 0;
2217 else
Chris Mason99d8f832010-07-07 10:51:48 -04002218 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002219
Zheng Yan31840ae2008-09-23 13:14:14 -04002220 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002221 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002222
Chris Mason44871b12009-03-13 10:04:31 -04002223 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002224 i = left_nritems - 1;
2225 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002226 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002227
Zheng Yan31840ae2008-09-23 13:14:14 -04002228 if (!empty && push_items > 0) {
2229 if (path->slots[0] > i)
2230 break;
2231 if (path->slots[0] == i) {
2232 int space = btrfs_leaf_free_space(root, left);
2233 if (space + push_space * 2 > free_space)
2234 break;
2235 }
2236 }
2237
Chris Mason00ec4c52007-02-24 12:47:20 -05002238 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002239 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002240
2241 if (!left->map_token) {
2242 map_extent_buffer(left, (unsigned long)item,
2243 sizeof(struct btrfs_item),
2244 &left->map_token, &left->kaddr,
2245 &left->map_start, &left->map_len,
2246 KM_USER1);
2247 }
2248
2249 this_item_size = btrfs_item_size(left, item);
2250 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002251 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002252
Chris Mason00ec4c52007-02-24 12:47:20 -05002253 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002254 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002255 if (i == 0)
2256 break;
2257 i--;
Chris Masondb945352007-10-15 16:15:53 -04002258 }
2259 if (left->map_token) {
2260 unmap_extent_buffer(left, left->map_token, KM_USER1);
2261 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002262 }
Chris Mason5f39d392007-10-15 16:14:19 -04002263
Chris Mason925baed2008-06-25 16:01:30 -04002264 if (push_items == 0)
2265 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002266
Chris Mason34a38212007-11-07 13:31:03 -05002267 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002268 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002269
Chris Mason00ec4c52007-02-24 12:47:20 -05002270 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002271 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002272
Chris Mason5f39d392007-10-15 16:14:19 -04002273 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002274 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002275
Chris Mason00ec4c52007-02-24 12:47:20 -05002276 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002277 data_end = leaf_data_end(root, right);
2278 memmove_extent_buffer(right,
2279 btrfs_leaf_data(right) + data_end - push_space,
2280 btrfs_leaf_data(right) + data_end,
2281 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2282
Chris Mason00ec4c52007-02-24 12:47:20 -05002283 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002284 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002285 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2286 btrfs_leaf_data(left) + leaf_data_end(root, left),
2287 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002288
2289 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2290 btrfs_item_nr_offset(0),
2291 right_nritems * sizeof(struct btrfs_item));
2292
Chris Mason00ec4c52007-02-24 12:47:20 -05002293 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002294 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2295 btrfs_item_nr_offset(left_nritems - push_items),
2296 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002297
2298 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002299 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002300 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002301 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002302 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002303 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002304 if (!right->map_token) {
2305 map_extent_buffer(right, (unsigned long)item,
2306 sizeof(struct btrfs_item),
2307 &right->map_token, &right->kaddr,
2308 &right->map_start, &right->map_len,
2309 KM_USER1);
2310 }
2311 push_space -= btrfs_item_size(right, item);
2312 btrfs_set_item_offset(right, item, push_space);
2313 }
2314
2315 if (right->map_token) {
2316 unmap_extent_buffer(right, right->map_token, KM_USER1);
2317 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002318 }
Chris Mason7518a232007-03-12 12:01:18 -04002319 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002320 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002321
Chris Mason34a38212007-11-07 13:31:03 -05002322 if (left_nritems)
2323 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002324 else
2325 clean_tree_block(trans, root, left);
2326
Chris Mason5f39d392007-10-15 16:14:19 -04002327 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002328
Chris Mason5f39d392007-10-15 16:14:19 -04002329 btrfs_item_key(right, &disk_key, 0);
2330 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002331 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002332
Chris Mason00ec4c52007-02-24 12:47:20 -05002333 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002334 if (path->slots[0] >= left_nritems) {
2335 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002336 if (btrfs_header_nritems(path->nodes[0]) == 0)
2337 clean_tree_block(trans, root, path->nodes[0]);
2338 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002339 free_extent_buffer(path->nodes[0]);
2340 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002341 path->slots[1] += 1;
2342 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002343 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002344 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002345 }
2346 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002347
2348out_unlock:
2349 btrfs_tree_unlock(right);
2350 free_extent_buffer(right);
2351 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002352}
Chris Mason925baed2008-06-25 16:01:30 -04002353
Chris Mason00ec4c52007-02-24 12:47:20 -05002354/*
Chris Mason44871b12009-03-13 10:04:31 -04002355 * push some data in the path leaf to the right, trying to free up at
2356 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2357 *
2358 * returns 1 if the push failed because the other node didn't have enough
2359 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002360 *
2361 * this will push starting from min_slot to the end of the leaf. It won't
2362 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002363 */
2364static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002365 *root, struct btrfs_path *path,
2366 int min_data_size, int data_size,
2367 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002368{
2369 struct extent_buffer *left = path->nodes[0];
2370 struct extent_buffer *right;
2371 struct extent_buffer *upper;
2372 int slot;
2373 int free_space;
2374 u32 left_nritems;
2375 int ret;
2376
2377 if (!path->nodes[1])
2378 return 1;
2379
2380 slot = path->slots[1];
2381 upper = path->nodes[1];
2382 if (slot >= btrfs_header_nritems(upper) - 1)
2383 return 1;
2384
2385 btrfs_assert_tree_locked(path->nodes[1]);
2386
2387 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002388 if (right == NULL)
2389 return 1;
2390
Chris Mason44871b12009-03-13 10:04:31 -04002391 btrfs_tree_lock(right);
2392 btrfs_set_lock_blocking(right);
2393
2394 free_space = btrfs_leaf_free_space(root, right);
2395 if (free_space < data_size)
2396 goto out_unlock;
2397
2398 /* cow and double check */
2399 ret = btrfs_cow_block(trans, root, right, upper,
2400 slot + 1, &right);
2401 if (ret)
2402 goto out_unlock;
2403
2404 free_space = btrfs_leaf_free_space(root, right);
2405 if (free_space < data_size)
2406 goto out_unlock;
2407
2408 left_nritems = btrfs_header_nritems(left);
2409 if (left_nritems == 0)
2410 goto out_unlock;
2411
Chris Mason99d8f832010-07-07 10:51:48 -04002412 return __push_leaf_right(trans, root, path, min_data_size, empty,
2413 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002414out_unlock:
2415 btrfs_tree_unlock(right);
2416 free_extent_buffer(right);
2417 return 1;
2418}
2419
2420/*
Chris Mason74123bd2007-02-02 11:05:29 -05002421 * push some data in the path leaf to the left, trying to free up at
2422 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002423 *
2424 * max_slot can put a limit on how far into the leaf we'll push items. The
2425 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2426 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002427 */
Chris Mason44871b12009-03-13 10:04:31 -04002428static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2429 struct btrfs_root *root,
2430 struct btrfs_path *path, int data_size,
2431 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002432 int free_space, u32 right_nritems,
2433 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002434{
Chris Mason5f39d392007-10-15 16:14:19 -04002435 struct btrfs_disk_key disk_key;
2436 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002437 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002438 int push_space = 0;
2439 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002440 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002441 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002442 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002443 int ret = 0;
2444 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002445 u32 this_item_size;
2446 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002447
Chris Mason34a38212007-11-07 13:31:03 -05002448 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002449 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002450 else
Chris Mason99d8f832010-07-07 10:51:48 -04002451 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002452
2453 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002454 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002455 if (!right->map_token) {
2456 map_extent_buffer(right, (unsigned long)item,
2457 sizeof(struct btrfs_item),
2458 &right->map_token, &right->kaddr,
2459 &right->map_start, &right->map_len,
2460 KM_USER1);
2461 }
2462
Zheng Yan31840ae2008-09-23 13:14:14 -04002463 if (!empty && push_items > 0) {
2464 if (path->slots[0] < i)
2465 break;
2466 if (path->slots[0] == i) {
2467 int space = btrfs_leaf_free_space(root, right);
2468 if (space + push_space * 2 > free_space)
2469 break;
2470 }
2471 }
2472
Chris Masonbe0e5c02007-01-26 15:51:26 -05002473 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002474 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002475
2476 this_item_size = btrfs_item_size(right, item);
2477 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002478 break;
Chris Masondb945352007-10-15 16:15:53 -04002479
Chris Masonbe0e5c02007-01-26 15:51:26 -05002480 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002481 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002482 }
Chris Masondb945352007-10-15 16:15:53 -04002483
2484 if (right->map_token) {
2485 unmap_extent_buffer(right, right->map_token, KM_USER1);
2486 right->map_token = NULL;
2487 }
2488
Chris Masonbe0e5c02007-01-26 15:51:26 -05002489 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002490 ret = 1;
2491 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002492 }
Chris Mason34a38212007-11-07 13:31:03 -05002493 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002494 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002495
Chris Masonbe0e5c02007-01-26 15:51:26 -05002496 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002497 copy_extent_buffer(left, right,
2498 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2499 btrfs_item_nr_offset(0),
2500 push_items * sizeof(struct btrfs_item));
2501
Chris Mason123abc82007-03-14 14:14:43 -04002502 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002503 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002504
2505 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002506 leaf_data_end(root, left) - push_space,
2507 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002508 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002509 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002510 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002511 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002512
Chris Masondb945352007-10-15 16:15:53 -04002513 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002514 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002515 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002516
Chris Mason5f39d392007-10-15 16:14:19 -04002517 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002518 if (!left->map_token) {
2519 map_extent_buffer(left, (unsigned long)item,
2520 sizeof(struct btrfs_item),
2521 &left->map_token, &left->kaddr,
2522 &left->map_start, &left->map_len,
2523 KM_USER1);
2524 }
2525
Chris Mason5f39d392007-10-15 16:14:19 -04002526 ioff = btrfs_item_offset(left, item);
2527 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002528 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002529 }
Chris Mason5f39d392007-10-15 16:14:19 -04002530 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002531 if (left->map_token) {
2532 unmap_extent_buffer(left, left->map_token, KM_USER1);
2533 left->map_token = NULL;
2534 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002535
2536 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002537 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002538 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2539 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002540 WARN_ON(1);
2541 }
Chris Mason5f39d392007-10-15 16:14:19 -04002542
Chris Mason34a38212007-11-07 13:31:03 -05002543 if (push_items < right_nritems) {
2544 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2545 leaf_data_end(root, right);
2546 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2547 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2548 btrfs_leaf_data(right) +
2549 leaf_data_end(root, right), push_space);
2550
2551 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002552 btrfs_item_nr_offset(push_items),
2553 (btrfs_header_nritems(right) - push_items) *
2554 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002555 }
Yaneef1c492007-11-26 10:58:13 -05002556 right_nritems -= push_items;
2557 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002558 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002559 for (i = 0; i < right_nritems; i++) {
2560 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002561
2562 if (!right->map_token) {
2563 map_extent_buffer(right, (unsigned long)item,
2564 sizeof(struct btrfs_item),
2565 &right->map_token, &right->kaddr,
2566 &right->map_start, &right->map_len,
2567 KM_USER1);
2568 }
2569
2570 push_space = push_space - btrfs_item_size(right, item);
2571 btrfs_set_item_offset(right, item, push_space);
2572 }
2573 if (right->map_token) {
2574 unmap_extent_buffer(right, right->map_token, KM_USER1);
2575 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002576 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002577
Chris Mason5f39d392007-10-15 16:14:19 -04002578 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002579 if (right_nritems)
2580 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002581 else
2582 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002583
Chris Mason5f39d392007-10-15 16:14:19 -04002584 btrfs_item_key(right, &disk_key, 0);
2585 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002586 if (wret)
2587 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002588
2589 /* then fixup the leaf pointer in the path */
2590 if (path->slots[0] < push_items) {
2591 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002592 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002593 free_extent_buffer(path->nodes[0]);
2594 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002595 path->slots[1] -= 1;
2596 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002597 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002598 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002599 path->slots[0] -= push_items;
2600 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002601 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002602 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002603out:
2604 btrfs_tree_unlock(left);
2605 free_extent_buffer(left);
2606 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002607}
2608
Chris Mason74123bd2007-02-02 11:05:29 -05002609/*
Chris Mason44871b12009-03-13 10:04:31 -04002610 * push some data in the path leaf to the left, trying to free up at
2611 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002612 *
2613 * max_slot can put a limit on how far into the leaf we'll push items. The
2614 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2615 * items
Chris Mason44871b12009-03-13 10:04:31 -04002616 */
2617static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002618 *root, struct btrfs_path *path, int min_data_size,
2619 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002620{
2621 struct extent_buffer *right = path->nodes[0];
2622 struct extent_buffer *left;
2623 int slot;
2624 int free_space;
2625 u32 right_nritems;
2626 int ret = 0;
2627
2628 slot = path->slots[1];
2629 if (slot == 0)
2630 return 1;
2631 if (!path->nodes[1])
2632 return 1;
2633
2634 right_nritems = btrfs_header_nritems(right);
2635 if (right_nritems == 0)
2636 return 1;
2637
2638 btrfs_assert_tree_locked(path->nodes[1]);
2639
2640 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002641 if (left == NULL)
2642 return 1;
2643
Chris Mason44871b12009-03-13 10:04:31 -04002644 btrfs_tree_lock(left);
2645 btrfs_set_lock_blocking(left);
2646
2647 free_space = btrfs_leaf_free_space(root, left);
2648 if (free_space < data_size) {
2649 ret = 1;
2650 goto out;
2651 }
2652
2653 /* cow and double check */
2654 ret = btrfs_cow_block(trans, root, left,
2655 path->nodes[1], slot - 1, &left);
2656 if (ret) {
2657 /* we hit -ENOSPC, but it isn't fatal here */
2658 ret = 1;
2659 goto out;
2660 }
2661
2662 free_space = btrfs_leaf_free_space(root, left);
2663 if (free_space < data_size) {
2664 ret = 1;
2665 goto out;
2666 }
2667
Chris Mason99d8f832010-07-07 10:51:48 -04002668 return __push_leaf_left(trans, root, path, min_data_size,
2669 empty, left, free_space, right_nritems,
2670 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002671out:
2672 btrfs_tree_unlock(left);
2673 free_extent_buffer(left);
2674 return ret;
2675}
2676
2677/*
Chris Mason74123bd2007-02-02 11:05:29 -05002678 * split the path's leaf in two, making sure there is at least data_size
2679 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002680 *
2681 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002682 */
Chris Mason44871b12009-03-13 10:04:31 -04002683static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002684 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002685 struct btrfs_path *path,
2686 struct extent_buffer *l,
2687 struct extent_buffer *right,
2688 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002690 int data_copy_size;
2691 int rt_data_off;
2692 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002693 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002694 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002695 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002696
Chris Mason5f39d392007-10-15 16:14:19 -04002697 nritems = nritems - mid;
2698 btrfs_set_header_nritems(right, nritems);
2699 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2700
2701 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2702 btrfs_item_nr_offset(mid),
2703 nritems * sizeof(struct btrfs_item));
2704
2705 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002706 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2707 data_copy_size, btrfs_leaf_data(l) +
2708 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002709
Chris Mason5f39d392007-10-15 16:14:19 -04002710 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2711 btrfs_item_end_nr(l, mid);
2712
2713 for (i = 0; i < nritems; i++) {
2714 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002715 u32 ioff;
2716
2717 if (!right->map_token) {
2718 map_extent_buffer(right, (unsigned long)item,
2719 sizeof(struct btrfs_item),
2720 &right->map_token, &right->kaddr,
2721 &right->map_start, &right->map_len,
2722 KM_USER1);
2723 }
2724
2725 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002726 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002727 }
Chris Mason74123bd2007-02-02 11:05:29 -05002728
Chris Masondb945352007-10-15 16:15:53 -04002729 if (right->map_token) {
2730 unmap_extent_buffer(right, right->map_token, KM_USER1);
2731 right->map_token = NULL;
2732 }
2733
Chris Mason5f39d392007-10-15 16:14:19 -04002734 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002735 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002736 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002737 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2738 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002739 if (wret)
2740 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002741
2742 btrfs_mark_buffer_dirty(right);
2743 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002744 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002745
Chris Masonbe0e5c02007-01-26 15:51:26 -05002746 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002747 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002748 free_extent_buffer(path->nodes[0]);
2749 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002750 path->slots[0] -= mid;
2751 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002752 } else {
2753 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002754 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002755 }
Chris Mason5f39d392007-10-15 16:14:19 -04002756
Chris Masoneb60cea2007-02-02 09:18:22 -05002757 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002758
Chris Mason44871b12009-03-13 10:04:31 -04002759 return ret;
2760}
2761
2762/*
Chris Mason99d8f832010-07-07 10:51:48 -04002763 * double splits happen when we need to insert a big item in the middle
2764 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2765 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2766 * A B C
2767 *
2768 * We avoid this by trying to push the items on either side of our target
2769 * into the adjacent leaves. If all goes well we can avoid the double split
2770 * completely.
2771 */
2772static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2773 struct btrfs_root *root,
2774 struct btrfs_path *path,
2775 int data_size)
2776{
2777 int ret;
2778 int progress = 0;
2779 int slot;
2780 u32 nritems;
2781
2782 slot = path->slots[0];
2783
2784 /*
2785 * try to push all the items after our slot into the
2786 * right leaf
2787 */
2788 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2789 if (ret < 0)
2790 return ret;
2791
2792 if (ret == 0)
2793 progress++;
2794
2795 nritems = btrfs_header_nritems(path->nodes[0]);
2796 /*
2797 * our goal is to get our slot at the start or end of a leaf. If
2798 * we've done so we're done
2799 */
2800 if (path->slots[0] == 0 || path->slots[0] == nritems)
2801 return 0;
2802
2803 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2804 return 0;
2805
2806 /* try to push all the items before our slot into the next leaf */
2807 slot = path->slots[0];
2808 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2809 if (ret < 0)
2810 return ret;
2811
2812 if (ret == 0)
2813 progress++;
2814
2815 if (progress)
2816 return 0;
2817 return 1;
2818}
2819
2820/*
Chris Mason44871b12009-03-13 10:04:31 -04002821 * split the path's leaf in two, making sure there is at least data_size
2822 * available for the resulting leaf level of the path.
2823 *
2824 * returns 0 if all went well and < 0 on failure.
2825 */
2826static noinline int split_leaf(struct btrfs_trans_handle *trans,
2827 struct btrfs_root *root,
2828 struct btrfs_key *ins_key,
2829 struct btrfs_path *path, int data_size,
2830 int extend)
2831{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002832 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002833 struct extent_buffer *l;
2834 u32 nritems;
2835 int mid;
2836 int slot;
2837 struct extent_buffer *right;
2838 int ret = 0;
2839 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002840 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002841 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002842 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002843
Yan, Zhenga5719522009-09-24 09:17:31 -04002844 l = path->nodes[0];
2845 slot = path->slots[0];
2846 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2847 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2848 return -EOVERFLOW;
2849
Chris Mason44871b12009-03-13 10:04:31 -04002850 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002851 if (data_size) {
2852 wret = push_leaf_right(trans, root, path, data_size,
2853 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002854 if (wret < 0)
2855 return wret;
2856 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002857 wret = push_leaf_left(trans, root, path, data_size,
2858 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002859 if (wret < 0)
2860 return wret;
2861 }
2862 l = path->nodes[0];
2863
2864 /* did the pushes work? */
2865 if (btrfs_leaf_free_space(root, l) >= data_size)
2866 return 0;
2867 }
2868
2869 if (!path->nodes[1]) {
2870 ret = insert_new_root(trans, root, path, 1);
2871 if (ret)
2872 return ret;
2873 }
2874again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002875 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002876 l = path->nodes[0];
2877 slot = path->slots[0];
2878 nritems = btrfs_header_nritems(l);
2879 mid = (nritems + 1) / 2;
2880
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002881 if (mid <= slot) {
2882 if (nritems == 1 ||
2883 leaf_space_used(l, mid, nritems - mid) + data_size >
2884 BTRFS_LEAF_DATA_SIZE(root)) {
2885 if (slot >= nritems) {
2886 split = 0;
2887 } else {
2888 mid = slot;
2889 if (mid != nritems &&
2890 leaf_space_used(l, mid, nritems - mid) +
2891 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002892 if (data_size && !tried_avoid_double)
2893 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002894 split = 2;
2895 }
2896 }
2897 }
2898 } else {
2899 if (leaf_space_used(l, 0, mid) + data_size >
2900 BTRFS_LEAF_DATA_SIZE(root)) {
2901 if (!extend && data_size && slot == 0) {
2902 split = 0;
2903 } else if ((extend || !data_size) && slot == 0) {
2904 mid = 1;
2905 } else {
2906 mid = slot;
2907 if (mid != nritems &&
2908 leaf_space_used(l, mid, nritems - mid) +
2909 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002910 if (data_size && !tried_avoid_double)
2911 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002912 split = 2 ;
2913 }
2914 }
2915 }
2916 }
2917
2918 if (split == 0)
2919 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2920 else
2921 btrfs_item_key(l, &disk_key, mid);
2922
2923 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002924 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002925 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002926 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002927 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002928
2929 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002930
2931 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2932 btrfs_set_header_bytenr(right, right->start);
2933 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002934 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002935 btrfs_set_header_owner(right, root->root_key.objectid);
2936 btrfs_set_header_level(right, 0);
2937 write_extent_buffer(right, root->fs_info->fsid,
2938 (unsigned long)btrfs_header_fsid(right),
2939 BTRFS_FSID_SIZE);
2940
2941 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2942 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2943 BTRFS_UUID_SIZE);
2944
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002945 if (split == 0) {
2946 if (mid <= slot) {
2947 btrfs_set_header_nritems(right, 0);
2948 wret = insert_ptr(trans, root, path,
2949 &disk_key, right->start,
2950 path->slots[1] + 1, 1);
2951 if (wret)
2952 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002953
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002954 btrfs_tree_unlock(path->nodes[0]);
2955 free_extent_buffer(path->nodes[0]);
2956 path->nodes[0] = right;
2957 path->slots[0] = 0;
2958 path->slots[1] += 1;
2959 } else {
2960 btrfs_set_header_nritems(right, 0);
2961 wret = insert_ptr(trans, root, path,
2962 &disk_key,
2963 right->start,
2964 path->slots[1], 1);
2965 if (wret)
2966 ret = wret;
2967 btrfs_tree_unlock(path->nodes[0]);
2968 free_extent_buffer(path->nodes[0]);
2969 path->nodes[0] = right;
2970 path->slots[0] = 0;
2971 if (path->slots[1] == 0) {
2972 wret = fixup_low_keys(trans, root,
2973 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002974 if (wret)
2975 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002976 }
2977 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002978 btrfs_mark_buffer_dirty(right);
2979 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002980 }
2981
2982 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2983 BUG_ON(ret);
2984
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002985 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002986 BUG_ON(num_doubles != 0);
2987 num_doubles++;
2988 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002989 }
Chris Mason44871b12009-03-13 10:04:31 -04002990
Chris Masonbe0e5c02007-01-26 15:51:26 -05002991 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04002992
2993push_for_double:
2994 push_for_double_split(trans, root, path, data_size);
2995 tried_avoid_double = 1;
2996 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2997 return 0;
2998 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002999}
3000
Yan, Zhengad48fd752009-11-12 09:33:58 +00003001static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3002 struct btrfs_root *root,
3003 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003004{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003005 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003006 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003007 struct btrfs_file_extent_item *fi;
3008 u64 extent_len = 0;
3009 u32 item_size;
3010 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003011
3012 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003013 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3014
3015 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3016 key.type != BTRFS_EXTENT_CSUM_KEY);
3017
3018 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3019 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003020
3021 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003022 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3023 fi = btrfs_item_ptr(leaf, path->slots[0],
3024 struct btrfs_file_extent_item);
3025 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3026 }
Chris Mason459931e2008-12-10 09:10:46 -05003027 btrfs_release_path(root, path);
3028
Chris Mason459931e2008-12-10 09:10:46 -05003029 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003030 path->search_for_split = 1;
3031 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003032 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003033 if (ret < 0)
3034 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003035
Yan, Zhengad48fd752009-11-12 09:33:58 +00003036 ret = -EAGAIN;
3037 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003038 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003039 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3040 goto err;
3041
Chris Mason109f6ae2010-04-02 09:20:18 -04003042 /* the leaf has changed, it now has room. return now */
3043 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3044 goto err;
3045
Yan, Zhengad48fd752009-11-12 09:33:58 +00003046 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3047 fi = btrfs_item_ptr(leaf, path->slots[0],
3048 struct btrfs_file_extent_item);
3049 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3050 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003051 }
3052
Chris Masonb9473432009-03-13 11:00:37 -04003053 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003054 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003055 if (ret)
3056 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003057
Yan, Zhengad48fd752009-11-12 09:33:58 +00003058 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003059 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003060 return 0;
3061err:
3062 path->keep_locks = 0;
3063 return ret;
3064}
3065
3066static noinline int split_item(struct btrfs_trans_handle *trans,
3067 struct btrfs_root *root,
3068 struct btrfs_path *path,
3069 struct btrfs_key *new_key,
3070 unsigned long split_offset)
3071{
3072 struct extent_buffer *leaf;
3073 struct btrfs_item *item;
3074 struct btrfs_item *new_item;
3075 int slot;
3076 char *buf;
3077 u32 nritems;
3078 u32 item_size;
3079 u32 orig_offset;
3080 struct btrfs_disk_key disk_key;
3081
Chris Masonb9473432009-03-13 11:00:37 -04003082 leaf = path->nodes[0];
3083 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3084
Chris Masonb4ce94d2009-02-04 09:25:08 -05003085 btrfs_set_path_blocking(path);
3086
Chris Mason459931e2008-12-10 09:10:46 -05003087 item = btrfs_item_nr(leaf, path->slots[0]);
3088 orig_offset = btrfs_item_offset(leaf, item);
3089 item_size = btrfs_item_size(leaf, item);
3090
Chris Mason459931e2008-12-10 09:10:46 -05003091 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003092 if (!buf)
3093 return -ENOMEM;
3094
Chris Mason459931e2008-12-10 09:10:46 -05003095 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3096 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003097
Chris Mason459931e2008-12-10 09:10:46 -05003098 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003099 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003100 if (slot != nritems) {
3101 /* shift the items */
3102 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003103 btrfs_item_nr_offset(slot),
3104 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003105 }
3106
3107 btrfs_cpu_key_to_disk(&disk_key, new_key);
3108 btrfs_set_item_key(leaf, &disk_key, slot);
3109
3110 new_item = btrfs_item_nr(leaf, slot);
3111
3112 btrfs_set_item_offset(leaf, new_item, orig_offset);
3113 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3114
3115 btrfs_set_item_offset(leaf, item,
3116 orig_offset + item_size - split_offset);
3117 btrfs_set_item_size(leaf, item, split_offset);
3118
3119 btrfs_set_header_nritems(leaf, nritems + 1);
3120
3121 /* write the data for the start of the original item */
3122 write_extent_buffer(leaf, buf,
3123 btrfs_item_ptr_offset(leaf, path->slots[0]),
3124 split_offset);
3125
3126 /* write the data for the new item */
3127 write_extent_buffer(leaf, buf + split_offset,
3128 btrfs_item_ptr_offset(leaf, slot),
3129 item_size - split_offset);
3130 btrfs_mark_buffer_dirty(leaf);
3131
Yan, Zhengad48fd752009-11-12 09:33:58 +00003132 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003133 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003134 return 0;
3135}
3136
3137/*
3138 * This function splits a single item into two items,
3139 * giving 'new_key' to the new item and splitting the
3140 * old one at split_offset (from the start of the item).
3141 *
3142 * The path may be released by this operation. After
3143 * the split, the path is pointing to the old item. The
3144 * new item is going to be in the same node as the old one.
3145 *
3146 * Note, the item being split must be smaller enough to live alone on
3147 * a tree block with room for one extra struct btrfs_item
3148 *
3149 * This allows us to split the item in place, keeping a lock on the
3150 * leaf the entire time.
3151 */
3152int btrfs_split_item(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *root,
3154 struct btrfs_path *path,
3155 struct btrfs_key *new_key,
3156 unsigned long split_offset)
3157{
3158 int ret;
3159 ret = setup_leaf_for_split(trans, root, path,
3160 sizeof(struct btrfs_item));
3161 if (ret)
3162 return ret;
3163
3164 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003165 return ret;
3166}
3167
3168/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003169 * This function duplicate a item, giving 'new_key' to the new item.
3170 * It guarantees both items live in the same tree leaf and the new item
3171 * is contiguous with the original item.
3172 *
3173 * This allows us to split file extent in place, keeping a lock on the
3174 * leaf the entire time.
3175 */
3176int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3177 struct btrfs_root *root,
3178 struct btrfs_path *path,
3179 struct btrfs_key *new_key)
3180{
3181 struct extent_buffer *leaf;
3182 int ret;
3183 u32 item_size;
3184
3185 leaf = path->nodes[0];
3186 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3187 ret = setup_leaf_for_split(trans, root, path,
3188 item_size + sizeof(struct btrfs_item));
3189 if (ret)
3190 return ret;
3191
3192 path->slots[0]++;
3193 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3194 item_size, item_size +
3195 sizeof(struct btrfs_item), 1);
3196 BUG_ON(ret);
3197
3198 leaf = path->nodes[0];
3199 memcpy_extent_buffer(leaf,
3200 btrfs_item_ptr_offset(leaf, path->slots[0]),
3201 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3202 item_size);
3203 return 0;
3204}
3205
3206/*
Chris Masond352ac62008-09-29 15:18:18 -04003207 * make the item pointed to by the path smaller. new_size indicates
3208 * how small to make it, and from_end tells us if we just chop bytes
3209 * off the end of the item or if we shift the item to chop bytes off
3210 * the front.
3211 */
Chris Masonb18c6682007-04-17 13:26:50 -04003212int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3213 struct btrfs_root *root,
3214 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003215 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003216{
3217 int ret = 0;
3218 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003219 struct extent_buffer *leaf;
3220 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003221 u32 nritems;
3222 unsigned int data_end;
3223 unsigned int old_data_start;
3224 unsigned int old_size;
3225 unsigned int size_diff;
3226 int i;
3227
Chris Mason5f39d392007-10-15 16:14:19 -04003228 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003229 slot = path->slots[0];
3230
3231 old_size = btrfs_item_size_nr(leaf, slot);
3232 if (old_size == new_size)
3233 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003234
Chris Mason5f39d392007-10-15 16:14:19 -04003235 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003236 data_end = leaf_data_end(root, leaf);
3237
Chris Mason5f39d392007-10-15 16:14:19 -04003238 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003239
Chris Masonb18c6682007-04-17 13:26:50 -04003240 size_diff = old_size - new_size;
3241
3242 BUG_ON(slot < 0);
3243 BUG_ON(slot >= nritems);
3244
3245 /*
3246 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3247 */
3248 /* first correct the data pointers */
3249 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003250 u32 ioff;
3251 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003252
3253 if (!leaf->map_token) {
3254 map_extent_buffer(leaf, (unsigned long)item,
3255 sizeof(struct btrfs_item),
3256 &leaf->map_token, &leaf->kaddr,
3257 &leaf->map_start, &leaf->map_len,
3258 KM_USER1);
3259 }
3260
Chris Mason5f39d392007-10-15 16:14:19 -04003261 ioff = btrfs_item_offset(leaf, item);
3262 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003263 }
Chris Masondb945352007-10-15 16:15:53 -04003264
3265 if (leaf->map_token) {
3266 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3267 leaf->map_token = NULL;
3268 }
3269
Chris Masonb18c6682007-04-17 13:26:50 -04003270 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003271 if (from_end) {
3272 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3273 data_end + size_diff, btrfs_leaf_data(leaf) +
3274 data_end, old_data_start + new_size - data_end);
3275 } else {
3276 struct btrfs_disk_key disk_key;
3277 u64 offset;
3278
3279 btrfs_item_key(leaf, &disk_key, slot);
3280
3281 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3282 unsigned long ptr;
3283 struct btrfs_file_extent_item *fi;
3284
3285 fi = btrfs_item_ptr(leaf, slot,
3286 struct btrfs_file_extent_item);
3287 fi = (struct btrfs_file_extent_item *)(
3288 (unsigned long)fi - size_diff);
3289
3290 if (btrfs_file_extent_type(leaf, fi) ==
3291 BTRFS_FILE_EXTENT_INLINE) {
3292 ptr = btrfs_item_ptr_offset(leaf, slot);
3293 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003294 (unsigned long)fi,
3295 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003296 disk_bytenr));
3297 }
3298 }
3299
3300 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3301 data_end + size_diff, btrfs_leaf_data(leaf) +
3302 data_end, old_data_start - data_end);
3303
3304 offset = btrfs_disk_key_offset(&disk_key);
3305 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3306 btrfs_set_item_key(leaf, &disk_key, slot);
3307 if (slot == 0)
3308 fixup_low_keys(trans, root, path, &disk_key, 1);
3309 }
Chris Mason5f39d392007-10-15 16:14:19 -04003310
3311 item = btrfs_item_nr(leaf, slot);
3312 btrfs_set_item_size(leaf, item, new_size);
3313 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003314
3315 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003316 if (btrfs_leaf_free_space(root, leaf) < 0) {
3317 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003318 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003319 }
Chris Masonb18c6682007-04-17 13:26:50 -04003320 return ret;
3321}
3322
Chris Masond352ac62008-09-29 15:18:18 -04003323/*
3324 * make the item pointed to by the path bigger, data_size is the new size.
3325 */
Chris Mason5f39d392007-10-15 16:14:19 -04003326int btrfs_extend_item(struct btrfs_trans_handle *trans,
3327 struct btrfs_root *root, struct btrfs_path *path,
3328 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003329{
3330 int ret = 0;
3331 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003332 struct extent_buffer *leaf;
3333 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003334 u32 nritems;
3335 unsigned int data_end;
3336 unsigned int old_data;
3337 unsigned int old_size;
3338 int i;
3339
Chris Mason5f39d392007-10-15 16:14:19 -04003340 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003341
Chris Mason5f39d392007-10-15 16:14:19 -04003342 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003343 data_end = leaf_data_end(root, leaf);
3344
Chris Mason5f39d392007-10-15 16:14:19 -04003345 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3346 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003347 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003348 }
Chris Mason6567e832007-04-16 09:22:45 -04003349 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003350 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003351
3352 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003353 if (slot >= nritems) {
3354 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003355 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3356 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003357 BUG_ON(1);
3358 }
Chris Mason6567e832007-04-16 09:22:45 -04003359
3360 /*
3361 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3362 */
3363 /* first correct the data pointers */
3364 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003365 u32 ioff;
3366 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003367
3368 if (!leaf->map_token) {
3369 map_extent_buffer(leaf, (unsigned long)item,
3370 sizeof(struct btrfs_item),
3371 &leaf->map_token, &leaf->kaddr,
3372 &leaf->map_start, &leaf->map_len,
3373 KM_USER1);
3374 }
Chris Mason5f39d392007-10-15 16:14:19 -04003375 ioff = btrfs_item_offset(leaf, item);
3376 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003377 }
Chris Mason5f39d392007-10-15 16:14:19 -04003378
Chris Masondb945352007-10-15 16:15:53 -04003379 if (leaf->map_token) {
3380 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3381 leaf->map_token = NULL;
3382 }
3383
Chris Mason6567e832007-04-16 09:22:45 -04003384 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003385 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003386 data_end - data_size, btrfs_leaf_data(leaf) +
3387 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003388
Chris Mason6567e832007-04-16 09:22:45 -04003389 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003390 old_size = btrfs_item_size_nr(leaf, slot);
3391 item = btrfs_item_nr(leaf, slot);
3392 btrfs_set_item_size(leaf, item, old_size + data_size);
3393 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003394
3395 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003396 if (btrfs_leaf_free_space(root, leaf) < 0) {
3397 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003398 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003399 }
Chris Mason6567e832007-04-16 09:22:45 -04003400 return ret;
3401}
3402
Chris Mason74123bd2007-02-02 11:05:29 -05003403/*
Chris Masond352ac62008-09-29 15:18:18 -04003404 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003405 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003406 * Returns the number of keys that were inserted.
3407 */
3408int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3409 struct btrfs_root *root,
3410 struct btrfs_path *path,
3411 struct btrfs_key *cpu_key, u32 *data_size,
3412 int nr)
3413{
3414 struct extent_buffer *leaf;
3415 struct btrfs_item *item;
3416 int ret = 0;
3417 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003418 int i;
3419 u32 nritems;
3420 u32 total_data = 0;
3421 u32 total_size = 0;
3422 unsigned int data_end;
3423 struct btrfs_disk_key disk_key;
3424 struct btrfs_key found_key;
3425
Yan Zheng87b29b22008-12-17 10:21:48 -05003426 for (i = 0; i < nr; i++) {
3427 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3428 BTRFS_LEAF_DATA_SIZE(root)) {
3429 break;
3430 nr = i;
3431 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003432 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003433 total_size += data_size[i] + sizeof(struct btrfs_item);
3434 }
3435 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003436
Josef Bacikf3465ca2008-11-12 14:19:50 -05003437 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3438 if (ret == 0)
3439 return -EEXIST;
3440 if (ret < 0)
3441 goto out;
3442
Josef Bacikf3465ca2008-11-12 14:19:50 -05003443 leaf = path->nodes[0];
3444
3445 nritems = btrfs_header_nritems(leaf);
3446 data_end = leaf_data_end(root, leaf);
3447
3448 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3449 for (i = nr; i >= 0; i--) {
3450 total_data -= data_size[i];
3451 total_size -= data_size[i] + sizeof(struct btrfs_item);
3452 if (total_size < btrfs_leaf_free_space(root, leaf))
3453 break;
3454 }
3455 nr = i;
3456 }
3457
3458 slot = path->slots[0];
3459 BUG_ON(slot < 0);
3460
3461 if (slot != nritems) {
3462 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3463
3464 item = btrfs_item_nr(leaf, slot);
3465 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3466
3467 /* figure out how many keys we can insert in here */
3468 total_data = data_size[0];
3469 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003470 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003471 break;
3472 total_data += data_size[i];
3473 }
3474 nr = i;
3475
3476 if (old_data < data_end) {
3477 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003478 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003479 slot, old_data, data_end);
3480 BUG_ON(1);
3481 }
3482 /*
3483 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3484 */
3485 /* first correct the data pointers */
3486 WARN_ON(leaf->map_token);
3487 for (i = slot; i < nritems; i++) {
3488 u32 ioff;
3489
3490 item = btrfs_item_nr(leaf, i);
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 }
3498
3499 ioff = btrfs_item_offset(leaf, item);
3500 btrfs_set_item_offset(leaf, item, ioff - total_data);
3501 }
3502 if (leaf->map_token) {
3503 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3504 leaf->map_token = NULL;
3505 }
3506
3507 /* shift the items */
3508 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3509 btrfs_item_nr_offset(slot),
3510 (nritems - slot) * sizeof(struct btrfs_item));
3511
3512 /* shift the data */
3513 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3514 data_end - total_data, btrfs_leaf_data(leaf) +
3515 data_end, old_data - data_end);
3516 data_end = old_data;
3517 } else {
3518 /*
3519 * this sucks but it has to be done, if we are inserting at
3520 * the end of the leaf only insert 1 of the items, since we
3521 * have no way of knowing whats on the next leaf and we'd have
3522 * to drop our current locks to figure it out
3523 */
3524 nr = 1;
3525 }
3526
3527 /* setup the item for the new data */
3528 for (i = 0; i < nr; i++) {
3529 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3530 btrfs_set_item_key(leaf, &disk_key, slot + i);
3531 item = btrfs_item_nr(leaf, slot + i);
3532 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3533 data_end -= data_size[i];
3534 btrfs_set_item_size(leaf, item, data_size[i]);
3535 }
3536 btrfs_set_header_nritems(leaf, nritems + nr);
3537 btrfs_mark_buffer_dirty(leaf);
3538
3539 ret = 0;
3540 if (slot == 0) {
3541 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3542 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3543 }
3544
3545 if (btrfs_leaf_free_space(root, leaf) < 0) {
3546 btrfs_print_leaf(root, leaf);
3547 BUG();
3548 }
3549out:
3550 if (!ret)
3551 ret = nr;
3552 return ret;
3553}
3554
3555/*
Chris Mason44871b12009-03-13 10:04:31 -04003556 * this is a helper for btrfs_insert_empty_items, the main goal here is
3557 * to save stack depth by doing the bulk of the work in a function
3558 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003559 */
Chris Mason44871b12009-03-13 10:04:31 -04003560static noinline_for_stack int
3561setup_items_for_insert(struct btrfs_trans_handle *trans,
3562 struct btrfs_root *root, struct btrfs_path *path,
3563 struct btrfs_key *cpu_key, u32 *data_size,
3564 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003565{
Chris Mason5f39d392007-10-15 16:14:19 -04003566 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003567 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003568 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003569 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003570 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003571 int ret;
3572 struct extent_buffer *leaf;
3573 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003574
Chris Mason5f39d392007-10-15 16:14:19 -04003575 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003576 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003577
Chris Mason5f39d392007-10-15 16:14:19 -04003578 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003579 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003580
Chris Masonf25956c2008-09-12 15:32:53 -04003581 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003582 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003583 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003584 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003585 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003586 }
Chris Mason5f39d392007-10-15 16:14:19 -04003587
Chris Masonbe0e5c02007-01-26 15:51:26 -05003588 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003589 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003590
Chris Mason5f39d392007-10-15 16:14:19 -04003591 if (old_data < data_end) {
3592 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003593 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003594 slot, old_data, data_end);
3595 BUG_ON(1);
3596 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003597 /*
3598 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3599 */
3600 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003601 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003602 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003603 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003604
Chris Mason5f39d392007-10-15 16:14:19 -04003605 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003606 if (!leaf->map_token) {
3607 map_extent_buffer(leaf, (unsigned long)item,
3608 sizeof(struct btrfs_item),
3609 &leaf->map_token, &leaf->kaddr,
3610 &leaf->map_start, &leaf->map_len,
3611 KM_USER1);
3612 }
3613
Chris Mason5f39d392007-10-15 16:14:19 -04003614 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003615 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003616 }
Chris Masondb945352007-10-15 16:15:53 -04003617 if (leaf->map_token) {
3618 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3619 leaf->map_token = NULL;
3620 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003621
3622 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003623 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003624 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003625 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003626
3627 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003628 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003629 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003630 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003631 data_end = old_data;
3632 }
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Chris Mason62e27492007-03-15 12:56:47 -04003634 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003635 for (i = 0; i < nr; i++) {
3636 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3637 btrfs_set_item_key(leaf, &disk_key, slot + i);
3638 item = btrfs_item_nr(leaf, slot + i);
3639 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3640 data_end -= data_size[i];
3641 btrfs_set_item_size(leaf, item, data_size[i]);
3642 }
Chris Mason44871b12009-03-13 10:04:31 -04003643
Chris Mason9c583092008-01-29 15:15:18 -05003644 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003645
3646 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003647 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003648 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003649 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003650 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003651 }
Chris Masonb9473432009-03-13 11:00:37 -04003652 btrfs_unlock_up_safe(path, 1);
3653 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003654
Chris Mason5f39d392007-10-15 16:14:19 -04003655 if (btrfs_leaf_free_space(root, leaf) < 0) {
3656 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003657 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003658 }
Chris Mason44871b12009-03-13 10:04:31 -04003659 return ret;
3660}
3661
3662/*
3663 * Given a key and some data, insert items into the tree.
3664 * This does all the path init required, making room in the tree if needed.
3665 */
3666int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3667 struct btrfs_root *root,
3668 struct btrfs_path *path,
3669 struct btrfs_key *cpu_key, u32 *data_size,
3670 int nr)
3671{
Chris Mason44871b12009-03-13 10:04:31 -04003672 int ret = 0;
3673 int slot;
3674 int i;
3675 u32 total_size = 0;
3676 u32 total_data = 0;
3677
3678 for (i = 0; i < nr; i++)
3679 total_data += data_size[i];
3680
3681 total_size = total_data + (nr * sizeof(struct btrfs_item));
3682 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3683 if (ret == 0)
3684 return -EEXIST;
3685 if (ret < 0)
3686 goto out;
3687
Chris Mason44871b12009-03-13 10:04:31 -04003688 slot = path->slots[0];
3689 BUG_ON(slot < 0);
3690
3691 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3692 total_data, total_size, nr);
3693
Chris Masoned2ff2c2007-03-01 18:59:40 -05003694out:
Chris Mason62e27492007-03-15 12:56:47 -04003695 return ret;
3696}
3697
3698/*
3699 * Given a key and some data, insert an item into the tree.
3700 * This does all the path init required, making room in the tree if needed.
3701 */
Chris Masone089f052007-03-16 16:20:31 -04003702int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3703 *root, struct btrfs_key *cpu_key, void *data, u32
3704 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003705{
3706 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003707 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003708 struct extent_buffer *leaf;
3709 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003710
Chris Mason2c90e5d2007-04-02 10:50:19 -04003711 path = btrfs_alloc_path();
3712 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003713 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003714 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003715 leaf = path->nodes[0];
3716 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3717 write_extent_buffer(leaf, data, ptr, data_size);
3718 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003719 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003720 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003721 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003722}
3723
Chris Mason74123bd2007-02-02 11:05:29 -05003724/*
Chris Mason5de08d72007-02-24 06:24:44 -05003725 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003726 *
Chris Masond352ac62008-09-29 15:18:18 -04003727 * the tree should have been previously balanced so the deletion does not
3728 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003729 */
Chris Masone089f052007-03-16 16:20:31 -04003730static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3731 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003732{
Chris Mason5f39d392007-10-15 16:14:19 -04003733 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003734 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003735 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003736 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003737
Chris Mason5f39d392007-10-15 16:14:19 -04003738 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003739 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003740 memmove_extent_buffer(parent,
3741 btrfs_node_key_ptr_offset(slot),
3742 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003743 sizeof(struct btrfs_key_ptr) *
3744 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003745 }
Chris Mason7518a232007-03-12 12:01:18 -04003746 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003747 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003748 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003749 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003750 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003751 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003752 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003753 struct btrfs_disk_key disk_key;
3754
3755 btrfs_node_key(parent, &disk_key, 0);
3756 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003757 if (wret)
3758 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003759 }
Chris Masond6025572007-03-30 14:27:56 -04003760 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003761 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003762}
3763
Chris Mason74123bd2007-02-02 11:05:29 -05003764/*
Chris Mason323ac952008-10-01 19:05:46 -04003765 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003766 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003767 *
3768 * This deletes the pointer in path->nodes[1] and frees the leaf
3769 * block extent. zero is returned if it all worked out, < 0 otherwise.
3770 *
3771 * The path must have already been setup for deleting the leaf, including
3772 * all the proper balancing. path->nodes[1] must be locked.
3773 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003774static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3775 struct btrfs_root *root,
3776 struct btrfs_path *path,
3777 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003778{
3779 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003780
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003781 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003782 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3783 if (ret)
3784 return ret;
3785
Chris Mason4d081c42009-02-04 09:31:28 -05003786 /*
3787 * btrfs_free_extent is expensive, we want to make sure we
3788 * aren't holding any locks when we call it
3789 */
3790 btrfs_unlock_up_safe(path, 0);
3791
Yan, Zhengf0486c62010-05-16 10:46:25 -04003792 root_sub_used(root, leaf->len);
3793
3794 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3795 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003796}
3797/*
Chris Mason74123bd2007-02-02 11:05:29 -05003798 * delete the item at the leaf level in path. If that empties
3799 * the leaf, remove it from the tree
3800 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003801int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3802 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003803{
Chris Mason5f39d392007-10-15 16:14:19 -04003804 struct extent_buffer *leaf;
3805 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003806 int last_off;
3807 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003808 int ret = 0;
3809 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003810 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003811 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003812
Chris Mason5f39d392007-10-15 16:14:19 -04003813 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003814 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3815
3816 for (i = 0; i < nr; i++)
3817 dsize += btrfs_item_size_nr(leaf, slot + i);
3818
Chris Mason5f39d392007-10-15 16:14:19 -04003819 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003820
Chris Mason85e21ba2008-01-29 15:11:36 -05003821 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003822 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003823
3824 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003825 data_end + dsize,
3826 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003827 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003828
Chris Mason85e21ba2008-01-29 15:11:36 -05003829 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003830 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003831
Chris Mason5f39d392007-10-15 16:14:19 -04003832 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003833 if (!leaf->map_token) {
3834 map_extent_buffer(leaf, (unsigned long)item,
3835 sizeof(struct btrfs_item),
3836 &leaf->map_token, &leaf->kaddr,
3837 &leaf->map_start, &leaf->map_len,
3838 KM_USER1);
3839 }
Chris Mason5f39d392007-10-15 16:14:19 -04003840 ioff = btrfs_item_offset(leaf, item);
3841 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003842 }
Chris Masondb945352007-10-15 16:15:53 -04003843
3844 if (leaf->map_token) {
3845 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3846 leaf->map_token = NULL;
3847 }
3848
Chris Mason5f39d392007-10-15 16:14:19 -04003849 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003850 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003851 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003852 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003853 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003854 btrfs_set_header_nritems(leaf, nritems - nr);
3855 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003856
Chris Mason74123bd2007-02-02 11:05:29 -05003857 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003858 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003859 if (leaf == root->node) {
3860 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003861 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003862 btrfs_set_path_blocking(path);
3863 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003864 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003865 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003866 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003867 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003868 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003869 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003870 struct btrfs_disk_key disk_key;
3871
3872 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003873 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003874 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003875 if (wret)
3876 ret = wret;
3877 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003878
Chris Mason74123bd2007-02-02 11:05:29 -05003879 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003880 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003881 /* push_leaf_left fixes the path.
3882 * make sure the path still points to our leaf
3883 * for possible call to del_ptr below
3884 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003885 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003886 extent_buffer_get(leaf);
3887
Chris Masonb9473432009-03-13 11:00:37 -04003888 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003889 wret = push_leaf_left(trans, root, path, 1, 1,
3890 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003891 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003892 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003893
3894 if (path->nodes[0] == leaf &&
3895 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003896 wret = push_leaf_right(trans, root, path, 1,
3897 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003898 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003899 ret = wret;
3900 }
Chris Mason5f39d392007-10-15 16:14:19 -04003901
3902 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003903 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003904 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003905 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003906 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003907 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003908 /* if we're still in the path, make sure
3909 * we're dirty. Otherwise, one of the
3910 * push_leaf functions must have already
3911 * dirtied this buffer
3912 */
3913 if (path->nodes[0] == leaf)
3914 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003915 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003916 }
Chris Masond5719762007-03-23 10:01:08 -04003917 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003918 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003919 }
3920 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003921 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003922}
3923
Chris Mason97571fd2007-02-24 13:39:08 -05003924/*
Chris Mason925baed2008-06-25 16:01:30 -04003925 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003926 * returns 0 if it found something or 1 if there are no lesser leaves.
3927 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003928 *
3929 * This may release the path, and so you may lose any locks held at the
3930 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003931 */
3932int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3933{
Chris Mason925baed2008-06-25 16:01:30 -04003934 struct btrfs_key key;
3935 struct btrfs_disk_key found_key;
3936 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003937
Chris Mason925baed2008-06-25 16:01:30 -04003938 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003939
Chris Mason925baed2008-06-25 16:01:30 -04003940 if (key.offset > 0)
3941 key.offset--;
3942 else if (key.type > 0)
3943 key.type--;
3944 else if (key.objectid > 0)
3945 key.objectid--;
3946 else
3947 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003948
Chris Mason925baed2008-06-25 16:01:30 -04003949 btrfs_release_path(root, path);
3950 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3951 if (ret < 0)
3952 return ret;
3953 btrfs_item_key(path->nodes[0], &found_key, 0);
3954 ret = comp_keys(&found_key, &key);
3955 if (ret < 0)
3956 return 0;
3957 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003958}
3959
Chris Mason3f157a22008-06-25 16:01:31 -04003960/*
3961 * A helper function to walk down the tree starting at min_key, and looking
3962 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003963 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003964 *
3965 * This does not cow, but it does stuff the starting key it finds back
3966 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3967 * key and get a writable path.
3968 *
3969 * This does lock as it descends, and path->keep_locks should be set
3970 * to 1 by the caller.
3971 *
3972 * This honors path->lowest_level to prevent descent past a given level
3973 * of the tree.
3974 *
Chris Masond352ac62008-09-29 15:18:18 -04003975 * min_trans indicates the oldest transaction that you are interested
3976 * in walking through. Any nodes or leaves older than min_trans are
3977 * skipped over (without reading them).
3978 *
Chris Mason3f157a22008-06-25 16:01:31 -04003979 * returns zero if something useful was found, < 0 on error and 1 if there
3980 * was nothing in the tree that matched the search criteria.
3981 */
3982int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003983 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003984 struct btrfs_path *path, int cache_only,
3985 u64 min_trans)
3986{
3987 struct extent_buffer *cur;
3988 struct btrfs_key found_key;
3989 int slot;
Yan96524802008-07-24 12:19:49 -04003990 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003991 u32 nritems;
3992 int level;
3993 int ret = 1;
3994
Chris Mason934d3752008-12-08 16:43:10 -05003995 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003996again:
3997 cur = btrfs_lock_root_node(root);
3998 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003999 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004000 path->nodes[level] = cur;
4001 path->locks[level] = 1;
4002
4003 if (btrfs_header_generation(cur) < min_trans) {
4004 ret = 1;
4005 goto out;
4006 }
Chris Masond3977122009-01-05 21:25:51 -05004007 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004008 nritems = btrfs_header_nritems(cur);
4009 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004010 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004011
Chris Mason323ac952008-10-01 19:05:46 -04004012 /* at the lowest level, we're done, setup the path and exit */
4013 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004014 if (slot >= nritems)
4015 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004016 ret = 0;
4017 path->slots[level] = slot;
4018 btrfs_item_key_to_cpu(cur, &found_key, slot);
4019 goto out;
4020 }
Yan96524802008-07-24 12:19:49 -04004021 if (sret && slot > 0)
4022 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004023 /*
4024 * check this node pointer against the cache_only and
4025 * min_trans parameters. If it isn't in cache or is too
4026 * old, skip to the next one.
4027 */
Chris Masond3977122009-01-05 21:25:51 -05004028 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004029 u64 blockptr;
4030 u64 gen;
4031 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004032 struct btrfs_disk_key disk_key;
4033
Chris Mason3f157a22008-06-25 16:01:31 -04004034 blockptr = btrfs_node_blockptr(cur, slot);
4035 gen = btrfs_node_ptr_generation(cur, slot);
4036 if (gen < min_trans) {
4037 slot++;
4038 continue;
4039 }
4040 if (!cache_only)
4041 break;
4042
Chris Masone02119d2008-09-05 16:13:11 -04004043 if (max_key) {
4044 btrfs_node_key(cur, &disk_key, slot);
4045 if (comp_keys(&disk_key, max_key) >= 0) {
4046 ret = 1;
4047 goto out;
4048 }
4049 }
4050
Chris Mason3f157a22008-06-25 16:01:31 -04004051 tmp = btrfs_find_tree_block(root, blockptr,
4052 btrfs_level_size(root, level - 1));
4053
4054 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4055 free_extent_buffer(tmp);
4056 break;
4057 }
4058 if (tmp)
4059 free_extent_buffer(tmp);
4060 slot++;
4061 }
Chris Masone02119d2008-09-05 16:13:11 -04004062find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004063 /*
4064 * we didn't find a candidate key in this node, walk forward
4065 * and find another one
4066 */
4067 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004068 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004069 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004070 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004071 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004072 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004073 btrfs_release_path(root, path);
4074 goto again;
4075 } else {
4076 goto out;
4077 }
4078 }
4079 /* save our key for returning back */
4080 btrfs_node_key_to_cpu(cur, &found_key, slot);
4081 path->slots[level] = slot;
4082 if (level == path->lowest_level) {
4083 ret = 0;
4084 unlock_up(path, level, 1);
4085 goto out;
4086 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004087 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004088 cur = read_node_slot(root, cur, slot);
4089
4090 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004091
Chris Mason3f157a22008-06-25 16:01:31 -04004092 path->locks[level - 1] = 1;
4093 path->nodes[level - 1] = cur;
4094 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004095 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004096 }
4097out:
4098 if (ret == 0)
4099 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004100 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004101 return ret;
4102}
4103
4104/*
4105 * this is similar to btrfs_next_leaf, but does not try to preserve
4106 * and fixup the path. It looks for and returns the next key in the
4107 * tree based on the current path and the cache_only and min_trans
4108 * parameters.
4109 *
4110 * 0 is returned if another key is found, < 0 if there are any errors
4111 * and 1 is returned if there are no higher keys in the tree
4112 *
4113 * path->keep_locks should be set to 1 on the search made before
4114 * calling this function.
4115 */
Chris Masone7a84562008-06-25 16:01:31 -04004116int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004117 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004118 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004119{
Chris Masone7a84562008-06-25 16:01:31 -04004120 int slot;
4121 struct extent_buffer *c;
4122
Chris Mason934d3752008-12-08 16:43:10 -05004123 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004124 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004125 if (!path->nodes[level])
4126 return 1;
4127
4128 slot = path->slots[level] + 1;
4129 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004130next:
Chris Masone7a84562008-06-25 16:01:31 -04004131 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004132 int ret;
4133 int orig_lowest;
4134 struct btrfs_key cur_key;
4135 if (level + 1 >= BTRFS_MAX_LEVEL ||
4136 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004137 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004138
4139 if (path->locks[level + 1]) {
4140 level++;
4141 continue;
4142 }
4143
4144 slot = btrfs_header_nritems(c) - 1;
4145 if (level == 0)
4146 btrfs_item_key_to_cpu(c, &cur_key, slot);
4147 else
4148 btrfs_node_key_to_cpu(c, &cur_key, slot);
4149
4150 orig_lowest = path->lowest_level;
4151 btrfs_release_path(root, path);
4152 path->lowest_level = level;
4153 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4154 0, 0);
4155 path->lowest_level = orig_lowest;
4156 if (ret < 0)
4157 return ret;
4158
4159 c = path->nodes[level];
4160 slot = path->slots[level];
4161 if (ret == 0)
4162 slot++;
4163 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004164 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004165
Chris Masone7a84562008-06-25 16:01:31 -04004166 if (level == 0)
4167 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004168 else {
4169 u64 blockptr = btrfs_node_blockptr(c, slot);
4170 u64 gen = btrfs_node_ptr_generation(c, slot);
4171
4172 if (cache_only) {
4173 struct extent_buffer *cur;
4174 cur = btrfs_find_tree_block(root, blockptr,
4175 btrfs_level_size(root, level - 1));
4176 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4177 slot++;
4178 if (cur)
4179 free_extent_buffer(cur);
4180 goto next;
4181 }
4182 free_extent_buffer(cur);
4183 }
4184 if (gen < min_trans) {
4185 slot++;
4186 goto next;
4187 }
Chris Masone7a84562008-06-25 16:01:31 -04004188 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004189 }
Chris Masone7a84562008-06-25 16:01:31 -04004190 return 0;
4191 }
4192 return 1;
4193}
4194
Chris Mason7bb86312007-12-11 09:25:06 -05004195/*
Chris Mason925baed2008-06-25 16:01:30 -04004196 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004197 * returns 0 if it found something or 1 if there are no greater leaves.
4198 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004199 */
Chris Mason234b63a2007-03-13 10:46:10 -04004200int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004201{
4202 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004203 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004204 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004205 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004206 struct btrfs_key key;
4207 u32 nritems;
4208 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004209 int old_spinning = path->leave_spinning;
4210 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004211
4212 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004213 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004214 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004215
Chris Mason8e73f272009-04-03 10:14:18 -04004216 /*
4217 * we take the blocks in an order that upsets lockdep. Using
4218 * blocking mode is the only way around it.
4219 */
4220#ifdef CONFIG_DEBUG_LOCK_ALLOC
4221 force_blocking = 1;
4222#endif
Chris Mason925baed2008-06-25 16:01:30 -04004223
Chris Mason8e73f272009-04-03 10:14:18 -04004224 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4225again:
4226 level = 1;
4227 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004228 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004229
Chris Masona2135012008-06-25 16:01:30 -04004230 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004231
4232 if (!force_blocking)
4233 path->leave_spinning = 1;
4234
Chris Mason925baed2008-06-25 16:01:30 -04004235 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4236 path->keep_locks = 0;
4237
4238 if (ret < 0)
4239 return ret;
4240
Chris Masona2135012008-06-25 16:01:30 -04004241 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004242 /*
4243 * by releasing the path above we dropped all our locks. A balance
4244 * could have added more items next to the key that used to be
4245 * at the very end of the block. So, check again here and
4246 * advance the path if there are now more items available.
4247 */
Chris Masona2135012008-06-25 16:01:30 -04004248 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004249 if (ret == 0)
4250 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004251 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004252 goto done;
4253 }
Chris Masond97e63b2007-02-20 16:40:44 -05004254
Chris Masond3977122009-01-05 21:25:51 -05004255 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004256 if (!path->nodes[level]) {
4257 ret = 1;
4258 goto done;
4259 }
Chris Mason5f39d392007-10-15 16:14:19 -04004260
Chris Masond97e63b2007-02-20 16:40:44 -05004261 slot = path->slots[level] + 1;
4262 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004263 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004264 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004265 if (level == BTRFS_MAX_LEVEL) {
4266 ret = 1;
4267 goto done;
4268 }
Chris Masond97e63b2007-02-20 16:40:44 -05004269 continue;
4270 }
Chris Mason5f39d392007-10-15 16:14:19 -04004271
Chris Mason925baed2008-06-25 16:01:30 -04004272 if (next) {
4273 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004274 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004275 }
Chris Mason5f39d392007-10-15 16:14:19 -04004276
Chris Mason8e73f272009-04-03 10:14:18 -04004277 next = c;
4278 ret = read_block_for_search(NULL, root, path, &next, level,
4279 slot, &key);
4280 if (ret == -EAGAIN)
4281 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004282
Chris Mason76a05b32009-05-14 13:24:30 -04004283 if (ret < 0) {
4284 btrfs_release_path(root, path);
4285 goto done;
4286 }
4287
Chris Mason5cd57b22008-06-25 16:01:30 -04004288 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004289 ret = btrfs_try_spin_lock(next);
4290 if (!ret) {
4291 btrfs_set_path_blocking(path);
4292 btrfs_tree_lock(next);
4293 if (!force_blocking)
4294 btrfs_clear_path_blocking(path, next);
4295 }
4296 if (force_blocking)
4297 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004298 }
Chris Masond97e63b2007-02-20 16:40:44 -05004299 break;
4300 }
4301 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004302 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004303 level--;
4304 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004305 if (path->locks[level])
4306 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004307
Chris Mason5f39d392007-10-15 16:14:19 -04004308 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004309 path->nodes[level] = next;
4310 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004311 if (!path->skip_locking)
4312 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004313
Chris Masond97e63b2007-02-20 16:40:44 -05004314 if (!level)
4315 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004316
Chris Mason8e73f272009-04-03 10:14:18 -04004317 ret = read_block_for_search(NULL, root, path, &next, level,
4318 0, &key);
4319 if (ret == -EAGAIN)
4320 goto again;
4321
Chris Mason76a05b32009-05-14 13:24:30 -04004322 if (ret < 0) {
4323 btrfs_release_path(root, path);
4324 goto done;
4325 }
4326
Chris Mason5cd57b22008-06-25 16:01:30 -04004327 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004328 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004329 ret = btrfs_try_spin_lock(next);
4330 if (!ret) {
4331 btrfs_set_path_blocking(path);
4332 btrfs_tree_lock(next);
4333 if (!force_blocking)
4334 btrfs_clear_path_blocking(path, next);
4335 }
4336 if (force_blocking)
4337 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004338 }
Chris Masond97e63b2007-02-20 16:40:44 -05004339 }
Chris Mason8e73f272009-04-03 10:14:18 -04004340 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004341done:
4342 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004343 path->leave_spinning = old_spinning;
4344 if (!old_spinning)
4345 btrfs_set_path_blocking(path);
4346
4347 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004348}
Chris Mason0b86a832008-03-24 15:01:56 -04004349
Chris Mason3f157a22008-06-25 16:01:31 -04004350/*
4351 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4352 * searching until it gets past min_objectid or finds an item of 'type'
4353 *
4354 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4355 */
Chris Mason0b86a832008-03-24 15:01:56 -04004356int btrfs_previous_item(struct btrfs_root *root,
4357 struct btrfs_path *path, u64 min_objectid,
4358 int type)
4359{
4360 struct btrfs_key found_key;
4361 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004362 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004363 int ret;
4364
Chris Masond3977122009-01-05 21:25:51 -05004365 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004366 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004367 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004368 ret = btrfs_prev_leaf(root, path);
4369 if (ret != 0)
4370 return ret;
4371 } else {
4372 path->slots[0]--;
4373 }
4374 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004375 nritems = btrfs_header_nritems(leaf);
4376 if (nritems == 0)
4377 return 1;
4378 if (path->slots[0] == nritems)
4379 path->slots[0]--;
4380
Chris Mason0b86a832008-03-24 15:01:56 -04004381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004382 if (found_key.objectid < min_objectid)
4383 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004384 if (found_key.type == type)
4385 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004386 if (found_key.objectid == min_objectid &&
4387 found_key.type < type)
4388 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004389 }
4390 return 1;
4391}