blob: 99599f1c15549791a2f3cb2d560c579be8c7b920 [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;
150 spin_lock(&root->node_lock);
151 eb = root->node;
152 extent_buffer_get(eb);
153 spin_unlock(&root->node_lock);
154 return eb;
155}
156
Chris Masond352ac62008-09-29 15:18:18 -0400157/* loop around taking references on and locking the root node of the
158 * tree until you end up with a lock on the root. A locked buffer
159 * is returned, with a reference held.
160 */
Chris Mason925baed2008-06-25 16:01:30 -0400161struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
162{
163 struct extent_buffer *eb;
164
Chris Masond3977122009-01-05 21:25:51 -0500165 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400166 eb = btrfs_root_node(root);
167 btrfs_tree_lock(eb);
168
169 spin_lock(&root->node_lock);
170 if (eb == root->node) {
171 spin_unlock(&root->node_lock);
172 break;
173 }
174 spin_unlock(&root->node_lock);
175
176 btrfs_tree_unlock(eb);
177 free_extent_buffer(eb);
178 }
179 return eb;
180}
181
Chris Masond352ac62008-09-29 15:18:18 -0400182/* cowonly root (everything not a reference counted cow subvolume), just get
183 * put onto a simple dirty list. transaction.c walks this to make sure they
184 * get properly updated on disk.
185 */
Chris Mason0b86a832008-03-24 15:01:56 -0400186static void add_root_to_dirty_list(struct btrfs_root *root)
187{
188 if (root->track_dirty && list_empty(&root->dirty_list)) {
189 list_add(&root->dirty_list,
190 &root->fs_info->dirty_cowonly_roots);
191 }
192}
193
Chris Masond352ac62008-09-29 15:18:18 -0400194/*
195 * used by snapshot creation to make a copy of a root for a tree with
196 * a given objectid. The buffer with the new root node is returned in
197 * cow_ret, and this func returns zero on success or a negative error code.
198 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500199int btrfs_copy_root(struct btrfs_trans_handle *trans,
200 struct btrfs_root *root,
201 struct extent_buffer *buf,
202 struct extent_buffer **cow_ret, u64 new_root_objectid)
203{
204 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500205 int ret = 0;
206 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400207 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500208
209 WARN_ON(root->ref_cows && trans->transid !=
210 root->fs_info->running_transaction->transid);
211 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
212
213 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400214 if (level == 0)
215 btrfs_item_key(buf, &disk_key, 0);
216 else
217 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400218
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400219 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
220 new_root_objectid, &disk_key, level,
221 buf->start, 0);
222 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500223 return PTR_ERR(cow);
224
225 copy_extent_buffer(cow, buf, 0, 0, cow->len);
226 btrfs_set_header_bytenr(cow, cow->start);
227 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400228 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
229 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
230 BTRFS_HEADER_FLAG_RELOC);
231 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
232 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
233 else
234 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500235
Yan Zheng2b820322008-11-17 21:11:30 -0500236 write_extent_buffer(cow, root->fs_info->fsid,
237 (unsigned long)btrfs_header_fsid(cow),
238 BTRFS_FSID_SIZE);
239
Chris Masonbe20aa92007-12-17 20:14:01 -0500240 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400241 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
242 ret = btrfs_inc_ref(trans, root, cow, 1);
243 else
244 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500245
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 if (ret)
247 return ret;
248
249 btrfs_mark_buffer_dirty(cow);
250 *cow_ret = cow;
251 return 0;
252}
253
Chris Masond352ac62008-09-29 15:18:18 -0400254/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 * check if the tree block can be shared by multiple trees
256 */
257int btrfs_block_can_be_shared(struct btrfs_root *root,
258 struct extent_buffer *buf)
259{
260 /*
261 * Tree blocks not in refernece counted trees and tree roots
262 * are never shared. If a block was allocated after the last
263 * snapshot and the block was not allocated by tree relocation,
264 * we know the block is not shared.
265 */
266 if (root->ref_cows &&
267 buf != root->node && buf != root->commit_root &&
268 (btrfs_header_generation(buf) <=
269 btrfs_root_last_snapshot(&root->root_item) ||
270 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
271 return 1;
272#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
273 if (root->ref_cows &&
274 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
275 return 1;
276#endif
277 return 0;
278}
279
280static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root,
282 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400283 struct extent_buffer *cow,
284 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400285{
286 u64 refs;
287 u64 owner;
288 u64 flags;
289 u64 new_flags = 0;
290 int ret;
291
292 /*
293 * Backrefs update rules:
294 *
295 * Always use full backrefs for extent pointers in tree block
296 * allocated by tree relocation.
297 *
298 * If a shared tree block is no longer referenced by its owner
299 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
300 * use full backrefs for extent pointers in tree block.
301 *
302 * If a tree block is been relocating
303 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
304 * use full backrefs for extent pointers in tree block.
305 * The reason for this is some operations (such as drop tree)
306 * are only allowed for blocks use full backrefs.
307 */
308
309 if (btrfs_block_can_be_shared(root, buf)) {
310 ret = btrfs_lookup_extent_info(trans, root, buf->start,
311 buf->len, &refs, &flags);
312 BUG_ON(ret);
313 BUG_ON(refs == 0);
314 } else {
315 refs = 1;
316 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
317 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
318 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
319 else
320 flags = 0;
321 }
322
323 owner = btrfs_header_owner(buf);
324 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
325 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
326
327 if (refs > 1) {
328 if ((owner == root->root_key.objectid ||
329 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
330 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
331 ret = btrfs_inc_ref(trans, root, buf, 1);
332 BUG_ON(ret);
333
334 if (root->root_key.objectid ==
335 BTRFS_TREE_RELOC_OBJECTID) {
336 ret = btrfs_dec_ref(trans, root, buf, 0);
337 BUG_ON(ret);
338 ret = btrfs_inc_ref(trans, root, cow, 1);
339 BUG_ON(ret);
340 }
341 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
342 } else {
343
344 if (root->root_key.objectid ==
345 BTRFS_TREE_RELOC_OBJECTID)
346 ret = btrfs_inc_ref(trans, root, cow, 1);
347 else
348 ret = btrfs_inc_ref(trans, root, cow, 0);
349 BUG_ON(ret);
350 }
351 if (new_flags != 0) {
352 ret = btrfs_set_disk_extent_flags(trans, root,
353 buf->start,
354 buf->len,
355 new_flags, 0);
356 BUG_ON(ret);
357 }
358 } else {
359 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
360 if (root->root_key.objectid ==
361 BTRFS_TREE_RELOC_OBJECTID)
362 ret = btrfs_inc_ref(trans, root, cow, 1);
363 else
364 ret = btrfs_inc_ref(trans, root, cow, 0);
365 BUG_ON(ret);
366 ret = btrfs_dec_ref(trans, root, buf, 1);
367 BUG_ON(ret);
368 }
369 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400370 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400371 }
372 return 0;
373}
374
375/*
Chris Masond3977122009-01-05 21:25:51 -0500376 * does the dirty work in cow of a single block. The parent block (if
377 * supplied) is updated to point to the new cow copy. The new buffer is marked
378 * dirty and returned locked. If you modify the block it needs to be marked
379 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400380 *
381 * search_start -- an allocation hint for the new block
382 *
Chris Masond3977122009-01-05 21:25:51 -0500383 * empty_size -- a hint that you plan on doing more cow. This is the size in
384 * bytes the allocator should try to find free next to the block it returns.
385 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400386 */
Chris Masond3977122009-01-05 21:25:51 -0500387static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400388 struct btrfs_root *root,
389 struct extent_buffer *buf,
390 struct extent_buffer *parent, int parent_slot,
391 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400392 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400393{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400394 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400395 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500396 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400397 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400398 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400399 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400400
Chris Mason925baed2008-06-25 16:01:30 -0400401 if (*cow_ret == buf)
402 unlock_orig = 1;
403
Chris Masonb9447ef2009-03-09 11:45:38 -0400404 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400405
Chris Mason7bb86312007-12-11 09:25:06 -0500406 WARN_ON(root->ref_cows && trans->transid !=
407 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400408 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400409
Chris Mason7bb86312007-12-11 09:25:06 -0500410 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400411
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400412 if (level == 0)
413 btrfs_item_key(buf, &disk_key, 0);
414 else
415 btrfs_node_key(buf, &disk_key, 0);
416
417 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
418 if (parent)
419 parent_start = parent->start;
420 else
421 parent_start = 0;
422 } else
423 parent_start = 0;
424
425 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
426 root->root_key.objectid, &disk_key,
427 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400428 if (IS_ERR(cow))
429 return PTR_ERR(cow);
430
Chris Masonb4ce94d2009-02-04 09:25:08 -0500431 /* cow is set to blocking by btrfs_init_new_buffer */
432
Chris Mason5f39d392007-10-15 16:14:19 -0400433 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400434 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400435 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
437 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
438 BTRFS_HEADER_FLAG_RELOC);
439 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
440 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
441 else
442 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400443
Yan Zheng2b820322008-11-17 21:11:30 -0500444 write_extent_buffer(cow, root->fs_info->fsid,
445 (unsigned long)btrfs_header_fsid(cow),
446 BTRFS_FSID_SIZE);
447
Yan, Zhengf0486c62010-05-16 10:46:25 -0400448 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400449
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400450 if (root->ref_cows)
451 btrfs_reloc_cow_block(trans, root, buf, cow);
452
Chris Mason6702ed42007-08-07 16:15:09 -0400453 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400454 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400455 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
456 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
457 parent_start = buf->start;
458 else
459 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400460
461 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400462 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400463 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400464 spin_unlock(&root->node_lock);
465
Yan, Zhengf0486c62010-05-16 10:46:25 -0400466 btrfs_free_tree_block(trans, root, buf, parent_start,
467 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400468 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400469 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400470 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400471 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
472 parent_start = parent->start;
473 else
474 parent_start = 0;
475
476 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400477 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400478 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500479 btrfs_set_node_ptr_generation(parent, parent_slot,
480 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400481 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400482 btrfs_free_tree_block(trans, root, buf, parent_start,
483 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400484 }
Chris Mason925baed2008-06-25 16:01:30 -0400485 if (unlock_orig)
486 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400487 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400488 btrfs_mark_buffer_dirty(cow);
489 *cow_ret = cow;
490 return 0;
491}
492
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400493static inline int should_cow_block(struct btrfs_trans_handle *trans,
494 struct btrfs_root *root,
495 struct extent_buffer *buf)
496{
497 if (btrfs_header_generation(buf) == trans->transid &&
498 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
499 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
500 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
501 return 0;
502 return 1;
503}
504
Chris Masond352ac62008-09-29 15:18:18 -0400505/*
506 * cows a single block, see __btrfs_cow_block for the real work.
507 * This version of it has extra checks so that a block isn't cow'd more than
508 * once per transaction, as long as it hasn't been written yet
509 */
Chris Masond3977122009-01-05 21:25:51 -0500510noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400511 struct btrfs_root *root, struct extent_buffer *buf,
512 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400513 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500514{
Chris Mason6702ed42007-08-07 16:15:09 -0400515 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400516 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500517
Chris Masonccd467d2007-06-28 15:57:36 -0400518 if (trans->transaction != root->fs_info->running_transaction) {
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)
Chris Masonccd467d2007-06-28 15:57:36 -0400522 root->fs_info->running_transaction->transid);
523 WARN_ON(1);
524 }
525 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500526 printk(KERN_CRIT "trans %llu running %llu\n",
527 (unsigned long long)trans->transid,
528 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400529 WARN_ON(1);
530 }
Chris Masondc17ff82008-01-08 15:46:30 -0500531
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400532 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500533 *cow_ret = buf;
534 return 0;
535 }
Chris Masonc4876852009-02-04 09:24:25 -0500536
Chris Mason0b86a832008-03-24 15:01:56 -0400537 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500538
539 if (parent)
540 btrfs_set_lock_blocking(parent);
541 btrfs_set_lock_blocking(buf);
542
Chris Masonf510cfe2007-10-15 16:14:48 -0400543 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400544 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400545 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400546}
547
Chris Masond352ac62008-09-29 15:18:18 -0400548/*
549 * helper function for defrag to decide if two blocks pointed to by a
550 * node are actually close by
551 */
Chris Mason6b800532007-10-15 16:17:34 -0400552static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400553{
Chris Mason6b800532007-10-15 16:17:34 -0400554 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400555 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400556 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400557 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500558 return 0;
559}
560
Chris Mason081e9572007-11-06 10:26:24 -0500561/*
562 * compare two keys in a memcmp fashion
563 */
564static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
565{
566 struct btrfs_key k1;
567
568 btrfs_disk_key_to_cpu(&k1, disk);
569
Diego Calleja20736ab2009-07-24 11:06:52 -0400570 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500571}
572
Josef Bacikf3465ca2008-11-12 14:19:50 -0500573/*
574 * same as comp_keys only with two btrfs_key's
575 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400576int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500577{
578 if (k1->objectid > k2->objectid)
579 return 1;
580 if (k1->objectid < k2->objectid)
581 return -1;
582 if (k1->type > k2->type)
583 return 1;
584 if (k1->type < k2->type)
585 return -1;
586 if (k1->offset > k2->offset)
587 return 1;
588 if (k1->offset < k2->offset)
589 return -1;
590 return 0;
591}
Chris Mason081e9572007-11-06 10:26:24 -0500592
Chris Masond352ac62008-09-29 15:18:18 -0400593/*
594 * this is used by the defrag code to go through all the
595 * leaves pointed to by a node and reallocate them so that
596 * disk order is close to key order
597 */
Chris Mason6702ed42007-08-07 16:15:09 -0400598int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400599 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400600 int start_slot, int cache_only, u64 *last_ret,
601 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400602{
Chris Mason6b800532007-10-15 16:17:34 -0400603 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400604 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400605 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400606 u64 search_start = *last_ret;
607 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400608 u64 other;
609 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400610 int end_slot;
611 int i;
612 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400613 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400614 int uptodate;
615 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500616 int progress_passed = 0;
617 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400618
Chris Mason5708b952007-10-25 15:43:18 -0400619 parent_level = btrfs_header_level(parent);
620 if (cache_only && parent_level != 1)
621 return 0;
622
Chris Masond3977122009-01-05 21:25:51 -0500623 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400624 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500625 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400626 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400627
Chris Mason6b800532007-10-15 16:17:34 -0400628 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400629 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400630 end_slot = parent_nritems;
631
632 if (parent_nritems == 1)
633 return 0;
634
Chris Masonb4ce94d2009-02-04 09:25:08 -0500635 btrfs_set_lock_blocking(parent);
636
Chris Mason6702ed42007-08-07 16:15:09 -0400637 for (i = start_slot; i < end_slot; i++) {
638 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400639
Chris Mason5708b952007-10-25 15:43:18 -0400640 if (!parent->map_token) {
641 map_extent_buffer(parent,
642 btrfs_node_key_ptr_offset(i),
643 sizeof(struct btrfs_key_ptr),
644 &parent->map_token, &parent->kaddr,
645 &parent->map_start, &parent->map_len,
646 KM_USER1);
647 }
Chris Mason081e9572007-11-06 10:26:24 -0500648 btrfs_node_key(parent, &disk_key, i);
649 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
650 continue;
651
652 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400653 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400654 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400655 if (last_block == 0)
656 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400657
Chris Mason6702ed42007-08-07 16:15:09 -0400658 if (i > 0) {
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 Mason0ef3e662008-05-24 14:04:53 -0400662 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400663 other = btrfs_node_blockptr(parent, i + 1);
664 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400665 }
Chris Masone9d0b132007-08-10 14:06:19 -0400666 if (close) {
667 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400668 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400669 }
Chris Mason5708b952007-10-25 15:43:18 -0400670 if (parent->map_token) {
671 unmap_extent_buffer(parent, parent->map_token,
672 KM_USER1);
673 parent->map_token = NULL;
674 }
Chris Mason6702ed42007-08-07 16:15:09 -0400675
Chris Mason6b800532007-10-15 16:17:34 -0400676 cur = btrfs_find_tree_block(root, blocknr, blocksize);
677 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400678 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400679 else
680 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400681 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400682 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400683 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400684 continue;
685 }
Chris Mason6b800532007-10-15 16:17:34 -0400686 if (!cur) {
687 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400688 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400689 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400690 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400691 }
Chris Mason6702ed42007-08-07 16:15:09 -0400692 }
Chris Masone9d0b132007-08-10 14:06:19 -0400693 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400694 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400695
Chris Masone7a84562008-06-25 16:01:31 -0400696 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500697 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400698 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400699 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400700 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400701 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400702 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400703 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400704 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400705 break;
Yan252c38f2007-08-29 09:11:44 -0400706 }
Chris Masone7a84562008-06-25 16:01:31 -0400707 search_start = cur->start;
708 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400709 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400710 btrfs_tree_unlock(cur);
711 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400712 }
Chris Mason5708b952007-10-25 15:43:18 -0400713 if (parent->map_token) {
714 unmap_extent_buffer(parent, parent->map_token,
715 KM_USER1);
716 parent->map_token = NULL;
717 }
Chris Mason6702ed42007-08-07 16:15:09 -0400718 return err;
719}
720
Chris Mason74123bd2007-02-02 11:05:29 -0500721/*
722 * The leaf data grows from end-to-front in the node.
723 * this returns the address of the start of the last item,
724 * which is the stop of the leaf data stack
725 */
Chris Mason123abc82007-03-14 14:14:43 -0400726static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400727 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728{
Chris Mason5f39d392007-10-15 16:14:19 -0400729 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500730 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400731 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400732 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500733}
734
Chris Masond352ac62008-09-29 15:18:18 -0400735/*
736 * extra debugging checks to make sure all the items in a key are
737 * well formed and in the proper order
738 */
Chris Mason123abc82007-03-14 14:14:43 -0400739static int check_node(struct btrfs_root *root, struct btrfs_path *path,
740 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500741{
Chris Mason5f39d392007-10-15 16:14:19 -0400742 struct extent_buffer *parent = NULL;
743 struct extent_buffer *node = path->nodes[level];
744 struct btrfs_disk_key parent_key;
745 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500746 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400747 int slot;
748 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400749 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500750
751 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400752 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400753
Chris Mason8d7be552007-05-10 11:24:42 -0400754 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400755 BUG_ON(nritems == 0);
756 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400757 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400758 btrfs_node_key(parent, &parent_key, parent_slot);
759 btrfs_node_key(node, &node_key, 0);
760 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400761 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400762 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400763 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500764 }
Chris Mason123abc82007-03-14 14:14:43 -0400765 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400766 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400767 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
768 btrfs_node_key(node, &node_key, slot);
769 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400770 }
771 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400772 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
773 btrfs_node_key(node, &node_key, slot);
774 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500775 }
776 return 0;
777}
778
Chris Masond352ac62008-09-29 15:18:18 -0400779/*
780 * extra checking to make sure all the items in a leaf are
781 * well formed and in the proper order
782 */
Chris Mason123abc82007-03-14 14:14:43 -0400783static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
784 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500785{
Chris Mason5f39d392007-10-15 16:14:19 -0400786 struct extent_buffer *leaf = path->nodes[level];
787 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500788 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400789 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400790 struct btrfs_disk_key parent_key;
791 struct btrfs_disk_key leaf_key;
792 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400793
Chris Mason5f39d392007-10-15 16:14:19 -0400794 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500795
796 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400797 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400798
799 if (nritems == 0)
800 return 0;
801
802 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400803 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400804 btrfs_node_key(parent, &parent_key, parent_slot);
805 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400806
Chris Mason5f39d392007-10-15 16:14:19 -0400807 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400808 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400809 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400810 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500811 }
Chris Mason5f39d392007-10-15 16:14:19 -0400812 if (slot != 0 && slot < nritems - 1) {
813 btrfs_item_key(leaf, &leaf_key, slot);
814 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
815 if (comp_keys(&leaf_key, &cpukey) <= 0) {
816 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500817 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400818 BUG_ON(1);
819 }
820 if (btrfs_item_offset_nr(leaf, slot - 1) !=
821 btrfs_item_end_nr(leaf, slot)) {
822 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500823 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400824 BUG_ON(1);
825 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500826 }
Chris Mason8d7be552007-05-10 11:24:42 -0400827 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400828 btrfs_item_key(leaf, &leaf_key, slot);
829 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
830 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
831 if (btrfs_item_offset_nr(leaf, slot) !=
832 btrfs_item_end_nr(leaf, slot + 1)) {
833 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500834 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400835 BUG_ON(1);
836 }
Chris Mason8d7be552007-05-10 11:24:42 -0400837 }
Chris Mason5f39d392007-10-15 16:14:19 -0400838 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
839 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500840 return 0;
841}
842
Chris Masond3977122009-01-05 21:25:51 -0500843static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500844 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500845{
Chris Mason85d824c2008-04-10 10:23:19 -0400846 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500847 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400848 return check_leaf(root, path, level);
849 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500850}
851
Chris Mason74123bd2007-02-02 11:05:29 -0500852/*
Chris Mason5f39d392007-10-15 16:14:19 -0400853 * search for key in the extent_buffer. The items start at offset p,
854 * and they are item_size apart. There are 'max' items in p.
855 *
Chris Mason74123bd2007-02-02 11:05:29 -0500856 * the slot in the array is returned via slot, and it points to
857 * the place where you would insert key if it is not found in
858 * the array.
859 *
860 * slot may point to max if the key is bigger than all of the keys
861 */
Chris Masone02119d2008-09-05 16:13:11 -0400862static noinline int generic_bin_search(struct extent_buffer *eb,
863 unsigned long p,
864 int item_size, struct btrfs_key *key,
865 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500866{
867 int low = 0;
868 int high = max;
869 int mid;
870 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400871 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400872 struct btrfs_disk_key unaligned;
873 unsigned long offset;
874 char *map_token = NULL;
875 char *kaddr = NULL;
876 unsigned long map_start = 0;
877 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400878 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500879
Chris Masond3977122009-01-05 21:25:51 -0500880 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500881 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 offset = p + mid * item_size;
883
884 if (!map_token || offset < map_start ||
885 (offset + sizeof(struct btrfs_disk_key)) >
886 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400887 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400888 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400889 map_token = NULL;
890 }
Chris Mason934d3752008-12-08 16:43:10 -0500891
892 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400893 sizeof(struct btrfs_disk_key),
894 &map_token, &kaddr,
895 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400896
Chris Mason479965d2007-10-15 16:14:27 -0400897 if (!err) {
898 tmp = (struct btrfs_disk_key *)(kaddr + offset -
899 map_start);
900 } else {
901 read_extent_buffer(eb, &unaligned,
902 offset, sizeof(unaligned));
903 tmp = &unaligned;
904 }
905
Chris Mason5f39d392007-10-15 16:14:19 -0400906 } else {
907 tmp = (struct btrfs_disk_key *)(kaddr + offset -
908 map_start);
909 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500910 ret = comp_keys(tmp, key);
911
912 if (ret < 0)
913 low = mid + 1;
914 else if (ret > 0)
915 high = mid;
916 else {
917 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400918 if (map_token)
919 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500920 return 0;
921 }
922 }
923 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400924 if (map_token)
925 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500926 return 1;
927}
928
Chris Mason97571fd2007-02-24 13:39:08 -0500929/*
930 * simple bin_search frontend that does the right thing for
931 * leaves vs nodes
932 */
Chris Mason5f39d392007-10-15 16:14:19 -0400933static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
934 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500935{
Chris Mason5f39d392007-10-15 16:14:19 -0400936 if (level == 0) {
937 return generic_bin_search(eb,
938 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400939 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400940 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400941 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500942 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400943 return generic_bin_search(eb,
944 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400945 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400946 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400947 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500948 }
949 return -1;
950}
951
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
953 int level, int *slot)
954{
955 return bin_search(eb, key, level, slot);
956}
957
Yan, Zhengf0486c62010-05-16 10:46:25 -0400958static void root_add_used(struct btrfs_root *root, u32 size)
959{
960 spin_lock(&root->accounting_lock);
961 btrfs_set_root_used(&root->root_item,
962 btrfs_root_used(&root->root_item) + size);
963 spin_unlock(&root->accounting_lock);
964}
965
966static void root_sub_used(struct btrfs_root *root, u32 size)
967{
968 spin_lock(&root->accounting_lock);
969 btrfs_set_root_used(&root->root_item,
970 btrfs_root_used(&root->root_item) - size);
971 spin_unlock(&root->accounting_lock);
972}
973
Chris Masond352ac62008-09-29 15:18:18 -0400974/* given a node and slot number, this reads the blocks it points to. The
975 * extent buffer is returned with a reference taken (but unlocked).
976 * NULL is returned on error.
977 */
Chris Masone02119d2008-09-05 16:13:11 -0400978static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400979 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500980{
Chris Masonca7a79a2008-05-12 12:59:19 -0400981 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500982 if (slot < 0)
983 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400984 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500985 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400986
987 BUG_ON(level == 0);
988
Chris Masondb945352007-10-15 16:15:53 -0400989 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400990 btrfs_level_size(root, level - 1),
991 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500992}
993
Chris Masond352ac62008-09-29 15:18:18 -0400994/*
995 * node level balancing, used to make sure nodes are in proper order for
996 * item deletion. We balance from the top down, so we have to make sure
997 * that a deletion won't leave an node completely empty later on.
998 */
Chris Masone02119d2008-09-05 16:13:11 -0400999static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001000 struct btrfs_root *root,
1001 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001002{
Chris Mason5f39d392007-10-15 16:14:19 -04001003 struct extent_buffer *right = NULL;
1004 struct extent_buffer *mid;
1005 struct extent_buffer *left = NULL;
1006 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001007 int ret = 0;
1008 int wret;
1009 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001010 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001011 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001012
1013 if (level == 0)
1014 return 0;
1015
Chris Mason5f39d392007-10-15 16:14:19 -04001016 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001017
Chris Mason925baed2008-06-25 16:01:30 -04001018 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -05001019 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1020
Chris Mason1d4f8a02007-03-13 09:28:32 -04001021 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001022
Chris Mason234b63a2007-03-13 10:46:10 -04001023 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001024 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001025 pslot = path->slots[level + 1];
1026
Chris Mason40689472007-03-17 14:29:23 -04001027 /*
1028 * deal with the case where there is only one pointer in the root
1029 * by promoting the node below to a root
1030 */
Chris Mason5f39d392007-10-15 16:14:19 -04001031 if (!parent) {
1032 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001033
Chris Mason5f39d392007-10-15 16:14:19 -04001034 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001035 return 0;
1036
1037 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001038 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001039 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001040 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001041 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001042 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001043 if (ret) {
1044 btrfs_tree_unlock(child);
1045 free_extent_buffer(child);
1046 goto enospc;
1047 }
Yan2f375ab2008-02-01 14:58:07 -05001048
Chris Mason925baed2008-06-25 16:01:30 -04001049 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001050 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001051 spin_unlock(&root->node_lock);
1052
Chris Mason0b86a832008-03-24 15:01:56 -04001053 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001054 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001055
Chris Mason925baed2008-06-25 16:01:30 -04001056 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001057 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001058 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001059 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001060 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001061 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001062
1063 root_sub_used(root, mid->len);
1064 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001065 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001066 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001067 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001068 }
Chris Mason5f39d392007-10-15 16:14:19 -04001069 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001070 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001071 return 0;
1072
Andi Kleen559af822010-10-29 15:14:37 -04001073 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001074
Chris Mason5f39d392007-10-15 16:14:19 -04001075 left = read_node_slot(root, parent, pslot - 1);
1076 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001077 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001078 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001079 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001080 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001081 if (wret) {
1082 ret = wret;
1083 goto enospc;
1084 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001085 }
Chris Mason5f39d392007-10-15 16:14:19 -04001086 right = read_node_slot(root, parent, pslot + 1);
1087 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001088 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001089 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001090 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001091 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001092 if (wret) {
1093 ret = wret;
1094 goto enospc;
1095 }
1096 }
1097
1098 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001099 if (left) {
1100 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001101 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001102 if (wret < 0)
1103 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001104 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001105 }
Chris Mason79f95c82007-03-01 15:16:26 -05001106
1107 /*
1108 * then try to empty the right most buffer into the middle
1109 */
Chris Mason5f39d392007-10-15 16:14:19 -04001110 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001111 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001112 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001113 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001114 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001115 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001116 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -04001117 wret = del_ptr(trans, root, path, level + 1, pslot +
1118 1);
Chris Masonbb803952007-03-01 12:04:21 -05001119 if (wret)
1120 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001121 root_sub_used(root, right->len);
1122 btrfs_free_tree_block(trans, root, right, 0, 1);
1123 free_extent_buffer(right);
1124 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001125 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001126 struct btrfs_disk_key right_key;
1127 btrfs_node_key(right, &right_key, 0);
1128 btrfs_set_node_key(parent, &right_key, pslot + 1);
1129 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001130 }
1131 }
Chris Mason5f39d392007-10-15 16:14:19 -04001132 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001133 /*
1134 * we're not allowed to leave a node with one item in the
1135 * tree during a delete. A deletion from lower in the tree
1136 * could try to delete the only pointer in this node.
1137 * So, pull some keys from the left.
1138 * There has to be a left pointer at this point because
1139 * otherwise we would have pulled some pointers from the
1140 * right
1141 */
Chris Mason5f39d392007-10-15 16:14:19 -04001142 BUG_ON(!left);
1143 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001144 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001145 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001146 goto enospc;
1147 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001148 if (wret == 1) {
1149 wret = push_node_left(trans, root, left, mid, 1);
1150 if (wret < 0)
1151 ret = wret;
1152 }
Chris Mason79f95c82007-03-01 15:16:26 -05001153 BUG_ON(wret == 1);
1154 }
Chris Mason5f39d392007-10-15 16:14:19 -04001155 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001156 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001157 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001158 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001159 if (wret)
1160 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001161 root_sub_used(root, mid->len);
1162 btrfs_free_tree_block(trans, root, mid, 0, 1);
1163 free_extent_buffer(mid);
1164 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001165 } else {
1166 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001167 struct btrfs_disk_key mid_key;
1168 btrfs_node_key(mid, &mid_key, 0);
1169 btrfs_set_node_key(parent, &mid_key, pslot);
1170 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001171 }
Chris Masonbb803952007-03-01 12:04:21 -05001172
Chris Mason79f95c82007-03-01 15:16:26 -05001173 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001174 if (left) {
1175 if (btrfs_header_nritems(left) > orig_slot) {
1176 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001177 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001178 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001179 path->slots[level + 1] -= 1;
1180 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001181 if (mid) {
1182 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001183 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001184 }
Chris Masonbb803952007-03-01 12:04:21 -05001185 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001186 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001187 path->slots[level] = orig_slot;
1188 }
1189 }
Chris Mason79f95c82007-03-01 15:16:26 -05001190 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001191 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001192 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001193 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001194 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001195enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001196 if (right) {
1197 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001198 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001199 }
1200 if (left) {
1201 if (path->nodes[level] != left)
1202 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001203 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001204 }
Chris Masonbb803952007-03-01 12:04:21 -05001205 return ret;
1206}
1207
Chris Masond352ac62008-09-29 15:18:18 -04001208/* Node balancing for insertion. Here we only split or push nodes around
1209 * when they are completely full. This is also done top down, so we
1210 * have to be pessimistic.
1211 */
Chris Masond3977122009-01-05 21:25:51 -05001212static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001213 struct btrfs_root *root,
1214 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001215{
Chris Mason5f39d392007-10-15 16:14:19 -04001216 struct extent_buffer *right = NULL;
1217 struct extent_buffer *mid;
1218 struct extent_buffer *left = NULL;
1219 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001220 int ret = 0;
1221 int wret;
1222 int pslot;
1223 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001224
1225 if (level == 0)
1226 return 1;
1227
Chris Mason5f39d392007-10-15 16:14:19 -04001228 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001229 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001230
1231 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001232 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001233 pslot = path->slots[level + 1];
1234
Chris Mason5f39d392007-10-15 16:14:19 -04001235 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001236 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001237
Chris Mason5f39d392007-10-15 16:14:19 -04001238 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001239
1240 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001241 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001242 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001243
1244 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001245 btrfs_set_lock_blocking(left);
1246
Chris Mason5f39d392007-10-15 16:14:19 -04001247 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001248 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1249 wret = 1;
1250 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001251 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001252 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001253 if (ret)
1254 wret = 1;
1255 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001256 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001257 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001258 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001259 }
Chris Masone66f7092007-04-20 13:16:02 -04001260 if (wret < 0)
1261 ret = wret;
1262 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001263 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001264 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001265 btrfs_node_key(mid, &disk_key, 0);
1266 btrfs_set_node_key(parent, &disk_key, pslot);
1267 btrfs_mark_buffer_dirty(parent);
1268 if (btrfs_header_nritems(left) > orig_slot) {
1269 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001270 path->slots[level + 1] -= 1;
1271 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001272 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001273 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001274 } else {
1275 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001276 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001277 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001278 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001279 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001280 }
Chris Masone66f7092007-04-20 13:16:02 -04001281 return 0;
1282 }
Chris Mason925baed2008-06-25 16:01:30 -04001283 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001284 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001285 }
Chris Mason925baed2008-06-25 16:01:30 -04001286 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001287
1288 /*
1289 * then try to empty the right most buffer into the middle
1290 */
Chris Mason5f39d392007-10-15 16:14:19 -04001291 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001292 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001293
Chris Mason925baed2008-06-25 16:01:30 -04001294 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001295 btrfs_set_lock_blocking(right);
1296
Chris Mason5f39d392007-10-15 16:14:19 -04001297 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001298 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1299 wret = 1;
1300 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001301 ret = btrfs_cow_block(trans, root, right,
1302 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001303 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001304 if (ret)
1305 wret = 1;
1306 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001307 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001308 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001309 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001310 }
Chris Masone66f7092007-04-20 13:16:02 -04001311 if (wret < 0)
1312 ret = wret;
1313 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001314 struct btrfs_disk_key disk_key;
1315
1316 btrfs_node_key(right, &disk_key, 0);
1317 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1318 btrfs_mark_buffer_dirty(parent);
1319
1320 if (btrfs_header_nritems(mid) <= orig_slot) {
1321 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001322 path->slots[level + 1] += 1;
1323 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001324 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001325 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001326 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001327 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001328 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001329 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001330 }
Chris Masone66f7092007-04-20 13:16:02 -04001331 return 0;
1332 }
Chris Mason925baed2008-06-25 16:01:30 -04001333 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001334 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001335 }
Chris Masone66f7092007-04-20 13:16:02 -04001336 return 1;
1337}
1338
Chris Mason74123bd2007-02-02 11:05:29 -05001339/*
Chris Masond352ac62008-09-29 15:18:18 -04001340 * readahead one full node of leaves, finding things that are close
1341 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001342 */
Chris Masonc8c42862009-04-03 10:14:18 -04001343static void reada_for_search(struct btrfs_root *root,
1344 struct btrfs_path *path,
1345 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001346{
Chris Mason5f39d392007-10-15 16:14:19 -04001347 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001348 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001349 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001350 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001351 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001352 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001353 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001354 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001355 u32 nr;
1356 u32 blocksize;
1357 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001358
Chris Masona6b6e752007-10-15 16:22:39 -04001359 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001360 return;
1361
Chris Mason6702ed42007-08-07 16:15:09 -04001362 if (!path->nodes[level])
1363 return;
1364
Chris Mason5f39d392007-10-15 16:14:19 -04001365 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001366
Chris Mason3c69fae2007-08-07 15:52:22 -04001367 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001368 blocksize = btrfs_level_size(root, level - 1);
1369 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001370 if (eb) {
1371 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001372 return;
1373 }
1374
Chris Masona7175312009-01-22 09:23:10 -05001375 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001376
Chris Mason5f39d392007-10-15 16:14:19 -04001377 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001378 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001379 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001380 if (direction < 0) {
1381 if (nr == 0)
1382 break;
1383 nr--;
1384 } else if (direction > 0) {
1385 nr++;
1386 if (nr >= nritems)
1387 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001388 }
Chris Mason01f46652007-12-21 16:24:26 -05001389 if (path->reada < 0 && objectid) {
1390 btrfs_node_key(node, &disk_key, nr);
1391 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1392 break;
1393 }
Chris Mason6b800532007-10-15 16:17:34 -04001394 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001395 if ((search <= target && target - search <= 65536) ||
1396 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001397 readahead_tree_block(root, search, blocksize,
1398 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001399 nread += blocksize;
1400 }
1401 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001402 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001403 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001404 }
1405}
Chris Mason925baed2008-06-25 16:01:30 -04001406
Chris Masond352ac62008-09-29 15:18:18 -04001407/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001408 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1409 * cache
1410 */
1411static noinline int reada_for_balance(struct btrfs_root *root,
1412 struct btrfs_path *path, int level)
1413{
1414 int slot;
1415 int nritems;
1416 struct extent_buffer *parent;
1417 struct extent_buffer *eb;
1418 u64 gen;
1419 u64 block1 = 0;
1420 u64 block2 = 0;
1421 int ret = 0;
1422 int blocksize;
1423
Chris Mason8c594ea2009-04-20 15:50:10 -04001424 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001425 if (!parent)
1426 return 0;
1427
1428 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001429 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001430 blocksize = btrfs_level_size(root, level);
1431
1432 if (slot > 0) {
1433 block1 = btrfs_node_blockptr(parent, slot - 1);
1434 gen = btrfs_node_ptr_generation(parent, slot - 1);
1435 eb = btrfs_find_tree_block(root, block1, blocksize);
1436 if (eb && btrfs_buffer_uptodate(eb, gen))
1437 block1 = 0;
1438 free_extent_buffer(eb);
1439 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001440 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001441 block2 = btrfs_node_blockptr(parent, slot + 1);
1442 gen = btrfs_node_ptr_generation(parent, slot + 1);
1443 eb = btrfs_find_tree_block(root, block2, blocksize);
1444 if (eb && btrfs_buffer_uptodate(eb, gen))
1445 block2 = 0;
1446 free_extent_buffer(eb);
1447 }
1448 if (block1 || block2) {
1449 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001450
1451 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001452 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001453
1454 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001455 if (block1)
1456 readahead_tree_block(root, block1, blocksize, 0);
1457 if (block2)
1458 readahead_tree_block(root, block2, blocksize, 0);
1459
1460 if (block1) {
1461 eb = read_tree_block(root, block1, blocksize, 0);
1462 free_extent_buffer(eb);
1463 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001464 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001465 eb = read_tree_block(root, block2, blocksize, 0);
1466 free_extent_buffer(eb);
1467 }
1468 }
1469 return ret;
1470}
1471
1472
1473/*
Chris Masond3977122009-01-05 21:25:51 -05001474 * when we walk down the tree, it is usually safe to unlock the higher layers
1475 * in the tree. The exceptions are when our path goes through slot 0, because
1476 * operations on the tree might require changing key pointers higher up in the
1477 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001478 *
Chris Masond3977122009-01-05 21:25:51 -05001479 * callers might also have set path->keep_locks, which tells this code to keep
1480 * the lock if the path points to the last slot in the block. This is part of
1481 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001482 *
Chris Masond3977122009-01-05 21:25:51 -05001483 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1484 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001485 */
Chris Masone02119d2008-09-05 16:13:11 -04001486static noinline void unlock_up(struct btrfs_path *path, int level,
1487 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001488{
1489 int i;
1490 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001491 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001492 struct extent_buffer *t;
1493
1494 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1495 if (!path->nodes[i])
1496 break;
1497 if (!path->locks[i])
1498 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001499 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001500 skip_level = i + 1;
1501 continue;
1502 }
Chris Mason051e1b92008-06-25 16:01:30 -04001503 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001504 u32 nritems;
1505 t = path->nodes[i];
1506 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001507 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001508 skip_level = i + 1;
1509 continue;
1510 }
1511 }
Chris Mason051e1b92008-06-25 16:01:30 -04001512 if (skip_level < i && i >= lowest_unlock)
1513 no_skips = 1;
1514
Chris Mason925baed2008-06-25 16:01:30 -04001515 t = path->nodes[i];
1516 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1517 btrfs_tree_unlock(t);
1518 path->locks[i] = 0;
1519 }
1520 }
1521}
1522
Chris Mason3c69fae2007-08-07 15:52:22 -04001523/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001524 * This releases any locks held in the path starting at level and
1525 * going all the way up to the root.
1526 *
1527 * btrfs_search_slot will keep the lock held on higher nodes in a few
1528 * corner cases, such as COW of the block at slot zero in the node. This
1529 * ignores those rules, and it should only be called when there are no
1530 * more updates to be done higher up in the tree.
1531 */
1532noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1533{
1534 int i;
1535
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001536 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001537 return;
1538
1539 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1540 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001541 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001542 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001543 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001544 btrfs_tree_unlock(path->nodes[i]);
1545 path->locks[i] = 0;
1546 }
1547}
1548
1549/*
Chris Masonc8c42862009-04-03 10:14:18 -04001550 * helper function for btrfs_search_slot. The goal is to find a block
1551 * in cache without setting the path to blocking. If we find the block
1552 * we return zero and the path is unchanged.
1553 *
1554 * If we can't find the block, we set the path blocking and do some
1555 * reada. -EAGAIN is returned and the search must be repeated.
1556 */
1557static int
1558read_block_for_search(struct btrfs_trans_handle *trans,
1559 struct btrfs_root *root, struct btrfs_path *p,
1560 struct extent_buffer **eb_ret, int level, int slot,
1561 struct btrfs_key *key)
1562{
1563 u64 blocknr;
1564 u64 gen;
1565 u32 blocksize;
1566 struct extent_buffer *b = *eb_ret;
1567 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001568 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001569
1570 blocknr = btrfs_node_blockptr(b, slot);
1571 gen = btrfs_node_ptr_generation(b, slot);
1572 blocksize = btrfs_level_size(root, level - 1);
1573
1574 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001575 if (tmp) {
1576 if (btrfs_buffer_uptodate(tmp, 0)) {
1577 if (btrfs_buffer_uptodate(tmp, gen)) {
1578 /*
1579 * we found an up to date block without
1580 * sleeping, return
1581 * right away
1582 */
1583 *eb_ret = tmp;
1584 return 0;
1585 }
1586 /* the pages were up to date, but we failed
1587 * the generation number check. Do a full
1588 * read for the generation number that is correct.
1589 * We must do this without dropping locks so
1590 * we can trust our generation number
1591 */
1592 free_extent_buffer(tmp);
1593 tmp = read_tree_block(root, blocknr, blocksize, gen);
1594 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1595 *eb_ret = tmp;
1596 return 0;
1597 }
1598 free_extent_buffer(tmp);
1599 btrfs_release_path(NULL, p);
1600 return -EIO;
1601 }
Chris Masonc8c42862009-04-03 10:14:18 -04001602 }
1603
1604 /*
1605 * reduce lock contention at high levels
1606 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001607 * we read. Don't release the lock on the current
1608 * level because we need to walk this node to figure
1609 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001610 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001611 btrfs_unlock_up_safe(p, level + 1);
1612 btrfs_set_path_blocking(p);
1613
Chris Masoncb449212010-10-24 11:01:27 -04001614 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001615 if (p->reada)
1616 reada_for_search(root, p, level, slot, key->objectid);
1617
Chris Mason8c594ea2009-04-20 15:50:10 -04001618 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001619
1620 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001621 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001622 if (tmp) {
1623 /*
1624 * If the read above didn't mark this buffer up to date,
1625 * it will never end up being up to date. Set ret to EIO now
1626 * and give up so that our caller doesn't loop forever
1627 * on our EAGAINs.
1628 */
1629 if (!btrfs_buffer_uptodate(tmp, 0))
1630 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001631 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001632 }
1633 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001634}
1635
1636/*
1637 * helper function for btrfs_search_slot. This does all of the checks
1638 * for node-level blocks and does any balancing required based on
1639 * the ins_len.
1640 *
1641 * If no extra work was required, zero is returned. If we had to
1642 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1643 * start over
1644 */
1645static int
1646setup_nodes_for_search(struct btrfs_trans_handle *trans,
1647 struct btrfs_root *root, struct btrfs_path *p,
1648 struct extent_buffer *b, int level, int ins_len)
1649{
1650 int ret;
1651 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1652 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1653 int sret;
1654
1655 sret = reada_for_balance(root, p, level);
1656 if (sret)
1657 goto again;
1658
1659 btrfs_set_path_blocking(p);
1660 sret = split_node(trans, root, p, level);
1661 btrfs_clear_path_blocking(p, NULL);
1662
1663 BUG_ON(sret > 0);
1664 if (sret) {
1665 ret = sret;
1666 goto done;
1667 }
1668 b = p->nodes[level];
1669 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001670 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001671 int sret;
1672
1673 sret = reada_for_balance(root, p, level);
1674 if (sret)
1675 goto again;
1676
1677 btrfs_set_path_blocking(p);
1678 sret = balance_level(trans, root, p, level);
1679 btrfs_clear_path_blocking(p, NULL);
1680
1681 if (sret) {
1682 ret = sret;
1683 goto done;
1684 }
1685 b = p->nodes[level];
1686 if (!b) {
1687 btrfs_release_path(NULL, p);
1688 goto again;
1689 }
1690 BUG_ON(btrfs_header_nritems(b) == 1);
1691 }
1692 return 0;
1693
1694again:
1695 ret = -EAGAIN;
1696done:
1697 return ret;
1698}
1699
1700/*
Chris Mason74123bd2007-02-02 11:05:29 -05001701 * look for key in the tree. path is filled in with nodes along the way
1702 * if key is found, we return zero and you can find the item in the leaf
1703 * level of the path (level 0)
1704 *
1705 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001706 * be inserted, and 1 is returned. If there are other errors during the
1707 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001708 *
1709 * if ins_len > 0, nodes and leaves will be split as we walk down the
1710 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1711 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001712 */
Chris Masone089f052007-03-16 16:20:31 -04001713int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1714 *root, struct btrfs_key *key, struct btrfs_path *p, int
1715 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001716{
Chris Mason5f39d392007-10-15 16:14:19 -04001717 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001718 int slot;
1719 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001720 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001721 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001722 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001723 u8 lowest_level = 0;
1724
Chris Mason6702ed42007-08-07 16:15:09 -04001725 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001726 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001727 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001728
Chris Mason925baed2008-06-25 16:01:30 -04001729 if (ins_len < 0)
1730 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001731
Chris Masonbb803952007-03-01 12:04:21 -05001732again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001733 if (p->search_commit_root) {
1734 b = root->commit_root;
1735 extent_buffer_get(b);
1736 if (!p->skip_locking)
1737 btrfs_tree_lock(b);
1738 } else {
1739 if (p->skip_locking)
1740 b = btrfs_root_node(root);
1741 else
1742 b = btrfs_lock_root_node(root);
1743 }
Chris Mason925baed2008-06-25 16:01:30 -04001744
Chris Masoneb60cea2007-02-02 09:18:22 -05001745 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001746 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001747
1748 /*
1749 * setup the path here so we can release it under lock
1750 * contention with the cow code
1751 */
1752 p->nodes[level] = b;
1753 if (!p->skip_locking)
1754 p->locks[level] = 1;
1755
Chris Mason02217ed2007-03-02 16:08:05 -05001756 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001757 /*
1758 * if we don't really need to cow this block
1759 * then we don't want to set the path blocking,
1760 * so we test it here
1761 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001762 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001763 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001764
Chris Masonb4ce94d2009-02-04 09:25:08 -05001765 btrfs_set_path_blocking(p);
1766
Yan Zheng33c66f42009-07-22 09:59:00 -04001767 err = btrfs_cow_block(trans, root, b,
1768 p->nodes[level + 1],
1769 p->slots[level + 1], &b);
1770 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001771 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001772 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001773 }
Chris Mason02217ed2007-03-02 16:08:05 -05001774 }
Chris Mason65b51a02008-08-01 15:11:20 -04001775cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001776 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001777 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001778 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001779 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001780
Chris Masoneb60cea2007-02-02 09:18:22 -05001781 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001782 if (!p->skip_locking)
1783 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001784
Chris Mason4008c042009-02-12 14:09:45 -05001785 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001786
1787 /*
1788 * we have a lock on b and as long as we aren't changing
1789 * the tree, there is no way to for the items in b to change.
1790 * It is safe to drop the lock on our parent before we
1791 * go through the expensive btree search on b.
1792 *
1793 * If cow is true, then we might be changing slot zero,
1794 * which may require changing the parent. So, we can't
1795 * drop the lock until after we know which slot we're
1796 * operating on.
1797 */
1798 if (!cow)
1799 btrfs_unlock_up_safe(p, level + 1);
1800
Chris Mason123abc82007-03-14 14:14:43 -04001801 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001802 if (ret) {
1803 ret = -1;
1804 goto done;
1805 }
Chris Mason925baed2008-06-25 16:01:30 -04001806
Chris Mason5f39d392007-10-15 16:14:19 -04001807 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001808
Chris Mason5f39d392007-10-15 16:14:19 -04001809 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001810 int dec = 0;
1811 if (ret && slot > 0) {
1812 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001813 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001814 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001815 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001816 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001817 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001818 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001819 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001820 if (err) {
1821 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001822 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001823 }
Chris Masonc8c42862009-04-03 10:14:18 -04001824 b = p->nodes[level];
1825 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001826
Chris Masonf9efa9c2008-06-25 16:14:04 -04001827 unlock_up(p, level, lowest_unlock);
1828
Chris Mason925baed2008-06-25 16:01:30 -04001829 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001830 if (dec)
1831 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001832 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001833 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001834
Yan Zheng33c66f42009-07-22 09:59:00 -04001835 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001836 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001837 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001838 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001839 if (err) {
1840 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001841 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001842 }
Chris Mason76a05b32009-05-14 13:24:30 -04001843
Chris Masonb4ce94d2009-02-04 09:25:08 -05001844 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001845 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001846 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001847
Yan Zheng33c66f42009-07-22 09:59:00 -04001848 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001849 btrfs_set_path_blocking(p);
1850 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001851 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001852 }
1853 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001854 } else {
1855 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001856 if (ins_len > 0 &&
1857 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001858 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001859 err = split_leaf(trans, root, key,
1860 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001861 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001862
Yan Zheng33c66f42009-07-22 09:59:00 -04001863 BUG_ON(err > 0);
1864 if (err) {
1865 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001866 goto done;
1867 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001868 }
Chris Mason459931e2008-12-10 09:10:46 -05001869 if (!p->search_for_split)
1870 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001871 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001872 }
1873 }
Chris Mason65b51a02008-08-01 15:11:20 -04001874 ret = 1;
1875done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001876 /*
1877 * we don't really know what they plan on doing with the path
1878 * from here on, so for now just mark it as blocking
1879 */
Chris Masonb9473432009-03-13 11:00:37 -04001880 if (!p->leave_spinning)
1881 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001882 if (ret < 0)
1883 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001884 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001885}
1886
Chris Mason74123bd2007-02-02 11:05:29 -05001887/*
1888 * adjust the pointers going up the tree, starting at level
1889 * making sure the right key of each node is points to 'key'.
1890 * This is used after shifting pointers to the left, so it stops
1891 * fixing up pointers when a given leaf/node is not in slot 0 of the
1892 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001893 *
1894 * If this fails to write a tree block, it returns -1, but continues
1895 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001896 */
Chris Mason5f39d392007-10-15 16:14:19 -04001897static int fixup_low_keys(struct btrfs_trans_handle *trans,
1898 struct btrfs_root *root, struct btrfs_path *path,
1899 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001900{
1901 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001902 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001903 struct extent_buffer *t;
1904
Chris Mason234b63a2007-03-13 10:46:10 -04001905 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001906 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001907 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001908 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001909 t = path->nodes[i];
1910 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001911 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001912 if (tslot != 0)
1913 break;
1914 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001915 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001916}
1917
Chris Mason74123bd2007-02-02 11:05:29 -05001918/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001919 * update item key.
1920 *
1921 * This function isn't completely safe. It's the caller's responsibility
1922 * that the new key won't break the order
1923 */
1924int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1925 struct btrfs_root *root, struct btrfs_path *path,
1926 struct btrfs_key *new_key)
1927{
1928 struct btrfs_disk_key disk_key;
1929 struct extent_buffer *eb;
1930 int slot;
1931
1932 eb = path->nodes[0];
1933 slot = path->slots[0];
1934 if (slot > 0) {
1935 btrfs_item_key(eb, &disk_key, slot - 1);
1936 if (comp_keys(&disk_key, new_key) >= 0)
1937 return -1;
1938 }
1939 if (slot < btrfs_header_nritems(eb) - 1) {
1940 btrfs_item_key(eb, &disk_key, slot + 1);
1941 if (comp_keys(&disk_key, new_key) <= 0)
1942 return -1;
1943 }
1944
1945 btrfs_cpu_key_to_disk(&disk_key, new_key);
1946 btrfs_set_item_key(eb, &disk_key, slot);
1947 btrfs_mark_buffer_dirty(eb);
1948 if (slot == 0)
1949 fixup_low_keys(trans, root, path, &disk_key, 1);
1950 return 0;
1951}
1952
1953/*
Chris Mason74123bd2007-02-02 11:05:29 -05001954 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001955 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001956 *
1957 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1958 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001959 */
Chris Mason98ed5172008-01-03 10:01:48 -05001960static int push_node_left(struct btrfs_trans_handle *trans,
1961 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001962 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001963{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001964 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001965 int src_nritems;
1966 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001967 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001968
Chris Mason5f39d392007-10-15 16:14:19 -04001969 src_nritems = btrfs_header_nritems(src);
1970 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001971 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001972 WARN_ON(btrfs_header_generation(src) != trans->transid);
1973 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001974
Chris Masonbce4eae2008-04-24 14:42:46 -04001975 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001976 return 1;
1977
Chris Masond3977122009-01-05 21:25:51 -05001978 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001979 return 1;
1980
Chris Masonbce4eae2008-04-24 14:42:46 -04001981 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001982 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001983 if (push_items < src_nritems) {
1984 /* leave at least 8 pointers in the node if
1985 * we aren't going to empty it
1986 */
1987 if (src_nritems - push_items < 8) {
1988 if (push_items <= 8)
1989 return 1;
1990 push_items -= 8;
1991 }
1992 }
1993 } else
1994 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001995
Chris Mason5f39d392007-10-15 16:14:19 -04001996 copy_extent_buffer(dst, src,
1997 btrfs_node_key_ptr_offset(dst_nritems),
1998 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001999 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002000
Chris Masonbb803952007-03-01 12:04:21 -05002001 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002002 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2003 btrfs_node_key_ptr_offset(push_items),
2004 (src_nritems - push_items) *
2005 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002006 }
Chris Mason5f39d392007-10-15 16:14:19 -04002007 btrfs_set_header_nritems(src, src_nritems - push_items);
2008 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2009 btrfs_mark_buffer_dirty(src);
2010 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002011
Chris Masonbb803952007-03-01 12:04:21 -05002012 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002013}
2014
Chris Mason97571fd2007-02-24 13:39:08 -05002015/*
Chris Mason79f95c82007-03-01 15:16:26 -05002016 * try to push data from one node into the next node right in the
2017 * tree.
2018 *
2019 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2020 * error, and > 0 if there was no room in the right hand block.
2021 *
2022 * this will only push up to 1/2 the contents of the left node over
2023 */
Chris Mason5f39d392007-10-15 16:14:19 -04002024static int balance_node_right(struct btrfs_trans_handle *trans,
2025 struct btrfs_root *root,
2026 struct extent_buffer *dst,
2027 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002028{
Chris Mason79f95c82007-03-01 15:16:26 -05002029 int push_items = 0;
2030 int max_push;
2031 int src_nritems;
2032 int dst_nritems;
2033 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002034
Chris Mason7bb86312007-12-11 09:25:06 -05002035 WARN_ON(btrfs_header_generation(src) != trans->transid);
2036 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2037
Chris Mason5f39d392007-10-15 16:14:19 -04002038 src_nritems = btrfs_header_nritems(src);
2039 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002040 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002041 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002042 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002043
Chris Masond3977122009-01-05 21:25:51 -05002044 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002045 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002046
2047 max_push = src_nritems / 2 + 1;
2048 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002049 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002050 return 1;
Yan252c38f2007-08-29 09:11:44 -04002051
Chris Mason79f95c82007-03-01 15:16:26 -05002052 if (max_push < push_items)
2053 push_items = max_push;
2054
Chris Mason5f39d392007-10-15 16:14:19 -04002055 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2056 btrfs_node_key_ptr_offset(0),
2057 (dst_nritems) *
2058 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002059
Chris Mason5f39d392007-10-15 16:14:19 -04002060 copy_extent_buffer(dst, src,
2061 btrfs_node_key_ptr_offset(0),
2062 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002063 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002064
Chris Mason5f39d392007-10-15 16:14:19 -04002065 btrfs_set_header_nritems(src, src_nritems - push_items);
2066 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002067
Chris Mason5f39d392007-10-15 16:14:19 -04002068 btrfs_mark_buffer_dirty(src);
2069 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002070
Chris Mason79f95c82007-03-01 15:16:26 -05002071 return ret;
2072}
2073
2074/*
Chris Mason97571fd2007-02-24 13:39:08 -05002075 * helper function to insert a new root level in the tree.
2076 * A new node is allocated, and a single item is inserted to
2077 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002078 *
2079 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002080 */
Chris Masond3977122009-01-05 21:25:51 -05002081static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002082 struct btrfs_root *root,
2083 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002084{
Chris Mason7bb86312007-12-11 09:25:06 -05002085 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002086 struct extent_buffer *lower;
2087 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002088 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002089 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002090
2091 BUG_ON(path->nodes[level]);
2092 BUG_ON(path->nodes[level-1] != root->node);
2093
Chris Mason7bb86312007-12-11 09:25:06 -05002094 lower = path->nodes[level-1];
2095 if (level == 1)
2096 btrfs_item_key(lower, &lower_key, 0);
2097 else
2098 btrfs_node_key(lower, &lower_key, 0);
2099
Zheng Yan31840ae2008-09-23 13:14:14 -04002100 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002101 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002102 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002103 if (IS_ERR(c))
2104 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002105
Yan, Zhengf0486c62010-05-16 10:46:25 -04002106 root_add_used(root, root->nodesize);
2107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002108 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002109 btrfs_set_header_nritems(c, 1);
2110 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002111 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002112 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002114 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002115
Chris Mason5f39d392007-10-15 16:14:19 -04002116 write_extent_buffer(c, root->fs_info->fsid,
2117 (unsigned long)btrfs_header_fsid(c),
2118 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002119
2120 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2121 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2122 BTRFS_UUID_SIZE);
2123
Chris Mason5f39d392007-10-15 16:14:19 -04002124 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002125 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002126 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002127 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002128
2129 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002130
2131 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002132
Chris Mason925baed2008-06-25 16:01:30 -04002133 spin_lock(&root->node_lock);
2134 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002135 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002136 spin_unlock(&root->node_lock);
2137
2138 /* the super has an extra ref to root->node */
2139 free_extent_buffer(old);
2140
Chris Mason0b86a832008-03-24 15:01:56 -04002141 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 extent_buffer_get(c);
2143 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002144 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002145 path->slots[level] = 0;
2146 return 0;
2147}
2148
Chris Mason74123bd2007-02-02 11:05:29 -05002149/*
2150 * worker function to insert a single pointer in a node.
2151 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002152 *
Chris Mason74123bd2007-02-02 11:05:29 -05002153 * slot and level indicate where you want the key to go, and
2154 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002155 *
2156 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002157 */
Chris Masone089f052007-03-16 16:20:31 -04002158static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2159 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002160 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002161{
Chris Mason5f39d392007-10-15 16:14:19 -04002162 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002163 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002164
2165 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002166 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002167 lower = path->nodes[level];
2168 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002169 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002170 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002171 BUG();
2172 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002173 memmove_extent_buffer(lower,
2174 btrfs_node_key_ptr_offset(slot + 1),
2175 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002176 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002177 }
Chris Mason5f39d392007-10-15 16:14:19 -04002178 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002179 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002180 WARN_ON(trans->transid == 0);
2181 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 btrfs_set_header_nritems(lower, nritems + 1);
2183 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002184 return 0;
2185}
2186
Chris Mason97571fd2007-02-24 13:39:08 -05002187/*
2188 * split the node at the specified level in path in two.
2189 * The path is corrected to point to the appropriate node after the split
2190 *
2191 * Before splitting this tries to make some room in the node by pushing
2192 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002193 *
2194 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002195 */
Chris Masone02119d2008-09-05 16:13:11 -04002196static noinline int split_node(struct btrfs_trans_handle *trans,
2197 struct btrfs_root *root,
2198 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002199{
Chris Mason5f39d392007-10-15 16:14:19 -04002200 struct extent_buffer *c;
2201 struct extent_buffer *split;
2202 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002203 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002204 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002205 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002206 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002207
Chris Mason5f39d392007-10-15 16:14:19 -04002208 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002209 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002210 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002211 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002212 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002213 if (ret)
2214 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002215 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002216 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002217 c = path->nodes[level];
2218 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002219 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002220 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002221 if (ret < 0)
2222 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002223 }
Chris Masone66f7092007-04-20 13:16:02 -04002224
Chris Mason5f39d392007-10-15 16:14:19 -04002225 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002226 mid = (c_nritems + 1) / 2;
2227 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002228
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002229 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002230 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002231 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002232 if (IS_ERR(split))
2233 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002234
Yan, Zhengf0486c62010-05-16 10:46:25 -04002235 root_add_used(root, root->nodesize);
2236
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002237 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002238 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002239 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002240 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002242 btrfs_set_header_owner(split, root->root_key.objectid);
2243 write_extent_buffer(split, root->fs_info->fsid,
2244 (unsigned long)btrfs_header_fsid(split),
2245 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002246 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2247 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2248 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002249
Chris Mason5f39d392007-10-15 16:14:19 -04002250
2251 copy_extent_buffer(split, c,
2252 btrfs_node_key_ptr_offset(0),
2253 btrfs_node_key_ptr_offset(mid),
2254 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2255 btrfs_set_header_nritems(split, c_nritems - mid);
2256 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002257 ret = 0;
2258
Chris Mason5f39d392007-10-15 16:14:19 -04002259 btrfs_mark_buffer_dirty(c);
2260 btrfs_mark_buffer_dirty(split);
2261
Chris Masondb945352007-10-15 16:15:53 -04002262 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002263 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002264 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002265 if (wret)
2266 ret = wret;
2267
Chris Mason5de08d72007-02-24 06:24:44 -05002268 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002269 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002270 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002271 free_extent_buffer(c);
2272 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002273 path->slots[level + 1] += 1;
2274 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002275 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002276 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002277 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002278 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002279}
2280
Chris Mason74123bd2007-02-02 11:05:29 -05002281/*
2282 * how many bytes are required to store the items in a leaf. start
2283 * and nr indicate which items in the leaf to check. This totals up the
2284 * space used both by the item structs and the item data
2285 */
Chris Mason5f39d392007-10-15 16:14:19 -04002286static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002287{
2288 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002289 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002290 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002291
2292 if (!nr)
2293 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002294 data_len = btrfs_item_end_nr(l, start);
2295 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002296 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002297 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002298 return data_len;
2299}
2300
Chris Mason74123bd2007-02-02 11:05:29 -05002301/*
Chris Masond4dbff92007-04-04 14:08:15 -04002302 * The space between the end of the leaf items and
2303 * the start of the leaf data. IOW, how much room
2304 * the leaf has left for both items and data
2305 */
Chris Masond3977122009-01-05 21:25:51 -05002306noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002307 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002308{
Chris Mason5f39d392007-10-15 16:14:19 -04002309 int nritems = btrfs_header_nritems(leaf);
2310 int ret;
2311 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2312 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002313 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2314 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002315 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002316 leaf_space_used(leaf, 0, nritems), nritems);
2317 }
2318 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002319}
2320
Chris Mason99d8f832010-07-07 10:51:48 -04002321/*
2322 * min slot controls the lowest index we're willing to push to the
2323 * right. We'll push up to and including min_slot, but no lower
2324 */
Chris Mason44871b12009-03-13 10:04:31 -04002325static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2326 struct btrfs_root *root,
2327 struct btrfs_path *path,
2328 int data_size, int empty,
2329 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002330 int free_space, u32 left_nritems,
2331 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002332{
Chris Mason5f39d392007-10-15 16:14:19 -04002333 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002334 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002335 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002336 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002337 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002338 int push_space = 0;
2339 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002340 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002341 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002342 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002343 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002344 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002345
Chris Mason34a38212007-11-07 13:31:03 -05002346 if (empty)
2347 nr = 0;
2348 else
Chris Mason99d8f832010-07-07 10:51:48 -04002349 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002350
Zheng Yan31840ae2008-09-23 13:14:14 -04002351 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002352 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002353
Chris Mason44871b12009-03-13 10:04:31 -04002354 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002355 i = left_nritems - 1;
2356 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002357 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002358
Zheng Yan31840ae2008-09-23 13:14:14 -04002359 if (!empty && push_items > 0) {
2360 if (path->slots[0] > i)
2361 break;
2362 if (path->slots[0] == i) {
2363 int space = btrfs_leaf_free_space(root, left);
2364 if (space + push_space * 2 > free_space)
2365 break;
2366 }
2367 }
2368
Chris Mason00ec4c52007-02-24 12:47:20 -05002369 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002370 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002371
2372 if (!left->map_token) {
2373 map_extent_buffer(left, (unsigned long)item,
2374 sizeof(struct btrfs_item),
2375 &left->map_token, &left->kaddr,
2376 &left->map_start, &left->map_len,
2377 KM_USER1);
2378 }
2379
2380 this_item_size = btrfs_item_size(left, item);
2381 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002382 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002383
Chris Mason00ec4c52007-02-24 12:47:20 -05002384 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002385 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002386 if (i == 0)
2387 break;
2388 i--;
Chris Masondb945352007-10-15 16:15:53 -04002389 }
2390 if (left->map_token) {
2391 unmap_extent_buffer(left, left->map_token, KM_USER1);
2392 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002393 }
Chris Mason5f39d392007-10-15 16:14:19 -04002394
Chris Mason925baed2008-06-25 16:01:30 -04002395 if (push_items == 0)
2396 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002397
Chris Mason34a38212007-11-07 13:31:03 -05002398 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002399 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002400
Chris Mason00ec4c52007-02-24 12:47:20 -05002401 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002402 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002403
Chris Mason5f39d392007-10-15 16:14:19 -04002404 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002405 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002406
Chris Mason00ec4c52007-02-24 12:47:20 -05002407 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002408 data_end = leaf_data_end(root, right);
2409 memmove_extent_buffer(right,
2410 btrfs_leaf_data(right) + data_end - push_space,
2411 btrfs_leaf_data(right) + data_end,
2412 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2413
Chris Mason00ec4c52007-02-24 12:47:20 -05002414 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002415 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002416 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2417 btrfs_leaf_data(left) + leaf_data_end(root, left),
2418 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002419
2420 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2421 btrfs_item_nr_offset(0),
2422 right_nritems * sizeof(struct btrfs_item));
2423
Chris Mason00ec4c52007-02-24 12:47:20 -05002424 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002425 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2426 btrfs_item_nr_offset(left_nritems - push_items),
2427 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002428
2429 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002430 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002431 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002432 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002433 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002434 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002435 if (!right->map_token) {
2436 map_extent_buffer(right, (unsigned long)item,
2437 sizeof(struct btrfs_item),
2438 &right->map_token, &right->kaddr,
2439 &right->map_start, &right->map_len,
2440 KM_USER1);
2441 }
2442 push_space -= btrfs_item_size(right, item);
2443 btrfs_set_item_offset(right, item, push_space);
2444 }
2445
2446 if (right->map_token) {
2447 unmap_extent_buffer(right, right->map_token, KM_USER1);
2448 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002449 }
Chris Mason7518a232007-03-12 12:01:18 -04002450 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002451 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002452
Chris Mason34a38212007-11-07 13:31:03 -05002453 if (left_nritems)
2454 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002455 else
2456 clean_tree_block(trans, root, left);
2457
Chris Mason5f39d392007-10-15 16:14:19 -04002458 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002459
Chris Mason5f39d392007-10-15 16:14:19 -04002460 btrfs_item_key(right, &disk_key, 0);
2461 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002462 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002463
Chris Mason00ec4c52007-02-24 12:47:20 -05002464 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002465 if (path->slots[0] >= left_nritems) {
2466 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002467 if (btrfs_header_nritems(path->nodes[0]) == 0)
2468 clean_tree_block(trans, root, path->nodes[0]);
2469 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002470 free_extent_buffer(path->nodes[0]);
2471 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002472 path->slots[1] += 1;
2473 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002474 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002475 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002476 }
2477 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002478
2479out_unlock:
2480 btrfs_tree_unlock(right);
2481 free_extent_buffer(right);
2482 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002483}
Chris Mason925baed2008-06-25 16:01:30 -04002484
Chris Mason00ec4c52007-02-24 12:47:20 -05002485/*
Chris Mason44871b12009-03-13 10:04:31 -04002486 * push some data in the path leaf to the right, trying to free up at
2487 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2488 *
2489 * returns 1 if the push failed because the other node didn't have enough
2490 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002491 *
2492 * this will push starting from min_slot to the end of the leaf. It won't
2493 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002494 */
2495static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002496 *root, struct btrfs_path *path,
2497 int min_data_size, int data_size,
2498 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002499{
2500 struct extent_buffer *left = path->nodes[0];
2501 struct extent_buffer *right;
2502 struct extent_buffer *upper;
2503 int slot;
2504 int free_space;
2505 u32 left_nritems;
2506 int ret;
2507
2508 if (!path->nodes[1])
2509 return 1;
2510
2511 slot = path->slots[1];
2512 upper = path->nodes[1];
2513 if (slot >= btrfs_header_nritems(upper) - 1)
2514 return 1;
2515
2516 btrfs_assert_tree_locked(path->nodes[1]);
2517
2518 right = read_node_slot(root, upper, slot + 1);
2519 btrfs_tree_lock(right);
2520 btrfs_set_lock_blocking(right);
2521
2522 free_space = btrfs_leaf_free_space(root, right);
2523 if (free_space < data_size)
2524 goto out_unlock;
2525
2526 /* cow and double check */
2527 ret = btrfs_cow_block(trans, root, right, upper,
2528 slot + 1, &right);
2529 if (ret)
2530 goto out_unlock;
2531
2532 free_space = btrfs_leaf_free_space(root, right);
2533 if (free_space < data_size)
2534 goto out_unlock;
2535
2536 left_nritems = btrfs_header_nritems(left);
2537 if (left_nritems == 0)
2538 goto out_unlock;
2539
Chris Mason99d8f832010-07-07 10:51:48 -04002540 return __push_leaf_right(trans, root, path, min_data_size, empty,
2541 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002542out_unlock:
2543 btrfs_tree_unlock(right);
2544 free_extent_buffer(right);
2545 return 1;
2546}
2547
2548/*
Chris Mason74123bd2007-02-02 11:05:29 -05002549 * push some data in the path leaf to the left, trying to free up at
2550 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002551 *
2552 * max_slot can put a limit on how far into the leaf we'll push items. The
2553 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2554 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002555 */
Chris Mason44871b12009-03-13 10:04:31 -04002556static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2557 struct btrfs_root *root,
2558 struct btrfs_path *path, int data_size,
2559 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002560 int free_space, u32 right_nritems,
2561 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002562{
Chris Mason5f39d392007-10-15 16:14:19 -04002563 struct btrfs_disk_key disk_key;
2564 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002565 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002566 int push_space = 0;
2567 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002568 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002569 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002570 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002571 int ret = 0;
2572 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002573 u32 this_item_size;
2574 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002575
Chris Mason34a38212007-11-07 13:31:03 -05002576 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002577 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002578 else
Chris Mason99d8f832010-07-07 10:51:48 -04002579 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002580
2581 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002582 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002583 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
Zheng Yan31840ae2008-09-23 13:14:14 -04002591 if (!empty && push_items > 0) {
2592 if (path->slots[0] < i)
2593 break;
2594 if (path->slots[0] == i) {
2595 int space = btrfs_leaf_free_space(root, right);
2596 if (space + push_space * 2 > free_space)
2597 break;
2598 }
2599 }
2600
Chris Masonbe0e5c02007-01-26 15:51:26 -05002601 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002602 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002603
2604 this_item_size = btrfs_item_size(right, item);
2605 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002606 break;
Chris Masondb945352007-10-15 16:15:53 -04002607
Chris Masonbe0e5c02007-01-26 15:51:26 -05002608 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002609 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610 }
Chris Masondb945352007-10-15 16:15:53 -04002611
2612 if (right->map_token) {
2613 unmap_extent_buffer(right, right->map_token, KM_USER1);
2614 right->map_token = NULL;
2615 }
2616
Chris Masonbe0e5c02007-01-26 15:51:26 -05002617 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002618 ret = 1;
2619 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002620 }
Chris Mason34a38212007-11-07 13:31:03 -05002621 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002622 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002623
Chris Masonbe0e5c02007-01-26 15:51:26 -05002624 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002625 copy_extent_buffer(left, right,
2626 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2627 btrfs_item_nr_offset(0),
2628 push_items * sizeof(struct btrfs_item));
2629
Chris Mason123abc82007-03-14 14:14:43 -04002630 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002631 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002632
2633 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002634 leaf_data_end(root, left) - push_space,
2635 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002636 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002637 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002638 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002639 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002640
Chris Masondb945352007-10-15 16:15:53 -04002641 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002642 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002643 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002644
Chris Mason5f39d392007-10-15 16:14:19 -04002645 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002646 if (!left->map_token) {
2647 map_extent_buffer(left, (unsigned long)item,
2648 sizeof(struct btrfs_item),
2649 &left->map_token, &left->kaddr,
2650 &left->map_start, &left->map_len,
2651 KM_USER1);
2652 }
2653
Chris Mason5f39d392007-10-15 16:14:19 -04002654 ioff = btrfs_item_offset(left, item);
2655 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002656 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002657 }
Chris Mason5f39d392007-10-15 16:14:19 -04002658 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002659 if (left->map_token) {
2660 unmap_extent_buffer(left, left->map_token, KM_USER1);
2661 left->map_token = NULL;
2662 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002663
2664 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002665 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002666 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2667 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002668 WARN_ON(1);
2669 }
Chris Mason5f39d392007-10-15 16:14:19 -04002670
Chris Mason34a38212007-11-07 13:31:03 -05002671 if (push_items < right_nritems) {
2672 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2673 leaf_data_end(root, right);
2674 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2675 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2676 btrfs_leaf_data(right) +
2677 leaf_data_end(root, right), push_space);
2678
2679 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002680 btrfs_item_nr_offset(push_items),
2681 (btrfs_header_nritems(right) - push_items) *
2682 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002683 }
Yaneef1c492007-11-26 10:58:13 -05002684 right_nritems -= push_items;
2685 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002686 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002687 for (i = 0; i < right_nritems; i++) {
2688 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002689
2690 if (!right->map_token) {
2691 map_extent_buffer(right, (unsigned long)item,
2692 sizeof(struct btrfs_item),
2693 &right->map_token, &right->kaddr,
2694 &right->map_start, &right->map_len,
2695 KM_USER1);
2696 }
2697
2698 push_space = push_space - btrfs_item_size(right, item);
2699 btrfs_set_item_offset(right, item, push_space);
2700 }
2701 if (right->map_token) {
2702 unmap_extent_buffer(right, right->map_token, KM_USER1);
2703 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002705
Chris Mason5f39d392007-10-15 16:14:19 -04002706 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002707 if (right_nritems)
2708 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002709 else
2710 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002711
Chris Mason5f39d392007-10-15 16:14:19 -04002712 btrfs_item_key(right, &disk_key, 0);
2713 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002714 if (wret)
2715 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002716
2717 /* then fixup the leaf pointer in the path */
2718 if (path->slots[0] < push_items) {
2719 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002720 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002721 free_extent_buffer(path->nodes[0]);
2722 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002723 path->slots[1] -= 1;
2724 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002725 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002726 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002727 path->slots[0] -= push_items;
2728 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002729 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002730 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002731out:
2732 btrfs_tree_unlock(left);
2733 free_extent_buffer(left);
2734 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002735}
2736
Chris Mason74123bd2007-02-02 11:05:29 -05002737/*
Chris Mason44871b12009-03-13 10:04:31 -04002738 * push some data in the path leaf to the left, trying to free up at
2739 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002740 *
2741 * max_slot can put a limit on how far into the leaf we'll push items. The
2742 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2743 * items
Chris Mason44871b12009-03-13 10:04:31 -04002744 */
2745static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002746 *root, struct btrfs_path *path, int min_data_size,
2747 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002748{
2749 struct extent_buffer *right = path->nodes[0];
2750 struct extent_buffer *left;
2751 int slot;
2752 int free_space;
2753 u32 right_nritems;
2754 int ret = 0;
2755
2756 slot = path->slots[1];
2757 if (slot == 0)
2758 return 1;
2759 if (!path->nodes[1])
2760 return 1;
2761
2762 right_nritems = btrfs_header_nritems(right);
2763 if (right_nritems == 0)
2764 return 1;
2765
2766 btrfs_assert_tree_locked(path->nodes[1]);
2767
2768 left = read_node_slot(root, path->nodes[1], slot - 1);
2769 btrfs_tree_lock(left);
2770 btrfs_set_lock_blocking(left);
2771
2772 free_space = btrfs_leaf_free_space(root, left);
2773 if (free_space < data_size) {
2774 ret = 1;
2775 goto out;
2776 }
2777
2778 /* cow and double check */
2779 ret = btrfs_cow_block(trans, root, left,
2780 path->nodes[1], slot - 1, &left);
2781 if (ret) {
2782 /* we hit -ENOSPC, but it isn't fatal here */
2783 ret = 1;
2784 goto out;
2785 }
2786
2787 free_space = btrfs_leaf_free_space(root, left);
2788 if (free_space < data_size) {
2789 ret = 1;
2790 goto out;
2791 }
2792
Chris Mason99d8f832010-07-07 10:51:48 -04002793 return __push_leaf_left(trans, root, path, min_data_size,
2794 empty, left, free_space, right_nritems,
2795 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002796out:
2797 btrfs_tree_unlock(left);
2798 free_extent_buffer(left);
2799 return ret;
2800}
2801
2802/*
Chris Mason74123bd2007-02-02 11:05:29 -05002803 * split the path's leaf in two, making sure there is at least data_size
2804 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002805 *
2806 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002807 */
Chris Mason44871b12009-03-13 10:04:31 -04002808static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002809 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002810 struct btrfs_path *path,
2811 struct extent_buffer *l,
2812 struct extent_buffer *right,
2813 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002814{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002815 int data_copy_size;
2816 int rt_data_off;
2817 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002818 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002819 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002820 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002821
Chris Mason5f39d392007-10-15 16:14:19 -04002822 nritems = nritems - mid;
2823 btrfs_set_header_nritems(right, nritems);
2824 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2825
2826 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2827 btrfs_item_nr_offset(mid),
2828 nritems * sizeof(struct btrfs_item));
2829
2830 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002831 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2832 data_copy_size, btrfs_leaf_data(l) +
2833 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002834
Chris Mason5f39d392007-10-15 16:14:19 -04002835 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2836 btrfs_item_end_nr(l, mid);
2837
2838 for (i = 0; i < nritems; i++) {
2839 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002840 u32 ioff;
2841
2842 if (!right->map_token) {
2843 map_extent_buffer(right, (unsigned long)item,
2844 sizeof(struct btrfs_item),
2845 &right->map_token, &right->kaddr,
2846 &right->map_start, &right->map_len,
2847 KM_USER1);
2848 }
2849
2850 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002851 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002852 }
Chris Mason74123bd2007-02-02 11:05:29 -05002853
Chris Masondb945352007-10-15 16:15:53 -04002854 if (right->map_token) {
2855 unmap_extent_buffer(right, right->map_token, KM_USER1);
2856 right->map_token = NULL;
2857 }
2858
Chris Mason5f39d392007-10-15 16:14:19 -04002859 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002860 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002861 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002862 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2863 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002864 if (wret)
2865 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002866
2867 btrfs_mark_buffer_dirty(right);
2868 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002869 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002870
Chris Masonbe0e5c02007-01-26 15:51:26 -05002871 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002872 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002873 free_extent_buffer(path->nodes[0]);
2874 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002875 path->slots[0] -= mid;
2876 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002877 } else {
2878 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002879 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002880 }
Chris Mason5f39d392007-10-15 16:14:19 -04002881
Chris Masoneb60cea2007-02-02 09:18:22 -05002882 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002883
Chris Mason44871b12009-03-13 10:04:31 -04002884 return ret;
2885}
2886
2887/*
Chris Mason99d8f832010-07-07 10:51:48 -04002888 * double splits happen when we need to insert a big item in the middle
2889 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2890 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2891 * A B C
2892 *
2893 * We avoid this by trying to push the items on either side of our target
2894 * into the adjacent leaves. If all goes well we can avoid the double split
2895 * completely.
2896 */
2897static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2898 struct btrfs_root *root,
2899 struct btrfs_path *path,
2900 int data_size)
2901{
2902 int ret;
2903 int progress = 0;
2904 int slot;
2905 u32 nritems;
2906
2907 slot = path->slots[0];
2908
2909 /*
2910 * try to push all the items after our slot into the
2911 * right leaf
2912 */
2913 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2914 if (ret < 0)
2915 return ret;
2916
2917 if (ret == 0)
2918 progress++;
2919
2920 nritems = btrfs_header_nritems(path->nodes[0]);
2921 /*
2922 * our goal is to get our slot at the start or end of a leaf. If
2923 * we've done so we're done
2924 */
2925 if (path->slots[0] == 0 || path->slots[0] == nritems)
2926 return 0;
2927
2928 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2929 return 0;
2930
2931 /* try to push all the items before our slot into the next leaf */
2932 slot = path->slots[0];
2933 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2934 if (ret < 0)
2935 return ret;
2936
2937 if (ret == 0)
2938 progress++;
2939
2940 if (progress)
2941 return 0;
2942 return 1;
2943}
2944
2945/*
Chris Mason44871b12009-03-13 10:04:31 -04002946 * split the path's leaf in two, making sure there is at least data_size
2947 * available for the resulting leaf level of the path.
2948 *
2949 * returns 0 if all went well and < 0 on failure.
2950 */
2951static noinline int split_leaf(struct btrfs_trans_handle *trans,
2952 struct btrfs_root *root,
2953 struct btrfs_key *ins_key,
2954 struct btrfs_path *path, int data_size,
2955 int extend)
2956{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002957 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002958 struct extent_buffer *l;
2959 u32 nritems;
2960 int mid;
2961 int slot;
2962 struct extent_buffer *right;
2963 int ret = 0;
2964 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002965 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002966 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002967 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002968
Yan, Zhenga5719522009-09-24 09:17:31 -04002969 l = path->nodes[0];
2970 slot = path->slots[0];
2971 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2972 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2973 return -EOVERFLOW;
2974
Chris Mason44871b12009-03-13 10:04:31 -04002975 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002976 if (data_size) {
2977 wret = push_leaf_right(trans, root, path, data_size,
2978 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002979 if (wret < 0)
2980 return wret;
2981 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002982 wret = push_leaf_left(trans, root, path, data_size,
2983 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002984 if (wret < 0)
2985 return wret;
2986 }
2987 l = path->nodes[0];
2988
2989 /* did the pushes work? */
2990 if (btrfs_leaf_free_space(root, l) >= data_size)
2991 return 0;
2992 }
2993
2994 if (!path->nodes[1]) {
2995 ret = insert_new_root(trans, root, path, 1);
2996 if (ret)
2997 return ret;
2998 }
2999again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003001 l = path->nodes[0];
3002 slot = path->slots[0];
3003 nritems = btrfs_header_nritems(l);
3004 mid = (nritems + 1) / 2;
3005
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003006 if (mid <= slot) {
3007 if (nritems == 1 ||
3008 leaf_space_used(l, mid, nritems - mid) + data_size >
3009 BTRFS_LEAF_DATA_SIZE(root)) {
3010 if (slot >= nritems) {
3011 split = 0;
3012 } else {
3013 mid = slot;
3014 if (mid != nritems &&
3015 leaf_space_used(l, mid, nritems - mid) +
3016 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003017 if (data_size && !tried_avoid_double)
3018 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003019 split = 2;
3020 }
3021 }
3022 }
3023 } else {
3024 if (leaf_space_used(l, 0, mid) + data_size >
3025 BTRFS_LEAF_DATA_SIZE(root)) {
3026 if (!extend && data_size && slot == 0) {
3027 split = 0;
3028 } else if ((extend || !data_size) && slot == 0) {
3029 mid = 1;
3030 } else {
3031 mid = slot;
3032 if (mid != nritems &&
3033 leaf_space_used(l, mid, nritems - mid) +
3034 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003035 if (data_size && !tried_avoid_double)
3036 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037 split = 2 ;
3038 }
3039 }
3040 }
3041 }
3042
3043 if (split == 0)
3044 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3045 else
3046 btrfs_item_key(l, &disk_key, mid);
3047
3048 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003049 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003050 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003051 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003052 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003053
3054 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003055
3056 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3057 btrfs_set_header_bytenr(right, right->start);
3058 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003059 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003060 btrfs_set_header_owner(right, root->root_key.objectid);
3061 btrfs_set_header_level(right, 0);
3062 write_extent_buffer(right, root->fs_info->fsid,
3063 (unsigned long)btrfs_header_fsid(right),
3064 BTRFS_FSID_SIZE);
3065
3066 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3067 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3068 BTRFS_UUID_SIZE);
3069
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003070 if (split == 0) {
3071 if (mid <= slot) {
3072 btrfs_set_header_nritems(right, 0);
3073 wret = insert_ptr(trans, root, path,
3074 &disk_key, right->start,
3075 path->slots[1] + 1, 1);
3076 if (wret)
3077 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003078
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003079 btrfs_tree_unlock(path->nodes[0]);
3080 free_extent_buffer(path->nodes[0]);
3081 path->nodes[0] = right;
3082 path->slots[0] = 0;
3083 path->slots[1] += 1;
3084 } else {
3085 btrfs_set_header_nritems(right, 0);
3086 wret = insert_ptr(trans, root, path,
3087 &disk_key,
3088 right->start,
3089 path->slots[1], 1);
3090 if (wret)
3091 ret = wret;
3092 btrfs_tree_unlock(path->nodes[0]);
3093 free_extent_buffer(path->nodes[0]);
3094 path->nodes[0] = right;
3095 path->slots[0] = 0;
3096 if (path->slots[1] == 0) {
3097 wret = fixup_low_keys(trans, root,
3098 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003099 if (wret)
3100 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003101 }
3102 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003103 btrfs_mark_buffer_dirty(right);
3104 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003105 }
3106
3107 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3108 BUG_ON(ret);
3109
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003110 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003111 BUG_ON(num_doubles != 0);
3112 num_doubles++;
3113 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003114 }
Chris Mason44871b12009-03-13 10:04:31 -04003115
Chris Masonbe0e5c02007-01-26 15:51:26 -05003116 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04003117
3118push_for_double:
3119 push_for_double_split(trans, root, path, data_size);
3120 tried_avoid_double = 1;
3121 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3122 return 0;
3123 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003124}
3125
Yan, Zhengad48fd752009-11-12 09:33:58 +00003126static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3127 struct btrfs_root *root,
3128 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003129{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003130 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003131 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003132 struct btrfs_file_extent_item *fi;
3133 u64 extent_len = 0;
3134 u32 item_size;
3135 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003136
3137 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003138 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3139
3140 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3141 key.type != BTRFS_EXTENT_CSUM_KEY);
3142
3143 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3144 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003145
3146 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003147 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3148 fi = btrfs_item_ptr(leaf, path->slots[0],
3149 struct btrfs_file_extent_item);
3150 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3151 }
Chris Mason459931e2008-12-10 09:10:46 -05003152 btrfs_release_path(root, path);
3153
Chris Mason459931e2008-12-10 09:10:46 -05003154 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003155 path->search_for_split = 1;
3156 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003157 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003158 if (ret < 0)
3159 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003160
Yan, Zhengad48fd752009-11-12 09:33:58 +00003161 ret = -EAGAIN;
3162 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003163 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003164 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3165 goto err;
3166
Chris Mason109f6ae2010-04-02 09:20:18 -04003167 /* the leaf has changed, it now has room. return now */
3168 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3169 goto err;
3170
Yan, Zhengad48fd752009-11-12 09:33:58 +00003171 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3172 fi = btrfs_item_ptr(leaf, path->slots[0],
3173 struct btrfs_file_extent_item);
3174 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3175 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003176 }
3177
Chris Masonb9473432009-03-13 11:00:37 -04003178 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003179 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003180 if (ret)
3181 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003182
Yan, Zhengad48fd752009-11-12 09:33:58 +00003183 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003184 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003185 return 0;
3186err:
3187 path->keep_locks = 0;
3188 return ret;
3189}
3190
3191static noinline int split_item(struct btrfs_trans_handle *trans,
3192 struct btrfs_root *root,
3193 struct btrfs_path *path,
3194 struct btrfs_key *new_key,
3195 unsigned long split_offset)
3196{
3197 struct extent_buffer *leaf;
3198 struct btrfs_item *item;
3199 struct btrfs_item *new_item;
3200 int slot;
3201 char *buf;
3202 u32 nritems;
3203 u32 item_size;
3204 u32 orig_offset;
3205 struct btrfs_disk_key disk_key;
3206
Chris Masonb9473432009-03-13 11:00:37 -04003207 leaf = path->nodes[0];
3208 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3209
Chris Masonb4ce94d2009-02-04 09:25:08 -05003210 btrfs_set_path_blocking(path);
3211
Chris Mason459931e2008-12-10 09:10:46 -05003212 item = btrfs_item_nr(leaf, path->slots[0]);
3213 orig_offset = btrfs_item_offset(leaf, item);
3214 item_size = btrfs_item_size(leaf, item);
3215
Chris Mason459931e2008-12-10 09:10:46 -05003216 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003217 if (!buf)
3218 return -ENOMEM;
3219
Chris Mason459931e2008-12-10 09:10:46 -05003220 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3221 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003222
Chris Mason459931e2008-12-10 09:10:46 -05003223 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003224 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003225 if (slot != nritems) {
3226 /* shift the items */
3227 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003228 btrfs_item_nr_offset(slot),
3229 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003230 }
3231
3232 btrfs_cpu_key_to_disk(&disk_key, new_key);
3233 btrfs_set_item_key(leaf, &disk_key, slot);
3234
3235 new_item = btrfs_item_nr(leaf, slot);
3236
3237 btrfs_set_item_offset(leaf, new_item, orig_offset);
3238 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3239
3240 btrfs_set_item_offset(leaf, item,
3241 orig_offset + item_size - split_offset);
3242 btrfs_set_item_size(leaf, item, split_offset);
3243
3244 btrfs_set_header_nritems(leaf, nritems + 1);
3245
3246 /* write the data for the start of the original item */
3247 write_extent_buffer(leaf, buf,
3248 btrfs_item_ptr_offset(leaf, path->slots[0]),
3249 split_offset);
3250
3251 /* write the data for the new item */
3252 write_extent_buffer(leaf, buf + split_offset,
3253 btrfs_item_ptr_offset(leaf, slot),
3254 item_size - split_offset);
3255 btrfs_mark_buffer_dirty(leaf);
3256
Yan, Zhengad48fd752009-11-12 09:33:58 +00003257 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003258 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003259 return 0;
3260}
3261
3262/*
3263 * This function splits a single item into two items,
3264 * giving 'new_key' to the new item and splitting the
3265 * old one at split_offset (from the start of the item).
3266 *
3267 * The path may be released by this operation. After
3268 * the split, the path is pointing to the old item. The
3269 * new item is going to be in the same node as the old one.
3270 *
3271 * Note, the item being split must be smaller enough to live alone on
3272 * a tree block with room for one extra struct btrfs_item
3273 *
3274 * This allows us to split the item in place, keeping a lock on the
3275 * leaf the entire time.
3276 */
3277int btrfs_split_item(struct btrfs_trans_handle *trans,
3278 struct btrfs_root *root,
3279 struct btrfs_path *path,
3280 struct btrfs_key *new_key,
3281 unsigned long split_offset)
3282{
3283 int ret;
3284 ret = setup_leaf_for_split(trans, root, path,
3285 sizeof(struct btrfs_item));
3286 if (ret)
3287 return ret;
3288
3289 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003290 return ret;
3291}
3292
3293/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003294 * This function duplicate a item, giving 'new_key' to the new item.
3295 * It guarantees both items live in the same tree leaf and the new item
3296 * is contiguous with the original item.
3297 *
3298 * This allows us to split file extent in place, keeping a lock on the
3299 * leaf the entire time.
3300 */
3301int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3302 struct btrfs_root *root,
3303 struct btrfs_path *path,
3304 struct btrfs_key *new_key)
3305{
3306 struct extent_buffer *leaf;
3307 int ret;
3308 u32 item_size;
3309
3310 leaf = path->nodes[0];
3311 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3312 ret = setup_leaf_for_split(trans, root, path,
3313 item_size + sizeof(struct btrfs_item));
3314 if (ret)
3315 return ret;
3316
3317 path->slots[0]++;
3318 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3319 item_size, item_size +
3320 sizeof(struct btrfs_item), 1);
3321 BUG_ON(ret);
3322
3323 leaf = path->nodes[0];
3324 memcpy_extent_buffer(leaf,
3325 btrfs_item_ptr_offset(leaf, path->slots[0]),
3326 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3327 item_size);
3328 return 0;
3329}
3330
3331/*
Chris Masond352ac62008-09-29 15:18:18 -04003332 * make the item pointed to by the path smaller. new_size indicates
3333 * how small to make it, and from_end tells us if we just chop bytes
3334 * off the end of the item or if we shift the item to chop bytes off
3335 * the front.
3336 */
Chris Masonb18c6682007-04-17 13:26:50 -04003337int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3338 struct btrfs_root *root,
3339 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003340 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003341{
3342 int ret = 0;
3343 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003344 struct extent_buffer *leaf;
3345 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003346 u32 nritems;
3347 unsigned int data_end;
3348 unsigned int old_data_start;
3349 unsigned int old_size;
3350 unsigned int size_diff;
3351 int i;
3352
Chris Mason5f39d392007-10-15 16:14:19 -04003353 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003354 slot = path->slots[0];
3355
3356 old_size = btrfs_item_size_nr(leaf, slot);
3357 if (old_size == new_size)
3358 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003359
Chris Mason5f39d392007-10-15 16:14:19 -04003360 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003361 data_end = leaf_data_end(root, leaf);
3362
Chris Mason5f39d392007-10-15 16:14:19 -04003363 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003364
Chris Masonb18c6682007-04-17 13:26:50 -04003365 size_diff = old_size - new_size;
3366
3367 BUG_ON(slot < 0);
3368 BUG_ON(slot >= nritems);
3369
3370 /*
3371 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3372 */
3373 /* first correct the data pointers */
3374 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003375 u32 ioff;
3376 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003377
3378 if (!leaf->map_token) {
3379 map_extent_buffer(leaf, (unsigned long)item,
3380 sizeof(struct btrfs_item),
3381 &leaf->map_token, &leaf->kaddr,
3382 &leaf->map_start, &leaf->map_len,
3383 KM_USER1);
3384 }
3385
Chris Mason5f39d392007-10-15 16:14:19 -04003386 ioff = btrfs_item_offset(leaf, item);
3387 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003388 }
Chris Masondb945352007-10-15 16:15:53 -04003389
3390 if (leaf->map_token) {
3391 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3392 leaf->map_token = NULL;
3393 }
3394
Chris Masonb18c6682007-04-17 13:26:50 -04003395 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003396 if (from_end) {
3397 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3398 data_end + size_diff, btrfs_leaf_data(leaf) +
3399 data_end, old_data_start + new_size - data_end);
3400 } else {
3401 struct btrfs_disk_key disk_key;
3402 u64 offset;
3403
3404 btrfs_item_key(leaf, &disk_key, slot);
3405
3406 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3407 unsigned long ptr;
3408 struct btrfs_file_extent_item *fi;
3409
3410 fi = btrfs_item_ptr(leaf, slot,
3411 struct btrfs_file_extent_item);
3412 fi = (struct btrfs_file_extent_item *)(
3413 (unsigned long)fi - size_diff);
3414
3415 if (btrfs_file_extent_type(leaf, fi) ==
3416 BTRFS_FILE_EXTENT_INLINE) {
3417 ptr = btrfs_item_ptr_offset(leaf, slot);
3418 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003419 (unsigned long)fi,
3420 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003421 disk_bytenr));
3422 }
3423 }
3424
3425 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3426 data_end + size_diff, btrfs_leaf_data(leaf) +
3427 data_end, old_data_start - data_end);
3428
3429 offset = btrfs_disk_key_offset(&disk_key);
3430 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3431 btrfs_set_item_key(leaf, &disk_key, slot);
3432 if (slot == 0)
3433 fixup_low_keys(trans, root, path, &disk_key, 1);
3434 }
Chris Mason5f39d392007-10-15 16:14:19 -04003435
3436 item = btrfs_item_nr(leaf, slot);
3437 btrfs_set_item_size(leaf, item, new_size);
3438 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003439
3440 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003441 if (btrfs_leaf_free_space(root, leaf) < 0) {
3442 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003443 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003444 }
Chris Masonb18c6682007-04-17 13:26:50 -04003445 return ret;
3446}
3447
Chris Masond352ac62008-09-29 15:18:18 -04003448/*
3449 * make the item pointed to by the path bigger, data_size is the new size.
3450 */
Chris Mason5f39d392007-10-15 16:14:19 -04003451int btrfs_extend_item(struct btrfs_trans_handle *trans,
3452 struct btrfs_root *root, struct btrfs_path *path,
3453 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003454{
3455 int ret = 0;
3456 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003457 struct extent_buffer *leaf;
3458 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003459 u32 nritems;
3460 unsigned int data_end;
3461 unsigned int old_data;
3462 unsigned int old_size;
3463 int i;
3464
Chris Mason5f39d392007-10-15 16:14:19 -04003465 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003466
Chris Mason5f39d392007-10-15 16:14:19 -04003467 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003468 data_end = leaf_data_end(root, leaf);
3469
Chris Mason5f39d392007-10-15 16:14:19 -04003470 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3471 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003472 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003473 }
Chris Mason6567e832007-04-16 09:22:45 -04003474 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003475 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003476
3477 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003478 if (slot >= nritems) {
3479 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003480 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3481 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003482 BUG_ON(1);
3483 }
Chris Mason6567e832007-04-16 09:22:45 -04003484
3485 /*
3486 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3487 */
3488 /* first correct the data pointers */
3489 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003490 u32 ioff;
3491 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003492
3493 if (!leaf->map_token) {
3494 map_extent_buffer(leaf, (unsigned long)item,
3495 sizeof(struct btrfs_item),
3496 &leaf->map_token, &leaf->kaddr,
3497 &leaf->map_start, &leaf->map_len,
3498 KM_USER1);
3499 }
Chris Mason5f39d392007-10-15 16:14:19 -04003500 ioff = btrfs_item_offset(leaf, item);
3501 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003502 }
Chris Mason5f39d392007-10-15 16:14:19 -04003503
Chris Masondb945352007-10-15 16:15:53 -04003504 if (leaf->map_token) {
3505 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3506 leaf->map_token = NULL;
3507 }
3508
Chris Mason6567e832007-04-16 09:22:45 -04003509 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003510 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003511 data_end - data_size, btrfs_leaf_data(leaf) +
3512 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003513
Chris Mason6567e832007-04-16 09:22:45 -04003514 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003515 old_size = btrfs_item_size_nr(leaf, slot);
3516 item = btrfs_item_nr(leaf, slot);
3517 btrfs_set_item_size(leaf, item, old_size + data_size);
3518 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003519
3520 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003521 if (btrfs_leaf_free_space(root, leaf) < 0) {
3522 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003523 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003524 }
Chris Mason6567e832007-04-16 09:22:45 -04003525 return ret;
3526}
3527
Chris Mason74123bd2007-02-02 11:05:29 -05003528/*
Chris Masond352ac62008-09-29 15:18:18 -04003529 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003530 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003531 * Returns the number of keys that were inserted.
3532 */
3533int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3534 struct btrfs_root *root,
3535 struct btrfs_path *path,
3536 struct btrfs_key *cpu_key, u32 *data_size,
3537 int nr)
3538{
3539 struct extent_buffer *leaf;
3540 struct btrfs_item *item;
3541 int ret = 0;
3542 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003543 int i;
3544 u32 nritems;
3545 u32 total_data = 0;
3546 u32 total_size = 0;
3547 unsigned int data_end;
3548 struct btrfs_disk_key disk_key;
3549 struct btrfs_key found_key;
3550
Yan Zheng87b29b22008-12-17 10:21:48 -05003551 for (i = 0; i < nr; i++) {
3552 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3553 BTRFS_LEAF_DATA_SIZE(root)) {
3554 break;
3555 nr = i;
3556 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003557 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003558 total_size += data_size[i] + sizeof(struct btrfs_item);
3559 }
3560 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003561
Josef Bacikf3465ca2008-11-12 14:19:50 -05003562 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3563 if (ret == 0)
3564 return -EEXIST;
3565 if (ret < 0)
3566 goto out;
3567
Josef Bacikf3465ca2008-11-12 14:19:50 -05003568 leaf = path->nodes[0];
3569
3570 nritems = btrfs_header_nritems(leaf);
3571 data_end = leaf_data_end(root, leaf);
3572
3573 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3574 for (i = nr; i >= 0; i--) {
3575 total_data -= data_size[i];
3576 total_size -= data_size[i] + sizeof(struct btrfs_item);
3577 if (total_size < btrfs_leaf_free_space(root, leaf))
3578 break;
3579 }
3580 nr = i;
3581 }
3582
3583 slot = path->slots[0];
3584 BUG_ON(slot < 0);
3585
3586 if (slot != nritems) {
3587 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3588
3589 item = btrfs_item_nr(leaf, slot);
3590 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3591
3592 /* figure out how many keys we can insert in here */
3593 total_data = data_size[0];
3594 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003595 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003596 break;
3597 total_data += data_size[i];
3598 }
3599 nr = i;
3600
3601 if (old_data < data_end) {
3602 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003603 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003604 slot, old_data, data_end);
3605 BUG_ON(1);
3606 }
3607 /*
3608 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3609 */
3610 /* first correct the data pointers */
3611 WARN_ON(leaf->map_token);
3612 for (i = slot; i < nritems; i++) {
3613 u32 ioff;
3614
3615 item = btrfs_item_nr(leaf, i);
3616 if (!leaf->map_token) {
3617 map_extent_buffer(leaf, (unsigned long)item,
3618 sizeof(struct btrfs_item),
3619 &leaf->map_token, &leaf->kaddr,
3620 &leaf->map_start, &leaf->map_len,
3621 KM_USER1);
3622 }
3623
3624 ioff = btrfs_item_offset(leaf, item);
3625 btrfs_set_item_offset(leaf, item, ioff - total_data);
3626 }
3627 if (leaf->map_token) {
3628 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3629 leaf->map_token = NULL;
3630 }
3631
3632 /* shift the items */
3633 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3634 btrfs_item_nr_offset(slot),
3635 (nritems - slot) * sizeof(struct btrfs_item));
3636
3637 /* shift the data */
3638 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3639 data_end - total_data, btrfs_leaf_data(leaf) +
3640 data_end, old_data - data_end);
3641 data_end = old_data;
3642 } else {
3643 /*
3644 * this sucks but it has to be done, if we are inserting at
3645 * the end of the leaf only insert 1 of the items, since we
3646 * have no way of knowing whats on the next leaf and we'd have
3647 * to drop our current locks to figure it out
3648 */
3649 nr = 1;
3650 }
3651
3652 /* setup the item for the new data */
3653 for (i = 0; i < nr; i++) {
3654 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3655 btrfs_set_item_key(leaf, &disk_key, slot + i);
3656 item = btrfs_item_nr(leaf, slot + i);
3657 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3658 data_end -= data_size[i];
3659 btrfs_set_item_size(leaf, item, data_size[i]);
3660 }
3661 btrfs_set_header_nritems(leaf, nritems + nr);
3662 btrfs_mark_buffer_dirty(leaf);
3663
3664 ret = 0;
3665 if (slot == 0) {
3666 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3667 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3668 }
3669
3670 if (btrfs_leaf_free_space(root, leaf) < 0) {
3671 btrfs_print_leaf(root, leaf);
3672 BUG();
3673 }
3674out:
3675 if (!ret)
3676 ret = nr;
3677 return ret;
3678}
3679
3680/*
Chris Mason44871b12009-03-13 10:04:31 -04003681 * this is a helper for btrfs_insert_empty_items, the main goal here is
3682 * to save stack depth by doing the bulk of the work in a function
3683 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003684 */
Chris Mason44871b12009-03-13 10:04:31 -04003685static noinline_for_stack int
3686setup_items_for_insert(struct btrfs_trans_handle *trans,
3687 struct btrfs_root *root, struct btrfs_path *path,
3688 struct btrfs_key *cpu_key, u32 *data_size,
3689 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003690{
Chris Mason5f39d392007-10-15 16:14:19 -04003691 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003692 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003693 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003694 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003695 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003696 int ret;
3697 struct extent_buffer *leaf;
3698 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003699
Chris Mason5f39d392007-10-15 16:14:19 -04003700 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003701 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003702
Chris Mason5f39d392007-10-15 16:14:19 -04003703 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003704 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003705
Chris Masonf25956c2008-09-12 15:32:53 -04003706 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003707 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003708 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003709 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003710 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003711 }
Chris Mason5f39d392007-10-15 16:14:19 -04003712
Chris Masonbe0e5c02007-01-26 15:51:26 -05003713 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003714 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003715
Chris Mason5f39d392007-10-15 16:14:19 -04003716 if (old_data < data_end) {
3717 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003718 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003719 slot, old_data, data_end);
3720 BUG_ON(1);
3721 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003722 /*
3723 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3724 */
3725 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003726 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003727 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003728 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003729
Chris Mason5f39d392007-10-15 16:14:19 -04003730 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003731 if (!leaf->map_token) {
3732 map_extent_buffer(leaf, (unsigned long)item,
3733 sizeof(struct btrfs_item),
3734 &leaf->map_token, &leaf->kaddr,
3735 &leaf->map_start, &leaf->map_len,
3736 KM_USER1);
3737 }
3738
Chris Mason5f39d392007-10-15 16:14:19 -04003739 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003740 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003741 }
Chris Masondb945352007-10-15 16:15:53 -04003742 if (leaf->map_token) {
3743 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3744 leaf->map_token = NULL;
3745 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003746
3747 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003748 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003749 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003750 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003751
3752 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003753 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003754 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003755 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003756 data_end = old_data;
3757 }
Chris Mason5f39d392007-10-15 16:14:19 -04003758
Chris Mason62e27492007-03-15 12:56:47 -04003759 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003760 for (i = 0; i < nr; i++) {
3761 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3762 btrfs_set_item_key(leaf, &disk_key, slot + i);
3763 item = btrfs_item_nr(leaf, slot + i);
3764 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3765 data_end -= data_size[i];
3766 btrfs_set_item_size(leaf, item, data_size[i]);
3767 }
Chris Mason44871b12009-03-13 10:04:31 -04003768
Chris Mason9c583092008-01-29 15:15:18 -05003769 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003770
3771 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003772 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003773 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003774 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003775 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003776 }
Chris Masonb9473432009-03-13 11:00:37 -04003777 btrfs_unlock_up_safe(path, 1);
3778 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003779
Chris Mason5f39d392007-10-15 16:14:19 -04003780 if (btrfs_leaf_free_space(root, leaf) < 0) {
3781 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003782 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003783 }
Chris Mason44871b12009-03-13 10:04:31 -04003784 return ret;
3785}
3786
3787/*
3788 * Given a key and some data, insert items into the tree.
3789 * This does all the path init required, making room in the tree if needed.
3790 */
3791int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3792 struct btrfs_root *root,
3793 struct btrfs_path *path,
3794 struct btrfs_key *cpu_key, u32 *data_size,
3795 int nr)
3796{
Chris Mason44871b12009-03-13 10:04:31 -04003797 int ret = 0;
3798 int slot;
3799 int i;
3800 u32 total_size = 0;
3801 u32 total_data = 0;
3802
3803 for (i = 0; i < nr; i++)
3804 total_data += data_size[i];
3805
3806 total_size = total_data + (nr * sizeof(struct btrfs_item));
3807 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3808 if (ret == 0)
3809 return -EEXIST;
3810 if (ret < 0)
3811 goto out;
3812
Chris Mason44871b12009-03-13 10:04:31 -04003813 slot = path->slots[0];
3814 BUG_ON(slot < 0);
3815
3816 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3817 total_data, total_size, nr);
3818
Chris Masoned2ff2c2007-03-01 18:59:40 -05003819out:
Chris Mason62e27492007-03-15 12:56:47 -04003820 return ret;
3821}
3822
3823/*
3824 * Given a key and some data, insert an item into the tree.
3825 * This does all the path init required, making room in the tree if needed.
3826 */
Chris Masone089f052007-03-16 16:20:31 -04003827int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3828 *root, struct btrfs_key *cpu_key, void *data, u32
3829 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003830{
3831 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003832 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003833 struct extent_buffer *leaf;
3834 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003835
Chris Mason2c90e5d2007-04-02 10:50:19 -04003836 path = btrfs_alloc_path();
3837 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003838 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003839 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003840 leaf = path->nodes[0];
3841 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3842 write_extent_buffer(leaf, data, ptr, data_size);
3843 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003844 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003845 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003846 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003847}
3848
Chris Mason74123bd2007-02-02 11:05:29 -05003849/*
Chris Mason5de08d72007-02-24 06:24:44 -05003850 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003851 *
Chris Masond352ac62008-09-29 15:18:18 -04003852 * the tree should have been previously balanced so the deletion does not
3853 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003854 */
Chris Masone089f052007-03-16 16:20:31 -04003855static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3856 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003857{
Chris Mason5f39d392007-10-15 16:14:19 -04003858 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003859 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003860 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003861 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003862
Chris Mason5f39d392007-10-15 16:14:19 -04003863 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003864 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003865 memmove_extent_buffer(parent,
3866 btrfs_node_key_ptr_offset(slot),
3867 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003868 sizeof(struct btrfs_key_ptr) *
3869 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003870 }
Chris Mason7518a232007-03-12 12:01:18 -04003871 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003872 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003873 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003874 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003875 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003876 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003877 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003878 struct btrfs_disk_key disk_key;
3879
3880 btrfs_node_key(parent, &disk_key, 0);
3881 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003882 if (wret)
3883 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003884 }
Chris Masond6025572007-03-30 14:27:56 -04003885 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003886 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003887}
3888
Chris Mason74123bd2007-02-02 11:05:29 -05003889/*
Chris Mason323ac952008-10-01 19:05:46 -04003890 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003891 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003892 *
3893 * This deletes the pointer in path->nodes[1] and frees the leaf
3894 * block extent. zero is returned if it all worked out, < 0 otherwise.
3895 *
3896 * The path must have already been setup for deleting the leaf, including
3897 * all the proper balancing. path->nodes[1] must be locked.
3898 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003899static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3900 struct btrfs_root *root,
3901 struct btrfs_path *path,
3902 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003903{
3904 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003905
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003906 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003907 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3908 if (ret)
3909 return ret;
3910
Chris Mason4d081c42009-02-04 09:31:28 -05003911 /*
3912 * btrfs_free_extent is expensive, we want to make sure we
3913 * aren't holding any locks when we call it
3914 */
3915 btrfs_unlock_up_safe(path, 0);
3916
Yan, Zhengf0486c62010-05-16 10:46:25 -04003917 root_sub_used(root, leaf->len);
3918
3919 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3920 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003921}
3922/*
Chris Mason74123bd2007-02-02 11:05:29 -05003923 * delete the item at the leaf level in path. If that empties
3924 * the leaf, remove it from the tree
3925 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003926int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3927 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003928{
Chris Mason5f39d392007-10-15 16:14:19 -04003929 struct extent_buffer *leaf;
3930 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003931 int last_off;
3932 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003933 int ret = 0;
3934 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003935 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003936 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003937
Chris Mason5f39d392007-10-15 16:14:19 -04003938 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003939 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3940
3941 for (i = 0; i < nr; i++)
3942 dsize += btrfs_item_size_nr(leaf, slot + i);
3943
Chris Mason5f39d392007-10-15 16:14:19 -04003944 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003945
Chris Mason85e21ba2008-01-29 15:11:36 -05003946 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003947 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003948
3949 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003950 data_end + dsize,
3951 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003952 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003953
Chris Mason85e21ba2008-01-29 15:11:36 -05003954 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003955 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003956
Chris Mason5f39d392007-10-15 16:14:19 -04003957 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003958 if (!leaf->map_token) {
3959 map_extent_buffer(leaf, (unsigned long)item,
3960 sizeof(struct btrfs_item),
3961 &leaf->map_token, &leaf->kaddr,
3962 &leaf->map_start, &leaf->map_len,
3963 KM_USER1);
3964 }
Chris Mason5f39d392007-10-15 16:14:19 -04003965 ioff = btrfs_item_offset(leaf, item);
3966 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003967 }
Chris Masondb945352007-10-15 16:15:53 -04003968
3969 if (leaf->map_token) {
3970 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3971 leaf->map_token = NULL;
3972 }
3973
Chris Mason5f39d392007-10-15 16:14:19 -04003974 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003975 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003976 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003977 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003978 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003979 btrfs_set_header_nritems(leaf, nritems - nr);
3980 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003981
Chris Mason74123bd2007-02-02 11:05:29 -05003982 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003983 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003984 if (leaf == root->node) {
3985 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003986 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003987 btrfs_set_path_blocking(path);
3988 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003989 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003990 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003991 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003992 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003993 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003994 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003995 struct btrfs_disk_key disk_key;
3996
3997 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003998 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003999 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004000 if (wret)
4001 ret = wret;
4002 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004003
Chris Mason74123bd2007-02-02 11:05:29 -05004004 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004005 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004006 /* push_leaf_left fixes the path.
4007 * make sure the path still points to our leaf
4008 * for possible call to del_ptr below
4009 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004010 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004011 extent_buffer_get(leaf);
4012
Chris Masonb9473432009-03-13 11:00:37 -04004013 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004014 wret = push_leaf_left(trans, root, path, 1, 1,
4015 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004016 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004017 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004018
4019 if (path->nodes[0] == leaf &&
4020 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004021 wret = push_leaf_right(trans, root, path, 1,
4022 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004023 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004024 ret = wret;
4025 }
Chris Mason5f39d392007-10-15 16:14:19 -04004026
4027 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004028 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004029 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004030 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004031 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05004032 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004033 /* if we're still in the path, make sure
4034 * we're dirty. Otherwise, one of the
4035 * push_leaf functions must have already
4036 * dirtied this buffer
4037 */
4038 if (path->nodes[0] == leaf)
4039 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004040 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004041 }
Chris Masond5719762007-03-23 10:01:08 -04004042 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004043 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004044 }
4045 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004046 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004047}
4048
Chris Mason97571fd2007-02-24 13:39:08 -05004049/*
Chris Mason925baed2008-06-25 16:01:30 -04004050 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004051 * returns 0 if it found something or 1 if there are no lesser leaves.
4052 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004053 *
4054 * This may release the path, and so you may lose any locks held at the
4055 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004056 */
4057int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4058{
Chris Mason925baed2008-06-25 16:01:30 -04004059 struct btrfs_key key;
4060 struct btrfs_disk_key found_key;
4061 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004062
Chris Mason925baed2008-06-25 16:01:30 -04004063 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004064
Chris Mason925baed2008-06-25 16:01:30 -04004065 if (key.offset > 0)
4066 key.offset--;
4067 else if (key.type > 0)
4068 key.type--;
4069 else if (key.objectid > 0)
4070 key.objectid--;
4071 else
4072 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004073
Chris Mason925baed2008-06-25 16:01:30 -04004074 btrfs_release_path(root, path);
4075 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4076 if (ret < 0)
4077 return ret;
4078 btrfs_item_key(path->nodes[0], &found_key, 0);
4079 ret = comp_keys(&found_key, &key);
4080 if (ret < 0)
4081 return 0;
4082 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004083}
4084
Chris Mason3f157a22008-06-25 16:01:31 -04004085/*
4086 * A helper function to walk down the tree starting at min_key, and looking
4087 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004088 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004089 *
4090 * This does not cow, but it does stuff the starting key it finds back
4091 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4092 * key and get a writable path.
4093 *
4094 * This does lock as it descends, and path->keep_locks should be set
4095 * to 1 by the caller.
4096 *
4097 * This honors path->lowest_level to prevent descent past a given level
4098 * of the tree.
4099 *
Chris Masond352ac62008-09-29 15:18:18 -04004100 * min_trans indicates the oldest transaction that you are interested
4101 * in walking through. Any nodes or leaves older than min_trans are
4102 * skipped over (without reading them).
4103 *
Chris Mason3f157a22008-06-25 16:01:31 -04004104 * returns zero if something useful was found, < 0 on error and 1 if there
4105 * was nothing in the tree that matched the search criteria.
4106 */
4107int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004108 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004109 struct btrfs_path *path, int cache_only,
4110 u64 min_trans)
4111{
4112 struct extent_buffer *cur;
4113 struct btrfs_key found_key;
4114 int slot;
Yan96524802008-07-24 12:19:49 -04004115 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004116 u32 nritems;
4117 int level;
4118 int ret = 1;
4119
Chris Mason934d3752008-12-08 16:43:10 -05004120 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004121again:
4122 cur = btrfs_lock_root_node(root);
4123 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004124 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004125 path->nodes[level] = cur;
4126 path->locks[level] = 1;
4127
4128 if (btrfs_header_generation(cur) < min_trans) {
4129 ret = 1;
4130 goto out;
4131 }
Chris Masond3977122009-01-05 21:25:51 -05004132 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004133 nritems = btrfs_header_nritems(cur);
4134 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004135 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004136
Chris Mason323ac952008-10-01 19:05:46 -04004137 /* at the lowest level, we're done, setup the path and exit */
4138 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004139 if (slot >= nritems)
4140 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004141 ret = 0;
4142 path->slots[level] = slot;
4143 btrfs_item_key_to_cpu(cur, &found_key, slot);
4144 goto out;
4145 }
Yan96524802008-07-24 12:19:49 -04004146 if (sret && slot > 0)
4147 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004148 /*
4149 * check this node pointer against the cache_only and
4150 * min_trans parameters. If it isn't in cache or is too
4151 * old, skip to the next one.
4152 */
Chris Masond3977122009-01-05 21:25:51 -05004153 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004154 u64 blockptr;
4155 u64 gen;
4156 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004157 struct btrfs_disk_key disk_key;
4158
Chris Mason3f157a22008-06-25 16:01:31 -04004159 blockptr = btrfs_node_blockptr(cur, slot);
4160 gen = btrfs_node_ptr_generation(cur, slot);
4161 if (gen < min_trans) {
4162 slot++;
4163 continue;
4164 }
4165 if (!cache_only)
4166 break;
4167
Chris Masone02119d2008-09-05 16:13:11 -04004168 if (max_key) {
4169 btrfs_node_key(cur, &disk_key, slot);
4170 if (comp_keys(&disk_key, max_key) >= 0) {
4171 ret = 1;
4172 goto out;
4173 }
4174 }
4175
Chris Mason3f157a22008-06-25 16:01:31 -04004176 tmp = btrfs_find_tree_block(root, blockptr,
4177 btrfs_level_size(root, level - 1));
4178
4179 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4180 free_extent_buffer(tmp);
4181 break;
4182 }
4183 if (tmp)
4184 free_extent_buffer(tmp);
4185 slot++;
4186 }
Chris Masone02119d2008-09-05 16:13:11 -04004187find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004188 /*
4189 * we didn't find a candidate key in this node, walk forward
4190 * and find another one
4191 */
4192 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004193 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004194 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004195 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004196 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004197 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004198 btrfs_release_path(root, path);
4199 goto again;
4200 } else {
4201 goto out;
4202 }
4203 }
4204 /* save our key for returning back */
4205 btrfs_node_key_to_cpu(cur, &found_key, slot);
4206 path->slots[level] = slot;
4207 if (level == path->lowest_level) {
4208 ret = 0;
4209 unlock_up(path, level, 1);
4210 goto out;
4211 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004212 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004213 cur = read_node_slot(root, cur, slot);
4214
4215 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004216
Chris Mason3f157a22008-06-25 16:01:31 -04004217 path->locks[level - 1] = 1;
4218 path->nodes[level - 1] = cur;
4219 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004220 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004221 }
4222out:
4223 if (ret == 0)
4224 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004225 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004226 return ret;
4227}
4228
4229/*
4230 * this is similar to btrfs_next_leaf, but does not try to preserve
4231 * and fixup the path. It looks for and returns the next key in the
4232 * tree based on the current path and the cache_only and min_trans
4233 * parameters.
4234 *
4235 * 0 is returned if another key is found, < 0 if there are any errors
4236 * and 1 is returned if there are no higher keys in the tree
4237 *
4238 * path->keep_locks should be set to 1 on the search made before
4239 * calling this function.
4240 */
Chris Masone7a84562008-06-25 16:01:31 -04004241int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004242 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004243 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004244{
Chris Masone7a84562008-06-25 16:01:31 -04004245 int slot;
4246 struct extent_buffer *c;
4247
Chris Mason934d3752008-12-08 16:43:10 -05004248 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004249 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004250 if (!path->nodes[level])
4251 return 1;
4252
4253 slot = path->slots[level] + 1;
4254 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004255next:
Chris Masone7a84562008-06-25 16:01:31 -04004256 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004257 int ret;
4258 int orig_lowest;
4259 struct btrfs_key cur_key;
4260 if (level + 1 >= BTRFS_MAX_LEVEL ||
4261 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004262 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004263
4264 if (path->locks[level + 1]) {
4265 level++;
4266 continue;
4267 }
4268
4269 slot = btrfs_header_nritems(c) - 1;
4270 if (level == 0)
4271 btrfs_item_key_to_cpu(c, &cur_key, slot);
4272 else
4273 btrfs_node_key_to_cpu(c, &cur_key, slot);
4274
4275 orig_lowest = path->lowest_level;
4276 btrfs_release_path(root, path);
4277 path->lowest_level = level;
4278 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4279 0, 0);
4280 path->lowest_level = orig_lowest;
4281 if (ret < 0)
4282 return ret;
4283
4284 c = path->nodes[level];
4285 slot = path->slots[level];
4286 if (ret == 0)
4287 slot++;
4288 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004289 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004290
Chris Masone7a84562008-06-25 16:01:31 -04004291 if (level == 0)
4292 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004293 else {
4294 u64 blockptr = btrfs_node_blockptr(c, slot);
4295 u64 gen = btrfs_node_ptr_generation(c, slot);
4296
4297 if (cache_only) {
4298 struct extent_buffer *cur;
4299 cur = btrfs_find_tree_block(root, blockptr,
4300 btrfs_level_size(root, level - 1));
4301 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4302 slot++;
4303 if (cur)
4304 free_extent_buffer(cur);
4305 goto next;
4306 }
4307 free_extent_buffer(cur);
4308 }
4309 if (gen < min_trans) {
4310 slot++;
4311 goto next;
4312 }
Chris Masone7a84562008-06-25 16:01:31 -04004313 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004314 }
Chris Masone7a84562008-06-25 16:01:31 -04004315 return 0;
4316 }
4317 return 1;
4318}
4319
Chris Mason7bb86312007-12-11 09:25:06 -05004320/*
Chris Mason925baed2008-06-25 16:01:30 -04004321 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004322 * returns 0 if it found something or 1 if there are no greater leaves.
4323 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004324 */
Chris Mason234b63a2007-03-13 10:46:10 -04004325int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004326{
4327 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004328 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004329 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004330 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004331 struct btrfs_key key;
4332 u32 nritems;
4333 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004334 int old_spinning = path->leave_spinning;
4335 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004336
4337 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004338 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004339 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004340
Chris Mason8e73f272009-04-03 10:14:18 -04004341 /*
4342 * we take the blocks in an order that upsets lockdep. Using
4343 * blocking mode is the only way around it.
4344 */
4345#ifdef CONFIG_DEBUG_LOCK_ALLOC
4346 force_blocking = 1;
4347#endif
Chris Mason925baed2008-06-25 16:01:30 -04004348
Chris Mason8e73f272009-04-03 10:14:18 -04004349 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4350again:
4351 level = 1;
4352 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004353 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004354
Chris Masona2135012008-06-25 16:01:30 -04004355 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004356
4357 if (!force_blocking)
4358 path->leave_spinning = 1;
4359
Chris Mason925baed2008-06-25 16:01:30 -04004360 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4361 path->keep_locks = 0;
4362
4363 if (ret < 0)
4364 return ret;
4365
Chris Masona2135012008-06-25 16:01:30 -04004366 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004367 /*
4368 * by releasing the path above we dropped all our locks. A balance
4369 * could have added more items next to the key that used to be
4370 * at the very end of the block. So, check again here and
4371 * advance the path if there are now more items available.
4372 */
Chris Masona2135012008-06-25 16:01:30 -04004373 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004374 if (ret == 0)
4375 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004376 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004377 goto done;
4378 }
Chris Masond97e63b2007-02-20 16:40:44 -05004379
Chris Masond3977122009-01-05 21:25:51 -05004380 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004381 if (!path->nodes[level]) {
4382 ret = 1;
4383 goto done;
4384 }
Chris Mason5f39d392007-10-15 16:14:19 -04004385
Chris Masond97e63b2007-02-20 16:40:44 -05004386 slot = path->slots[level] + 1;
4387 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004388 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004389 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004390 if (level == BTRFS_MAX_LEVEL) {
4391 ret = 1;
4392 goto done;
4393 }
Chris Masond97e63b2007-02-20 16:40:44 -05004394 continue;
4395 }
Chris Mason5f39d392007-10-15 16:14:19 -04004396
Chris Mason925baed2008-06-25 16:01:30 -04004397 if (next) {
4398 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004399 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004400 }
Chris Mason5f39d392007-10-15 16:14:19 -04004401
Chris Mason8e73f272009-04-03 10:14:18 -04004402 next = c;
4403 ret = read_block_for_search(NULL, root, path, &next, level,
4404 slot, &key);
4405 if (ret == -EAGAIN)
4406 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004407
Chris Mason76a05b32009-05-14 13:24:30 -04004408 if (ret < 0) {
4409 btrfs_release_path(root, path);
4410 goto done;
4411 }
4412
Chris Mason5cd57b22008-06-25 16:01:30 -04004413 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004414 ret = btrfs_try_spin_lock(next);
4415 if (!ret) {
4416 btrfs_set_path_blocking(path);
4417 btrfs_tree_lock(next);
4418 if (!force_blocking)
4419 btrfs_clear_path_blocking(path, next);
4420 }
4421 if (force_blocking)
4422 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004423 }
Chris Masond97e63b2007-02-20 16:40:44 -05004424 break;
4425 }
4426 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004427 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004428 level--;
4429 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004430 if (path->locks[level])
4431 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004432
Chris Mason5f39d392007-10-15 16:14:19 -04004433 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004434 path->nodes[level] = next;
4435 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004436 if (!path->skip_locking)
4437 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004438
Chris Masond97e63b2007-02-20 16:40:44 -05004439 if (!level)
4440 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004441
Chris Mason8e73f272009-04-03 10:14:18 -04004442 ret = read_block_for_search(NULL, root, path, &next, level,
4443 0, &key);
4444 if (ret == -EAGAIN)
4445 goto again;
4446
Chris Mason76a05b32009-05-14 13:24:30 -04004447 if (ret < 0) {
4448 btrfs_release_path(root, path);
4449 goto done;
4450 }
4451
Chris Mason5cd57b22008-06-25 16:01:30 -04004452 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004453 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004454 ret = btrfs_try_spin_lock(next);
4455 if (!ret) {
4456 btrfs_set_path_blocking(path);
4457 btrfs_tree_lock(next);
4458 if (!force_blocking)
4459 btrfs_clear_path_blocking(path, next);
4460 }
4461 if (force_blocking)
4462 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004463 }
Chris Masond97e63b2007-02-20 16:40:44 -05004464 }
Chris Mason8e73f272009-04-03 10:14:18 -04004465 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004466done:
4467 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004468 path->leave_spinning = old_spinning;
4469 if (!old_spinning)
4470 btrfs_set_path_blocking(path);
4471
4472 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004473}
Chris Mason0b86a832008-03-24 15:01:56 -04004474
Chris Mason3f157a22008-06-25 16:01:31 -04004475/*
4476 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4477 * searching until it gets past min_objectid or finds an item of 'type'
4478 *
4479 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4480 */
Chris Mason0b86a832008-03-24 15:01:56 -04004481int btrfs_previous_item(struct btrfs_root *root,
4482 struct btrfs_path *path, u64 min_objectid,
4483 int type)
4484{
4485 struct btrfs_key found_key;
4486 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004487 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004488 int ret;
4489
Chris Masond3977122009-01-05 21:25:51 -05004490 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004491 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004492 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004493 ret = btrfs_prev_leaf(root, path);
4494 if (ret != 0)
4495 return ret;
4496 } else {
4497 path->slots[0]--;
4498 }
4499 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004500 nritems = btrfs_header_nritems(leaf);
4501 if (nritems == 0)
4502 return 1;
4503 if (path->slots[0] == nritems)
4504 path->slots[0]--;
4505
Chris Mason0b86a832008-03-24 15:01:56 -04004506 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004507 if (found_key.objectid < min_objectid)
4508 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004509 if (found_key.type == type)
4510 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004511 if (found_key.objectid == min_objectid &&
4512 found_key.type < type)
4513 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004514 }
4515 return 1;
4516}