blob: 009bcf7f1e4b9226b8e150177613295e5e5d6aa5 [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 Masonb9447ef2009-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);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000685 if (!cur)
686 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400687 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400688 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400689 }
Chris Mason6702ed42007-08-07 16:15:09 -0400690 }
Chris Masone9d0b132007-08-10 14:06:19 -0400691 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400692 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400693
Chris Masone7a84562008-06-25 16:01:31 -0400694 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500695 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400696 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400697 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400698 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400699 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400700 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400701 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400702 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400703 break;
Yan252c38f2007-08-29 09:11:44 -0400704 }
Chris Masone7a84562008-06-25 16:01:31 -0400705 search_start = cur->start;
706 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400707 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400708 btrfs_tree_unlock(cur);
709 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400710 }
Chris Mason5708b952007-10-25 15:43:18 -0400711 if (parent->map_token) {
712 unmap_extent_buffer(parent, parent->map_token,
713 KM_USER1);
714 parent->map_token = NULL;
715 }
Chris Mason6702ed42007-08-07 16:15:09 -0400716 return err;
717}
718
Chris Mason74123bd2007-02-02 11:05:29 -0500719/*
720 * The leaf data grows from end-to-front in the node.
721 * this returns the address of the start of the last item,
722 * which is the stop of the leaf data stack
723 */
Chris Mason123abc82007-03-14 14:14:43 -0400724static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400725 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726{
Chris Mason5f39d392007-10-15 16:14:19 -0400727 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400729 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400730 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500731}
732
Chris Masonaa5d6be2007-02-28 16:35:06 -0500733
Chris Mason74123bd2007-02-02 11:05:29 -0500734/*
Chris Mason5f39d392007-10-15 16:14:19 -0400735 * search for key in the extent_buffer. The items start at offset p,
736 * and they are item_size apart. There are 'max' items in p.
737 *
Chris Mason74123bd2007-02-02 11:05:29 -0500738 * the slot in the array is returned via slot, and it points to
739 * the place where you would insert key if it is not found in
740 * the array.
741 *
742 * slot may point to max if the key is bigger than all of the keys
743 */
Chris Masone02119d2008-09-05 16:13:11 -0400744static noinline int generic_bin_search(struct extent_buffer *eb,
745 unsigned long p,
746 int item_size, struct btrfs_key *key,
747 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500748{
749 int low = 0;
750 int high = max;
751 int mid;
752 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400753 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400754 struct btrfs_disk_key unaligned;
755 unsigned long offset;
756 char *map_token = NULL;
757 char *kaddr = NULL;
758 unsigned long map_start = 0;
759 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400760 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500761
Chris Masond3977122009-01-05 21:25:51 -0500762 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500763 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400764 offset = p + mid * item_size;
765
766 if (!map_token || offset < map_start ||
767 (offset + sizeof(struct btrfs_disk_key)) >
768 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400769 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400770 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400771 map_token = NULL;
772 }
Chris Mason934d3752008-12-08 16:43:10 -0500773
774 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400775 sizeof(struct btrfs_disk_key),
776 &map_token, &kaddr,
777 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400778
Chris Mason479965d2007-10-15 16:14:27 -0400779 if (!err) {
780 tmp = (struct btrfs_disk_key *)(kaddr + offset -
781 map_start);
782 } else {
783 read_extent_buffer(eb, &unaligned,
784 offset, sizeof(unaligned));
785 tmp = &unaligned;
786 }
787
Chris Mason5f39d392007-10-15 16:14:19 -0400788 } else {
789 tmp = (struct btrfs_disk_key *)(kaddr + offset -
790 map_start);
791 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500792 ret = comp_keys(tmp, key);
793
794 if (ret < 0)
795 low = mid + 1;
796 else if (ret > 0)
797 high = mid;
798 else {
799 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400800 if (map_token)
801 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500802 return 0;
803 }
804 }
805 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400806 if (map_token)
807 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500808 return 1;
809}
810
Chris Mason97571fd2007-02-24 13:39:08 -0500811/*
812 * simple bin_search frontend that does the right thing for
813 * leaves vs nodes
814 */
Chris Mason5f39d392007-10-15 16:14:19 -0400815static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
816 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500817{
Chris Mason5f39d392007-10-15 16:14:19 -0400818 if (level == 0) {
819 return generic_bin_search(eb,
820 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400821 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400822 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400823 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500824 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400825 return generic_bin_search(eb,
826 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400827 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400828 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400829 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500830 }
831 return -1;
832}
833
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400834int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
835 int level, int *slot)
836{
837 return bin_search(eb, key, level, slot);
838}
839
Yan, Zhengf0486c62010-05-16 10:46:25 -0400840static void root_add_used(struct btrfs_root *root, u32 size)
841{
842 spin_lock(&root->accounting_lock);
843 btrfs_set_root_used(&root->root_item,
844 btrfs_root_used(&root->root_item) + size);
845 spin_unlock(&root->accounting_lock);
846}
847
848static void root_sub_used(struct btrfs_root *root, u32 size)
849{
850 spin_lock(&root->accounting_lock);
851 btrfs_set_root_used(&root->root_item,
852 btrfs_root_used(&root->root_item) - size);
853 spin_unlock(&root->accounting_lock);
854}
855
Chris Masond352ac62008-09-29 15:18:18 -0400856/* given a node and slot number, this reads the blocks it points to. The
857 * extent buffer is returned with a reference taken (but unlocked).
858 * NULL is returned on error.
859 */
Chris Masone02119d2008-09-05 16:13:11 -0400860static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400861 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500862{
Chris Masonca7a79a2008-05-12 12:59:19 -0400863 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500864 if (slot < 0)
865 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400866 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500867 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400868
869 BUG_ON(level == 0);
870
Chris Masondb945352007-10-15 16:15:53 -0400871 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400872 btrfs_level_size(root, level - 1),
873 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500874}
875
Chris Masond352ac62008-09-29 15:18:18 -0400876/*
877 * node level balancing, used to make sure nodes are in proper order for
878 * item deletion. We balance from the top down, so we have to make sure
879 * that a deletion won't leave an node completely empty later on.
880 */
Chris Masone02119d2008-09-05 16:13:11 -0400881static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500882 struct btrfs_root *root,
883 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500884{
Chris Mason5f39d392007-10-15 16:14:19 -0400885 struct extent_buffer *right = NULL;
886 struct extent_buffer *mid;
887 struct extent_buffer *left = NULL;
888 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500889 int ret = 0;
890 int wret;
891 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500892 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500893 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500894
895 if (level == 0)
896 return 0;
897
Chris Mason5f39d392007-10-15 16:14:19 -0400898 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500899
Chris Mason925baed2008-06-25 16:01:30 -0400900 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500901 WARN_ON(btrfs_header_generation(mid) != trans->transid);
902
Chris Mason1d4f8a02007-03-13 09:28:32 -0400903 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500904
Chris Mason234b63a2007-03-13 10:46:10 -0400905 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400906 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500907 pslot = path->slots[level + 1];
908
Chris Mason40689472007-03-17 14:29:23 -0400909 /*
910 * deal with the case where there is only one pointer in the root
911 * by promoting the node below to a root
912 */
Chris Mason5f39d392007-10-15 16:14:19 -0400913 if (!parent) {
914 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500915
Chris Mason5f39d392007-10-15 16:14:19 -0400916 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500917 return 0;
918
919 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400920 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500921 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400922 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500923 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400924 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400925 if (ret) {
926 btrfs_tree_unlock(child);
927 free_extent_buffer(child);
928 goto enospc;
929 }
Yan2f375ab2008-02-01 14:58:07 -0500930
Chris Mason240f62c2011-03-23 14:54:42 -0400931 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400932
Chris Mason0b86a832008-03-24 15:01:56 -0400933 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400934 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500935
Chris Mason925baed2008-06-25 16:01:30 -0400936 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500937 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400938 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400939 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500940 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400941 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400942
943 root_sub_used(root, mid->len);
944 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500945 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400946 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400947 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500948 }
Chris Mason5f39d392007-10-15 16:14:19 -0400949 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400950 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500951 return 0;
952
Andi Kleen559af822010-10-29 15:14:37 -0400953 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400954
Chris Mason5f39d392007-10-15 16:14:19 -0400955 left = read_node_slot(root, parent, pslot - 1);
956 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400957 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500958 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400959 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400960 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400961 if (wret) {
962 ret = wret;
963 goto enospc;
964 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400965 }
Chris Mason5f39d392007-10-15 16:14:19 -0400966 right = read_node_slot(root, parent, pslot + 1);
967 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400968 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500969 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400970 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400971 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400972 if (wret) {
973 ret = wret;
974 goto enospc;
975 }
976 }
977
978 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400979 if (left) {
980 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400981 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500982 if (wret < 0)
983 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -0400984 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500985 }
Chris Mason79f95c82007-03-01 15:16:26 -0500986
987 /*
988 * then try to empty the right most buffer into the middle
989 */
Chris Mason5f39d392007-10-15 16:14:19 -0400990 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400991 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400992 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500993 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400994 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400995 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400996 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -0400997 wret = del_ptr(trans, root, path, level + 1, pslot +
998 1);
Chris Masonbb803952007-03-01 12:04:21 -0500999 if (wret)
1000 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001001 root_sub_used(root, right->len);
1002 btrfs_free_tree_block(trans, root, right, 0, 1);
1003 free_extent_buffer(right);
1004 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001005 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001006 struct btrfs_disk_key right_key;
1007 btrfs_node_key(right, &right_key, 0);
1008 btrfs_set_node_key(parent, &right_key, pslot + 1);
1009 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001010 }
1011 }
Chris Mason5f39d392007-10-15 16:14:19 -04001012 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001013 /*
1014 * we're not allowed to leave a node with one item in the
1015 * tree during a delete. A deletion from lower in the tree
1016 * could try to delete the only pointer in this node.
1017 * So, pull some keys from the left.
1018 * There has to be a left pointer at this point because
1019 * otherwise we would have pulled some pointers from the
1020 * right
1021 */
Chris Mason5f39d392007-10-15 16:14:19 -04001022 BUG_ON(!left);
1023 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001024 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001025 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001026 goto enospc;
1027 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001028 if (wret == 1) {
1029 wret = push_node_left(trans, root, left, mid, 1);
1030 if (wret < 0)
1031 ret = wret;
1032 }
Chris Mason79f95c82007-03-01 15:16:26 -05001033 BUG_ON(wret == 1);
1034 }
Chris Mason5f39d392007-10-15 16:14:19 -04001035 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001036 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001037 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001038 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001039 if (wret)
1040 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001041 root_sub_used(root, mid->len);
1042 btrfs_free_tree_block(trans, root, mid, 0, 1);
1043 free_extent_buffer(mid);
1044 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001045 } else {
1046 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001047 struct btrfs_disk_key mid_key;
1048 btrfs_node_key(mid, &mid_key, 0);
1049 btrfs_set_node_key(parent, &mid_key, pslot);
1050 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001051 }
Chris Masonbb803952007-03-01 12:04:21 -05001052
Chris Mason79f95c82007-03-01 15:16:26 -05001053 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001054 if (left) {
1055 if (btrfs_header_nritems(left) > orig_slot) {
1056 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001057 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001058 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001059 path->slots[level + 1] -= 1;
1060 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001061 if (mid) {
1062 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001063 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001064 }
Chris Masonbb803952007-03-01 12:04:21 -05001065 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001066 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001067 path->slots[level] = orig_slot;
1068 }
1069 }
Chris Mason79f95c82007-03-01 15:16:26 -05001070 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001071 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001072 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001073 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001074enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001075 if (right) {
1076 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001077 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001078 }
1079 if (left) {
1080 if (path->nodes[level] != left)
1081 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001082 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001083 }
Chris Masonbb803952007-03-01 12:04:21 -05001084 return ret;
1085}
1086
Chris Masond352ac62008-09-29 15:18:18 -04001087/* Node balancing for insertion. Here we only split or push nodes around
1088 * when they are completely full. This is also done top down, so we
1089 * have to be pessimistic.
1090 */
Chris Masond3977122009-01-05 21:25:51 -05001091static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001092 struct btrfs_root *root,
1093 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001094{
Chris Mason5f39d392007-10-15 16:14:19 -04001095 struct extent_buffer *right = NULL;
1096 struct extent_buffer *mid;
1097 struct extent_buffer *left = NULL;
1098 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001099 int ret = 0;
1100 int wret;
1101 int pslot;
1102 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001103
1104 if (level == 0)
1105 return 1;
1106
Chris Mason5f39d392007-10-15 16:14:19 -04001107 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001108 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001109
1110 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001111 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001112 pslot = path->slots[level + 1];
1113
Chris Mason5f39d392007-10-15 16:14:19 -04001114 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001115 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001116
Chris Mason5f39d392007-10-15 16:14:19 -04001117 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001118
1119 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001120 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001121 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001122
1123 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001124 btrfs_set_lock_blocking(left);
1125
Chris Mason5f39d392007-10-15 16:14:19 -04001126 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001127 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1128 wret = 1;
1129 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001130 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001131 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001132 if (ret)
1133 wret = 1;
1134 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001135 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001136 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001137 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001138 }
Chris Masone66f7092007-04-20 13:16:02 -04001139 if (wret < 0)
1140 ret = wret;
1141 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001142 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001143 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001144 btrfs_node_key(mid, &disk_key, 0);
1145 btrfs_set_node_key(parent, &disk_key, pslot);
1146 btrfs_mark_buffer_dirty(parent);
1147 if (btrfs_header_nritems(left) > orig_slot) {
1148 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001149 path->slots[level + 1] -= 1;
1150 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001151 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001152 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001153 } else {
1154 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001155 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001156 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001157 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001158 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001159 }
Chris Masone66f7092007-04-20 13:16:02 -04001160 return 0;
1161 }
Chris Mason925baed2008-06-25 16:01:30 -04001162 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001163 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001164 }
Chris Mason925baed2008-06-25 16:01:30 -04001165 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001166
1167 /*
1168 * then try to empty the right most buffer into the middle
1169 */
Chris Mason5f39d392007-10-15 16:14:19 -04001170 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001171 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001172
Chris Mason925baed2008-06-25 16:01:30 -04001173 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001174 btrfs_set_lock_blocking(right);
1175
Chris Mason5f39d392007-10-15 16:14:19 -04001176 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001177 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1178 wret = 1;
1179 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001180 ret = btrfs_cow_block(trans, root, right,
1181 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001182 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001183 if (ret)
1184 wret = 1;
1185 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001186 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001187 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001188 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001189 }
Chris Masone66f7092007-04-20 13:16:02 -04001190 if (wret < 0)
1191 ret = wret;
1192 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001193 struct btrfs_disk_key disk_key;
1194
1195 btrfs_node_key(right, &disk_key, 0);
1196 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1197 btrfs_mark_buffer_dirty(parent);
1198
1199 if (btrfs_header_nritems(mid) <= orig_slot) {
1200 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001201 path->slots[level + 1] += 1;
1202 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001203 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001204 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001205 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001206 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001207 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001208 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001209 }
Chris Masone66f7092007-04-20 13:16:02 -04001210 return 0;
1211 }
Chris Mason925baed2008-06-25 16:01:30 -04001212 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001213 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001214 }
Chris Masone66f7092007-04-20 13:16:02 -04001215 return 1;
1216}
1217
Chris Mason74123bd2007-02-02 11:05:29 -05001218/*
Chris Masond352ac62008-09-29 15:18:18 -04001219 * readahead one full node of leaves, finding things that are close
1220 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001221 */
Chris Masonc8c42862009-04-03 10:14:18 -04001222static void reada_for_search(struct btrfs_root *root,
1223 struct btrfs_path *path,
1224 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001225{
Chris Mason5f39d392007-10-15 16:14:19 -04001226 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001227 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001228 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001229 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001230 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001231 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001232 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001233 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001234 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001235 u32 nr;
1236 u32 blocksize;
1237 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001238
Chris Masona6b6e752007-10-15 16:22:39 -04001239 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001240 return;
1241
Chris Mason6702ed42007-08-07 16:15:09 -04001242 if (!path->nodes[level])
1243 return;
1244
Chris Mason5f39d392007-10-15 16:14:19 -04001245 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001246
Chris Mason3c69fae2007-08-07 15:52:22 -04001247 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001248 blocksize = btrfs_level_size(root, level - 1);
1249 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001250 if (eb) {
1251 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001252 return;
1253 }
1254
Chris Masona7175312009-01-22 09:23:10 -05001255 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001256
Chris Mason5f39d392007-10-15 16:14:19 -04001257 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001258 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001259 while (1) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001260 if (!node->map_token) {
1261 unsigned long offset = btrfs_node_key_ptr_offset(nr);
1262 map_private_extent_buffer(node, offset,
1263 sizeof(struct btrfs_key_ptr),
1264 &node->map_token,
1265 &node->kaddr,
1266 &node->map_start,
1267 &node->map_len, KM_USER1);
1268 }
Chris Mason6b800532007-10-15 16:17:34 -04001269 if (direction < 0) {
1270 if (nr == 0)
1271 break;
1272 nr--;
1273 } else if (direction > 0) {
1274 nr++;
1275 if (nr >= nritems)
1276 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001277 }
Chris Mason01f46652007-12-21 16:24:26 -05001278 if (path->reada < 0 && objectid) {
1279 btrfs_node_key(node, &disk_key, nr);
1280 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1281 break;
1282 }
Chris Mason6b800532007-10-15 16:17:34 -04001283 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001284 if ((search <= target && target - search <= 65536) ||
1285 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001286 gen = btrfs_node_ptr_generation(node, nr);
1287 if (node->map_token) {
1288 unmap_extent_buffer(node, node->map_token,
1289 KM_USER1);
1290 node->map_token = NULL;
1291 }
1292 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001293 nread += blocksize;
1294 }
1295 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001296 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001297 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001298 }
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001299 if (node->map_token) {
1300 unmap_extent_buffer(node, node->map_token, KM_USER1);
1301 node->map_token = NULL;
1302 }
Chris Mason3c69fae2007-08-07 15:52:22 -04001303}
Chris Mason925baed2008-06-25 16:01:30 -04001304
Chris Masond352ac62008-09-29 15:18:18 -04001305/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001306 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1307 * cache
1308 */
1309static noinline int reada_for_balance(struct btrfs_root *root,
1310 struct btrfs_path *path, int level)
1311{
1312 int slot;
1313 int nritems;
1314 struct extent_buffer *parent;
1315 struct extent_buffer *eb;
1316 u64 gen;
1317 u64 block1 = 0;
1318 u64 block2 = 0;
1319 int ret = 0;
1320 int blocksize;
1321
Chris Mason8c594ea2009-04-20 15:50:10 -04001322 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001323 if (!parent)
1324 return 0;
1325
1326 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001327 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001328 blocksize = btrfs_level_size(root, level);
1329
1330 if (slot > 0) {
1331 block1 = btrfs_node_blockptr(parent, slot - 1);
1332 gen = btrfs_node_ptr_generation(parent, slot - 1);
1333 eb = btrfs_find_tree_block(root, block1, blocksize);
1334 if (eb && btrfs_buffer_uptodate(eb, gen))
1335 block1 = 0;
1336 free_extent_buffer(eb);
1337 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001338 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001339 block2 = btrfs_node_blockptr(parent, slot + 1);
1340 gen = btrfs_node_ptr_generation(parent, slot + 1);
1341 eb = btrfs_find_tree_block(root, block2, blocksize);
1342 if (eb && btrfs_buffer_uptodate(eb, gen))
1343 block2 = 0;
1344 free_extent_buffer(eb);
1345 }
1346 if (block1 || block2) {
1347 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001348
1349 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001350 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001351
1352 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001353 if (block1)
1354 readahead_tree_block(root, block1, blocksize, 0);
1355 if (block2)
1356 readahead_tree_block(root, block2, blocksize, 0);
1357
1358 if (block1) {
1359 eb = read_tree_block(root, block1, blocksize, 0);
1360 free_extent_buffer(eb);
1361 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001362 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001363 eb = read_tree_block(root, block2, blocksize, 0);
1364 free_extent_buffer(eb);
1365 }
1366 }
1367 return ret;
1368}
1369
1370
1371/*
Chris Masond3977122009-01-05 21:25:51 -05001372 * when we walk down the tree, it is usually safe to unlock the higher layers
1373 * in the tree. The exceptions are when our path goes through slot 0, because
1374 * operations on the tree might require changing key pointers higher up in the
1375 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001376 *
Chris Masond3977122009-01-05 21:25:51 -05001377 * callers might also have set path->keep_locks, which tells this code to keep
1378 * the lock if the path points to the last slot in the block. This is part of
1379 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001380 *
Chris Masond3977122009-01-05 21:25:51 -05001381 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1382 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001383 */
Chris Masone02119d2008-09-05 16:13:11 -04001384static noinline void unlock_up(struct btrfs_path *path, int level,
1385 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001386{
1387 int i;
1388 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001389 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001390 struct extent_buffer *t;
1391
1392 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1393 if (!path->nodes[i])
1394 break;
1395 if (!path->locks[i])
1396 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001397 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001398 skip_level = i + 1;
1399 continue;
1400 }
Chris Mason051e1b92008-06-25 16:01:30 -04001401 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001402 u32 nritems;
1403 t = path->nodes[i];
1404 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001405 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001406 skip_level = i + 1;
1407 continue;
1408 }
1409 }
Chris Mason051e1b92008-06-25 16:01:30 -04001410 if (skip_level < i && i >= lowest_unlock)
1411 no_skips = 1;
1412
Chris Mason925baed2008-06-25 16:01:30 -04001413 t = path->nodes[i];
1414 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1415 btrfs_tree_unlock(t);
1416 path->locks[i] = 0;
1417 }
1418 }
1419}
1420
Chris Mason3c69fae2007-08-07 15:52:22 -04001421/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001422 * This releases any locks held in the path starting at level and
1423 * going all the way up to the root.
1424 *
1425 * btrfs_search_slot will keep the lock held on higher nodes in a few
1426 * corner cases, such as COW of the block at slot zero in the node. This
1427 * ignores those rules, and it should only be called when there are no
1428 * more updates to be done higher up in the tree.
1429 */
1430noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1431{
1432 int i;
1433
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001434 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001435 return;
1436
1437 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1438 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001439 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001440 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001441 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001442 btrfs_tree_unlock(path->nodes[i]);
1443 path->locks[i] = 0;
1444 }
1445}
1446
1447/*
Chris Masonc8c42862009-04-03 10:14:18 -04001448 * helper function for btrfs_search_slot. The goal is to find a block
1449 * in cache without setting the path to blocking. If we find the block
1450 * we return zero and the path is unchanged.
1451 *
1452 * If we can't find the block, we set the path blocking and do some
1453 * reada. -EAGAIN is returned and the search must be repeated.
1454 */
1455static int
1456read_block_for_search(struct btrfs_trans_handle *trans,
1457 struct btrfs_root *root, struct btrfs_path *p,
1458 struct extent_buffer **eb_ret, int level, int slot,
1459 struct btrfs_key *key)
1460{
1461 u64 blocknr;
1462 u64 gen;
1463 u32 blocksize;
1464 struct extent_buffer *b = *eb_ret;
1465 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001466 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001467
1468 blocknr = btrfs_node_blockptr(b, slot);
1469 gen = btrfs_node_ptr_generation(b, slot);
1470 blocksize = btrfs_level_size(root, level - 1);
1471
1472 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001473 if (tmp) {
1474 if (btrfs_buffer_uptodate(tmp, 0)) {
1475 if (btrfs_buffer_uptodate(tmp, gen)) {
1476 /*
1477 * we found an up to date block without
1478 * sleeping, return
1479 * right away
1480 */
1481 *eb_ret = tmp;
1482 return 0;
1483 }
1484 /* the pages were up to date, but we failed
1485 * the generation number check. Do a full
1486 * read for the generation number that is correct.
1487 * We must do this without dropping locks so
1488 * we can trust our generation number
1489 */
1490 free_extent_buffer(tmp);
1491 tmp = read_tree_block(root, blocknr, blocksize, gen);
1492 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1493 *eb_ret = tmp;
1494 return 0;
1495 }
1496 free_extent_buffer(tmp);
1497 btrfs_release_path(NULL, p);
1498 return -EIO;
1499 }
Chris Masonc8c42862009-04-03 10:14:18 -04001500 }
1501
1502 /*
1503 * reduce lock contention at high levels
1504 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001505 * we read. Don't release the lock on the current
1506 * level because we need to walk this node to figure
1507 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001508 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001509 btrfs_unlock_up_safe(p, level + 1);
1510 btrfs_set_path_blocking(p);
1511
Chris Masoncb449212010-10-24 11:01:27 -04001512 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001513 if (p->reada)
1514 reada_for_search(root, p, level, slot, key->objectid);
1515
Chris Mason8c594ea2009-04-20 15:50:10 -04001516 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001517
1518 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001519 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001520 if (tmp) {
1521 /*
1522 * If the read above didn't mark this buffer up to date,
1523 * it will never end up being up to date. Set ret to EIO now
1524 * and give up so that our caller doesn't loop forever
1525 * on our EAGAINs.
1526 */
1527 if (!btrfs_buffer_uptodate(tmp, 0))
1528 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001529 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001530 }
1531 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001532}
1533
1534/*
1535 * helper function for btrfs_search_slot. This does all of the checks
1536 * for node-level blocks and does any balancing required based on
1537 * the ins_len.
1538 *
1539 * If no extra work was required, zero is returned. If we had to
1540 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1541 * start over
1542 */
1543static int
1544setup_nodes_for_search(struct btrfs_trans_handle *trans,
1545 struct btrfs_root *root, struct btrfs_path *p,
1546 struct extent_buffer *b, int level, int ins_len)
1547{
1548 int ret;
1549 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1550 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1551 int sret;
1552
1553 sret = reada_for_balance(root, p, level);
1554 if (sret)
1555 goto again;
1556
1557 btrfs_set_path_blocking(p);
1558 sret = split_node(trans, root, p, level);
1559 btrfs_clear_path_blocking(p, NULL);
1560
1561 BUG_ON(sret > 0);
1562 if (sret) {
1563 ret = sret;
1564 goto done;
1565 }
1566 b = p->nodes[level];
1567 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001568 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001569 int sret;
1570
1571 sret = reada_for_balance(root, p, level);
1572 if (sret)
1573 goto again;
1574
1575 btrfs_set_path_blocking(p);
1576 sret = balance_level(trans, root, p, level);
1577 btrfs_clear_path_blocking(p, NULL);
1578
1579 if (sret) {
1580 ret = sret;
1581 goto done;
1582 }
1583 b = p->nodes[level];
1584 if (!b) {
1585 btrfs_release_path(NULL, p);
1586 goto again;
1587 }
1588 BUG_ON(btrfs_header_nritems(b) == 1);
1589 }
1590 return 0;
1591
1592again:
1593 ret = -EAGAIN;
1594done:
1595 return ret;
1596}
1597
1598/*
Chris Mason74123bd2007-02-02 11:05:29 -05001599 * look for key in the tree. path is filled in with nodes along the way
1600 * if key is found, we return zero and you can find the item in the leaf
1601 * level of the path (level 0)
1602 *
1603 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001604 * be inserted, and 1 is returned. If there are other errors during the
1605 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001606 *
1607 * if ins_len > 0, nodes and leaves will be split as we walk down the
1608 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1609 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001610 */
Chris Masone089f052007-03-16 16:20:31 -04001611int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1612 *root, struct btrfs_key *key, struct btrfs_path *p, int
1613 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001614{
Chris Mason5f39d392007-10-15 16:14:19 -04001615 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001616 int slot;
1617 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001618 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001619 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001620 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001621 u8 lowest_level = 0;
1622
Chris Mason6702ed42007-08-07 16:15:09 -04001623 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001624 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001625 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001626
Chris Mason925baed2008-06-25 16:01:30 -04001627 if (ins_len < 0)
1628 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001629
Chris Masonbb803952007-03-01 12:04:21 -05001630again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001631 if (p->search_commit_root) {
1632 b = root->commit_root;
1633 extent_buffer_get(b);
1634 if (!p->skip_locking)
1635 btrfs_tree_lock(b);
1636 } else {
1637 if (p->skip_locking)
1638 b = btrfs_root_node(root);
1639 else
1640 b = btrfs_lock_root_node(root);
1641 }
Chris Mason925baed2008-06-25 16:01:30 -04001642
Chris Masoneb60cea2007-02-02 09:18:22 -05001643 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001644 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001645
1646 /*
1647 * setup the path here so we can release it under lock
1648 * contention with the cow code
1649 */
1650 p->nodes[level] = b;
1651 if (!p->skip_locking)
1652 p->locks[level] = 1;
1653
Chris Mason02217ed2007-03-02 16:08:05 -05001654 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001655 /*
1656 * if we don't really need to cow this block
1657 * then we don't want to set the path blocking,
1658 * so we test it here
1659 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001660 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001661 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662
Chris Masonb4ce94d2009-02-04 09:25:08 -05001663 btrfs_set_path_blocking(p);
1664
Yan Zheng33c66f42009-07-22 09:59:00 -04001665 err = btrfs_cow_block(trans, root, b,
1666 p->nodes[level + 1],
1667 p->slots[level + 1], &b);
1668 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001669 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001670 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001671 }
Chris Mason02217ed2007-03-02 16:08:05 -05001672 }
Chris Mason65b51a02008-08-01 15:11:20 -04001673cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001674 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001675 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001676 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001677 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001678
Chris Masoneb60cea2007-02-02 09:18:22 -05001679 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001680 if (!p->skip_locking)
1681 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001682
Chris Mason4008c042009-02-12 14:09:45 -05001683 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001684
1685 /*
1686 * we have a lock on b and as long as we aren't changing
1687 * the tree, there is no way to for the items in b to change.
1688 * It is safe to drop the lock on our parent before we
1689 * go through the expensive btree search on b.
1690 *
1691 * If cow is true, then we might be changing slot zero,
1692 * which may require changing the parent. So, we can't
1693 * drop the lock until after we know which slot we're
1694 * operating on.
1695 */
1696 if (!cow)
1697 btrfs_unlock_up_safe(p, level + 1);
1698
Chris Mason5f39d392007-10-15 16:14:19 -04001699 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001700
Chris Mason5f39d392007-10-15 16:14:19 -04001701 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001702 int dec = 0;
1703 if (ret && slot > 0) {
1704 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001705 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001706 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001707 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001708 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001709 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001710 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001711 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001712 if (err) {
1713 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001714 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001715 }
Chris Masonc8c42862009-04-03 10:14:18 -04001716 b = p->nodes[level];
1717 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001718
Chris Masonf9efa9c2008-06-25 16:14:04 -04001719 unlock_up(p, level, lowest_unlock);
1720
Chris Mason925baed2008-06-25 16:01:30 -04001721 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001722 if (dec)
1723 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001724 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001725 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001726
Yan Zheng33c66f42009-07-22 09:59:00 -04001727 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001728 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001729 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001730 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001731 if (err) {
1732 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001733 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001734 }
Chris Mason76a05b32009-05-14 13:24:30 -04001735
Chris Masonb4ce94d2009-02-04 09:25:08 -05001736 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001737 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001738 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001739
Yan Zheng33c66f42009-07-22 09:59:00 -04001740 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001741 btrfs_set_path_blocking(p);
1742 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001743 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001744 }
1745 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001746 } else {
1747 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001748 if (ins_len > 0 &&
1749 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001750 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001751 err = split_leaf(trans, root, key,
1752 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001753 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001754
Yan Zheng33c66f42009-07-22 09:59:00 -04001755 BUG_ON(err > 0);
1756 if (err) {
1757 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001758 goto done;
1759 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001760 }
Chris Mason459931e2008-12-10 09:10:46 -05001761 if (!p->search_for_split)
1762 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001763 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001764 }
1765 }
Chris Mason65b51a02008-08-01 15:11:20 -04001766 ret = 1;
1767done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001768 /*
1769 * we don't really know what they plan on doing with the path
1770 * from here on, so for now just mark it as blocking
1771 */
Chris Masonb9473432009-03-13 11:00:37 -04001772 if (!p->leave_spinning)
1773 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001774 if (ret < 0)
1775 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001776 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001777}
1778
Chris Mason74123bd2007-02-02 11:05:29 -05001779/*
1780 * adjust the pointers going up the tree, starting at level
1781 * making sure the right key of each node is points to 'key'.
1782 * This is used after shifting pointers to the left, so it stops
1783 * fixing up pointers when a given leaf/node is not in slot 0 of the
1784 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001785 *
1786 * If this fails to write a tree block, it returns -1, but continues
1787 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001788 */
Chris Mason5f39d392007-10-15 16:14:19 -04001789static int fixup_low_keys(struct btrfs_trans_handle *trans,
1790 struct btrfs_root *root, struct btrfs_path *path,
1791 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001792{
1793 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001794 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001795 struct extent_buffer *t;
1796
Chris Mason234b63a2007-03-13 10:46:10 -04001797 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001798 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001799 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001800 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001801 t = path->nodes[i];
1802 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001803 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001804 if (tslot != 0)
1805 break;
1806 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001807 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001808}
1809
Chris Mason74123bd2007-02-02 11:05:29 -05001810/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001811 * update item key.
1812 *
1813 * This function isn't completely safe. It's the caller's responsibility
1814 * that the new key won't break the order
1815 */
1816int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1817 struct btrfs_root *root, struct btrfs_path *path,
1818 struct btrfs_key *new_key)
1819{
1820 struct btrfs_disk_key disk_key;
1821 struct extent_buffer *eb;
1822 int slot;
1823
1824 eb = path->nodes[0];
1825 slot = path->slots[0];
1826 if (slot > 0) {
1827 btrfs_item_key(eb, &disk_key, slot - 1);
1828 if (comp_keys(&disk_key, new_key) >= 0)
1829 return -1;
1830 }
1831 if (slot < btrfs_header_nritems(eb) - 1) {
1832 btrfs_item_key(eb, &disk_key, slot + 1);
1833 if (comp_keys(&disk_key, new_key) <= 0)
1834 return -1;
1835 }
1836
1837 btrfs_cpu_key_to_disk(&disk_key, new_key);
1838 btrfs_set_item_key(eb, &disk_key, slot);
1839 btrfs_mark_buffer_dirty(eb);
1840 if (slot == 0)
1841 fixup_low_keys(trans, root, path, &disk_key, 1);
1842 return 0;
1843}
1844
1845/*
Chris Mason74123bd2007-02-02 11:05:29 -05001846 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001847 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001848 *
1849 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1850 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001851 */
Chris Mason98ed5172008-01-03 10:01:48 -05001852static int push_node_left(struct btrfs_trans_handle *trans,
1853 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001854 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001855{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001856 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001857 int src_nritems;
1858 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001859 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001860
Chris Mason5f39d392007-10-15 16:14:19 -04001861 src_nritems = btrfs_header_nritems(src);
1862 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001863 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001864 WARN_ON(btrfs_header_generation(src) != trans->transid);
1865 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001866
Chris Masonbce4eae2008-04-24 14:42:46 -04001867 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001868 return 1;
1869
Chris Masond3977122009-01-05 21:25:51 -05001870 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001871 return 1;
1872
Chris Masonbce4eae2008-04-24 14:42:46 -04001873 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001874 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001875 if (push_items < src_nritems) {
1876 /* leave at least 8 pointers in the node if
1877 * we aren't going to empty it
1878 */
1879 if (src_nritems - push_items < 8) {
1880 if (push_items <= 8)
1881 return 1;
1882 push_items -= 8;
1883 }
1884 }
1885 } else
1886 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001887
Chris Mason5f39d392007-10-15 16:14:19 -04001888 copy_extent_buffer(dst, src,
1889 btrfs_node_key_ptr_offset(dst_nritems),
1890 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001891 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001892
Chris Masonbb803952007-03-01 12:04:21 -05001893 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001894 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1895 btrfs_node_key_ptr_offset(push_items),
1896 (src_nritems - push_items) *
1897 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001898 }
Chris Mason5f39d392007-10-15 16:14:19 -04001899 btrfs_set_header_nritems(src, src_nritems - push_items);
1900 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1901 btrfs_mark_buffer_dirty(src);
1902 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001903
Chris Masonbb803952007-03-01 12:04:21 -05001904 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001905}
1906
Chris Mason97571fd2007-02-24 13:39:08 -05001907/*
Chris Mason79f95c82007-03-01 15:16:26 -05001908 * try to push data from one node into the next node right in the
1909 * tree.
1910 *
1911 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1912 * error, and > 0 if there was no room in the right hand block.
1913 *
1914 * this will only push up to 1/2 the contents of the left node over
1915 */
Chris Mason5f39d392007-10-15 16:14:19 -04001916static int balance_node_right(struct btrfs_trans_handle *trans,
1917 struct btrfs_root *root,
1918 struct extent_buffer *dst,
1919 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001920{
Chris Mason79f95c82007-03-01 15:16:26 -05001921 int push_items = 0;
1922 int max_push;
1923 int src_nritems;
1924 int dst_nritems;
1925 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001926
Chris Mason7bb86312007-12-11 09:25:06 -05001927 WARN_ON(btrfs_header_generation(src) != trans->transid);
1928 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1929
Chris Mason5f39d392007-10-15 16:14:19 -04001930 src_nritems = btrfs_header_nritems(src);
1931 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001932 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001933 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001934 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001935
Chris Masond3977122009-01-05 21:25:51 -05001936 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001937 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001938
1939 max_push = src_nritems / 2 + 1;
1940 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001941 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001942 return 1;
Yan252c38f2007-08-29 09:11:44 -04001943
Chris Mason79f95c82007-03-01 15:16:26 -05001944 if (max_push < push_items)
1945 push_items = max_push;
1946
Chris Mason5f39d392007-10-15 16:14:19 -04001947 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1948 btrfs_node_key_ptr_offset(0),
1949 (dst_nritems) *
1950 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001951
Chris Mason5f39d392007-10-15 16:14:19 -04001952 copy_extent_buffer(dst, src,
1953 btrfs_node_key_ptr_offset(0),
1954 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001955 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001956
Chris Mason5f39d392007-10-15 16:14:19 -04001957 btrfs_set_header_nritems(src, src_nritems - push_items);
1958 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001959
Chris Mason5f39d392007-10-15 16:14:19 -04001960 btrfs_mark_buffer_dirty(src);
1961 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001962
Chris Mason79f95c82007-03-01 15:16:26 -05001963 return ret;
1964}
1965
1966/*
Chris Mason97571fd2007-02-24 13:39:08 -05001967 * helper function to insert a new root level in the tree.
1968 * A new node is allocated, and a single item is inserted to
1969 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001970 *
1971 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001972 */
Chris Masond3977122009-01-05 21:25:51 -05001973static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001974 struct btrfs_root *root,
1975 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001976{
Chris Mason7bb86312007-12-11 09:25:06 -05001977 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001978 struct extent_buffer *lower;
1979 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001980 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001981 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001982
1983 BUG_ON(path->nodes[level]);
1984 BUG_ON(path->nodes[level-1] != root->node);
1985
Chris Mason7bb86312007-12-11 09:25:06 -05001986 lower = path->nodes[level-1];
1987 if (level == 1)
1988 btrfs_item_key(lower, &lower_key, 0);
1989 else
1990 btrfs_node_key(lower, &lower_key, 0);
1991
Zheng Yan31840ae2008-09-23 13:14:14 -04001992 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001993 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001994 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001995 if (IS_ERR(c))
1996 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001997
Yan, Zhengf0486c62010-05-16 10:46:25 -04001998 root_add_used(root, root->nodesize);
1999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002000 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002001 btrfs_set_header_nritems(c, 1);
2002 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002003 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002004 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002005 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002006 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002007
Chris Mason5f39d392007-10-15 16:14:19 -04002008 write_extent_buffer(c, root->fs_info->fsid,
2009 (unsigned long)btrfs_header_fsid(c),
2010 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002011
2012 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2013 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2014 BTRFS_UUID_SIZE);
2015
Chris Mason5f39d392007-10-15 16:14:19 -04002016 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002017 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002018 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002019 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002020
2021 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002022
2023 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002024
Chris Mason925baed2008-06-25 16:01:30 -04002025 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002026 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002027
2028 /* the super has an extra ref to root->node */
2029 free_extent_buffer(old);
2030
Chris Mason0b86a832008-03-24 15:01:56 -04002031 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002032 extent_buffer_get(c);
2033 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002034 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002035 path->slots[level] = 0;
2036 return 0;
2037}
2038
Chris Mason74123bd2007-02-02 11:05:29 -05002039/*
2040 * worker function to insert a single pointer in a node.
2041 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002042 *
Chris Mason74123bd2007-02-02 11:05:29 -05002043 * slot and level indicate where you want the key to go, and
2044 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002045 *
2046 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002047 */
Chris Masone089f052007-03-16 16:20:31 -04002048static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2049 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002050 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002051{
Chris Mason5f39d392007-10-15 16:14:19 -04002052 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002053 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002054
2055 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002056 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002057 lower = path->nodes[level];
2058 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002059 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002060 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002061 BUG();
2062 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002063 memmove_extent_buffer(lower,
2064 btrfs_node_key_ptr_offset(slot + 1),
2065 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002066 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002067 }
Chris Mason5f39d392007-10-15 16:14:19 -04002068 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002069 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002070 WARN_ON(trans->transid == 0);
2071 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002072 btrfs_set_header_nritems(lower, nritems + 1);
2073 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002074 return 0;
2075}
2076
Chris Mason97571fd2007-02-24 13:39:08 -05002077/*
2078 * split the node at the specified level in path in two.
2079 * The path is corrected to point to the appropriate node after the split
2080 *
2081 * Before splitting this tries to make some room in the node by pushing
2082 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002083 *
2084 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002085 */
Chris Masone02119d2008-09-05 16:13:11 -04002086static noinline int split_node(struct btrfs_trans_handle *trans,
2087 struct btrfs_root *root,
2088 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002089{
Chris Mason5f39d392007-10-15 16:14:19 -04002090 struct extent_buffer *c;
2091 struct extent_buffer *split;
2092 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002093 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002094 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002095 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002096 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002097
Chris Mason5f39d392007-10-15 16:14:19 -04002098 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002099 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002101 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002102 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002103 if (ret)
2104 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002105 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002106 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002107 c = path->nodes[level];
2108 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002109 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002110 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002111 if (ret < 0)
2112 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002113 }
Chris Masone66f7092007-04-20 13:16:02 -04002114
Chris Mason5f39d392007-10-15 16:14:19 -04002115 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002116 mid = (c_nritems + 1) / 2;
2117 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002118
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002119 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002120 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002121 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002122 if (IS_ERR(split))
2123 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002124
Yan, Zhengf0486c62010-05-16 10:46:25 -04002125 root_add_used(root, root->nodesize);
2126
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002127 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002128 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002129 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002131 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002132 btrfs_set_header_owner(split, root->root_key.objectid);
2133 write_extent_buffer(split, root->fs_info->fsid,
2134 (unsigned long)btrfs_header_fsid(split),
2135 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002136 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2137 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2138 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002139
Chris Mason5f39d392007-10-15 16:14:19 -04002140
2141 copy_extent_buffer(split, c,
2142 btrfs_node_key_ptr_offset(0),
2143 btrfs_node_key_ptr_offset(mid),
2144 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2145 btrfs_set_header_nritems(split, c_nritems - mid);
2146 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002147 ret = 0;
2148
Chris Mason5f39d392007-10-15 16:14:19 -04002149 btrfs_mark_buffer_dirty(c);
2150 btrfs_mark_buffer_dirty(split);
2151
Chris Masondb945352007-10-15 16:15:53 -04002152 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002153 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002154 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002155 if (wret)
2156 ret = wret;
2157
Chris Mason5de08d72007-02-24 06:24:44 -05002158 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002159 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002160 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002161 free_extent_buffer(c);
2162 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002163 path->slots[level + 1] += 1;
2164 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002165 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002166 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002167 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002168 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169}
2170
Chris Mason74123bd2007-02-02 11:05:29 -05002171/*
2172 * how many bytes are required to store the items in a leaf. start
2173 * and nr indicate which items in the leaf to check. This totals up the
2174 * space used both by the item structs and the item data
2175 */
Chris Mason5f39d392007-10-15 16:14:19 -04002176static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002177{
2178 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002179 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002180 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002181
2182 if (!nr)
2183 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002184 data_len = btrfs_item_end_nr(l, start);
2185 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002186 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002187 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002188 return data_len;
2189}
2190
Chris Mason74123bd2007-02-02 11:05:29 -05002191/*
Chris Masond4dbff92007-04-04 14:08:15 -04002192 * The space between the end of the leaf items and
2193 * the start of the leaf data. IOW, how much room
2194 * the leaf has left for both items and data
2195 */
Chris Masond3977122009-01-05 21:25:51 -05002196noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002197 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002198{
Chris Mason5f39d392007-10-15 16:14:19 -04002199 int nritems = btrfs_header_nritems(leaf);
2200 int ret;
2201 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2202 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002203 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2204 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002205 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002206 leaf_space_used(leaf, 0, nritems), nritems);
2207 }
2208 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002209}
2210
Chris Mason99d8f832010-07-07 10:51:48 -04002211/*
2212 * min slot controls the lowest index we're willing to push to the
2213 * right. We'll push up to and including min_slot, but no lower
2214 */
Chris Mason44871b12009-03-13 10:04:31 -04002215static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2216 struct btrfs_root *root,
2217 struct btrfs_path *path,
2218 int data_size, int empty,
2219 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002220 int free_space, u32 left_nritems,
2221 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002222{
Chris Mason5f39d392007-10-15 16:14:19 -04002223 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002224 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002225 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002226 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002227 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002228 int push_space = 0;
2229 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002230 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002231 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002232 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002233 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002234 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002235
Chris Mason34a38212007-11-07 13:31:03 -05002236 if (empty)
2237 nr = 0;
2238 else
Chris Mason99d8f832010-07-07 10:51:48 -04002239 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002240
Zheng Yan31840ae2008-09-23 13:14:14 -04002241 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002242 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002243
Chris Mason44871b12009-03-13 10:04:31 -04002244 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002245 i = left_nritems - 1;
2246 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002247 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002248
Zheng Yan31840ae2008-09-23 13:14:14 -04002249 if (!empty && push_items > 0) {
2250 if (path->slots[0] > i)
2251 break;
2252 if (path->slots[0] == i) {
2253 int space = btrfs_leaf_free_space(root, left);
2254 if (space + push_space * 2 > free_space)
2255 break;
2256 }
2257 }
2258
Chris Mason00ec4c52007-02-24 12:47:20 -05002259 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002260 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002261
2262 if (!left->map_token) {
2263 map_extent_buffer(left, (unsigned long)item,
2264 sizeof(struct btrfs_item),
2265 &left->map_token, &left->kaddr,
2266 &left->map_start, &left->map_len,
2267 KM_USER1);
2268 }
2269
2270 this_item_size = btrfs_item_size(left, item);
2271 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002272 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002273
Chris Mason00ec4c52007-02-24 12:47:20 -05002274 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002275 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002276 if (i == 0)
2277 break;
2278 i--;
Chris Masondb945352007-10-15 16:15:53 -04002279 }
2280 if (left->map_token) {
2281 unmap_extent_buffer(left, left->map_token, KM_USER1);
2282 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002283 }
Chris Mason5f39d392007-10-15 16:14:19 -04002284
Chris Mason925baed2008-06-25 16:01:30 -04002285 if (push_items == 0)
2286 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002287
Chris Mason34a38212007-11-07 13:31:03 -05002288 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002289 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002290
Chris Mason00ec4c52007-02-24 12:47:20 -05002291 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002292 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002293
Chris Mason5f39d392007-10-15 16:14:19 -04002294 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002295 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002296
Chris Mason00ec4c52007-02-24 12:47:20 -05002297 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002298 data_end = leaf_data_end(root, right);
2299 memmove_extent_buffer(right,
2300 btrfs_leaf_data(right) + data_end - push_space,
2301 btrfs_leaf_data(right) + data_end,
2302 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2303
Chris Mason00ec4c52007-02-24 12:47:20 -05002304 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002305 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002306 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2307 btrfs_leaf_data(left) + leaf_data_end(root, left),
2308 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002309
2310 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2311 btrfs_item_nr_offset(0),
2312 right_nritems * sizeof(struct btrfs_item));
2313
Chris Mason00ec4c52007-02-24 12:47:20 -05002314 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002315 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2316 btrfs_item_nr_offset(left_nritems - push_items),
2317 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002318
2319 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002320 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002321 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002322 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002323 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002324 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002325 if (!right->map_token) {
2326 map_extent_buffer(right, (unsigned long)item,
2327 sizeof(struct btrfs_item),
2328 &right->map_token, &right->kaddr,
2329 &right->map_start, &right->map_len,
2330 KM_USER1);
2331 }
2332 push_space -= btrfs_item_size(right, item);
2333 btrfs_set_item_offset(right, item, push_space);
2334 }
2335
2336 if (right->map_token) {
2337 unmap_extent_buffer(right, right->map_token, KM_USER1);
2338 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002339 }
Chris Mason7518a232007-03-12 12:01:18 -04002340 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002341 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002342
Chris Mason34a38212007-11-07 13:31:03 -05002343 if (left_nritems)
2344 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002345 else
2346 clean_tree_block(trans, root, left);
2347
Chris Mason5f39d392007-10-15 16:14:19 -04002348 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002349
Chris Mason5f39d392007-10-15 16:14:19 -04002350 btrfs_item_key(right, &disk_key, 0);
2351 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002352 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002353
Chris Mason00ec4c52007-02-24 12:47:20 -05002354 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002355 if (path->slots[0] >= left_nritems) {
2356 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002357 if (btrfs_header_nritems(path->nodes[0]) == 0)
2358 clean_tree_block(trans, root, path->nodes[0]);
2359 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002360 free_extent_buffer(path->nodes[0]);
2361 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002362 path->slots[1] += 1;
2363 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002364 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002365 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002366 }
2367 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002368
2369out_unlock:
2370 btrfs_tree_unlock(right);
2371 free_extent_buffer(right);
2372 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002373}
Chris Mason925baed2008-06-25 16:01:30 -04002374
Chris Mason00ec4c52007-02-24 12:47:20 -05002375/*
Chris Mason44871b12009-03-13 10:04:31 -04002376 * push some data in the path leaf to the right, trying to free up at
2377 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2378 *
2379 * returns 1 if the push failed because the other node didn't have enough
2380 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002381 *
2382 * this will push starting from min_slot to the end of the leaf. It won't
2383 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002384 */
2385static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002386 *root, struct btrfs_path *path,
2387 int min_data_size, int data_size,
2388 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002389{
2390 struct extent_buffer *left = path->nodes[0];
2391 struct extent_buffer *right;
2392 struct extent_buffer *upper;
2393 int slot;
2394 int free_space;
2395 u32 left_nritems;
2396 int ret;
2397
2398 if (!path->nodes[1])
2399 return 1;
2400
2401 slot = path->slots[1];
2402 upper = path->nodes[1];
2403 if (slot >= btrfs_header_nritems(upper) - 1)
2404 return 1;
2405
2406 btrfs_assert_tree_locked(path->nodes[1]);
2407
2408 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002409 if (right == NULL)
2410 return 1;
2411
Chris Mason44871b12009-03-13 10:04:31 -04002412 btrfs_tree_lock(right);
2413 btrfs_set_lock_blocking(right);
2414
2415 free_space = btrfs_leaf_free_space(root, right);
2416 if (free_space < data_size)
2417 goto out_unlock;
2418
2419 /* cow and double check */
2420 ret = btrfs_cow_block(trans, root, right, upper,
2421 slot + 1, &right);
2422 if (ret)
2423 goto out_unlock;
2424
2425 free_space = btrfs_leaf_free_space(root, right);
2426 if (free_space < data_size)
2427 goto out_unlock;
2428
2429 left_nritems = btrfs_header_nritems(left);
2430 if (left_nritems == 0)
2431 goto out_unlock;
2432
Chris Mason99d8f832010-07-07 10:51:48 -04002433 return __push_leaf_right(trans, root, path, min_data_size, empty,
2434 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002435out_unlock:
2436 btrfs_tree_unlock(right);
2437 free_extent_buffer(right);
2438 return 1;
2439}
2440
2441/*
Chris Mason74123bd2007-02-02 11:05:29 -05002442 * push some data in the path leaf to the left, trying to free up at
2443 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002444 *
2445 * max_slot can put a limit on how far into the leaf we'll push items. The
2446 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2447 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002448 */
Chris Mason44871b12009-03-13 10:04:31 -04002449static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2450 struct btrfs_root *root,
2451 struct btrfs_path *path, int data_size,
2452 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002453 int free_space, u32 right_nritems,
2454 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002455{
Chris Mason5f39d392007-10-15 16:14:19 -04002456 struct btrfs_disk_key disk_key;
2457 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002458 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002459 int push_space = 0;
2460 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002461 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002462 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002463 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002464 int ret = 0;
2465 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002466 u32 this_item_size;
2467 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002468
Chris Mason34a38212007-11-07 13:31:03 -05002469 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002470 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002471 else
Chris Mason99d8f832010-07-07 10:51:48 -04002472 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002473
2474 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002475 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002476 if (!right->map_token) {
2477 map_extent_buffer(right, (unsigned long)item,
2478 sizeof(struct btrfs_item),
2479 &right->map_token, &right->kaddr,
2480 &right->map_start, &right->map_len,
2481 KM_USER1);
2482 }
2483
Zheng Yan31840ae2008-09-23 13:14:14 -04002484 if (!empty && push_items > 0) {
2485 if (path->slots[0] < i)
2486 break;
2487 if (path->slots[0] == i) {
2488 int space = btrfs_leaf_free_space(root, right);
2489 if (space + push_space * 2 > free_space)
2490 break;
2491 }
2492 }
2493
Chris Masonbe0e5c02007-01-26 15:51:26 -05002494 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002495 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002496
2497 this_item_size = btrfs_item_size(right, item);
2498 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002499 break;
Chris Masondb945352007-10-15 16:15:53 -04002500
Chris Masonbe0e5c02007-01-26 15:51:26 -05002501 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002502 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002503 }
Chris Masondb945352007-10-15 16:15:53 -04002504
2505 if (right->map_token) {
2506 unmap_extent_buffer(right, right->map_token, KM_USER1);
2507 right->map_token = NULL;
2508 }
2509
Chris Masonbe0e5c02007-01-26 15:51:26 -05002510 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002511 ret = 1;
2512 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002513 }
Chris Mason34a38212007-11-07 13:31:03 -05002514 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002515 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002516
Chris Masonbe0e5c02007-01-26 15:51:26 -05002517 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002518 copy_extent_buffer(left, right,
2519 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2520 btrfs_item_nr_offset(0),
2521 push_items * sizeof(struct btrfs_item));
2522
Chris Mason123abc82007-03-14 14:14:43 -04002523 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002524 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002525
2526 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002527 leaf_data_end(root, left) - push_space,
2528 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002529 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002530 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002531 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002532 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002533
Chris Masondb945352007-10-15 16:15:53 -04002534 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002535 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002536 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002537
Chris Mason5f39d392007-10-15 16:14:19 -04002538 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002539 if (!left->map_token) {
2540 map_extent_buffer(left, (unsigned long)item,
2541 sizeof(struct btrfs_item),
2542 &left->map_token, &left->kaddr,
2543 &left->map_start, &left->map_len,
2544 KM_USER1);
2545 }
2546
Chris Mason5f39d392007-10-15 16:14:19 -04002547 ioff = btrfs_item_offset(left, item);
2548 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002549 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002550 }
Chris Mason5f39d392007-10-15 16:14:19 -04002551 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002552 if (left->map_token) {
2553 unmap_extent_buffer(left, left->map_token, KM_USER1);
2554 left->map_token = NULL;
2555 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002556
2557 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002558 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002559 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2560 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002561 WARN_ON(1);
2562 }
Chris Mason5f39d392007-10-15 16:14:19 -04002563
Chris Mason34a38212007-11-07 13:31:03 -05002564 if (push_items < right_nritems) {
2565 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2566 leaf_data_end(root, right);
2567 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2568 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2569 btrfs_leaf_data(right) +
2570 leaf_data_end(root, right), push_space);
2571
2572 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002573 btrfs_item_nr_offset(push_items),
2574 (btrfs_header_nritems(right) - push_items) *
2575 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002576 }
Yaneef1c492007-11-26 10:58:13 -05002577 right_nritems -= push_items;
2578 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002579 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002580 for (i = 0; i < right_nritems; i++) {
2581 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002582
2583 if (!right->map_token) {
2584 map_extent_buffer(right, (unsigned long)item,
2585 sizeof(struct btrfs_item),
2586 &right->map_token, &right->kaddr,
2587 &right->map_start, &right->map_len,
2588 KM_USER1);
2589 }
2590
2591 push_space = push_space - btrfs_item_size(right, item);
2592 btrfs_set_item_offset(right, item, push_space);
2593 }
2594 if (right->map_token) {
2595 unmap_extent_buffer(right, right->map_token, KM_USER1);
2596 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002597 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002598
Chris Mason5f39d392007-10-15 16:14:19 -04002599 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002600 if (right_nritems)
2601 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002602 else
2603 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002604
Chris Mason5f39d392007-10-15 16:14:19 -04002605 btrfs_item_key(right, &disk_key, 0);
2606 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002607 if (wret)
2608 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609
2610 /* then fixup the leaf pointer in the path */
2611 if (path->slots[0] < push_items) {
2612 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002613 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002614 free_extent_buffer(path->nodes[0]);
2615 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616 path->slots[1] -= 1;
2617 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002618 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002619 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002620 path->slots[0] -= push_items;
2621 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002622 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002623 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002624out:
2625 btrfs_tree_unlock(left);
2626 free_extent_buffer(left);
2627 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002628}
2629
Chris Mason74123bd2007-02-02 11:05:29 -05002630/*
Chris Mason44871b12009-03-13 10:04:31 -04002631 * push some data in the path leaf to the left, trying to free up at
2632 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002633 *
2634 * max_slot can put a limit on how far into the leaf we'll push items. The
2635 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2636 * items
Chris Mason44871b12009-03-13 10:04:31 -04002637 */
2638static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002639 *root, struct btrfs_path *path, int min_data_size,
2640 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002641{
2642 struct extent_buffer *right = path->nodes[0];
2643 struct extent_buffer *left;
2644 int slot;
2645 int free_space;
2646 u32 right_nritems;
2647 int ret = 0;
2648
2649 slot = path->slots[1];
2650 if (slot == 0)
2651 return 1;
2652 if (!path->nodes[1])
2653 return 1;
2654
2655 right_nritems = btrfs_header_nritems(right);
2656 if (right_nritems == 0)
2657 return 1;
2658
2659 btrfs_assert_tree_locked(path->nodes[1]);
2660
2661 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002662 if (left == NULL)
2663 return 1;
2664
Chris Mason44871b12009-03-13 10:04:31 -04002665 btrfs_tree_lock(left);
2666 btrfs_set_lock_blocking(left);
2667
2668 free_space = btrfs_leaf_free_space(root, left);
2669 if (free_space < data_size) {
2670 ret = 1;
2671 goto out;
2672 }
2673
2674 /* cow and double check */
2675 ret = btrfs_cow_block(trans, root, left,
2676 path->nodes[1], slot - 1, &left);
2677 if (ret) {
2678 /* we hit -ENOSPC, but it isn't fatal here */
2679 ret = 1;
2680 goto out;
2681 }
2682
2683 free_space = btrfs_leaf_free_space(root, left);
2684 if (free_space < data_size) {
2685 ret = 1;
2686 goto out;
2687 }
2688
Chris Mason99d8f832010-07-07 10:51:48 -04002689 return __push_leaf_left(trans, root, path, min_data_size,
2690 empty, left, free_space, right_nritems,
2691 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002692out:
2693 btrfs_tree_unlock(left);
2694 free_extent_buffer(left);
2695 return ret;
2696}
2697
2698/*
Chris Mason74123bd2007-02-02 11:05:29 -05002699 * split the path's leaf in two, making sure there is at least data_size
2700 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002701 *
2702 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002703 */
Chris Mason44871b12009-03-13 10:04:31 -04002704static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002705 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002706 struct btrfs_path *path,
2707 struct extent_buffer *l,
2708 struct extent_buffer *right,
2709 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002710{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002711 int data_copy_size;
2712 int rt_data_off;
2713 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002714 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002715 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002716 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002717
Chris Mason5f39d392007-10-15 16:14:19 -04002718 nritems = nritems - mid;
2719 btrfs_set_header_nritems(right, nritems);
2720 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2721
2722 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2723 btrfs_item_nr_offset(mid),
2724 nritems * sizeof(struct btrfs_item));
2725
2726 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002727 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2728 data_copy_size, btrfs_leaf_data(l) +
2729 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002730
Chris Mason5f39d392007-10-15 16:14:19 -04002731 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2732 btrfs_item_end_nr(l, mid);
2733
2734 for (i = 0; i < nritems; i++) {
2735 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002736 u32 ioff;
2737
2738 if (!right->map_token) {
2739 map_extent_buffer(right, (unsigned long)item,
2740 sizeof(struct btrfs_item),
2741 &right->map_token, &right->kaddr,
2742 &right->map_start, &right->map_len,
2743 KM_USER1);
2744 }
2745
2746 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002747 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002748 }
Chris Mason74123bd2007-02-02 11:05:29 -05002749
Chris Masondb945352007-10-15 16:15:53 -04002750 if (right->map_token) {
2751 unmap_extent_buffer(right, right->map_token, KM_USER1);
2752 right->map_token = NULL;
2753 }
2754
Chris Mason5f39d392007-10-15 16:14:19 -04002755 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002756 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002757 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002758 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2759 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002760 if (wret)
2761 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002762
2763 btrfs_mark_buffer_dirty(right);
2764 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002765 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002766
Chris Masonbe0e5c02007-01-26 15:51:26 -05002767 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002768 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002769 free_extent_buffer(path->nodes[0]);
2770 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002771 path->slots[0] -= mid;
2772 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002773 } else {
2774 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002775 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002776 }
Chris Mason5f39d392007-10-15 16:14:19 -04002777
Chris Masoneb60cea2007-02-02 09:18:22 -05002778 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002779
Chris Mason44871b12009-03-13 10:04:31 -04002780 return ret;
2781}
2782
2783/*
Chris Mason99d8f832010-07-07 10:51:48 -04002784 * double splits happen when we need to insert a big item in the middle
2785 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2786 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2787 * A B C
2788 *
2789 * We avoid this by trying to push the items on either side of our target
2790 * into the adjacent leaves. If all goes well we can avoid the double split
2791 * completely.
2792 */
2793static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2794 struct btrfs_root *root,
2795 struct btrfs_path *path,
2796 int data_size)
2797{
2798 int ret;
2799 int progress = 0;
2800 int slot;
2801 u32 nritems;
2802
2803 slot = path->slots[0];
2804
2805 /*
2806 * try to push all the items after our slot into the
2807 * right leaf
2808 */
2809 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2810 if (ret < 0)
2811 return ret;
2812
2813 if (ret == 0)
2814 progress++;
2815
2816 nritems = btrfs_header_nritems(path->nodes[0]);
2817 /*
2818 * our goal is to get our slot at the start or end of a leaf. If
2819 * we've done so we're done
2820 */
2821 if (path->slots[0] == 0 || path->slots[0] == nritems)
2822 return 0;
2823
2824 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2825 return 0;
2826
2827 /* try to push all the items before our slot into the next leaf */
2828 slot = path->slots[0];
2829 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2830 if (ret < 0)
2831 return ret;
2832
2833 if (ret == 0)
2834 progress++;
2835
2836 if (progress)
2837 return 0;
2838 return 1;
2839}
2840
2841/*
Chris Mason44871b12009-03-13 10:04:31 -04002842 * split the path's leaf in two, making sure there is at least data_size
2843 * available for the resulting leaf level of the path.
2844 *
2845 * returns 0 if all went well and < 0 on failure.
2846 */
2847static noinline int split_leaf(struct btrfs_trans_handle *trans,
2848 struct btrfs_root *root,
2849 struct btrfs_key *ins_key,
2850 struct btrfs_path *path, int data_size,
2851 int extend)
2852{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002853 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002854 struct extent_buffer *l;
2855 u32 nritems;
2856 int mid;
2857 int slot;
2858 struct extent_buffer *right;
2859 int ret = 0;
2860 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002861 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002862 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002863 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002864
Yan, Zhenga5719522009-09-24 09:17:31 -04002865 l = path->nodes[0];
2866 slot = path->slots[0];
2867 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2868 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2869 return -EOVERFLOW;
2870
Chris Mason44871b12009-03-13 10:04:31 -04002871 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002872 if (data_size) {
2873 wret = push_leaf_right(trans, root, path, data_size,
2874 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002875 if (wret < 0)
2876 return wret;
2877 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002878 wret = push_leaf_left(trans, root, path, data_size,
2879 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002880 if (wret < 0)
2881 return wret;
2882 }
2883 l = path->nodes[0];
2884
2885 /* did the pushes work? */
2886 if (btrfs_leaf_free_space(root, l) >= data_size)
2887 return 0;
2888 }
2889
2890 if (!path->nodes[1]) {
2891 ret = insert_new_root(trans, root, path, 1);
2892 if (ret)
2893 return ret;
2894 }
2895again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002896 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002897 l = path->nodes[0];
2898 slot = path->slots[0];
2899 nritems = btrfs_header_nritems(l);
2900 mid = (nritems + 1) / 2;
2901
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002902 if (mid <= slot) {
2903 if (nritems == 1 ||
2904 leaf_space_used(l, mid, nritems - mid) + data_size >
2905 BTRFS_LEAF_DATA_SIZE(root)) {
2906 if (slot >= nritems) {
2907 split = 0;
2908 } else {
2909 mid = slot;
2910 if (mid != nritems &&
2911 leaf_space_used(l, mid, nritems - mid) +
2912 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002913 if (data_size && !tried_avoid_double)
2914 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002915 split = 2;
2916 }
2917 }
2918 }
2919 } else {
2920 if (leaf_space_used(l, 0, mid) + data_size >
2921 BTRFS_LEAF_DATA_SIZE(root)) {
2922 if (!extend && data_size && slot == 0) {
2923 split = 0;
2924 } else if ((extend || !data_size) && slot == 0) {
2925 mid = 1;
2926 } else {
2927 mid = slot;
2928 if (mid != nritems &&
2929 leaf_space_used(l, mid, nritems - mid) +
2930 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002931 if (data_size && !tried_avoid_double)
2932 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002933 split = 2 ;
2934 }
2935 }
2936 }
2937 }
2938
2939 if (split == 0)
2940 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2941 else
2942 btrfs_item_key(l, &disk_key, mid);
2943
2944 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002945 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002946 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002947 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002948 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002949
2950 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002951
2952 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2953 btrfs_set_header_bytenr(right, right->start);
2954 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002955 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002956 btrfs_set_header_owner(right, root->root_key.objectid);
2957 btrfs_set_header_level(right, 0);
2958 write_extent_buffer(right, root->fs_info->fsid,
2959 (unsigned long)btrfs_header_fsid(right),
2960 BTRFS_FSID_SIZE);
2961
2962 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2963 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2964 BTRFS_UUID_SIZE);
2965
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002966 if (split == 0) {
2967 if (mid <= slot) {
2968 btrfs_set_header_nritems(right, 0);
2969 wret = insert_ptr(trans, root, path,
2970 &disk_key, right->start,
2971 path->slots[1] + 1, 1);
2972 if (wret)
2973 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002974
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002975 btrfs_tree_unlock(path->nodes[0]);
2976 free_extent_buffer(path->nodes[0]);
2977 path->nodes[0] = right;
2978 path->slots[0] = 0;
2979 path->slots[1] += 1;
2980 } else {
2981 btrfs_set_header_nritems(right, 0);
2982 wret = insert_ptr(trans, root, path,
2983 &disk_key,
2984 right->start,
2985 path->slots[1], 1);
2986 if (wret)
2987 ret = wret;
2988 btrfs_tree_unlock(path->nodes[0]);
2989 free_extent_buffer(path->nodes[0]);
2990 path->nodes[0] = right;
2991 path->slots[0] = 0;
2992 if (path->slots[1] == 0) {
2993 wret = fixup_low_keys(trans, root,
2994 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002995 if (wret)
2996 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002997 }
2998 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002999 btrfs_mark_buffer_dirty(right);
3000 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003001 }
3002
3003 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3004 BUG_ON(ret);
3005
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003006 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003007 BUG_ON(num_doubles != 0);
3008 num_doubles++;
3009 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003010 }
Chris Mason44871b12009-03-13 10:04:31 -04003011
Chris Masonbe0e5c02007-01-26 15:51:26 -05003012 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04003013
3014push_for_double:
3015 push_for_double_split(trans, root, path, data_size);
3016 tried_avoid_double = 1;
3017 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3018 return 0;
3019 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003020}
3021
Yan, Zhengad48fd752009-11-12 09:33:58 +00003022static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3023 struct btrfs_root *root,
3024 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003025{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003026 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003027 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003028 struct btrfs_file_extent_item *fi;
3029 u64 extent_len = 0;
3030 u32 item_size;
3031 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003032
3033 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003034 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3035
3036 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3037 key.type != BTRFS_EXTENT_CSUM_KEY);
3038
3039 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3040 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003041
3042 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003043 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3044 fi = btrfs_item_ptr(leaf, path->slots[0],
3045 struct btrfs_file_extent_item);
3046 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3047 }
Chris Mason459931e2008-12-10 09:10:46 -05003048 btrfs_release_path(root, path);
3049
Chris Mason459931e2008-12-10 09:10:46 -05003050 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003051 path->search_for_split = 1;
3052 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003053 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003054 if (ret < 0)
3055 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003056
Yan, Zhengad48fd752009-11-12 09:33:58 +00003057 ret = -EAGAIN;
3058 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003059 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003060 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3061 goto err;
3062
Chris Mason109f6ae2010-04-02 09:20:18 -04003063 /* the leaf has changed, it now has room. return now */
3064 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3065 goto err;
3066
Yan, Zhengad48fd752009-11-12 09:33:58 +00003067 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3068 fi = btrfs_item_ptr(leaf, path->slots[0],
3069 struct btrfs_file_extent_item);
3070 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3071 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003072 }
3073
Chris Masonb9473432009-03-13 11:00:37 -04003074 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003075 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003076 if (ret)
3077 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003078
Yan, Zhengad48fd752009-11-12 09:33:58 +00003079 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003080 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003081 return 0;
3082err:
3083 path->keep_locks = 0;
3084 return ret;
3085}
3086
3087static noinline int split_item(struct btrfs_trans_handle *trans,
3088 struct btrfs_root *root,
3089 struct btrfs_path *path,
3090 struct btrfs_key *new_key,
3091 unsigned long split_offset)
3092{
3093 struct extent_buffer *leaf;
3094 struct btrfs_item *item;
3095 struct btrfs_item *new_item;
3096 int slot;
3097 char *buf;
3098 u32 nritems;
3099 u32 item_size;
3100 u32 orig_offset;
3101 struct btrfs_disk_key disk_key;
3102
Chris Masonb9473432009-03-13 11:00:37 -04003103 leaf = path->nodes[0];
3104 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3105
Chris Masonb4ce94d2009-02-04 09:25:08 -05003106 btrfs_set_path_blocking(path);
3107
Chris Mason459931e2008-12-10 09:10:46 -05003108 item = btrfs_item_nr(leaf, path->slots[0]);
3109 orig_offset = btrfs_item_offset(leaf, item);
3110 item_size = btrfs_item_size(leaf, item);
3111
Chris Mason459931e2008-12-10 09:10:46 -05003112 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003113 if (!buf)
3114 return -ENOMEM;
3115
Chris Mason459931e2008-12-10 09:10:46 -05003116 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3117 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003118
Chris Mason459931e2008-12-10 09:10:46 -05003119 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003120 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003121 if (slot != nritems) {
3122 /* shift the items */
3123 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003124 btrfs_item_nr_offset(slot),
3125 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003126 }
3127
3128 btrfs_cpu_key_to_disk(&disk_key, new_key);
3129 btrfs_set_item_key(leaf, &disk_key, slot);
3130
3131 new_item = btrfs_item_nr(leaf, slot);
3132
3133 btrfs_set_item_offset(leaf, new_item, orig_offset);
3134 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3135
3136 btrfs_set_item_offset(leaf, item,
3137 orig_offset + item_size - split_offset);
3138 btrfs_set_item_size(leaf, item, split_offset);
3139
3140 btrfs_set_header_nritems(leaf, nritems + 1);
3141
3142 /* write the data for the start of the original item */
3143 write_extent_buffer(leaf, buf,
3144 btrfs_item_ptr_offset(leaf, path->slots[0]),
3145 split_offset);
3146
3147 /* write the data for the new item */
3148 write_extent_buffer(leaf, buf + split_offset,
3149 btrfs_item_ptr_offset(leaf, slot),
3150 item_size - split_offset);
3151 btrfs_mark_buffer_dirty(leaf);
3152
Yan, Zhengad48fd752009-11-12 09:33:58 +00003153 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003154 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003155 return 0;
3156}
3157
3158/*
3159 * This function splits a single item into two items,
3160 * giving 'new_key' to the new item and splitting the
3161 * old one at split_offset (from the start of the item).
3162 *
3163 * The path may be released by this operation. After
3164 * the split, the path is pointing to the old item. The
3165 * new item is going to be in the same node as the old one.
3166 *
3167 * Note, the item being split must be smaller enough to live alone on
3168 * a tree block with room for one extra struct btrfs_item
3169 *
3170 * This allows us to split the item in place, keeping a lock on the
3171 * leaf the entire time.
3172 */
3173int btrfs_split_item(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root,
3175 struct btrfs_path *path,
3176 struct btrfs_key *new_key,
3177 unsigned long split_offset)
3178{
3179 int ret;
3180 ret = setup_leaf_for_split(trans, root, path,
3181 sizeof(struct btrfs_item));
3182 if (ret)
3183 return ret;
3184
3185 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003186 return ret;
3187}
3188
3189/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003190 * This function duplicate a item, giving 'new_key' to the new item.
3191 * It guarantees both items live in the same tree leaf and the new item
3192 * is contiguous with the original item.
3193 *
3194 * This allows us to split file extent in place, keeping a lock on the
3195 * leaf the entire time.
3196 */
3197int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3198 struct btrfs_root *root,
3199 struct btrfs_path *path,
3200 struct btrfs_key *new_key)
3201{
3202 struct extent_buffer *leaf;
3203 int ret;
3204 u32 item_size;
3205
3206 leaf = path->nodes[0];
3207 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3208 ret = setup_leaf_for_split(trans, root, path,
3209 item_size + sizeof(struct btrfs_item));
3210 if (ret)
3211 return ret;
3212
3213 path->slots[0]++;
3214 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3215 item_size, item_size +
3216 sizeof(struct btrfs_item), 1);
3217 BUG_ON(ret);
3218
3219 leaf = path->nodes[0];
3220 memcpy_extent_buffer(leaf,
3221 btrfs_item_ptr_offset(leaf, path->slots[0]),
3222 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3223 item_size);
3224 return 0;
3225}
3226
3227/*
Chris Masond352ac62008-09-29 15:18:18 -04003228 * make the item pointed to by the path smaller. new_size indicates
3229 * how small to make it, and from_end tells us if we just chop bytes
3230 * off the end of the item or if we shift the item to chop bytes off
3231 * the front.
3232 */
Chris Masonb18c6682007-04-17 13:26:50 -04003233int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3234 struct btrfs_root *root,
3235 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003236 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003237{
3238 int ret = 0;
3239 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003240 struct extent_buffer *leaf;
3241 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003242 u32 nritems;
3243 unsigned int data_end;
3244 unsigned int old_data_start;
3245 unsigned int old_size;
3246 unsigned int size_diff;
3247 int i;
3248
Chris Mason5f39d392007-10-15 16:14:19 -04003249 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003250 slot = path->slots[0];
3251
3252 old_size = btrfs_item_size_nr(leaf, slot);
3253 if (old_size == new_size)
3254 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003255
Chris Mason5f39d392007-10-15 16:14:19 -04003256 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003257 data_end = leaf_data_end(root, leaf);
3258
Chris Mason5f39d392007-10-15 16:14:19 -04003259 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003260
Chris Masonb18c6682007-04-17 13:26:50 -04003261 size_diff = old_size - new_size;
3262
3263 BUG_ON(slot < 0);
3264 BUG_ON(slot >= nritems);
3265
3266 /*
3267 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3268 */
3269 /* first correct the data pointers */
3270 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003271 u32 ioff;
3272 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003273
3274 if (!leaf->map_token) {
3275 map_extent_buffer(leaf, (unsigned long)item,
3276 sizeof(struct btrfs_item),
3277 &leaf->map_token, &leaf->kaddr,
3278 &leaf->map_start, &leaf->map_len,
3279 KM_USER1);
3280 }
3281
Chris Mason5f39d392007-10-15 16:14:19 -04003282 ioff = btrfs_item_offset(leaf, item);
3283 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003284 }
Chris Masondb945352007-10-15 16:15:53 -04003285
3286 if (leaf->map_token) {
3287 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3288 leaf->map_token = NULL;
3289 }
3290
Chris Masonb18c6682007-04-17 13:26:50 -04003291 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003292 if (from_end) {
3293 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3294 data_end + size_diff, btrfs_leaf_data(leaf) +
3295 data_end, old_data_start + new_size - data_end);
3296 } else {
3297 struct btrfs_disk_key disk_key;
3298 u64 offset;
3299
3300 btrfs_item_key(leaf, &disk_key, slot);
3301
3302 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3303 unsigned long ptr;
3304 struct btrfs_file_extent_item *fi;
3305
3306 fi = btrfs_item_ptr(leaf, slot,
3307 struct btrfs_file_extent_item);
3308 fi = (struct btrfs_file_extent_item *)(
3309 (unsigned long)fi - size_diff);
3310
3311 if (btrfs_file_extent_type(leaf, fi) ==
3312 BTRFS_FILE_EXTENT_INLINE) {
3313 ptr = btrfs_item_ptr_offset(leaf, slot);
3314 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003315 (unsigned long)fi,
3316 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003317 disk_bytenr));
3318 }
3319 }
3320
3321 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3322 data_end + size_diff, btrfs_leaf_data(leaf) +
3323 data_end, old_data_start - data_end);
3324
3325 offset = btrfs_disk_key_offset(&disk_key);
3326 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3327 btrfs_set_item_key(leaf, &disk_key, slot);
3328 if (slot == 0)
3329 fixup_low_keys(trans, root, path, &disk_key, 1);
3330 }
Chris Mason5f39d392007-10-15 16:14:19 -04003331
3332 item = btrfs_item_nr(leaf, slot);
3333 btrfs_set_item_size(leaf, item, new_size);
3334 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003335
3336 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003337 if (btrfs_leaf_free_space(root, leaf) < 0) {
3338 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003339 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003340 }
Chris Masonb18c6682007-04-17 13:26:50 -04003341 return ret;
3342}
3343
Chris Masond352ac62008-09-29 15:18:18 -04003344/*
3345 * make the item pointed to by the path bigger, data_size is the new size.
3346 */
Chris Mason5f39d392007-10-15 16:14:19 -04003347int btrfs_extend_item(struct btrfs_trans_handle *trans,
3348 struct btrfs_root *root, struct btrfs_path *path,
3349 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003350{
3351 int ret = 0;
3352 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003353 struct extent_buffer *leaf;
3354 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003355 u32 nritems;
3356 unsigned int data_end;
3357 unsigned int old_data;
3358 unsigned int old_size;
3359 int i;
3360
Chris Mason5f39d392007-10-15 16:14:19 -04003361 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003362
Chris Mason5f39d392007-10-15 16:14:19 -04003363 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003364 data_end = leaf_data_end(root, leaf);
3365
Chris Mason5f39d392007-10-15 16:14:19 -04003366 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3367 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003368 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003369 }
Chris Mason6567e832007-04-16 09:22:45 -04003370 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003371 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003372
3373 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003374 if (slot >= nritems) {
3375 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003376 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3377 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003378 BUG_ON(1);
3379 }
Chris Mason6567e832007-04-16 09:22:45 -04003380
3381 /*
3382 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3383 */
3384 /* first correct the data pointers */
3385 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003386 u32 ioff;
3387 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003388
3389 if (!leaf->map_token) {
3390 map_extent_buffer(leaf, (unsigned long)item,
3391 sizeof(struct btrfs_item),
3392 &leaf->map_token, &leaf->kaddr,
3393 &leaf->map_start, &leaf->map_len,
3394 KM_USER1);
3395 }
Chris Mason5f39d392007-10-15 16:14:19 -04003396 ioff = btrfs_item_offset(leaf, item);
3397 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003398 }
Chris Mason5f39d392007-10-15 16:14:19 -04003399
Chris Masondb945352007-10-15 16:15:53 -04003400 if (leaf->map_token) {
3401 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3402 leaf->map_token = NULL;
3403 }
3404
Chris Mason6567e832007-04-16 09:22:45 -04003405 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003406 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003407 data_end - data_size, btrfs_leaf_data(leaf) +
3408 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003409
Chris Mason6567e832007-04-16 09:22:45 -04003410 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003411 old_size = btrfs_item_size_nr(leaf, slot);
3412 item = btrfs_item_nr(leaf, slot);
3413 btrfs_set_item_size(leaf, item, old_size + data_size);
3414 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003415
3416 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003417 if (btrfs_leaf_free_space(root, leaf) < 0) {
3418 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003419 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003420 }
Chris Mason6567e832007-04-16 09:22:45 -04003421 return ret;
3422}
3423
Chris Mason74123bd2007-02-02 11:05:29 -05003424/*
Chris Masond352ac62008-09-29 15:18:18 -04003425 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003426 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003427 * Returns the number of keys that were inserted.
3428 */
3429int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3430 struct btrfs_root *root,
3431 struct btrfs_path *path,
3432 struct btrfs_key *cpu_key, u32 *data_size,
3433 int nr)
3434{
3435 struct extent_buffer *leaf;
3436 struct btrfs_item *item;
3437 int ret = 0;
3438 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003439 int i;
3440 u32 nritems;
3441 u32 total_data = 0;
3442 u32 total_size = 0;
3443 unsigned int data_end;
3444 struct btrfs_disk_key disk_key;
3445 struct btrfs_key found_key;
3446
Yan Zheng87b29b22008-12-17 10:21:48 -05003447 for (i = 0; i < nr; i++) {
3448 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3449 BTRFS_LEAF_DATA_SIZE(root)) {
3450 break;
3451 nr = i;
3452 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003453 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003454 total_size += data_size[i] + sizeof(struct btrfs_item);
3455 }
3456 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003457
Josef Bacikf3465ca2008-11-12 14:19:50 -05003458 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3459 if (ret == 0)
3460 return -EEXIST;
3461 if (ret < 0)
3462 goto out;
3463
Josef Bacikf3465ca2008-11-12 14:19:50 -05003464 leaf = path->nodes[0];
3465
3466 nritems = btrfs_header_nritems(leaf);
3467 data_end = leaf_data_end(root, leaf);
3468
3469 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3470 for (i = nr; i >= 0; i--) {
3471 total_data -= data_size[i];
3472 total_size -= data_size[i] + sizeof(struct btrfs_item);
3473 if (total_size < btrfs_leaf_free_space(root, leaf))
3474 break;
3475 }
3476 nr = i;
3477 }
3478
3479 slot = path->slots[0];
3480 BUG_ON(slot < 0);
3481
3482 if (slot != nritems) {
3483 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3484
3485 item = btrfs_item_nr(leaf, slot);
3486 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3487
3488 /* figure out how many keys we can insert in here */
3489 total_data = data_size[0];
3490 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003491 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003492 break;
3493 total_data += data_size[i];
3494 }
3495 nr = i;
3496
3497 if (old_data < data_end) {
3498 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003499 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003500 slot, old_data, data_end);
3501 BUG_ON(1);
3502 }
3503 /*
3504 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3505 */
3506 /* first correct the data pointers */
3507 WARN_ON(leaf->map_token);
3508 for (i = slot; i < nritems; i++) {
3509 u32 ioff;
3510
3511 item = btrfs_item_nr(leaf, i);
3512 if (!leaf->map_token) {
3513 map_extent_buffer(leaf, (unsigned long)item,
3514 sizeof(struct btrfs_item),
3515 &leaf->map_token, &leaf->kaddr,
3516 &leaf->map_start, &leaf->map_len,
3517 KM_USER1);
3518 }
3519
3520 ioff = btrfs_item_offset(leaf, item);
3521 btrfs_set_item_offset(leaf, item, ioff - total_data);
3522 }
3523 if (leaf->map_token) {
3524 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3525 leaf->map_token = NULL;
3526 }
3527
3528 /* shift the items */
3529 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3530 btrfs_item_nr_offset(slot),
3531 (nritems - slot) * sizeof(struct btrfs_item));
3532
3533 /* shift the data */
3534 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3535 data_end - total_data, btrfs_leaf_data(leaf) +
3536 data_end, old_data - data_end);
3537 data_end = old_data;
3538 } else {
3539 /*
3540 * this sucks but it has to be done, if we are inserting at
3541 * the end of the leaf only insert 1 of the items, since we
3542 * have no way of knowing whats on the next leaf and we'd have
3543 * to drop our current locks to figure it out
3544 */
3545 nr = 1;
3546 }
3547
3548 /* setup the item for the new data */
3549 for (i = 0; i < nr; i++) {
3550 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3551 btrfs_set_item_key(leaf, &disk_key, slot + i);
3552 item = btrfs_item_nr(leaf, slot + i);
3553 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3554 data_end -= data_size[i];
3555 btrfs_set_item_size(leaf, item, data_size[i]);
3556 }
3557 btrfs_set_header_nritems(leaf, nritems + nr);
3558 btrfs_mark_buffer_dirty(leaf);
3559
3560 ret = 0;
3561 if (slot == 0) {
3562 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3563 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3564 }
3565
3566 if (btrfs_leaf_free_space(root, leaf) < 0) {
3567 btrfs_print_leaf(root, leaf);
3568 BUG();
3569 }
3570out:
3571 if (!ret)
3572 ret = nr;
3573 return ret;
3574}
3575
3576/*
Chris Mason44871b12009-03-13 10:04:31 -04003577 * this is a helper for btrfs_insert_empty_items, the main goal here is
3578 * to save stack depth by doing the bulk of the work in a function
3579 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003580 */
Chris Mason44871b12009-03-13 10:04:31 -04003581static noinline_for_stack int
3582setup_items_for_insert(struct btrfs_trans_handle *trans,
3583 struct btrfs_root *root, struct btrfs_path *path,
3584 struct btrfs_key *cpu_key, u32 *data_size,
3585 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003586{
Chris Mason5f39d392007-10-15 16:14:19 -04003587 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003588 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003589 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003590 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003591 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003592 int ret;
3593 struct extent_buffer *leaf;
3594 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003595
Chris Mason5f39d392007-10-15 16:14:19 -04003596 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003597 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003598
Chris Mason5f39d392007-10-15 16:14:19 -04003599 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003600 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003601
Chris Masonf25956c2008-09-12 15:32:53 -04003602 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003603 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003604 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003605 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003606 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003607 }
Chris Mason5f39d392007-10-15 16:14:19 -04003608
Chris Masonbe0e5c02007-01-26 15:51:26 -05003609 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003610 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003611
Chris Mason5f39d392007-10-15 16:14:19 -04003612 if (old_data < data_end) {
3613 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003614 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003615 slot, old_data, data_end);
3616 BUG_ON(1);
3617 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003618 /*
3619 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3620 */
3621 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003622 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003623 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003624 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003625
Chris Mason5f39d392007-10-15 16:14:19 -04003626 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003627 if (!leaf->map_token) {
3628 map_extent_buffer(leaf, (unsigned long)item,
3629 sizeof(struct btrfs_item),
3630 &leaf->map_token, &leaf->kaddr,
3631 &leaf->map_start, &leaf->map_len,
3632 KM_USER1);
3633 }
3634
Chris Mason5f39d392007-10-15 16:14:19 -04003635 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003636 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003637 }
Chris Masondb945352007-10-15 16:15:53 -04003638 if (leaf->map_token) {
3639 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3640 leaf->map_token = NULL;
3641 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003642
3643 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003644 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003645 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003646 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003647
3648 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003649 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003650 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003651 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003652 data_end = old_data;
3653 }
Chris Mason5f39d392007-10-15 16:14:19 -04003654
Chris Mason62e27492007-03-15 12:56:47 -04003655 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003656 for (i = 0; i < nr; i++) {
3657 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3658 btrfs_set_item_key(leaf, &disk_key, slot + i);
3659 item = btrfs_item_nr(leaf, slot + i);
3660 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3661 data_end -= data_size[i];
3662 btrfs_set_item_size(leaf, item, data_size[i]);
3663 }
Chris Mason44871b12009-03-13 10:04:31 -04003664
Chris Mason9c583092008-01-29 15:15:18 -05003665 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003666
3667 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003668 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003669 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003670 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003671 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003672 }
Chris Masonb9473432009-03-13 11:00:37 -04003673 btrfs_unlock_up_safe(path, 1);
3674 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003675
Chris Mason5f39d392007-10-15 16:14:19 -04003676 if (btrfs_leaf_free_space(root, leaf) < 0) {
3677 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003678 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003679 }
Chris Mason44871b12009-03-13 10:04:31 -04003680 return ret;
3681}
3682
3683/*
3684 * Given a key and some data, insert items into the tree.
3685 * This does all the path init required, making room in the tree if needed.
3686 */
3687int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3688 struct btrfs_root *root,
3689 struct btrfs_path *path,
3690 struct btrfs_key *cpu_key, u32 *data_size,
3691 int nr)
3692{
Chris Mason44871b12009-03-13 10:04:31 -04003693 int ret = 0;
3694 int slot;
3695 int i;
3696 u32 total_size = 0;
3697 u32 total_data = 0;
3698
3699 for (i = 0; i < nr; i++)
3700 total_data += data_size[i];
3701
3702 total_size = total_data + (nr * sizeof(struct btrfs_item));
3703 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3704 if (ret == 0)
3705 return -EEXIST;
3706 if (ret < 0)
3707 goto out;
3708
Chris Mason44871b12009-03-13 10:04:31 -04003709 slot = path->slots[0];
3710 BUG_ON(slot < 0);
3711
3712 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3713 total_data, total_size, nr);
3714
Chris Masoned2ff2c2007-03-01 18:59:40 -05003715out:
Chris Mason62e27492007-03-15 12:56:47 -04003716 return ret;
3717}
3718
3719/*
3720 * Given a key and some data, insert an item into the tree.
3721 * This does all the path init required, making room in the tree if needed.
3722 */
Chris Masone089f052007-03-16 16:20:31 -04003723int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3724 *root, struct btrfs_key *cpu_key, void *data, u32
3725 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003726{
3727 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003728 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003729 struct extent_buffer *leaf;
3730 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003731
Chris Mason2c90e5d2007-04-02 10:50:19 -04003732 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003733 if (!path)
3734 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003735 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003736 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003737 leaf = path->nodes[0];
3738 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3739 write_extent_buffer(leaf, data, ptr, data_size);
3740 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003741 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003742 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003743 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003744}
3745
Chris Mason74123bd2007-02-02 11:05:29 -05003746/*
Chris Mason5de08d72007-02-24 06:24:44 -05003747 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003748 *
Chris Masond352ac62008-09-29 15:18:18 -04003749 * the tree should have been previously balanced so the deletion does not
3750 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003751 */
Chris Masone089f052007-03-16 16:20:31 -04003752static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3753 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003754{
Chris Mason5f39d392007-10-15 16:14:19 -04003755 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003756 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003757 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003758 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003759
Chris Mason5f39d392007-10-15 16:14:19 -04003760 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003761 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003762 memmove_extent_buffer(parent,
3763 btrfs_node_key_ptr_offset(slot),
3764 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003765 sizeof(struct btrfs_key_ptr) *
3766 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003767 }
Chris Mason7518a232007-03-12 12:01:18 -04003768 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003769 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003770 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003771 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003772 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003773 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003774 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003775 struct btrfs_disk_key disk_key;
3776
3777 btrfs_node_key(parent, &disk_key, 0);
3778 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003779 if (wret)
3780 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003781 }
Chris Masond6025572007-03-30 14:27:56 -04003782 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003783 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003784}
3785
Chris Mason74123bd2007-02-02 11:05:29 -05003786/*
Chris Mason323ac952008-10-01 19:05:46 -04003787 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003788 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003789 *
3790 * This deletes the pointer in path->nodes[1] and frees the leaf
3791 * block extent. zero is returned if it all worked out, < 0 otherwise.
3792 *
3793 * The path must have already been setup for deleting the leaf, including
3794 * all the proper balancing. path->nodes[1] must be locked.
3795 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003796static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3797 struct btrfs_root *root,
3798 struct btrfs_path *path,
3799 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003800{
3801 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003802
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003803 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003804 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3805 if (ret)
3806 return ret;
3807
Chris Mason4d081c42009-02-04 09:31:28 -05003808 /*
3809 * btrfs_free_extent is expensive, we want to make sure we
3810 * aren't holding any locks when we call it
3811 */
3812 btrfs_unlock_up_safe(path, 0);
3813
Yan, Zhengf0486c62010-05-16 10:46:25 -04003814 root_sub_used(root, leaf->len);
3815
3816 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3817 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003818}
3819/*
Chris Mason74123bd2007-02-02 11:05:29 -05003820 * delete the item at the leaf level in path. If that empties
3821 * the leaf, remove it from the tree
3822 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003823int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3824 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003825{
Chris Mason5f39d392007-10-15 16:14:19 -04003826 struct extent_buffer *leaf;
3827 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003828 int last_off;
3829 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003830 int ret = 0;
3831 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003832 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003833 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003834
Chris Mason5f39d392007-10-15 16:14:19 -04003835 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003836 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3837
3838 for (i = 0; i < nr; i++)
3839 dsize += btrfs_item_size_nr(leaf, slot + i);
3840
Chris Mason5f39d392007-10-15 16:14:19 -04003841 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003842
Chris Mason85e21ba2008-01-29 15:11:36 -05003843 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003844 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003845
3846 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003847 data_end + dsize,
3848 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003849 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003850
Chris Mason85e21ba2008-01-29 15:11:36 -05003851 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003852 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003853
Chris Mason5f39d392007-10-15 16:14:19 -04003854 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003855 if (!leaf->map_token) {
3856 map_extent_buffer(leaf, (unsigned long)item,
3857 sizeof(struct btrfs_item),
3858 &leaf->map_token, &leaf->kaddr,
3859 &leaf->map_start, &leaf->map_len,
3860 KM_USER1);
3861 }
Chris Mason5f39d392007-10-15 16:14:19 -04003862 ioff = btrfs_item_offset(leaf, item);
3863 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003864 }
Chris Masondb945352007-10-15 16:15:53 -04003865
3866 if (leaf->map_token) {
3867 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3868 leaf->map_token = NULL;
3869 }
3870
Chris Mason5f39d392007-10-15 16:14:19 -04003871 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003872 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003873 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003874 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003875 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003876 btrfs_set_header_nritems(leaf, nritems - nr);
3877 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003878
Chris Mason74123bd2007-02-02 11:05:29 -05003879 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003880 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003881 if (leaf == root->node) {
3882 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003883 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003884 btrfs_set_path_blocking(path);
3885 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003886 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003887 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003888 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003889 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003890 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003891 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003892 struct btrfs_disk_key disk_key;
3893
3894 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003895 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003896 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003897 if (wret)
3898 ret = wret;
3899 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003900
Chris Mason74123bd2007-02-02 11:05:29 -05003901 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003902 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003903 /* push_leaf_left fixes the path.
3904 * make sure the path still points to our leaf
3905 * for possible call to del_ptr below
3906 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003907 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003908 extent_buffer_get(leaf);
3909
Chris Masonb9473432009-03-13 11:00:37 -04003910 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003911 wret = push_leaf_left(trans, root, path, 1, 1,
3912 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003913 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003914 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003915
3916 if (path->nodes[0] == leaf &&
3917 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003918 wret = push_leaf_right(trans, root, path, 1,
3919 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003920 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003921 ret = wret;
3922 }
Chris Mason5f39d392007-10-15 16:14:19 -04003923
3924 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003925 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003926 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003927 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003928 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003929 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003930 /* if we're still in the path, make sure
3931 * we're dirty. Otherwise, one of the
3932 * push_leaf functions must have already
3933 * dirtied this buffer
3934 */
3935 if (path->nodes[0] == leaf)
3936 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003937 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003938 }
Chris Masond5719762007-03-23 10:01:08 -04003939 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003940 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003941 }
3942 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003943 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003944}
3945
Chris Mason97571fd2007-02-24 13:39:08 -05003946/*
Chris Mason925baed2008-06-25 16:01:30 -04003947 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003948 * returns 0 if it found something or 1 if there are no lesser leaves.
3949 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003950 *
3951 * This may release the path, and so you may lose any locks held at the
3952 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003953 */
3954int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3955{
Chris Mason925baed2008-06-25 16:01:30 -04003956 struct btrfs_key key;
3957 struct btrfs_disk_key found_key;
3958 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003959
Chris Mason925baed2008-06-25 16:01:30 -04003960 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003961
Chris Mason925baed2008-06-25 16:01:30 -04003962 if (key.offset > 0)
3963 key.offset--;
3964 else if (key.type > 0)
3965 key.type--;
3966 else if (key.objectid > 0)
3967 key.objectid--;
3968 else
3969 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003970
Chris Mason925baed2008-06-25 16:01:30 -04003971 btrfs_release_path(root, path);
3972 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3973 if (ret < 0)
3974 return ret;
3975 btrfs_item_key(path->nodes[0], &found_key, 0);
3976 ret = comp_keys(&found_key, &key);
3977 if (ret < 0)
3978 return 0;
3979 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003980}
3981
Chris Mason3f157a22008-06-25 16:01:31 -04003982/*
3983 * A helper function to walk down the tree starting at min_key, and looking
3984 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003985 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003986 *
3987 * This does not cow, but it does stuff the starting key it finds back
3988 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3989 * key and get a writable path.
3990 *
3991 * This does lock as it descends, and path->keep_locks should be set
3992 * to 1 by the caller.
3993 *
3994 * This honors path->lowest_level to prevent descent past a given level
3995 * of the tree.
3996 *
Chris Masond352ac62008-09-29 15:18:18 -04003997 * min_trans indicates the oldest transaction that you are interested
3998 * in walking through. Any nodes or leaves older than min_trans are
3999 * skipped over (without reading them).
4000 *
Chris Mason3f157a22008-06-25 16:01:31 -04004001 * returns zero if something useful was found, < 0 on error and 1 if there
4002 * was nothing in the tree that matched the search criteria.
4003 */
4004int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004005 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004006 struct btrfs_path *path, int cache_only,
4007 u64 min_trans)
4008{
4009 struct extent_buffer *cur;
4010 struct btrfs_key found_key;
4011 int slot;
Yan96524802008-07-24 12:19:49 -04004012 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004013 u32 nritems;
4014 int level;
4015 int ret = 1;
4016
Chris Mason934d3752008-12-08 16:43:10 -05004017 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004018again:
4019 cur = btrfs_lock_root_node(root);
4020 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004021 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004022 path->nodes[level] = cur;
4023 path->locks[level] = 1;
4024
4025 if (btrfs_header_generation(cur) < min_trans) {
4026 ret = 1;
4027 goto out;
4028 }
Chris Masond3977122009-01-05 21:25:51 -05004029 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004030 nritems = btrfs_header_nritems(cur);
4031 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004032 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004033
Chris Mason323ac952008-10-01 19:05:46 -04004034 /* at the lowest level, we're done, setup the path and exit */
4035 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004036 if (slot >= nritems)
4037 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004038 ret = 0;
4039 path->slots[level] = slot;
4040 btrfs_item_key_to_cpu(cur, &found_key, slot);
4041 goto out;
4042 }
Yan96524802008-07-24 12:19:49 -04004043 if (sret && slot > 0)
4044 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004045 /*
4046 * check this node pointer against the cache_only and
4047 * min_trans parameters. If it isn't in cache or is too
4048 * old, skip to the next one.
4049 */
Chris Masond3977122009-01-05 21:25:51 -05004050 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004051 u64 blockptr;
4052 u64 gen;
4053 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004054 struct btrfs_disk_key disk_key;
4055
Chris Mason3f157a22008-06-25 16:01:31 -04004056 blockptr = btrfs_node_blockptr(cur, slot);
4057 gen = btrfs_node_ptr_generation(cur, slot);
4058 if (gen < min_trans) {
4059 slot++;
4060 continue;
4061 }
4062 if (!cache_only)
4063 break;
4064
Chris Masone02119d2008-09-05 16:13:11 -04004065 if (max_key) {
4066 btrfs_node_key(cur, &disk_key, slot);
4067 if (comp_keys(&disk_key, max_key) >= 0) {
4068 ret = 1;
4069 goto out;
4070 }
4071 }
4072
Chris Mason3f157a22008-06-25 16:01:31 -04004073 tmp = btrfs_find_tree_block(root, blockptr,
4074 btrfs_level_size(root, level - 1));
4075
4076 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4077 free_extent_buffer(tmp);
4078 break;
4079 }
4080 if (tmp)
4081 free_extent_buffer(tmp);
4082 slot++;
4083 }
Chris Masone02119d2008-09-05 16:13:11 -04004084find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004085 /*
4086 * we didn't find a candidate key in this node, walk forward
4087 * and find another one
4088 */
4089 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004090 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004091 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004092 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004093 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004094 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004095 btrfs_release_path(root, path);
4096 goto again;
4097 } else {
4098 goto out;
4099 }
4100 }
4101 /* save our key for returning back */
4102 btrfs_node_key_to_cpu(cur, &found_key, slot);
4103 path->slots[level] = slot;
4104 if (level == path->lowest_level) {
4105 ret = 0;
4106 unlock_up(path, level, 1);
4107 goto out;
4108 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004109 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004110 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004111 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004112
4113 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004114
Chris Mason3f157a22008-06-25 16:01:31 -04004115 path->locks[level - 1] = 1;
4116 path->nodes[level - 1] = cur;
4117 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004118 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004119 }
4120out:
4121 if (ret == 0)
4122 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004123 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004124 return ret;
4125}
4126
4127/*
4128 * this is similar to btrfs_next_leaf, but does not try to preserve
4129 * and fixup the path. It looks for and returns the next key in the
4130 * tree based on the current path and the cache_only and min_trans
4131 * parameters.
4132 *
4133 * 0 is returned if another key is found, < 0 if there are any errors
4134 * and 1 is returned if there are no higher keys in the tree
4135 *
4136 * path->keep_locks should be set to 1 on the search made before
4137 * calling this function.
4138 */
Chris Masone7a84562008-06-25 16:01:31 -04004139int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004140 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004141 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004142{
Chris Masone7a84562008-06-25 16:01:31 -04004143 int slot;
4144 struct extent_buffer *c;
4145
Chris Mason934d3752008-12-08 16:43:10 -05004146 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004147 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004148 if (!path->nodes[level])
4149 return 1;
4150
4151 slot = path->slots[level] + 1;
4152 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004153next:
Chris Masone7a84562008-06-25 16:01:31 -04004154 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004155 int ret;
4156 int orig_lowest;
4157 struct btrfs_key cur_key;
4158 if (level + 1 >= BTRFS_MAX_LEVEL ||
4159 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004160 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004161
4162 if (path->locks[level + 1]) {
4163 level++;
4164 continue;
4165 }
4166
4167 slot = btrfs_header_nritems(c) - 1;
4168 if (level == 0)
4169 btrfs_item_key_to_cpu(c, &cur_key, slot);
4170 else
4171 btrfs_node_key_to_cpu(c, &cur_key, slot);
4172
4173 orig_lowest = path->lowest_level;
4174 btrfs_release_path(root, path);
4175 path->lowest_level = level;
4176 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4177 0, 0);
4178 path->lowest_level = orig_lowest;
4179 if (ret < 0)
4180 return ret;
4181
4182 c = path->nodes[level];
4183 slot = path->slots[level];
4184 if (ret == 0)
4185 slot++;
4186 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004187 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004188
Chris Masone7a84562008-06-25 16:01:31 -04004189 if (level == 0)
4190 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004191 else {
4192 u64 blockptr = btrfs_node_blockptr(c, slot);
4193 u64 gen = btrfs_node_ptr_generation(c, slot);
4194
4195 if (cache_only) {
4196 struct extent_buffer *cur;
4197 cur = btrfs_find_tree_block(root, blockptr,
4198 btrfs_level_size(root, level - 1));
4199 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4200 slot++;
4201 if (cur)
4202 free_extent_buffer(cur);
4203 goto next;
4204 }
4205 free_extent_buffer(cur);
4206 }
4207 if (gen < min_trans) {
4208 slot++;
4209 goto next;
4210 }
Chris Masone7a84562008-06-25 16:01:31 -04004211 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004212 }
Chris Masone7a84562008-06-25 16:01:31 -04004213 return 0;
4214 }
4215 return 1;
4216}
4217
Chris Mason7bb86312007-12-11 09:25:06 -05004218/*
Chris Mason925baed2008-06-25 16:01:30 -04004219 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004220 * returns 0 if it found something or 1 if there are no greater leaves.
4221 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004222 */
Chris Mason234b63a2007-03-13 10:46:10 -04004223int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004224{
4225 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004226 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004227 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004228 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004229 struct btrfs_key key;
4230 u32 nritems;
4231 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004232 int old_spinning = path->leave_spinning;
4233 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004234
4235 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004236 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004237 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004238
Chris Mason8e73f272009-04-03 10:14:18 -04004239 /*
4240 * we take the blocks in an order that upsets lockdep. Using
4241 * blocking mode is the only way around it.
4242 */
4243#ifdef CONFIG_DEBUG_LOCK_ALLOC
4244 force_blocking = 1;
4245#endif
Chris Mason925baed2008-06-25 16:01:30 -04004246
Chris Mason8e73f272009-04-03 10:14:18 -04004247 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4248again:
4249 level = 1;
4250 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004251 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004252
Chris Masona2135012008-06-25 16:01:30 -04004253 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004254
4255 if (!force_blocking)
4256 path->leave_spinning = 1;
4257
Chris Mason925baed2008-06-25 16:01:30 -04004258 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4259 path->keep_locks = 0;
4260
4261 if (ret < 0)
4262 return ret;
4263
Chris Masona2135012008-06-25 16:01:30 -04004264 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004265 /*
4266 * by releasing the path above we dropped all our locks. A balance
4267 * could have added more items next to the key that used to be
4268 * at the very end of the block. So, check again here and
4269 * advance the path if there are now more items available.
4270 */
Chris Masona2135012008-06-25 16:01:30 -04004271 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004272 if (ret == 0)
4273 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004274 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004275 goto done;
4276 }
Chris Masond97e63b2007-02-20 16:40:44 -05004277
Chris Masond3977122009-01-05 21:25:51 -05004278 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004279 if (!path->nodes[level]) {
4280 ret = 1;
4281 goto done;
4282 }
Chris Mason5f39d392007-10-15 16:14:19 -04004283
Chris Masond97e63b2007-02-20 16:40:44 -05004284 slot = path->slots[level] + 1;
4285 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004286 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004287 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004288 if (level == BTRFS_MAX_LEVEL) {
4289 ret = 1;
4290 goto done;
4291 }
Chris Masond97e63b2007-02-20 16:40:44 -05004292 continue;
4293 }
Chris Mason5f39d392007-10-15 16:14:19 -04004294
Chris Mason925baed2008-06-25 16:01:30 -04004295 if (next) {
4296 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004297 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004298 }
Chris Mason5f39d392007-10-15 16:14:19 -04004299
Chris Mason8e73f272009-04-03 10:14:18 -04004300 next = c;
4301 ret = read_block_for_search(NULL, root, path, &next, level,
4302 slot, &key);
4303 if (ret == -EAGAIN)
4304 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004305
Chris Mason76a05b32009-05-14 13:24:30 -04004306 if (ret < 0) {
4307 btrfs_release_path(root, path);
4308 goto done;
4309 }
4310
Chris Mason5cd57b22008-06-25 16:01:30 -04004311 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004312 ret = btrfs_try_spin_lock(next);
4313 if (!ret) {
4314 btrfs_set_path_blocking(path);
4315 btrfs_tree_lock(next);
4316 if (!force_blocking)
4317 btrfs_clear_path_blocking(path, next);
4318 }
4319 if (force_blocking)
4320 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004321 }
Chris Masond97e63b2007-02-20 16:40:44 -05004322 break;
4323 }
4324 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004325 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004326 level--;
4327 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004328 if (path->locks[level])
4329 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004330
Chris Mason5f39d392007-10-15 16:14:19 -04004331 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004332 path->nodes[level] = next;
4333 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004334 if (!path->skip_locking)
4335 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004336
Chris Masond97e63b2007-02-20 16:40:44 -05004337 if (!level)
4338 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004339
Chris Mason8e73f272009-04-03 10:14:18 -04004340 ret = read_block_for_search(NULL, root, path, &next, level,
4341 0, &key);
4342 if (ret == -EAGAIN)
4343 goto again;
4344
Chris Mason76a05b32009-05-14 13:24:30 -04004345 if (ret < 0) {
4346 btrfs_release_path(root, path);
4347 goto done;
4348 }
4349
Chris Mason5cd57b22008-06-25 16:01:30 -04004350 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004351 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004352 ret = btrfs_try_spin_lock(next);
4353 if (!ret) {
4354 btrfs_set_path_blocking(path);
4355 btrfs_tree_lock(next);
4356 if (!force_blocking)
4357 btrfs_clear_path_blocking(path, next);
4358 }
4359 if (force_blocking)
4360 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004361 }
Chris Masond97e63b2007-02-20 16:40:44 -05004362 }
Chris Mason8e73f272009-04-03 10:14:18 -04004363 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004364done:
4365 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004366 path->leave_spinning = old_spinning;
4367 if (!old_spinning)
4368 btrfs_set_path_blocking(path);
4369
4370 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004371}
Chris Mason0b86a832008-03-24 15:01:56 -04004372
Chris Mason3f157a22008-06-25 16:01:31 -04004373/*
4374 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4375 * searching until it gets past min_objectid or finds an item of 'type'
4376 *
4377 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4378 */
Chris Mason0b86a832008-03-24 15:01:56 -04004379int btrfs_previous_item(struct btrfs_root *root,
4380 struct btrfs_path *path, u64 min_objectid,
4381 int type)
4382{
4383 struct btrfs_key found_key;
4384 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004385 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004386 int ret;
4387
Chris Masond3977122009-01-05 21:25:51 -05004388 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004389 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004390 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004391 ret = btrfs_prev_leaf(root, path);
4392 if (ret != 0)
4393 return ret;
4394 } else {
4395 path->slots[0]--;
4396 }
4397 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004398 nritems = btrfs_header_nritems(leaf);
4399 if (nritems == 0)
4400 return 1;
4401 if (path->slots[0] == nritems)
4402 path->slots[0]--;
4403
Chris Mason0b86a832008-03-24 15:01:56 -04004404 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004405 if (found_key.objectid < min_objectid)
4406 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004407 if (found_key.type == type)
4408 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004409 if (found_key.objectid == min_objectid &&
4410 found_key.type < type)
4411 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004412 }
4413 return 1;
4414}