blob: d84089349c82c15d1801787f62cf97c4ed469119 [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);
Chris Masond97e63b2007-02-20 16:40:44 -050041
Chris Mason2c90e5d2007-04-02 10:50:19 -040042struct btrfs_path *btrfs_alloc_path(void)
43{
Chris Masondf24a2b2007-04-04 09:36:31 -040044 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050045 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040046 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040047}
48
Chris Masonb4ce94d2009-02-04 09:25:08 -050049/*
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
52 */
53noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54{
55 int i;
56 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
57 if (p->nodes[i] && p->locks[i])
58 btrfs_set_lock_blocking(p->nodes[i]);
59 }
60}
61
62/*
63 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050064 *
65 * held is used to keep lockdep happy, when lockdep is enabled
66 * we set held to a blocking lock before we go around and
67 * retake all the spinlocks in the path. You can safely use NULL
68 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050069 */
Chris Mason4008c042009-02-12 14:09:45 -050070noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
71 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050072{
73 int i;
Chris Mason4008c042009-02-12 14:09:45 -050074
75#ifdef CONFIG_DEBUG_LOCK_ALLOC
76 /* lockdep really cares that we take all of these spinlocks
77 * in the right order. If any of the locks in the path are not
78 * currently blocking, it is going to complain. So, make really
79 * really sure by forcing the path to blocking before we clear
80 * the path blocking.
81 */
82 if (held)
83 btrfs_set_lock_blocking(held);
84 btrfs_set_path_blocking(p);
85#endif
86
87 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050088 if (p->nodes[i] && p->locks[i])
89 btrfs_clear_lock_blocking(p->nodes[i]);
90 }
Chris Mason4008c042009-02-12 14:09:45 -050091
92#ifdef CONFIG_DEBUG_LOCK_ALLOC
93 if (held)
94 btrfs_clear_lock_blocking(held);
95#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -050096}
97
Chris Masond352ac62008-09-29 15:18:18 -040098/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040099void btrfs_free_path(struct btrfs_path *p)
100{
Jesper Juhlff175d52010-12-25 21:22:30 +0000101 if (!p)
102 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200103 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400104 kmem_cache_free(btrfs_path_cachep, p);
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/*
108 * path release drops references on the extent buffers in the path
109 * and it drops any locks held by this path
110 *
111 * It is safe to call this on paths that no locks or extent buffers held.
112 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200113noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500114{
115 int i;
Chris Masona2135012008-06-25 16:01:30 -0400116
Chris Mason234b63a2007-03-13 10:46:10 -0400117 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400118 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500119 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400120 continue;
121 if (p->locks[i]) {
122 btrfs_tree_unlock(p->nodes[i]);
123 p->locks[i] = 0;
124 }
Chris Mason5f39d392007-10-15 16:14:19 -0400125 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400126 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500127 }
128}
129
Chris Masond352ac62008-09-29 15:18:18 -0400130/*
131 * safely gets a reference on the root node of a tree. A lock
132 * is not taken, so a concurrent writer may put a different node
133 * at the root of the tree. See btrfs_lock_root_node for the
134 * looping required.
135 *
136 * The extent buffer returned by this has a reference taken, so
137 * it won't disappear. It may stop being the root of the tree
138 * at any time because there are no locks held.
139 */
Chris Mason925baed2008-06-25 16:01:30 -0400140struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
141{
142 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400143
144 rcu_read_lock();
145 eb = rcu_dereference(root->node);
Chris Mason925baed2008-06-25 16:01:30 -0400146 extent_buffer_get(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400147 rcu_read_unlock();
Chris Mason925baed2008-06-25 16:01:30 -0400148 return eb;
149}
150
Chris Masond352ac62008-09-29 15:18:18 -0400151/* loop around taking references on and locking the root node of the
152 * tree until you end up with a lock on the root. A locked buffer
153 * is returned, with a reference held.
154 */
Chris Mason925baed2008-06-25 16:01:30 -0400155struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
156{
157 struct extent_buffer *eb;
158
Chris Masond3977122009-01-05 21:25:51 -0500159 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400160 eb = btrfs_root_node(root);
161 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400162 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400163 break;
Chris Mason925baed2008-06-25 16:01:30 -0400164 btrfs_tree_unlock(eb);
165 free_extent_buffer(eb);
166 }
167 return eb;
168}
169
Chris Masond352ac62008-09-29 15:18:18 -0400170/* cowonly root (everything not a reference counted cow subvolume), just get
171 * put onto a simple dirty list. transaction.c walks this to make sure they
172 * get properly updated on disk.
173 */
Chris Mason0b86a832008-03-24 15:01:56 -0400174static void add_root_to_dirty_list(struct btrfs_root *root)
175{
176 if (root->track_dirty && list_empty(&root->dirty_list)) {
177 list_add(&root->dirty_list,
178 &root->fs_info->dirty_cowonly_roots);
179 }
180}
181
Chris Masond352ac62008-09-29 15:18:18 -0400182/*
183 * used by snapshot creation to make a copy of a root for a tree with
184 * a given objectid. The buffer with the new root node is returned in
185 * cow_ret, and this func returns zero on success or a negative error code.
186 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500187int btrfs_copy_root(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root,
189 struct extent_buffer *buf,
190 struct extent_buffer **cow_ret, u64 new_root_objectid)
191{
192 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500193 int ret = 0;
194 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400195 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500196
197 WARN_ON(root->ref_cows && trans->transid !=
198 root->fs_info->running_transaction->transid);
199 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
200
201 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400202 if (level == 0)
203 btrfs_item_key(buf, &disk_key, 0);
204 else
205 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400206
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400207 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
208 new_root_objectid, &disk_key, level,
209 buf->start, 0);
210 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500211 return PTR_ERR(cow);
212
213 copy_extent_buffer(cow, buf, 0, 0, cow->len);
214 btrfs_set_header_bytenr(cow, cow->start);
215 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400216 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
217 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
218 BTRFS_HEADER_FLAG_RELOC);
219 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
220 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
221 else
222 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500223
Yan Zheng2b820322008-11-17 21:11:30 -0500224 write_extent_buffer(cow, root->fs_info->fsid,
225 (unsigned long)btrfs_header_fsid(cow),
226 BTRFS_FSID_SIZE);
227
Chris Masonbe20aa92007-12-17 20:14:01 -0500228 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400229 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
230 ret = btrfs_inc_ref(trans, root, cow, 1);
231 else
232 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500233
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 if (ret)
235 return ret;
236
237 btrfs_mark_buffer_dirty(cow);
238 *cow_ret = cow;
239 return 0;
240}
241
Chris Masond352ac62008-09-29 15:18:18 -0400242/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400243 * check if the tree block can be shared by multiple trees
244 */
245int btrfs_block_can_be_shared(struct btrfs_root *root,
246 struct extent_buffer *buf)
247{
248 /*
249 * Tree blocks not in refernece counted trees and tree roots
250 * are never shared. If a block was allocated after the last
251 * snapshot and the block was not allocated by tree relocation,
252 * we know the block is not shared.
253 */
254 if (root->ref_cows &&
255 buf != root->node && buf != root->commit_root &&
256 (btrfs_header_generation(buf) <=
257 btrfs_root_last_snapshot(&root->root_item) ||
258 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
259 return 1;
260#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
261 if (root->ref_cows &&
262 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
263 return 1;
264#endif
265 return 0;
266}
267
268static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
269 struct btrfs_root *root,
270 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400271 struct extent_buffer *cow,
272 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400273{
274 u64 refs;
275 u64 owner;
276 u64 flags;
277 u64 new_flags = 0;
278 int ret;
279
280 /*
281 * Backrefs update rules:
282 *
283 * Always use full backrefs for extent pointers in tree block
284 * allocated by tree relocation.
285 *
286 * If a shared tree block is no longer referenced by its owner
287 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
288 * use full backrefs for extent pointers in tree block.
289 *
290 * If a tree block is been relocating
291 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
292 * use full backrefs for extent pointers in tree block.
293 * The reason for this is some operations (such as drop tree)
294 * are only allowed for blocks use full backrefs.
295 */
296
297 if (btrfs_block_can_be_shared(root, buf)) {
298 ret = btrfs_lookup_extent_info(trans, root, buf->start,
299 buf->len, &refs, &flags);
300 BUG_ON(ret);
301 BUG_ON(refs == 0);
302 } else {
303 refs = 1;
304 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
305 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
306 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
307 else
308 flags = 0;
309 }
310
311 owner = btrfs_header_owner(buf);
312 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
313 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
314
315 if (refs > 1) {
316 if ((owner == root->root_key.objectid ||
317 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
318 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
319 ret = btrfs_inc_ref(trans, root, buf, 1);
320 BUG_ON(ret);
321
322 if (root->root_key.objectid ==
323 BTRFS_TREE_RELOC_OBJECTID) {
324 ret = btrfs_dec_ref(trans, root, buf, 0);
325 BUG_ON(ret);
326 ret = btrfs_inc_ref(trans, root, cow, 1);
327 BUG_ON(ret);
328 }
329 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
330 } else {
331
332 if (root->root_key.objectid ==
333 BTRFS_TREE_RELOC_OBJECTID)
334 ret = btrfs_inc_ref(trans, root, cow, 1);
335 else
336 ret = btrfs_inc_ref(trans, root, cow, 0);
337 BUG_ON(ret);
338 }
339 if (new_flags != 0) {
340 ret = btrfs_set_disk_extent_flags(trans, root,
341 buf->start,
342 buf->len,
343 new_flags, 0);
344 BUG_ON(ret);
345 }
346 } else {
347 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
348 if (root->root_key.objectid ==
349 BTRFS_TREE_RELOC_OBJECTID)
350 ret = btrfs_inc_ref(trans, root, cow, 1);
351 else
352 ret = btrfs_inc_ref(trans, root, cow, 0);
353 BUG_ON(ret);
354 ret = btrfs_dec_ref(trans, root, buf, 1);
355 BUG_ON(ret);
356 }
357 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400358 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400359 }
360 return 0;
361}
362
363/*
Chris Masond3977122009-01-05 21:25:51 -0500364 * does the dirty work in cow of a single block. The parent block (if
365 * supplied) is updated to point to the new cow copy. The new buffer is marked
366 * dirty and returned locked. If you modify the block it needs to be marked
367 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400368 *
369 * search_start -- an allocation hint for the new block
370 *
Chris Masond3977122009-01-05 21:25:51 -0500371 * empty_size -- a hint that you plan on doing more cow. This is the size in
372 * bytes the allocator should try to find free next to the block it returns.
373 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400374 */
Chris Masond3977122009-01-05 21:25:51 -0500375static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400376 struct btrfs_root *root,
377 struct extent_buffer *buf,
378 struct extent_buffer *parent, int parent_slot,
379 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400380 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400381{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400382 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400383 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500384 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400385 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400386 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400387 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400388
Chris Mason925baed2008-06-25 16:01:30 -0400389 if (*cow_ret == buf)
390 unlock_orig = 1;
391
Chris Masonb9447ef2009-03-09 11:45:38 -0400392 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400393
Chris Mason7bb86312007-12-11 09:25:06 -0500394 WARN_ON(root->ref_cows && trans->transid !=
395 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400396 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400397
Chris Mason7bb86312007-12-11 09:25:06 -0500398 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400399
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400400 if (level == 0)
401 btrfs_item_key(buf, &disk_key, 0);
402 else
403 btrfs_node_key(buf, &disk_key, 0);
404
405 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
406 if (parent)
407 parent_start = parent->start;
408 else
409 parent_start = 0;
410 } else
411 parent_start = 0;
412
413 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
414 root->root_key.objectid, &disk_key,
415 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400416 if (IS_ERR(cow))
417 return PTR_ERR(cow);
418
Chris Masonb4ce94d2009-02-04 09:25:08 -0500419 /* cow is set to blocking by btrfs_init_new_buffer */
420
Chris Mason5f39d392007-10-15 16:14:19 -0400421 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400422 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400423 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400424 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
425 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
426 BTRFS_HEADER_FLAG_RELOC);
427 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
428 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
429 else
430 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400431
Yan Zheng2b820322008-11-17 21:11:30 -0500432 write_extent_buffer(cow, root->fs_info->fsid,
433 (unsigned long)btrfs_header_fsid(cow),
434 BTRFS_FSID_SIZE);
435
Yan, Zhengf0486c62010-05-16 10:46:25 -0400436 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400437
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400438 if (root->ref_cows)
439 btrfs_reloc_cow_block(trans, root, buf, cow);
440
Chris Mason6702ed42007-08-07 16:15:09 -0400441 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400442 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400443 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
444 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
445 parent_start = buf->start;
446 else
447 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400448
Chris Mason5f39d392007-10-15 16:14:19 -0400449 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400450 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400451
Yan, Zhengf0486c62010-05-16 10:46:25 -0400452 btrfs_free_tree_block(trans, root, buf, parent_start,
453 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400454 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400455 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400456 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400457 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
458 parent_start = parent->start;
459 else
460 parent_start = 0;
461
462 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400463 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400464 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500465 btrfs_set_node_ptr_generation(parent, parent_slot,
466 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400467 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400468 btrfs_free_tree_block(trans, root, buf, parent_start,
469 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400470 }
Chris Mason925baed2008-06-25 16:01:30 -0400471 if (unlock_orig)
472 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400473 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400474 btrfs_mark_buffer_dirty(cow);
475 *cow_ret = cow;
476 return 0;
477}
478
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400479static inline int should_cow_block(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root,
481 struct extent_buffer *buf)
482{
483 if (btrfs_header_generation(buf) == trans->transid &&
484 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
485 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
486 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
487 return 0;
488 return 1;
489}
490
Chris Masond352ac62008-09-29 15:18:18 -0400491/*
492 * cows a single block, see __btrfs_cow_block for the real work.
493 * This version of it has extra checks so that a block isn't cow'd more than
494 * once per transaction, as long as it hasn't been written yet
495 */
Chris Masond3977122009-01-05 21:25:51 -0500496noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400497 struct btrfs_root *root, struct extent_buffer *buf,
498 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400499 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500500{
Chris Mason6702ed42007-08-07 16:15:09 -0400501 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400502 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500503
Chris Masonccd467d2007-06-28 15:57:36 -0400504 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500505 printk(KERN_CRIT "trans %llu running %llu\n",
506 (unsigned long long)trans->transid,
507 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400508 root->fs_info->running_transaction->transid);
509 WARN_ON(1);
510 }
511 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500512 printk(KERN_CRIT "trans %llu running %llu\n",
513 (unsigned long long)trans->transid,
514 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400515 WARN_ON(1);
516 }
Chris Masondc17ff82008-01-08 15:46:30 -0500517
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400518 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500519 *cow_ret = buf;
520 return 0;
521 }
Chris Masonc4876852009-02-04 09:24:25 -0500522
Chris Mason0b86a832008-03-24 15:01:56 -0400523 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500524
525 if (parent)
526 btrfs_set_lock_blocking(parent);
527 btrfs_set_lock_blocking(buf);
528
Chris Masonf510cfe2007-10-15 16:14:48 -0400529 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400530 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000531
532 trace_btrfs_cow_block(root, buf, *cow_ret);
533
Chris Masonf510cfe2007-10-15 16:14:48 -0400534 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400535}
536
Chris Masond352ac62008-09-29 15:18:18 -0400537/*
538 * helper function for defrag to decide if two blocks pointed to by a
539 * node are actually close by
540 */
Chris Mason6b800532007-10-15 16:17:34 -0400541static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400542{
Chris Mason6b800532007-10-15 16:17:34 -0400543 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400544 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400545 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400546 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500547 return 0;
548}
549
Chris Mason081e9572007-11-06 10:26:24 -0500550/*
551 * compare two keys in a memcmp fashion
552 */
553static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
554{
555 struct btrfs_key k1;
556
557 btrfs_disk_key_to_cpu(&k1, disk);
558
Diego Calleja20736ab2009-07-24 11:06:52 -0400559 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500560}
561
Josef Bacikf3465ca2008-11-12 14:19:50 -0500562/*
563 * same as comp_keys only with two btrfs_key's
564 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400565int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500566{
567 if (k1->objectid > k2->objectid)
568 return 1;
569 if (k1->objectid < k2->objectid)
570 return -1;
571 if (k1->type > k2->type)
572 return 1;
573 if (k1->type < k2->type)
574 return -1;
575 if (k1->offset > k2->offset)
576 return 1;
577 if (k1->offset < k2->offset)
578 return -1;
579 return 0;
580}
Chris Mason081e9572007-11-06 10:26:24 -0500581
Chris Masond352ac62008-09-29 15:18:18 -0400582/*
583 * this is used by the defrag code to go through all the
584 * leaves pointed to by a node and reallocate them so that
585 * disk order is close to key order
586 */
Chris Mason6702ed42007-08-07 16:15:09 -0400587int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400588 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400589 int start_slot, int cache_only, u64 *last_ret,
590 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400591{
Chris Mason6b800532007-10-15 16:17:34 -0400592 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400593 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400594 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400595 u64 search_start = *last_ret;
596 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400597 u64 other;
598 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400599 int end_slot;
600 int i;
601 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400602 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400603 int uptodate;
604 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500605 int progress_passed = 0;
606 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400607
Chris Mason5708b952007-10-25 15:43:18 -0400608 parent_level = btrfs_header_level(parent);
609 if (cache_only && parent_level != 1)
610 return 0;
611
Chris Masond3977122009-01-05 21:25:51 -0500612 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400613 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500614 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400615 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400616
Chris Mason6b800532007-10-15 16:17:34 -0400617 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400618 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400619 end_slot = parent_nritems;
620
621 if (parent_nritems == 1)
622 return 0;
623
Chris Masonb4ce94d2009-02-04 09:25:08 -0500624 btrfs_set_lock_blocking(parent);
625
Chris Mason6702ed42007-08-07 16:15:09 -0400626 for (i = start_slot; i < end_slot; i++) {
627 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400628
Chris Mason5708b952007-10-25 15:43:18 -0400629 if (!parent->map_token) {
630 map_extent_buffer(parent,
631 btrfs_node_key_ptr_offset(i),
632 sizeof(struct btrfs_key_ptr),
633 &parent->map_token, &parent->kaddr,
634 &parent->map_start, &parent->map_len,
635 KM_USER1);
636 }
Chris Mason081e9572007-11-06 10:26:24 -0500637 btrfs_node_key(parent, &disk_key, i);
638 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
639 continue;
640
641 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400642 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400643 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400644 if (last_block == 0)
645 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400646
Chris Mason6702ed42007-08-07 16:15:09 -0400647 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400648 other = btrfs_node_blockptr(parent, i - 1);
649 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400650 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400651 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400652 other = btrfs_node_blockptr(parent, i + 1);
653 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400654 }
Chris Masone9d0b132007-08-10 14:06:19 -0400655 if (close) {
656 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400657 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400658 }
Chris Mason5708b952007-10-25 15:43:18 -0400659 if (parent->map_token) {
660 unmap_extent_buffer(parent, parent->map_token,
661 KM_USER1);
662 parent->map_token = NULL;
663 }
Chris Mason6702ed42007-08-07 16:15:09 -0400664
Chris Mason6b800532007-10-15 16:17:34 -0400665 cur = btrfs_find_tree_block(root, blocknr, blocksize);
666 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400667 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400668 else
669 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400670 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400671 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400672 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400673 continue;
674 }
Chris Mason6b800532007-10-15 16:17:34 -0400675 if (!cur) {
676 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400677 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000678 if (!cur)
679 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400680 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400681 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400682 }
Chris Mason6702ed42007-08-07 16:15:09 -0400683 }
Chris Masone9d0b132007-08-10 14:06:19 -0400684 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400685 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400686
Chris Masone7a84562008-06-25 16:01:31 -0400687 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500688 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400689 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400690 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400691 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400692 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400693 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400694 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400695 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400696 break;
Yan252c38f2007-08-29 09:11:44 -0400697 }
Chris Masone7a84562008-06-25 16:01:31 -0400698 search_start = cur->start;
699 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400700 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400701 btrfs_tree_unlock(cur);
702 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400703 }
Chris Mason5708b952007-10-25 15:43:18 -0400704 if (parent->map_token) {
705 unmap_extent_buffer(parent, parent->map_token,
706 KM_USER1);
707 parent->map_token = NULL;
708 }
Chris Mason6702ed42007-08-07 16:15:09 -0400709 return err;
710}
711
Chris Mason74123bd2007-02-02 11:05:29 -0500712/*
713 * The leaf data grows from end-to-front in the node.
714 * this returns the address of the start of the last item,
715 * which is the stop of the leaf data stack
716 */
Chris Mason123abc82007-03-14 14:14:43 -0400717static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400718 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500719{
Chris Mason5f39d392007-10-15 16:14:19 -0400720 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500721 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400722 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400723 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500724}
725
Chris Masonaa5d6be2007-02-28 16:35:06 -0500726
Chris Mason74123bd2007-02-02 11:05:29 -0500727/*
Chris Mason5f39d392007-10-15 16:14:19 -0400728 * search for key in the extent_buffer. The items start at offset p,
729 * and they are item_size apart. There are 'max' items in p.
730 *
Chris Mason74123bd2007-02-02 11:05:29 -0500731 * the slot in the array is returned via slot, and it points to
732 * the place where you would insert key if it is not found in
733 * the array.
734 *
735 * slot may point to max if the key is bigger than all of the keys
736 */
Chris Masone02119d2008-09-05 16:13:11 -0400737static noinline int generic_bin_search(struct extent_buffer *eb,
738 unsigned long p,
739 int item_size, struct btrfs_key *key,
740 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500741{
742 int low = 0;
743 int high = max;
744 int mid;
745 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400746 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400747 struct btrfs_disk_key unaligned;
748 unsigned long offset;
749 char *map_token = NULL;
750 char *kaddr = NULL;
751 unsigned long map_start = 0;
752 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400753 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500754
Chris Masond3977122009-01-05 21:25:51 -0500755 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500756 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400757 offset = p + mid * item_size;
758
759 if (!map_token || offset < map_start ||
760 (offset + sizeof(struct btrfs_disk_key)) >
761 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400762 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400763 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400764 map_token = NULL;
765 }
Chris Mason934d3752008-12-08 16:43:10 -0500766
767 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400768 sizeof(struct btrfs_disk_key),
769 &map_token, &kaddr,
770 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400771
Chris Mason479965d2007-10-15 16:14:27 -0400772 if (!err) {
773 tmp = (struct btrfs_disk_key *)(kaddr + offset -
774 map_start);
775 } else {
776 read_extent_buffer(eb, &unaligned,
777 offset, sizeof(unaligned));
778 tmp = &unaligned;
779 }
780
Chris Mason5f39d392007-10-15 16:14:19 -0400781 } else {
782 tmp = (struct btrfs_disk_key *)(kaddr + offset -
783 map_start);
784 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500785 ret = comp_keys(tmp, key);
786
787 if (ret < 0)
788 low = mid + 1;
789 else if (ret > 0)
790 high = mid;
791 else {
792 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400793 if (map_token)
794 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500795 return 0;
796 }
797 }
798 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400799 if (map_token)
800 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500801 return 1;
802}
803
Chris Mason97571fd2007-02-24 13:39:08 -0500804/*
805 * simple bin_search frontend that does the right thing for
806 * leaves vs nodes
807 */
Chris Mason5f39d392007-10-15 16:14:19 -0400808static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
809 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500810{
Chris Mason5f39d392007-10-15 16:14:19 -0400811 if (level == 0) {
812 return generic_bin_search(eb,
813 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400814 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400815 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400816 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500817 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400818 return generic_bin_search(eb,
819 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400820 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400821 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400822 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500823 }
824 return -1;
825}
826
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400827int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
828 int level, int *slot)
829{
830 return bin_search(eb, key, level, slot);
831}
832
Yan, Zhengf0486c62010-05-16 10:46:25 -0400833static void root_add_used(struct btrfs_root *root, u32 size)
834{
835 spin_lock(&root->accounting_lock);
836 btrfs_set_root_used(&root->root_item,
837 btrfs_root_used(&root->root_item) + size);
838 spin_unlock(&root->accounting_lock);
839}
840
841static void root_sub_used(struct btrfs_root *root, u32 size)
842{
843 spin_lock(&root->accounting_lock);
844 btrfs_set_root_used(&root->root_item,
845 btrfs_root_used(&root->root_item) - size);
846 spin_unlock(&root->accounting_lock);
847}
848
Chris Masond352ac62008-09-29 15:18:18 -0400849/* given a node and slot number, this reads the blocks it points to. The
850 * extent buffer is returned with a reference taken (but unlocked).
851 * NULL is returned on error.
852 */
Chris Masone02119d2008-09-05 16:13:11 -0400853static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400854 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500855{
Chris Masonca7a79a2008-05-12 12:59:19 -0400856 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500857 if (slot < 0)
858 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400859 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500860 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400861
862 BUG_ON(level == 0);
863
Chris Masondb945352007-10-15 16:15:53 -0400864 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400865 btrfs_level_size(root, level - 1),
866 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500867}
868
Chris Masond352ac62008-09-29 15:18:18 -0400869/*
870 * node level balancing, used to make sure nodes are in proper order for
871 * item deletion. We balance from the top down, so we have to make sure
872 * that a deletion won't leave an node completely empty later on.
873 */
Chris Masone02119d2008-09-05 16:13:11 -0400874static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500875 struct btrfs_root *root,
876 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500877{
Chris Mason5f39d392007-10-15 16:14:19 -0400878 struct extent_buffer *right = NULL;
879 struct extent_buffer *mid;
880 struct extent_buffer *left = NULL;
881 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500882 int ret = 0;
883 int wret;
884 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500885 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500886 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500887
888 if (level == 0)
889 return 0;
890
Chris Mason5f39d392007-10-15 16:14:19 -0400891 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500892
Chris Mason925baed2008-06-25 16:01:30 -0400893 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500894 WARN_ON(btrfs_header_generation(mid) != trans->transid);
895
Chris Mason1d4f8a02007-03-13 09:28:32 -0400896 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500897
Chris Mason234b63a2007-03-13 10:46:10 -0400898 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400899 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500900 pslot = path->slots[level + 1];
901
Chris Mason40689472007-03-17 14:29:23 -0400902 /*
903 * deal with the case where there is only one pointer in the root
904 * by promoting the node below to a root
905 */
Chris Mason5f39d392007-10-15 16:14:19 -0400906 if (!parent) {
907 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500908
Chris Mason5f39d392007-10-15 16:14:19 -0400909 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500910 return 0;
911
912 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400913 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500914 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400915 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500916 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400917 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400918 if (ret) {
919 btrfs_tree_unlock(child);
920 free_extent_buffer(child);
921 goto enospc;
922 }
Yan2f375ab2008-02-01 14:58:07 -0500923
Chris Mason240f62c2011-03-23 14:54:42 -0400924 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400925
Chris Mason0b86a832008-03-24 15:01:56 -0400926 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400927 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500928
Chris Mason925baed2008-06-25 16:01:30 -0400929 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500930 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400931 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400932 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500933 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400934 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400935
936 root_sub_used(root, mid->len);
937 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500938 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400939 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400940 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500941 }
Chris Mason5f39d392007-10-15 16:14:19 -0400942 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400943 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500944 return 0;
945
Andi Kleen559af822010-10-29 15:14:37 -0400946 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400947
Chris Mason5f39d392007-10-15 16:14:19 -0400948 left = read_node_slot(root, parent, pslot - 1);
949 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400950 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500951 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400952 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400953 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400954 if (wret) {
955 ret = wret;
956 goto enospc;
957 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400958 }
Chris Mason5f39d392007-10-15 16:14:19 -0400959 right = read_node_slot(root, parent, pslot + 1);
960 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400961 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500962 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400963 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400964 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400965 if (wret) {
966 ret = wret;
967 goto enospc;
968 }
969 }
970
971 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400972 if (left) {
973 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400974 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500975 if (wret < 0)
976 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -0400977 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500978 }
Chris Mason79f95c82007-03-01 15:16:26 -0500979
980 /*
981 * then try to empty the right most buffer into the middle
982 */
Chris Mason5f39d392007-10-15 16:14:19 -0400983 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400984 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400985 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500986 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400987 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400988 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400989 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -0400990 wret = del_ptr(trans, root, path, level + 1, pslot +
991 1);
Chris Masonbb803952007-03-01 12:04:21 -0500992 if (wret)
993 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400994 root_sub_used(root, right->len);
995 btrfs_free_tree_block(trans, root, right, 0, 1);
996 free_extent_buffer(right);
997 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500998 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400999 struct btrfs_disk_key right_key;
1000 btrfs_node_key(right, &right_key, 0);
1001 btrfs_set_node_key(parent, &right_key, pslot + 1);
1002 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001003 }
1004 }
Chris Mason5f39d392007-10-15 16:14:19 -04001005 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001006 /*
1007 * we're not allowed to leave a node with one item in the
1008 * tree during a delete. A deletion from lower in the tree
1009 * could try to delete the only pointer in this node.
1010 * So, pull some keys from the left.
1011 * There has to be a left pointer at this point because
1012 * otherwise we would have pulled some pointers from the
1013 * right
1014 */
Chris Mason5f39d392007-10-15 16:14:19 -04001015 BUG_ON(!left);
1016 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001017 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001018 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001019 goto enospc;
1020 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001021 if (wret == 1) {
1022 wret = push_node_left(trans, root, left, mid, 1);
1023 if (wret < 0)
1024 ret = wret;
1025 }
Chris Mason79f95c82007-03-01 15:16:26 -05001026 BUG_ON(wret == 1);
1027 }
Chris Mason5f39d392007-10-15 16:14:19 -04001028 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001029 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001030 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001031 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001032 if (wret)
1033 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001034 root_sub_used(root, mid->len);
1035 btrfs_free_tree_block(trans, root, mid, 0, 1);
1036 free_extent_buffer(mid);
1037 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001038 } else {
1039 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001040 struct btrfs_disk_key mid_key;
1041 btrfs_node_key(mid, &mid_key, 0);
1042 btrfs_set_node_key(parent, &mid_key, pslot);
1043 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001044 }
Chris Masonbb803952007-03-01 12:04:21 -05001045
Chris Mason79f95c82007-03-01 15:16:26 -05001046 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001047 if (left) {
1048 if (btrfs_header_nritems(left) > orig_slot) {
1049 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001050 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001051 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001052 path->slots[level + 1] -= 1;
1053 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001054 if (mid) {
1055 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001056 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001057 }
Chris Masonbb803952007-03-01 12:04:21 -05001058 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001059 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001060 path->slots[level] = orig_slot;
1061 }
1062 }
Chris Mason79f95c82007-03-01 15:16:26 -05001063 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001064 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001065 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001066 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001067enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001068 if (right) {
1069 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001070 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001071 }
1072 if (left) {
1073 if (path->nodes[level] != left)
1074 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001075 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001076 }
Chris Masonbb803952007-03-01 12:04:21 -05001077 return ret;
1078}
1079
Chris Masond352ac62008-09-29 15:18:18 -04001080/* Node balancing for insertion. Here we only split or push nodes around
1081 * when they are completely full. This is also done top down, so we
1082 * have to be pessimistic.
1083 */
Chris Masond3977122009-01-05 21:25:51 -05001084static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001085 struct btrfs_root *root,
1086 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001087{
Chris Mason5f39d392007-10-15 16:14:19 -04001088 struct extent_buffer *right = NULL;
1089 struct extent_buffer *mid;
1090 struct extent_buffer *left = NULL;
1091 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001092 int ret = 0;
1093 int wret;
1094 int pslot;
1095 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001096
1097 if (level == 0)
1098 return 1;
1099
Chris Mason5f39d392007-10-15 16:14:19 -04001100 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001101 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001102
1103 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001104 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001105 pslot = path->slots[level + 1];
1106
Chris Mason5f39d392007-10-15 16:14:19 -04001107 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001108 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001109
Chris Mason5f39d392007-10-15 16:14:19 -04001110 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001111
1112 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001113 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001114 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001115
1116 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001117 btrfs_set_lock_blocking(left);
1118
Chris Mason5f39d392007-10-15 16:14:19 -04001119 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001120 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1121 wret = 1;
1122 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001123 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001124 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001125 if (ret)
1126 wret = 1;
1127 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001128 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001129 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001130 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001131 }
Chris Masone66f7092007-04-20 13:16:02 -04001132 if (wret < 0)
1133 ret = wret;
1134 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001135 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001136 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001137 btrfs_node_key(mid, &disk_key, 0);
1138 btrfs_set_node_key(parent, &disk_key, pslot);
1139 btrfs_mark_buffer_dirty(parent);
1140 if (btrfs_header_nritems(left) > orig_slot) {
1141 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001142 path->slots[level + 1] -= 1;
1143 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001144 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001145 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001146 } else {
1147 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001148 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001149 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001150 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001151 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001152 }
Chris Masone66f7092007-04-20 13:16:02 -04001153 return 0;
1154 }
Chris Mason925baed2008-06-25 16:01:30 -04001155 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001156 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001157 }
Chris Mason925baed2008-06-25 16:01:30 -04001158 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001159
1160 /*
1161 * then try to empty the right most buffer into the middle
1162 */
Chris Mason5f39d392007-10-15 16:14:19 -04001163 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001164 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001165
Chris Mason925baed2008-06-25 16:01:30 -04001166 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001167 btrfs_set_lock_blocking(right);
1168
Chris Mason5f39d392007-10-15 16:14:19 -04001169 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001170 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1171 wret = 1;
1172 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001173 ret = btrfs_cow_block(trans, root, right,
1174 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001175 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001176 if (ret)
1177 wret = 1;
1178 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001179 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001180 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001182 }
Chris Masone66f7092007-04-20 13:16:02 -04001183 if (wret < 0)
1184 ret = wret;
1185 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001186 struct btrfs_disk_key disk_key;
1187
1188 btrfs_node_key(right, &disk_key, 0);
1189 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1190 btrfs_mark_buffer_dirty(parent);
1191
1192 if (btrfs_header_nritems(mid) <= orig_slot) {
1193 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001194 path->slots[level + 1] += 1;
1195 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001196 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001197 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001198 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001199 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001200 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001201 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001202 }
Chris Masone66f7092007-04-20 13:16:02 -04001203 return 0;
1204 }
Chris Mason925baed2008-06-25 16:01:30 -04001205 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001206 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001207 }
Chris Masone66f7092007-04-20 13:16:02 -04001208 return 1;
1209}
1210
Chris Mason74123bd2007-02-02 11:05:29 -05001211/*
Chris Masond352ac62008-09-29 15:18:18 -04001212 * readahead one full node of leaves, finding things that are close
1213 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001214 */
Chris Masonc8c42862009-04-03 10:14:18 -04001215static void reada_for_search(struct btrfs_root *root,
1216 struct btrfs_path *path,
1217 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001218{
Chris Mason5f39d392007-10-15 16:14:19 -04001219 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001220 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001221 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001222 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001223 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001224 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001225 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001226 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001227 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001228 u32 nr;
1229 u32 blocksize;
1230 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001231
Chris Masona6b6e752007-10-15 16:22:39 -04001232 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001233 return;
1234
Chris Mason6702ed42007-08-07 16:15:09 -04001235 if (!path->nodes[level])
1236 return;
1237
Chris Mason5f39d392007-10-15 16:14:19 -04001238 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001239
Chris Mason3c69fae2007-08-07 15:52:22 -04001240 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001241 blocksize = btrfs_level_size(root, level - 1);
1242 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001243 if (eb) {
1244 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001245 return;
1246 }
1247
Chris Masona7175312009-01-22 09:23:10 -05001248 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001249
Chris Mason5f39d392007-10-15 16:14:19 -04001250 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001251 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001252 while (1) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001253 if (!node->map_token) {
1254 unsigned long offset = btrfs_node_key_ptr_offset(nr);
1255 map_private_extent_buffer(node, offset,
1256 sizeof(struct btrfs_key_ptr),
1257 &node->map_token,
1258 &node->kaddr,
1259 &node->map_start,
1260 &node->map_len, KM_USER1);
1261 }
Chris Mason6b800532007-10-15 16:17:34 -04001262 if (direction < 0) {
1263 if (nr == 0)
1264 break;
1265 nr--;
1266 } else if (direction > 0) {
1267 nr++;
1268 if (nr >= nritems)
1269 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001270 }
Chris Mason01f46652007-12-21 16:24:26 -05001271 if (path->reada < 0 && objectid) {
1272 btrfs_node_key(node, &disk_key, nr);
1273 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1274 break;
1275 }
Chris Mason6b800532007-10-15 16:17:34 -04001276 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001277 if ((search <= target && target - search <= 65536) ||
1278 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001279 gen = btrfs_node_ptr_generation(node, nr);
1280 if (node->map_token) {
1281 unmap_extent_buffer(node, node->map_token,
1282 KM_USER1);
1283 node->map_token = NULL;
1284 }
1285 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001286 nread += blocksize;
1287 }
1288 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001289 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001290 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001291 }
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001292 if (node->map_token) {
1293 unmap_extent_buffer(node, node->map_token, KM_USER1);
1294 node->map_token = NULL;
1295 }
Chris Mason3c69fae2007-08-07 15:52:22 -04001296}
Chris Mason925baed2008-06-25 16:01:30 -04001297
Chris Masond352ac62008-09-29 15:18:18 -04001298/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001299 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1300 * cache
1301 */
1302static noinline int reada_for_balance(struct btrfs_root *root,
1303 struct btrfs_path *path, int level)
1304{
1305 int slot;
1306 int nritems;
1307 struct extent_buffer *parent;
1308 struct extent_buffer *eb;
1309 u64 gen;
1310 u64 block1 = 0;
1311 u64 block2 = 0;
1312 int ret = 0;
1313 int blocksize;
1314
Chris Mason8c594ea2009-04-20 15:50:10 -04001315 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001316 if (!parent)
1317 return 0;
1318
1319 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001320 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001321 blocksize = btrfs_level_size(root, level);
1322
1323 if (slot > 0) {
1324 block1 = btrfs_node_blockptr(parent, slot - 1);
1325 gen = btrfs_node_ptr_generation(parent, slot - 1);
1326 eb = btrfs_find_tree_block(root, block1, blocksize);
1327 if (eb && btrfs_buffer_uptodate(eb, gen))
1328 block1 = 0;
1329 free_extent_buffer(eb);
1330 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001331 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001332 block2 = btrfs_node_blockptr(parent, slot + 1);
1333 gen = btrfs_node_ptr_generation(parent, slot + 1);
1334 eb = btrfs_find_tree_block(root, block2, blocksize);
1335 if (eb && btrfs_buffer_uptodate(eb, gen))
1336 block2 = 0;
1337 free_extent_buffer(eb);
1338 }
1339 if (block1 || block2) {
1340 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001341
1342 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001343 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001344
1345 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001346 if (block1)
1347 readahead_tree_block(root, block1, blocksize, 0);
1348 if (block2)
1349 readahead_tree_block(root, block2, blocksize, 0);
1350
1351 if (block1) {
1352 eb = read_tree_block(root, block1, blocksize, 0);
1353 free_extent_buffer(eb);
1354 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001355 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001356 eb = read_tree_block(root, block2, blocksize, 0);
1357 free_extent_buffer(eb);
1358 }
1359 }
1360 return ret;
1361}
1362
1363
1364/*
Chris Masond3977122009-01-05 21:25:51 -05001365 * when we walk down the tree, it is usually safe to unlock the higher layers
1366 * in the tree. The exceptions are when our path goes through slot 0, because
1367 * operations on the tree might require changing key pointers higher up in the
1368 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001369 *
Chris Masond3977122009-01-05 21:25:51 -05001370 * callers might also have set path->keep_locks, which tells this code to keep
1371 * the lock if the path points to the last slot in the block. This is part of
1372 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001373 *
Chris Masond3977122009-01-05 21:25:51 -05001374 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1375 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001376 */
Chris Masone02119d2008-09-05 16:13:11 -04001377static noinline void unlock_up(struct btrfs_path *path, int level,
1378 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001379{
1380 int i;
1381 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001382 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001383 struct extent_buffer *t;
1384
1385 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1386 if (!path->nodes[i])
1387 break;
1388 if (!path->locks[i])
1389 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001390 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001391 skip_level = i + 1;
1392 continue;
1393 }
Chris Mason051e1b92008-06-25 16:01:30 -04001394 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001395 u32 nritems;
1396 t = path->nodes[i];
1397 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001398 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001399 skip_level = i + 1;
1400 continue;
1401 }
1402 }
Chris Mason051e1b92008-06-25 16:01:30 -04001403 if (skip_level < i && i >= lowest_unlock)
1404 no_skips = 1;
1405
Chris Mason925baed2008-06-25 16:01:30 -04001406 t = path->nodes[i];
1407 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1408 btrfs_tree_unlock(t);
1409 path->locks[i] = 0;
1410 }
1411 }
1412}
1413
Chris Mason3c69fae2007-08-07 15:52:22 -04001414/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001415 * This releases any locks held in the path starting at level and
1416 * going all the way up to the root.
1417 *
1418 * btrfs_search_slot will keep the lock held on higher nodes in a few
1419 * corner cases, such as COW of the block at slot zero in the node. This
1420 * ignores those rules, and it should only be called when there are no
1421 * more updates to be done higher up in the tree.
1422 */
1423noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1424{
1425 int i;
1426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001427 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001428 return;
1429
1430 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1431 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001432 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001433 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001434 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001435 btrfs_tree_unlock(path->nodes[i]);
1436 path->locks[i] = 0;
1437 }
1438}
1439
1440/*
Chris Masonc8c42862009-04-03 10:14:18 -04001441 * helper function for btrfs_search_slot. The goal is to find a block
1442 * in cache without setting the path to blocking. If we find the block
1443 * we return zero and the path is unchanged.
1444 *
1445 * If we can't find the block, we set the path blocking and do some
1446 * reada. -EAGAIN is returned and the search must be repeated.
1447 */
1448static int
1449read_block_for_search(struct btrfs_trans_handle *trans,
1450 struct btrfs_root *root, struct btrfs_path *p,
1451 struct extent_buffer **eb_ret, int level, int slot,
1452 struct btrfs_key *key)
1453{
1454 u64 blocknr;
1455 u64 gen;
1456 u32 blocksize;
1457 struct extent_buffer *b = *eb_ret;
1458 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001459 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001460
1461 blocknr = btrfs_node_blockptr(b, slot);
1462 gen = btrfs_node_ptr_generation(b, slot);
1463 blocksize = btrfs_level_size(root, level - 1);
1464
1465 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001466 if (tmp) {
1467 if (btrfs_buffer_uptodate(tmp, 0)) {
1468 if (btrfs_buffer_uptodate(tmp, gen)) {
1469 /*
1470 * we found an up to date block without
1471 * sleeping, return
1472 * right away
1473 */
1474 *eb_ret = tmp;
1475 return 0;
1476 }
1477 /* the pages were up to date, but we failed
1478 * the generation number check. Do a full
1479 * read for the generation number that is correct.
1480 * We must do this without dropping locks so
1481 * we can trust our generation number
1482 */
1483 free_extent_buffer(tmp);
1484 tmp = read_tree_block(root, blocknr, blocksize, gen);
1485 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1486 *eb_ret = tmp;
1487 return 0;
1488 }
1489 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001490 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001491 return -EIO;
1492 }
Chris Masonc8c42862009-04-03 10:14:18 -04001493 }
1494
1495 /*
1496 * reduce lock contention at high levels
1497 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001498 * we read. Don't release the lock on the current
1499 * level because we need to walk this node to figure
1500 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001501 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001502 btrfs_unlock_up_safe(p, level + 1);
1503 btrfs_set_path_blocking(p);
1504
Chris Masoncb449212010-10-24 11:01:27 -04001505 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001506 if (p->reada)
1507 reada_for_search(root, p, level, slot, key->objectid);
1508
David Sterbab3b4aa72011-04-21 01:20:15 +02001509 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001510
1511 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001512 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001513 if (tmp) {
1514 /*
1515 * If the read above didn't mark this buffer up to date,
1516 * it will never end up being up to date. Set ret to EIO now
1517 * and give up so that our caller doesn't loop forever
1518 * on our EAGAINs.
1519 */
1520 if (!btrfs_buffer_uptodate(tmp, 0))
1521 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001522 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001523 }
1524 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001525}
1526
1527/*
1528 * helper function for btrfs_search_slot. This does all of the checks
1529 * for node-level blocks and does any balancing required based on
1530 * the ins_len.
1531 *
1532 * If no extra work was required, zero is returned. If we had to
1533 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1534 * start over
1535 */
1536static int
1537setup_nodes_for_search(struct btrfs_trans_handle *trans,
1538 struct btrfs_root *root, struct btrfs_path *p,
1539 struct extent_buffer *b, int level, int ins_len)
1540{
1541 int ret;
1542 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1543 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1544 int sret;
1545
1546 sret = reada_for_balance(root, p, level);
1547 if (sret)
1548 goto again;
1549
1550 btrfs_set_path_blocking(p);
1551 sret = split_node(trans, root, p, level);
1552 btrfs_clear_path_blocking(p, NULL);
1553
1554 BUG_ON(sret > 0);
1555 if (sret) {
1556 ret = sret;
1557 goto done;
1558 }
1559 b = p->nodes[level];
1560 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001561 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001562 int sret;
1563
1564 sret = reada_for_balance(root, p, level);
1565 if (sret)
1566 goto again;
1567
1568 btrfs_set_path_blocking(p);
1569 sret = balance_level(trans, root, p, level);
1570 btrfs_clear_path_blocking(p, NULL);
1571
1572 if (sret) {
1573 ret = sret;
1574 goto done;
1575 }
1576 b = p->nodes[level];
1577 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001578 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04001579 goto again;
1580 }
1581 BUG_ON(btrfs_header_nritems(b) == 1);
1582 }
1583 return 0;
1584
1585again:
1586 ret = -EAGAIN;
1587done:
1588 return ret;
1589}
1590
1591/*
Chris Mason74123bd2007-02-02 11:05:29 -05001592 * look for key in the tree. path is filled in with nodes along the way
1593 * if key is found, we return zero and you can find the item in the leaf
1594 * level of the path (level 0)
1595 *
1596 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001597 * be inserted, and 1 is returned. If there are other errors during the
1598 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001599 *
1600 * if ins_len > 0, nodes and leaves will be split as we walk down the
1601 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1602 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001603 */
Chris Masone089f052007-03-16 16:20:31 -04001604int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1605 *root, struct btrfs_key *key, struct btrfs_path *p, int
1606 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001607{
Chris Mason5f39d392007-10-15 16:14:19 -04001608 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001609 int slot;
1610 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001611 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001612 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001613 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001614 u8 lowest_level = 0;
1615
Chris Mason6702ed42007-08-07 16:15:09 -04001616 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001617 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001618 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001619
Chris Mason925baed2008-06-25 16:01:30 -04001620 if (ins_len < 0)
1621 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001622
Chris Masonbb803952007-03-01 12:04:21 -05001623again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001624 if (p->search_commit_root) {
1625 b = root->commit_root;
1626 extent_buffer_get(b);
1627 if (!p->skip_locking)
1628 btrfs_tree_lock(b);
1629 } else {
1630 if (p->skip_locking)
1631 b = btrfs_root_node(root);
1632 else
1633 b = btrfs_lock_root_node(root);
1634 }
Chris Mason925baed2008-06-25 16:01:30 -04001635
Chris Masoneb60cea2007-02-02 09:18:22 -05001636 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001637 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001638
1639 /*
1640 * setup the path here so we can release it under lock
1641 * contention with the cow code
1642 */
1643 p->nodes[level] = b;
1644 if (!p->skip_locking)
1645 p->locks[level] = 1;
1646
Chris Mason02217ed2007-03-02 16:08:05 -05001647 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001648 /*
1649 * if we don't really need to cow this block
1650 * then we don't want to set the path blocking,
1651 * so we test it here
1652 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001653 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001654 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001655
Chris Masonb4ce94d2009-02-04 09:25:08 -05001656 btrfs_set_path_blocking(p);
1657
Yan Zheng33c66f42009-07-22 09:59:00 -04001658 err = btrfs_cow_block(trans, root, b,
1659 p->nodes[level + 1],
1660 p->slots[level + 1], &b);
1661 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001662 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001663 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001664 }
Chris Mason02217ed2007-03-02 16:08:05 -05001665 }
Chris Mason65b51a02008-08-01 15:11:20 -04001666cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001667 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04001668
Chris Masoneb60cea2007-02-02 09:18:22 -05001669 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001670 if (!p->skip_locking)
1671 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001672
Chris Mason4008c042009-02-12 14:09:45 -05001673 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001674
1675 /*
1676 * we have a lock on b and as long as we aren't changing
1677 * the tree, there is no way to for the items in b to change.
1678 * It is safe to drop the lock on our parent before we
1679 * go through the expensive btree search on b.
1680 *
1681 * If cow is true, then we might be changing slot zero,
1682 * which may require changing the parent. So, we can't
1683 * drop the lock until after we know which slot we're
1684 * operating on.
1685 */
1686 if (!cow)
1687 btrfs_unlock_up_safe(p, level + 1);
1688
Chris Mason5f39d392007-10-15 16:14:19 -04001689 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001690
Chris Mason5f39d392007-10-15 16:14:19 -04001691 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001692 int dec = 0;
1693 if (ret && slot > 0) {
1694 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001695 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001696 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001697 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001698 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001699 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001700 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001701 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001702 if (err) {
1703 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001704 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001705 }
Chris Masonc8c42862009-04-03 10:14:18 -04001706 b = p->nodes[level];
1707 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001708
Chris Masonf9efa9c2008-06-25 16:14:04 -04001709 unlock_up(p, level, lowest_unlock);
1710
Chris Mason925baed2008-06-25 16:01:30 -04001711 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001712 if (dec)
1713 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001714 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001715 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001716
Yan Zheng33c66f42009-07-22 09:59:00 -04001717 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001718 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001719 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001720 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001721 if (err) {
1722 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001723 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001724 }
Chris Mason76a05b32009-05-14 13:24:30 -04001725
Chris Masonb4ce94d2009-02-04 09:25:08 -05001726 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001727 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001728 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001729
Yan Zheng33c66f42009-07-22 09:59:00 -04001730 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001731 btrfs_set_path_blocking(p);
1732 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001733 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001734 }
1735 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001736 } else {
1737 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001738 if (ins_len > 0 &&
1739 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001740 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001741 err = split_leaf(trans, root, key,
1742 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001743 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001744
Yan Zheng33c66f42009-07-22 09:59:00 -04001745 BUG_ON(err > 0);
1746 if (err) {
1747 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001748 goto done;
1749 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001750 }
Chris Mason459931e2008-12-10 09:10:46 -05001751 if (!p->search_for_split)
1752 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001753 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001754 }
1755 }
Chris Mason65b51a02008-08-01 15:11:20 -04001756 ret = 1;
1757done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001758 /*
1759 * we don't really know what they plan on doing with the path
1760 * from here on, so for now just mark it as blocking
1761 */
Chris Masonb9473432009-03-13 11:00:37 -04001762 if (!p->leave_spinning)
1763 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001764 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02001765 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001766 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001767}
1768
Chris Mason74123bd2007-02-02 11:05:29 -05001769/*
1770 * adjust the pointers going up the tree, starting at level
1771 * making sure the right key of each node is points to 'key'.
1772 * This is used after shifting pointers to the left, so it stops
1773 * fixing up pointers when a given leaf/node is not in slot 0 of the
1774 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001775 *
1776 * If this fails to write a tree block, it returns -1, but continues
1777 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001778 */
Chris Mason5f39d392007-10-15 16:14:19 -04001779static int fixup_low_keys(struct btrfs_trans_handle *trans,
1780 struct btrfs_root *root, struct btrfs_path *path,
1781 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001782{
1783 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001784 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001785 struct extent_buffer *t;
1786
Chris Mason234b63a2007-03-13 10:46:10 -04001787 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001788 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001789 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001790 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001791 t = path->nodes[i];
1792 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001793 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001794 if (tslot != 0)
1795 break;
1796 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001797 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001798}
1799
Chris Mason74123bd2007-02-02 11:05:29 -05001800/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001801 * update item key.
1802 *
1803 * This function isn't completely safe. It's the caller's responsibility
1804 * that the new key won't break the order
1805 */
1806int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1807 struct btrfs_root *root, struct btrfs_path *path,
1808 struct btrfs_key *new_key)
1809{
1810 struct btrfs_disk_key disk_key;
1811 struct extent_buffer *eb;
1812 int slot;
1813
1814 eb = path->nodes[0];
1815 slot = path->slots[0];
1816 if (slot > 0) {
1817 btrfs_item_key(eb, &disk_key, slot - 1);
1818 if (comp_keys(&disk_key, new_key) >= 0)
1819 return -1;
1820 }
1821 if (slot < btrfs_header_nritems(eb) - 1) {
1822 btrfs_item_key(eb, &disk_key, slot + 1);
1823 if (comp_keys(&disk_key, new_key) <= 0)
1824 return -1;
1825 }
1826
1827 btrfs_cpu_key_to_disk(&disk_key, new_key);
1828 btrfs_set_item_key(eb, &disk_key, slot);
1829 btrfs_mark_buffer_dirty(eb);
1830 if (slot == 0)
1831 fixup_low_keys(trans, root, path, &disk_key, 1);
1832 return 0;
1833}
1834
1835/*
Chris Mason74123bd2007-02-02 11:05:29 -05001836 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001837 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001838 *
1839 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1840 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001841 */
Chris Mason98ed5172008-01-03 10:01:48 -05001842static int push_node_left(struct btrfs_trans_handle *trans,
1843 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001844 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001845{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001846 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001847 int src_nritems;
1848 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001849 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001850
Chris Mason5f39d392007-10-15 16:14:19 -04001851 src_nritems = btrfs_header_nritems(src);
1852 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001853 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001854 WARN_ON(btrfs_header_generation(src) != trans->transid);
1855 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001856
Chris Masonbce4eae2008-04-24 14:42:46 -04001857 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001858 return 1;
1859
Chris Masond3977122009-01-05 21:25:51 -05001860 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001861 return 1;
1862
Chris Masonbce4eae2008-04-24 14:42:46 -04001863 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001864 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001865 if (push_items < src_nritems) {
1866 /* leave at least 8 pointers in the node if
1867 * we aren't going to empty it
1868 */
1869 if (src_nritems - push_items < 8) {
1870 if (push_items <= 8)
1871 return 1;
1872 push_items -= 8;
1873 }
1874 }
1875 } else
1876 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001877
Chris Mason5f39d392007-10-15 16:14:19 -04001878 copy_extent_buffer(dst, src,
1879 btrfs_node_key_ptr_offset(dst_nritems),
1880 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001881 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001882
Chris Masonbb803952007-03-01 12:04:21 -05001883 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001884 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1885 btrfs_node_key_ptr_offset(push_items),
1886 (src_nritems - push_items) *
1887 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001888 }
Chris Mason5f39d392007-10-15 16:14:19 -04001889 btrfs_set_header_nritems(src, src_nritems - push_items);
1890 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1891 btrfs_mark_buffer_dirty(src);
1892 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001893
Chris Masonbb803952007-03-01 12:04:21 -05001894 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001895}
1896
Chris Mason97571fd2007-02-24 13:39:08 -05001897/*
Chris Mason79f95c82007-03-01 15:16:26 -05001898 * try to push data from one node into the next node right in the
1899 * tree.
1900 *
1901 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1902 * error, and > 0 if there was no room in the right hand block.
1903 *
1904 * this will only push up to 1/2 the contents of the left node over
1905 */
Chris Mason5f39d392007-10-15 16:14:19 -04001906static int balance_node_right(struct btrfs_trans_handle *trans,
1907 struct btrfs_root *root,
1908 struct extent_buffer *dst,
1909 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001910{
Chris Mason79f95c82007-03-01 15:16:26 -05001911 int push_items = 0;
1912 int max_push;
1913 int src_nritems;
1914 int dst_nritems;
1915 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001916
Chris Mason7bb86312007-12-11 09:25:06 -05001917 WARN_ON(btrfs_header_generation(src) != trans->transid);
1918 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1919
Chris Mason5f39d392007-10-15 16:14:19 -04001920 src_nritems = btrfs_header_nritems(src);
1921 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001922 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001923 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001924 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001925
Chris Masond3977122009-01-05 21:25:51 -05001926 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001927 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001928
1929 max_push = src_nritems / 2 + 1;
1930 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001931 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001932 return 1;
Yan252c38f2007-08-29 09:11:44 -04001933
Chris Mason79f95c82007-03-01 15:16:26 -05001934 if (max_push < push_items)
1935 push_items = max_push;
1936
Chris Mason5f39d392007-10-15 16:14:19 -04001937 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1938 btrfs_node_key_ptr_offset(0),
1939 (dst_nritems) *
1940 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001941
Chris Mason5f39d392007-10-15 16:14:19 -04001942 copy_extent_buffer(dst, src,
1943 btrfs_node_key_ptr_offset(0),
1944 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001945 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001946
Chris Mason5f39d392007-10-15 16:14:19 -04001947 btrfs_set_header_nritems(src, src_nritems - push_items);
1948 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001949
Chris Mason5f39d392007-10-15 16:14:19 -04001950 btrfs_mark_buffer_dirty(src);
1951 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001952
Chris Mason79f95c82007-03-01 15:16:26 -05001953 return ret;
1954}
1955
1956/*
Chris Mason97571fd2007-02-24 13:39:08 -05001957 * helper function to insert a new root level in the tree.
1958 * A new node is allocated, and a single item is inserted to
1959 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001960 *
1961 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001962 */
Chris Masond3977122009-01-05 21:25:51 -05001963static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001964 struct btrfs_root *root,
1965 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001966{
Chris Mason7bb86312007-12-11 09:25:06 -05001967 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001968 struct extent_buffer *lower;
1969 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001970 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001971 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001972
1973 BUG_ON(path->nodes[level]);
1974 BUG_ON(path->nodes[level-1] != root->node);
1975
Chris Mason7bb86312007-12-11 09:25:06 -05001976 lower = path->nodes[level-1];
1977 if (level == 1)
1978 btrfs_item_key(lower, &lower_key, 0);
1979 else
1980 btrfs_node_key(lower, &lower_key, 0);
1981
Zheng Yan31840ae2008-09-23 13:14:14 -04001982 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001983 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001984 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001985 if (IS_ERR(c))
1986 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001987
Yan, Zhengf0486c62010-05-16 10:46:25 -04001988 root_add_used(root, root->nodesize);
1989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001990 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04001991 btrfs_set_header_nritems(c, 1);
1992 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001993 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001994 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001995 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04001996 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001997
Chris Mason5f39d392007-10-15 16:14:19 -04001998 write_extent_buffer(c, root->fs_info->fsid,
1999 (unsigned long)btrfs_header_fsid(c),
2000 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002001
2002 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2003 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2004 BTRFS_UUID_SIZE);
2005
Chris Mason5f39d392007-10-15 16:14:19 -04002006 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002007 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002008 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002009 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002010
2011 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002012
2013 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002014
Chris Mason925baed2008-06-25 16:01:30 -04002015 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002016 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002017
2018 /* the super has an extra ref to root->node */
2019 free_extent_buffer(old);
2020
Chris Mason0b86a832008-03-24 15:01:56 -04002021 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002022 extent_buffer_get(c);
2023 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002024 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002025 path->slots[level] = 0;
2026 return 0;
2027}
2028
Chris Mason74123bd2007-02-02 11:05:29 -05002029/*
2030 * worker function to insert a single pointer in a node.
2031 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002032 *
Chris Mason74123bd2007-02-02 11:05:29 -05002033 * slot and level indicate where you want the key to go, and
2034 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002035 *
2036 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002037 */
Chris Masone089f052007-03-16 16:20:31 -04002038static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2039 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002040 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002041{
Chris Mason5f39d392007-10-15 16:14:19 -04002042 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002043 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002044
2045 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002046 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002047 lower = path->nodes[level];
2048 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002049 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002050 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002051 BUG();
2052 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002053 memmove_extent_buffer(lower,
2054 btrfs_node_key_ptr_offset(slot + 1),
2055 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002056 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002057 }
Chris Mason5f39d392007-10-15 16:14:19 -04002058 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002059 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002060 WARN_ON(trans->transid == 0);
2061 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002062 btrfs_set_header_nritems(lower, nritems + 1);
2063 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002064 return 0;
2065}
2066
Chris Mason97571fd2007-02-24 13:39:08 -05002067/*
2068 * split the node at the specified level in path in two.
2069 * The path is corrected to point to the appropriate node after the split
2070 *
2071 * Before splitting this tries to make some room in the node by pushing
2072 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002073 *
2074 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002075 */
Chris Masone02119d2008-09-05 16:13:11 -04002076static noinline int split_node(struct btrfs_trans_handle *trans,
2077 struct btrfs_root *root,
2078 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002079{
Chris Mason5f39d392007-10-15 16:14:19 -04002080 struct extent_buffer *c;
2081 struct extent_buffer *split;
2082 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002083 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002084 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002085 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002086 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002087
Chris Mason5f39d392007-10-15 16:14:19 -04002088 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002089 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002090 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002091 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002092 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002093 if (ret)
2094 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002095 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002096 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002097 c = path->nodes[level];
2098 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002099 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002100 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002101 if (ret < 0)
2102 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002103 }
Chris Masone66f7092007-04-20 13:16:02 -04002104
Chris Mason5f39d392007-10-15 16:14:19 -04002105 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002106 mid = (c_nritems + 1) / 2;
2107 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002108
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002109 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002110 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002111 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002112 if (IS_ERR(split))
2113 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002114
Yan, Zhengf0486c62010-05-16 10:46:25 -04002115 root_add_used(root, root->nodesize);
2116
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002117 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002118 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002119 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002120 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002121 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002122 btrfs_set_header_owner(split, root->root_key.objectid);
2123 write_extent_buffer(split, root->fs_info->fsid,
2124 (unsigned long)btrfs_header_fsid(split),
2125 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002126 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2127 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2128 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002129
Chris Mason5f39d392007-10-15 16:14:19 -04002130
2131 copy_extent_buffer(split, c,
2132 btrfs_node_key_ptr_offset(0),
2133 btrfs_node_key_ptr_offset(mid),
2134 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2135 btrfs_set_header_nritems(split, c_nritems - mid);
2136 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002137 ret = 0;
2138
Chris Mason5f39d392007-10-15 16:14:19 -04002139 btrfs_mark_buffer_dirty(c);
2140 btrfs_mark_buffer_dirty(split);
2141
Chris Masondb945352007-10-15 16:15:53 -04002142 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002143 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002144 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002145 if (wret)
2146 ret = wret;
2147
Chris Mason5de08d72007-02-24 06:24:44 -05002148 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002149 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002150 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002151 free_extent_buffer(c);
2152 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002153 path->slots[level + 1] += 1;
2154 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002155 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002156 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002157 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002158 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002159}
2160
Chris Mason74123bd2007-02-02 11:05:29 -05002161/*
2162 * how many bytes are required to store the items in a leaf. start
2163 * and nr indicate which items in the leaf to check. This totals up the
2164 * space used both by the item structs and the item data
2165 */
Chris Mason5f39d392007-10-15 16:14:19 -04002166static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002167{
2168 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002169 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002170 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002171
2172 if (!nr)
2173 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002174 data_len = btrfs_item_end_nr(l, start);
2175 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002176 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002177 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002178 return data_len;
2179}
2180
Chris Mason74123bd2007-02-02 11:05:29 -05002181/*
Chris Masond4dbff92007-04-04 14:08:15 -04002182 * The space between the end of the leaf items and
2183 * the start of the leaf data. IOW, how much room
2184 * the leaf has left for both items and data
2185 */
Chris Masond3977122009-01-05 21:25:51 -05002186noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002187 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002188{
Chris Mason5f39d392007-10-15 16:14:19 -04002189 int nritems = btrfs_header_nritems(leaf);
2190 int ret;
2191 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2192 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002193 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2194 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002195 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002196 leaf_space_used(leaf, 0, nritems), nritems);
2197 }
2198 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002199}
2200
Chris Mason99d8f832010-07-07 10:51:48 -04002201/*
2202 * min slot controls the lowest index we're willing to push to the
2203 * right. We'll push up to and including min_slot, but no lower
2204 */
Chris Mason44871b12009-03-13 10:04:31 -04002205static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2206 struct btrfs_root *root,
2207 struct btrfs_path *path,
2208 int data_size, int empty,
2209 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002210 int free_space, u32 left_nritems,
2211 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002212{
Chris Mason5f39d392007-10-15 16:14:19 -04002213 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002214 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002215 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002216 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002217 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002218 int push_space = 0;
2219 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002220 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002221 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002222 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002223 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002224 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002225
Chris Mason34a38212007-11-07 13:31:03 -05002226 if (empty)
2227 nr = 0;
2228 else
Chris Mason99d8f832010-07-07 10:51:48 -04002229 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002230
Zheng Yan31840ae2008-09-23 13:14:14 -04002231 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002232 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002233
Chris Mason44871b12009-03-13 10:04:31 -04002234 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002235 i = left_nritems - 1;
2236 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002237 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002238
Zheng Yan31840ae2008-09-23 13:14:14 -04002239 if (!empty && push_items > 0) {
2240 if (path->slots[0] > i)
2241 break;
2242 if (path->slots[0] == i) {
2243 int space = btrfs_leaf_free_space(root, left);
2244 if (space + push_space * 2 > free_space)
2245 break;
2246 }
2247 }
2248
Chris Mason00ec4c52007-02-24 12:47:20 -05002249 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002250 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002251
2252 if (!left->map_token) {
2253 map_extent_buffer(left, (unsigned long)item,
2254 sizeof(struct btrfs_item),
2255 &left->map_token, &left->kaddr,
2256 &left->map_start, &left->map_len,
2257 KM_USER1);
2258 }
2259
2260 this_item_size = btrfs_item_size(left, item);
2261 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002262 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002263
Chris Mason00ec4c52007-02-24 12:47:20 -05002264 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002265 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002266 if (i == 0)
2267 break;
2268 i--;
Chris Masondb945352007-10-15 16:15:53 -04002269 }
2270 if (left->map_token) {
2271 unmap_extent_buffer(left, left->map_token, KM_USER1);
2272 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002273 }
Chris Mason5f39d392007-10-15 16:14:19 -04002274
Chris Mason925baed2008-06-25 16:01:30 -04002275 if (push_items == 0)
2276 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002277
Chris Mason34a38212007-11-07 13:31:03 -05002278 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002279 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002280
Chris Mason00ec4c52007-02-24 12:47:20 -05002281 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002282 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002283
Chris Mason5f39d392007-10-15 16:14:19 -04002284 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002285 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002286
Chris Mason00ec4c52007-02-24 12:47:20 -05002287 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002288 data_end = leaf_data_end(root, right);
2289 memmove_extent_buffer(right,
2290 btrfs_leaf_data(right) + data_end - push_space,
2291 btrfs_leaf_data(right) + data_end,
2292 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2293
Chris Mason00ec4c52007-02-24 12:47:20 -05002294 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002295 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002296 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2297 btrfs_leaf_data(left) + leaf_data_end(root, left),
2298 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002299
2300 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2301 btrfs_item_nr_offset(0),
2302 right_nritems * sizeof(struct btrfs_item));
2303
Chris Mason00ec4c52007-02-24 12:47:20 -05002304 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002305 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2306 btrfs_item_nr_offset(left_nritems - push_items),
2307 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002308
2309 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002310 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002311 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002312 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002313 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002314 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002315 if (!right->map_token) {
2316 map_extent_buffer(right, (unsigned long)item,
2317 sizeof(struct btrfs_item),
2318 &right->map_token, &right->kaddr,
2319 &right->map_start, &right->map_len,
2320 KM_USER1);
2321 }
2322 push_space -= btrfs_item_size(right, item);
2323 btrfs_set_item_offset(right, item, push_space);
2324 }
2325
2326 if (right->map_token) {
2327 unmap_extent_buffer(right, right->map_token, KM_USER1);
2328 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002329 }
Chris Mason7518a232007-03-12 12:01:18 -04002330 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002331 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002332
Chris Mason34a38212007-11-07 13:31:03 -05002333 if (left_nritems)
2334 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002335 else
2336 clean_tree_block(trans, root, left);
2337
Chris Mason5f39d392007-10-15 16:14:19 -04002338 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002339
Chris Mason5f39d392007-10-15 16:14:19 -04002340 btrfs_item_key(right, &disk_key, 0);
2341 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002342 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002343
Chris Mason00ec4c52007-02-24 12:47:20 -05002344 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002345 if (path->slots[0] >= left_nritems) {
2346 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002347 if (btrfs_header_nritems(path->nodes[0]) == 0)
2348 clean_tree_block(trans, root, path->nodes[0]);
2349 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002350 free_extent_buffer(path->nodes[0]);
2351 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002352 path->slots[1] += 1;
2353 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002354 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002355 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002356 }
2357 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002358
2359out_unlock:
2360 btrfs_tree_unlock(right);
2361 free_extent_buffer(right);
2362 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002363}
Chris Mason925baed2008-06-25 16:01:30 -04002364
Chris Mason00ec4c52007-02-24 12:47:20 -05002365/*
Chris Mason44871b12009-03-13 10:04:31 -04002366 * push some data in the path leaf to the right, trying to free up at
2367 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2368 *
2369 * returns 1 if the push failed because the other node didn't have enough
2370 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002371 *
2372 * this will push starting from min_slot to the end of the leaf. It won't
2373 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002374 */
2375static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002376 *root, struct btrfs_path *path,
2377 int min_data_size, int data_size,
2378 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002379{
2380 struct extent_buffer *left = path->nodes[0];
2381 struct extent_buffer *right;
2382 struct extent_buffer *upper;
2383 int slot;
2384 int free_space;
2385 u32 left_nritems;
2386 int ret;
2387
2388 if (!path->nodes[1])
2389 return 1;
2390
2391 slot = path->slots[1];
2392 upper = path->nodes[1];
2393 if (slot >= btrfs_header_nritems(upper) - 1)
2394 return 1;
2395
2396 btrfs_assert_tree_locked(path->nodes[1]);
2397
2398 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002399 if (right == NULL)
2400 return 1;
2401
Chris Mason44871b12009-03-13 10:04:31 -04002402 btrfs_tree_lock(right);
2403 btrfs_set_lock_blocking(right);
2404
2405 free_space = btrfs_leaf_free_space(root, right);
2406 if (free_space < data_size)
2407 goto out_unlock;
2408
2409 /* cow and double check */
2410 ret = btrfs_cow_block(trans, root, right, upper,
2411 slot + 1, &right);
2412 if (ret)
2413 goto out_unlock;
2414
2415 free_space = btrfs_leaf_free_space(root, right);
2416 if (free_space < data_size)
2417 goto out_unlock;
2418
2419 left_nritems = btrfs_header_nritems(left);
2420 if (left_nritems == 0)
2421 goto out_unlock;
2422
Chris Mason99d8f832010-07-07 10:51:48 -04002423 return __push_leaf_right(trans, root, path, min_data_size, empty,
2424 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002425out_unlock:
2426 btrfs_tree_unlock(right);
2427 free_extent_buffer(right);
2428 return 1;
2429}
2430
2431/*
Chris Mason74123bd2007-02-02 11:05:29 -05002432 * push some data in the path leaf to the left, trying to free up at
2433 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002434 *
2435 * max_slot can put a limit on how far into the leaf we'll push items. The
2436 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2437 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002438 */
Chris Mason44871b12009-03-13 10:04:31 -04002439static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2440 struct btrfs_root *root,
2441 struct btrfs_path *path, int data_size,
2442 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002443 int free_space, u32 right_nritems,
2444 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002445{
Chris Mason5f39d392007-10-15 16:14:19 -04002446 struct btrfs_disk_key disk_key;
2447 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002448 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002449 int push_space = 0;
2450 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002451 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002452 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002453 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002454 int ret = 0;
2455 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002456 u32 this_item_size;
2457 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002458
Chris Mason34a38212007-11-07 13:31:03 -05002459 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002460 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002461 else
Chris Mason99d8f832010-07-07 10:51:48 -04002462 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002463
2464 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002465 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002466 if (!right->map_token) {
2467 map_extent_buffer(right, (unsigned long)item,
2468 sizeof(struct btrfs_item),
2469 &right->map_token, &right->kaddr,
2470 &right->map_start, &right->map_len,
2471 KM_USER1);
2472 }
2473
Zheng Yan31840ae2008-09-23 13:14:14 -04002474 if (!empty && push_items > 0) {
2475 if (path->slots[0] < i)
2476 break;
2477 if (path->slots[0] == i) {
2478 int space = btrfs_leaf_free_space(root, right);
2479 if (space + push_space * 2 > free_space)
2480 break;
2481 }
2482 }
2483
Chris Masonbe0e5c02007-01-26 15:51:26 -05002484 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002485 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002486
2487 this_item_size = btrfs_item_size(right, item);
2488 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002489 break;
Chris Masondb945352007-10-15 16:15:53 -04002490
Chris Masonbe0e5c02007-01-26 15:51:26 -05002491 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002492 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002493 }
Chris Masondb945352007-10-15 16:15:53 -04002494
2495 if (right->map_token) {
2496 unmap_extent_buffer(right, right->map_token, KM_USER1);
2497 right->map_token = NULL;
2498 }
2499
Chris Masonbe0e5c02007-01-26 15:51:26 -05002500 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002501 ret = 1;
2502 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002503 }
Chris Mason34a38212007-11-07 13:31:03 -05002504 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002505 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002506
Chris Masonbe0e5c02007-01-26 15:51:26 -05002507 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002508 copy_extent_buffer(left, right,
2509 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2510 btrfs_item_nr_offset(0),
2511 push_items * sizeof(struct btrfs_item));
2512
Chris Mason123abc82007-03-14 14:14:43 -04002513 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002514 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002515
2516 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002517 leaf_data_end(root, left) - push_space,
2518 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002519 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002520 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002521 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002522 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002523
Chris Masondb945352007-10-15 16:15:53 -04002524 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002525 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002526 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002527
Chris Mason5f39d392007-10-15 16:14:19 -04002528 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002529 if (!left->map_token) {
2530 map_extent_buffer(left, (unsigned long)item,
2531 sizeof(struct btrfs_item),
2532 &left->map_token, &left->kaddr,
2533 &left->map_start, &left->map_len,
2534 KM_USER1);
2535 }
2536
Chris Mason5f39d392007-10-15 16:14:19 -04002537 ioff = btrfs_item_offset(left, item);
2538 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002539 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002540 }
Chris Mason5f39d392007-10-15 16:14:19 -04002541 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002542 if (left->map_token) {
2543 unmap_extent_buffer(left, left->map_token, KM_USER1);
2544 left->map_token = NULL;
2545 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002546
2547 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002548 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002549 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2550 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002551 WARN_ON(1);
2552 }
Chris Mason5f39d392007-10-15 16:14:19 -04002553
Chris Mason34a38212007-11-07 13:31:03 -05002554 if (push_items < right_nritems) {
2555 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2556 leaf_data_end(root, right);
2557 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2558 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2559 btrfs_leaf_data(right) +
2560 leaf_data_end(root, right), push_space);
2561
2562 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002563 btrfs_item_nr_offset(push_items),
2564 (btrfs_header_nritems(right) - push_items) *
2565 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002566 }
Yaneef1c492007-11-26 10:58:13 -05002567 right_nritems -= push_items;
2568 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002569 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002570 for (i = 0; i < right_nritems; i++) {
2571 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002572
2573 if (!right->map_token) {
2574 map_extent_buffer(right, (unsigned long)item,
2575 sizeof(struct btrfs_item),
2576 &right->map_token, &right->kaddr,
2577 &right->map_start, &right->map_len,
2578 KM_USER1);
2579 }
2580
2581 push_space = push_space - btrfs_item_size(right, item);
2582 btrfs_set_item_offset(right, item, push_space);
2583 }
2584 if (right->map_token) {
2585 unmap_extent_buffer(right, right->map_token, KM_USER1);
2586 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002587 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002588
Chris Mason5f39d392007-10-15 16:14:19 -04002589 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002590 if (right_nritems)
2591 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002592 else
2593 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002594
Chris Mason5f39d392007-10-15 16:14:19 -04002595 btrfs_item_key(right, &disk_key, 0);
2596 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002597 if (wret)
2598 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002599
2600 /* then fixup the leaf pointer in the path */
2601 if (path->slots[0] < push_items) {
2602 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002603 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002604 free_extent_buffer(path->nodes[0]);
2605 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002606 path->slots[1] -= 1;
2607 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002608 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002609 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610 path->slots[0] -= push_items;
2611 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002612 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002613 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002614out:
2615 btrfs_tree_unlock(left);
2616 free_extent_buffer(left);
2617 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002618}
2619
Chris Mason74123bd2007-02-02 11:05:29 -05002620/*
Chris Mason44871b12009-03-13 10:04:31 -04002621 * push some data in the path leaf to the left, trying to free up at
2622 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002623 *
2624 * max_slot can put a limit on how far into the leaf we'll push items. The
2625 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2626 * items
Chris Mason44871b12009-03-13 10:04:31 -04002627 */
2628static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002629 *root, struct btrfs_path *path, int min_data_size,
2630 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002631{
2632 struct extent_buffer *right = path->nodes[0];
2633 struct extent_buffer *left;
2634 int slot;
2635 int free_space;
2636 u32 right_nritems;
2637 int ret = 0;
2638
2639 slot = path->slots[1];
2640 if (slot == 0)
2641 return 1;
2642 if (!path->nodes[1])
2643 return 1;
2644
2645 right_nritems = btrfs_header_nritems(right);
2646 if (right_nritems == 0)
2647 return 1;
2648
2649 btrfs_assert_tree_locked(path->nodes[1]);
2650
2651 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002652 if (left == NULL)
2653 return 1;
2654
Chris Mason44871b12009-03-13 10:04:31 -04002655 btrfs_tree_lock(left);
2656 btrfs_set_lock_blocking(left);
2657
2658 free_space = btrfs_leaf_free_space(root, left);
2659 if (free_space < data_size) {
2660 ret = 1;
2661 goto out;
2662 }
2663
2664 /* cow and double check */
2665 ret = btrfs_cow_block(trans, root, left,
2666 path->nodes[1], slot - 1, &left);
2667 if (ret) {
2668 /* we hit -ENOSPC, but it isn't fatal here */
2669 ret = 1;
2670 goto out;
2671 }
2672
2673 free_space = btrfs_leaf_free_space(root, left);
2674 if (free_space < data_size) {
2675 ret = 1;
2676 goto out;
2677 }
2678
Chris Mason99d8f832010-07-07 10:51:48 -04002679 return __push_leaf_left(trans, root, path, min_data_size,
2680 empty, left, free_space, right_nritems,
2681 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002682out:
2683 btrfs_tree_unlock(left);
2684 free_extent_buffer(left);
2685 return ret;
2686}
2687
2688/*
Chris Mason74123bd2007-02-02 11:05:29 -05002689 * split the path's leaf in two, making sure there is at least data_size
2690 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002691 *
2692 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002693 */
Chris Mason44871b12009-03-13 10:04:31 -04002694static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002695 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002696 struct btrfs_path *path,
2697 struct extent_buffer *l,
2698 struct extent_buffer *right,
2699 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002700{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002701 int data_copy_size;
2702 int rt_data_off;
2703 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002704 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002705 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002706 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002707
Chris Mason5f39d392007-10-15 16:14:19 -04002708 nritems = nritems - mid;
2709 btrfs_set_header_nritems(right, nritems);
2710 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2711
2712 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2713 btrfs_item_nr_offset(mid),
2714 nritems * sizeof(struct btrfs_item));
2715
2716 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002717 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2718 data_copy_size, btrfs_leaf_data(l) +
2719 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002720
Chris Mason5f39d392007-10-15 16:14:19 -04002721 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2722 btrfs_item_end_nr(l, mid);
2723
2724 for (i = 0; i < nritems; i++) {
2725 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002726 u32 ioff;
2727
2728 if (!right->map_token) {
2729 map_extent_buffer(right, (unsigned long)item,
2730 sizeof(struct btrfs_item),
2731 &right->map_token, &right->kaddr,
2732 &right->map_start, &right->map_len,
2733 KM_USER1);
2734 }
2735
2736 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002737 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002738 }
Chris Mason74123bd2007-02-02 11:05:29 -05002739
Chris Masondb945352007-10-15 16:15:53 -04002740 if (right->map_token) {
2741 unmap_extent_buffer(right, right->map_token, KM_USER1);
2742 right->map_token = NULL;
2743 }
2744
Chris Mason5f39d392007-10-15 16:14:19 -04002745 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002746 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002747 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002748 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2749 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002750 if (wret)
2751 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002752
2753 btrfs_mark_buffer_dirty(right);
2754 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002755 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002756
Chris Masonbe0e5c02007-01-26 15:51:26 -05002757 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002758 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002759 free_extent_buffer(path->nodes[0]);
2760 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002761 path->slots[0] -= mid;
2762 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002763 } else {
2764 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002765 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002766 }
Chris Mason5f39d392007-10-15 16:14:19 -04002767
Chris Masoneb60cea2007-02-02 09:18:22 -05002768 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002769
Chris Mason44871b12009-03-13 10:04:31 -04002770 return ret;
2771}
2772
2773/*
Chris Mason99d8f832010-07-07 10:51:48 -04002774 * double splits happen when we need to insert a big item in the middle
2775 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2776 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2777 * A B C
2778 *
2779 * We avoid this by trying to push the items on either side of our target
2780 * into the adjacent leaves. If all goes well we can avoid the double split
2781 * completely.
2782 */
2783static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2784 struct btrfs_root *root,
2785 struct btrfs_path *path,
2786 int data_size)
2787{
2788 int ret;
2789 int progress = 0;
2790 int slot;
2791 u32 nritems;
2792
2793 slot = path->slots[0];
2794
2795 /*
2796 * try to push all the items after our slot into the
2797 * right leaf
2798 */
2799 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2800 if (ret < 0)
2801 return ret;
2802
2803 if (ret == 0)
2804 progress++;
2805
2806 nritems = btrfs_header_nritems(path->nodes[0]);
2807 /*
2808 * our goal is to get our slot at the start or end of a leaf. If
2809 * we've done so we're done
2810 */
2811 if (path->slots[0] == 0 || path->slots[0] == nritems)
2812 return 0;
2813
2814 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2815 return 0;
2816
2817 /* try to push all the items before our slot into the next leaf */
2818 slot = path->slots[0];
2819 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2820 if (ret < 0)
2821 return ret;
2822
2823 if (ret == 0)
2824 progress++;
2825
2826 if (progress)
2827 return 0;
2828 return 1;
2829}
2830
2831/*
Chris Mason44871b12009-03-13 10:04:31 -04002832 * split the path's leaf in two, making sure there is at least data_size
2833 * available for the resulting leaf level of the path.
2834 *
2835 * returns 0 if all went well and < 0 on failure.
2836 */
2837static noinline int split_leaf(struct btrfs_trans_handle *trans,
2838 struct btrfs_root *root,
2839 struct btrfs_key *ins_key,
2840 struct btrfs_path *path, int data_size,
2841 int extend)
2842{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002843 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002844 struct extent_buffer *l;
2845 u32 nritems;
2846 int mid;
2847 int slot;
2848 struct extent_buffer *right;
2849 int ret = 0;
2850 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002851 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002852 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002853 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002854
Yan, Zhenga5719522009-09-24 09:17:31 -04002855 l = path->nodes[0];
2856 slot = path->slots[0];
2857 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2858 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2859 return -EOVERFLOW;
2860
Chris Mason44871b12009-03-13 10:04:31 -04002861 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002862 if (data_size) {
2863 wret = push_leaf_right(trans, root, path, data_size,
2864 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002865 if (wret < 0)
2866 return wret;
2867 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002868 wret = push_leaf_left(trans, root, path, data_size,
2869 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002870 if (wret < 0)
2871 return wret;
2872 }
2873 l = path->nodes[0];
2874
2875 /* did the pushes work? */
2876 if (btrfs_leaf_free_space(root, l) >= data_size)
2877 return 0;
2878 }
2879
2880 if (!path->nodes[1]) {
2881 ret = insert_new_root(trans, root, path, 1);
2882 if (ret)
2883 return ret;
2884 }
2885again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002886 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002887 l = path->nodes[0];
2888 slot = path->slots[0];
2889 nritems = btrfs_header_nritems(l);
2890 mid = (nritems + 1) / 2;
2891
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002892 if (mid <= slot) {
2893 if (nritems == 1 ||
2894 leaf_space_used(l, mid, nritems - mid) + data_size >
2895 BTRFS_LEAF_DATA_SIZE(root)) {
2896 if (slot >= nritems) {
2897 split = 0;
2898 } else {
2899 mid = slot;
2900 if (mid != nritems &&
2901 leaf_space_used(l, mid, nritems - mid) +
2902 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002903 if (data_size && !tried_avoid_double)
2904 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002905 split = 2;
2906 }
2907 }
2908 }
2909 } else {
2910 if (leaf_space_used(l, 0, mid) + data_size >
2911 BTRFS_LEAF_DATA_SIZE(root)) {
2912 if (!extend && data_size && slot == 0) {
2913 split = 0;
2914 } else if ((extend || !data_size) && slot == 0) {
2915 mid = 1;
2916 } else {
2917 mid = slot;
2918 if (mid != nritems &&
2919 leaf_space_used(l, mid, nritems - mid) +
2920 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002921 if (data_size && !tried_avoid_double)
2922 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002923 split = 2 ;
2924 }
2925 }
2926 }
2927 }
2928
2929 if (split == 0)
2930 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2931 else
2932 btrfs_item_key(l, &disk_key, mid);
2933
2934 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002935 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002936 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002937 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002938 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002939
2940 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002941
2942 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2943 btrfs_set_header_bytenr(right, right->start);
2944 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002945 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002946 btrfs_set_header_owner(right, root->root_key.objectid);
2947 btrfs_set_header_level(right, 0);
2948 write_extent_buffer(right, root->fs_info->fsid,
2949 (unsigned long)btrfs_header_fsid(right),
2950 BTRFS_FSID_SIZE);
2951
2952 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2953 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2954 BTRFS_UUID_SIZE);
2955
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002956 if (split == 0) {
2957 if (mid <= slot) {
2958 btrfs_set_header_nritems(right, 0);
2959 wret = insert_ptr(trans, root, path,
2960 &disk_key, right->start,
2961 path->slots[1] + 1, 1);
2962 if (wret)
2963 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002964
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002965 btrfs_tree_unlock(path->nodes[0]);
2966 free_extent_buffer(path->nodes[0]);
2967 path->nodes[0] = right;
2968 path->slots[0] = 0;
2969 path->slots[1] += 1;
2970 } else {
2971 btrfs_set_header_nritems(right, 0);
2972 wret = insert_ptr(trans, root, path,
2973 &disk_key,
2974 right->start,
2975 path->slots[1], 1);
2976 if (wret)
2977 ret = wret;
2978 btrfs_tree_unlock(path->nodes[0]);
2979 free_extent_buffer(path->nodes[0]);
2980 path->nodes[0] = right;
2981 path->slots[0] = 0;
2982 if (path->slots[1] == 0) {
2983 wret = fixup_low_keys(trans, root,
2984 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002985 if (wret)
2986 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002987 }
2988 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002989 btrfs_mark_buffer_dirty(right);
2990 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002991 }
2992
2993 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2994 BUG_ON(ret);
2995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002996 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002997 BUG_ON(num_doubles != 0);
2998 num_doubles++;
2999 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003000 }
Chris Mason44871b12009-03-13 10:04:31 -04003001
Chris Masonbe0e5c02007-01-26 15:51:26 -05003002 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04003003
3004push_for_double:
3005 push_for_double_split(trans, root, path, data_size);
3006 tried_avoid_double = 1;
3007 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3008 return 0;
3009 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003010}
3011
Yan, Zhengad48fd752009-11-12 09:33:58 +00003012static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3013 struct btrfs_root *root,
3014 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003015{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003016 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003017 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003018 struct btrfs_file_extent_item *fi;
3019 u64 extent_len = 0;
3020 u32 item_size;
3021 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003022
3023 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003024 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3025
3026 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3027 key.type != BTRFS_EXTENT_CSUM_KEY);
3028
3029 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3030 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003031
3032 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003033 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3034 fi = btrfs_item_ptr(leaf, path->slots[0],
3035 struct btrfs_file_extent_item);
3036 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3037 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003038 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003039
Chris Mason459931e2008-12-10 09:10:46 -05003040 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003041 path->search_for_split = 1;
3042 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003043 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003044 if (ret < 0)
3045 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003046
Yan, Zhengad48fd752009-11-12 09:33:58 +00003047 ret = -EAGAIN;
3048 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003049 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003050 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3051 goto err;
3052
Chris Mason109f6ae2010-04-02 09:20:18 -04003053 /* the leaf has changed, it now has room. return now */
3054 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3055 goto err;
3056
Yan, Zhengad48fd752009-11-12 09:33:58 +00003057 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3058 fi = btrfs_item_ptr(leaf, path->slots[0],
3059 struct btrfs_file_extent_item);
3060 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3061 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003062 }
3063
Chris Masonb9473432009-03-13 11:00:37 -04003064 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003065 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003066 if (ret)
3067 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003068
Yan, Zhengad48fd752009-11-12 09:33:58 +00003069 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003070 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003071 return 0;
3072err:
3073 path->keep_locks = 0;
3074 return ret;
3075}
3076
3077static noinline int split_item(struct btrfs_trans_handle *trans,
3078 struct btrfs_root *root,
3079 struct btrfs_path *path,
3080 struct btrfs_key *new_key,
3081 unsigned long split_offset)
3082{
3083 struct extent_buffer *leaf;
3084 struct btrfs_item *item;
3085 struct btrfs_item *new_item;
3086 int slot;
3087 char *buf;
3088 u32 nritems;
3089 u32 item_size;
3090 u32 orig_offset;
3091 struct btrfs_disk_key disk_key;
3092
Chris Masonb9473432009-03-13 11:00:37 -04003093 leaf = path->nodes[0];
3094 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3095
Chris Masonb4ce94d2009-02-04 09:25:08 -05003096 btrfs_set_path_blocking(path);
3097
Chris Mason459931e2008-12-10 09:10:46 -05003098 item = btrfs_item_nr(leaf, path->slots[0]);
3099 orig_offset = btrfs_item_offset(leaf, item);
3100 item_size = btrfs_item_size(leaf, item);
3101
Chris Mason459931e2008-12-10 09:10:46 -05003102 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003103 if (!buf)
3104 return -ENOMEM;
3105
Chris Mason459931e2008-12-10 09:10:46 -05003106 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3107 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003108
Chris Mason459931e2008-12-10 09:10:46 -05003109 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003110 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003111 if (slot != nritems) {
3112 /* shift the items */
3113 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003114 btrfs_item_nr_offset(slot),
3115 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003116 }
3117
3118 btrfs_cpu_key_to_disk(&disk_key, new_key);
3119 btrfs_set_item_key(leaf, &disk_key, slot);
3120
3121 new_item = btrfs_item_nr(leaf, slot);
3122
3123 btrfs_set_item_offset(leaf, new_item, orig_offset);
3124 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3125
3126 btrfs_set_item_offset(leaf, item,
3127 orig_offset + item_size - split_offset);
3128 btrfs_set_item_size(leaf, item, split_offset);
3129
3130 btrfs_set_header_nritems(leaf, nritems + 1);
3131
3132 /* write the data for the start of the original item */
3133 write_extent_buffer(leaf, buf,
3134 btrfs_item_ptr_offset(leaf, path->slots[0]),
3135 split_offset);
3136
3137 /* write the data for the new item */
3138 write_extent_buffer(leaf, buf + split_offset,
3139 btrfs_item_ptr_offset(leaf, slot),
3140 item_size - split_offset);
3141 btrfs_mark_buffer_dirty(leaf);
3142
Yan, Zhengad48fd752009-11-12 09:33:58 +00003143 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003144 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003145 return 0;
3146}
3147
3148/*
3149 * This function splits a single item into two items,
3150 * giving 'new_key' to the new item and splitting the
3151 * old one at split_offset (from the start of the item).
3152 *
3153 * The path may be released by this operation. After
3154 * the split, the path is pointing to the old item. The
3155 * new item is going to be in the same node as the old one.
3156 *
3157 * Note, the item being split must be smaller enough to live alone on
3158 * a tree block with room for one extra struct btrfs_item
3159 *
3160 * This allows us to split the item in place, keeping a lock on the
3161 * leaf the entire time.
3162 */
3163int btrfs_split_item(struct btrfs_trans_handle *trans,
3164 struct btrfs_root *root,
3165 struct btrfs_path *path,
3166 struct btrfs_key *new_key,
3167 unsigned long split_offset)
3168{
3169 int ret;
3170 ret = setup_leaf_for_split(trans, root, path,
3171 sizeof(struct btrfs_item));
3172 if (ret)
3173 return ret;
3174
3175 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003176 return ret;
3177}
3178
3179/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003180 * This function duplicate a item, giving 'new_key' to the new item.
3181 * It guarantees both items live in the same tree leaf and the new item
3182 * is contiguous with the original item.
3183 *
3184 * This allows us to split file extent in place, keeping a lock on the
3185 * leaf the entire time.
3186 */
3187int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3188 struct btrfs_root *root,
3189 struct btrfs_path *path,
3190 struct btrfs_key *new_key)
3191{
3192 struct extent_buffer *leaf;
3193 int ret;
3194 u32 item_size;
3195
3196 leaf = path->nodes[0];
3197 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3198 ret = setup_leaf_for_split(trans, root, path,
3199 item_size + sizeof(struct btrfs_item));
3200 if (ret)
3201 return ret;
3202
3203 path->slots[0]++;
3204 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3205 item_size, item_size +
3206 sizeof(struct btrfs_item), 1);
3207 BUG_ON(ret);
3208
3209 leaf = path->nodes[0];
3210 memcpy_extent_buffer(leaf,
3211 btrfs_item_ptr_offset(leaf, path->slots[0]),
3212 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3213 item_size);
3214 return 0;
3215}
3216
3217/*
Chris Masond352ac62008-09-29 15:18:18 -04003218 * make the item pointed to by the path smaller. new_size indicates
3219 * how small to make it, and from_end tells us if we just chop bytes
3220 * off the end of the item or if we shift the item to chop bytes off
3221 * the front.
3222 */
Chris Masonb18c6682007-04-17 13:26:50 -04003223int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3224 struct btrfs_root *root,
3225 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003226 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003227{
Chris Masonb18c6682007-04-17 13:26:50 -04003228 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003229 struct extent_buffer *leaf;
3230 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003231 u32 nritems;
3232 unsigned int data_end;
3233 unsigned int old_data_start;
3234 unsigned int old_size;
3235 unsigned int size_diff;
3236 int i;
3237
Chris Mason5f39d392007-10-15 16:14:19 -04003238 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003239 slot = path->slots[0];
3240
3241 old_size = btrfs_item_size_nr(leaf, slot);
3242 if (old_size == new_size)
3243 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003244
Chris Mason5f39d392007-10-15 16:14:19 -04003245 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003246 data_end = leaf_data_end(root, leaf);
3247
Chris Mason5f39d392007-10-15 16:14:19 -04003248 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003249
Chris Masonb18c6682007-04-17 13:26:50 -04003250 size_diff = old_size - new_size;
3251
3252 BUG_ON(slot < 0);
3253 BUG_ON(slot >= nritems);
3254
3255 /*
3256 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3257 */
3258 /* first correct the data pointers */
3259 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003260 u32 ioff;
3261 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003262
3263 if (!leaf->map_token) {
3264 map_extent_buffer(leaf, (unsigned long)item,
3265 sizeof(struct btrfs_item),
3266 &leaf->map_token, &leaf->kaddr,
3267 &leaf->map_start, &leaf->map_len,
3268 KM_USER1);
3269 }
3270
Chris Mason5f39d392007-10-15 16:14:19 -04003271 ioff = btrfs_item_offset(leaf, item);
3272 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003273 }
Chris Masondb945352007-10-15 16:15:53 -04003274
3275 if (leaf->map_token) {
3276 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3277 leaf->map_token = NULL;
3278 }
3279
Chris Masonb18c6682007-04-17 13:26:50 -04003280 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003281 if (from_end) {
3282 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3283 data_end + size_diff, btrfs_leaf_data(leaf) +
3284 data_end, old_data_start + new_size - data_end);
3285 } else {
3286 struct btrfs_disk_key disk_key;
3287 u64 offset;
3288
3289 btrfs_item_key(leaf, &disk_key, slot);
3290
3291 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3292 unsigned long ptr;
3293 struct btrfs_file_extent_item *fi;
3294
3295 fi = btrfs_item_ptr(leaf, slot,
3296 struct btrfs_file_extent_item);
3297 fi = (struct btrfs_file_extent_item *)(
3298 (unsigned long)fi - size_diff);
3299
3300 if (btrfs_file_extent_type(leaf, fi) ==
3301 BTRFS_FILE_EXTENT_INLINE) {
3302 ptr = btrfs_item_ptr_offset(leaf, slot);
3303 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003304 (unsigned long)fi,
3305 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003306 disk_bytenr));
3307 }
3308 }
3309
3310 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3311 data_end + size_diff, btrfs_leaf_data(leaf) +
3312 data_end, old_data_start - data_end);
3313
3314 offset = btrfs_disk_key_offset(&disk_key);
3315 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3316 btrfs_set_item_key(leaf, &disk_key, slot);
3317 if (slot == 0)
3318 fixup_low_keys(trans, root, path, &disk_key, 1);
3319 }
Chris Mason5f39d392007-10-15 16:14:19 -04003320
3321 item = btrfs_item_nr(leaf, slot);
3322 btrfs_set_item_size(leaf, item, new_size);
3323 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003324
Chris Mason5f39d392007-10-15 16:14:19 -04003325 if (btrfs_leaf_free_space(root, leaf) < 0) {
3326 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003327 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003328 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003329 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003330}
3331
Chris Masond352ac62008-09-29 15:18:18 -04003332/*
3333 * make the item pointed to by the path bigger, data_size is the new size.
3334 */
Chris Mason5f39d392007-10-15 16:14:19 -04003335int btrfs_extend_item(struct btrfs_trans_handle *trans,
3336 struct btrfs_root *root, struct btrfs_path *path,
3337 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003338{
Chris Mason6567e832007-04-16 09:22:45 -04003339 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003340 struct extent_buffer *leaf;
3341 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003342 u32 nritems;
3343 unsigned int data_end;
3344 unsigned int old_data;
3345 unsigned int old_size;
3346 int i;
3347
Chris Mason5f39d392007-10-15 16:14:19 -04003348 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003349
Chris Mason5f39d392007-10-15 16:14:19 -04003350 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003351 data_end = leaf_data_end(root, leaf);
3352
Chris Mason5f39d392007-10-15 16:14:19 -04003353 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3354 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003355 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003356 }
Chris Mason6567e832007-04-16 09:22:45 -04003357 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003358 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003359
3360 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003361 if (slot >= nritems) {
3362 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003363 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3364 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003365 BUG_ON(1);
3366 }
Chris Mason6567e832007-04-16 09:22:45 -04003367
3368 /*
3369 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3370 */
3371 /* first correct the data pointers */
3372 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003373 u32 ioff;
3374 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003375
3376 if (!leaf->map_token) {
3377 map_extent_buffer(leaf, (unsigned long)item,
3378 sizeof(struct btrfs_item),
3379 &leaf->map_token, &leaf->kaddr,
3380 &leaf->map_start, &leaf->map_len,
3381 KM_USER1);
3382 }
Chris Mason5f39d392007-10-15 16:14:19 -04003383 ioff = btrfs_item_offset(leaf, item);
3384 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003385 }
Chris Mason5f39d392007-10-15 16:14:19 -04003386
Chris Masondb945352007-10-15 16:15:53 -04003387 if (leaf->map_token) {
3388 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3389 leaf->map_token = NULL;
3390 }
3391
Chris Mason6567e832007-04-16 09:22:45 -04003392 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003393 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003394 data_end - data_size, btrfs_leaf_data(leaf) +
3395 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003396
Chris Mason6567e832007-04-16 09:22:45 -04003397 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003398 old_size = btrfs_item_size_nr(leaf, slot);
3399 item = btrfs_item_nr(leaf, slot);
3400 btrfs_set_item_size(leaf, item, old_size + data_size);
3401 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003402
Chris Mason5f39d392007-10-15 16:14:19 -04003403 if (btrfs_leaf_free_space(root, leaf) < 0) {
3404 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003405 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003406 }
Tsutomu Itoh1cd30792011-05-19 05:19:08 +00003407 return 0;
Chris Mason6567e832007-04-16 09:22:45 -04003408}
3409
Chris Mason74123bd2007-02-02 11:05:29 -05003410/*
Chris Masond352ac62008-09-29 15:18:18 -04003411 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003412 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003413 * Returns the number of keys that were inserted.
3414 */
3415int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3416 struct btrfs_root *root,
3417 struct btrfs_path *path,
3418 struct btrfs_key *cpu_key, u32 *data_size,
3419 int nr)
3420{
3421 struct extent_buffer *leaf;
3422 struct btrfs_item *item;
3423 int ret = 0;
3424 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003425 int i;
3426 u32 nritems;
3427 u32 total_data = 0;
3428 u32 total_size = 0;
3429 unsigned int data_end;
3430 struct btrfs_disk_key disk_key;
3431 struct btrfs_key found_key;
3432
Yan Zheng87b29b22008-12-17 10:21:48 -05003433 for (i = 0; i < nr; i++) {
3434 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3435 BTRFS_LEAF_DATA_SIZE(root)) {
3436 break;
3437 nr = i;
3438 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003439 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003440 total_size += data_size[i] + sizeof(struct btrfs_item);
3441 }
3442 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003443
Josef Bacikf3465ca2008-11-12 14:19:50 -05003444 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3445 if (ret == 0)
3446 return -EEXIST;
3447 if (ret < 0)
3448 goto out;
3449
Josef Bacikf3465ca2008-11-12 14:19:50 -05003450 leaf = path->nodes[0];
3451
3452 nritems = btrfs_header_nritems(leaf);
3453 data_end = leaf_data_end(root, leaf);
3454
3455 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3456 for (i = nr; i >= 0; i--) {
3457 total_data -= data_size[i];
3458 total_size -= data_size[i] + sizeof(struct btrfs_item);
3459 if (total_size < btrfs_leaf_free_space(root, leaf))
3460 break;
3461 }
3462 nr = i;
3463 }
3464
3465 slot = path->slots[0];
3466 BUG_ON(slot < 0);
3467
3468 if (slot != nritems) {
3469 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3470
3471 item = btrfs_item_nr(leaf, slot);
3472 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3473
3474 /* figure out how many keys we can insert in here */
3475 total_data = data_size[0];
3476 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003477 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003478 break;
3479 total_data += data_size[i];
3480 }
3481 nr = i;
3482
3483 if (old_data < data_end) {
3484 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003485 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003486 slot, old_data, data_end);
3487 BUG_ON(1);
3488 }
3489 /*
3490 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3491 */
3492 /* first correct the data pointers */
3493 WARN_ON(leaf->map_token);
3494 for (i = slot; i < nritems; i++) {
3495 u32 ioff;
3496
3497 item = btrfs_item_nr(leaf, i);
3498 if (!leaf->map_token) {
3499 map_extent_buffer(leaf, (unsigned long)item,
3500 sizeof(struct btrfs_item),
3501 &leaf->map_token, &leaf->kaddr,
3502 &leaf->map_start, &leaf->map_len,
3503 KM_USER1);
3504 }
3505
3506 ioff = btrfs_item_offset(leaf, item);
3507 btrfs_set_item_offset(leaf, item, ioff - total_data);
3508 }
3509 if (leaf->map_token) {
3510 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3511 leaf->map_token = NULL;
3512 }
3513
3514 /* shift the items */
3515 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3516 btrfs_item_nr_offset(slot),
3517 (nritems - slot) * sizeof(struct btrfs_item));
3518
3519 /* shift the data */
3520 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3521 data_end - total_data, btrfs_leaf_data(leaf) +
3522 data_end, old_data - data_end);
3523 data_end = old_data;
3524 } else {
3525 /*
3526 * this sucks but it has to be done, if we are inserting at
3527 * the end of the leaf only insert 1 of the items, since we
3528 * have no way of knowing whats on the next leaf and we'd have
3529 * to drop our current locks to figure it out
3530 */
3531 nr = 1;
3532 }
3533
3534 /* setup the item for the new data */
3535 for (i = 0; i < nr; i++) {
3536 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3537 btrfs_set_item_key(leaf, &disk_key, slot + i);
3538 item = btrfs_item_nr(leaf, slot + i);
3539 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3540 data_end -= data_size[i];
3541 btrfs_set_item_size(leaf, item, data_size[i]);
3542 }
3543 btrfs_set_header_nritems(leaf, nritems + nr);
3544 btrfs_mark_buffer_dirty(leaf);
3545
3546 ret = 0;
3547 if (slot == 0) {
3548 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3549 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3550 }
3551
3552 if (btrfs_leaf_free_space(root, leaf) < 0) {
3553 btrfs_print_leaf(root, leaf);
3554 BUG();
3555 }
3556out:
3557 if (!ret)
3558 ret = nr;
3559 return ret;
3560}
3561
3562/*
Chris Mason44871b12009-03-13 10:04:31 -04003563 * this is a helper for btrfs_insert_empty_items, the main goal here is
3564 * to save stack depth by doing the bulk of the work in a function
3565 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003566 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003567int setup_items_for_insert(struct btrfs_trans_handle *trans,
3568 struct btrfs_root *root, struct btrfs_path *path,
3569 struct btrfs_key *cpu_key, u32 *data_size,
3570 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003571{
Chris Mason5f39d392007-10-15 16:14:19 -04003572 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003573 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003574 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003575 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003576 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003577 int ret;
3578 struct extent_buffer *leaf;
3579 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003580
Chris Mason5f39d392007-10-15 16:14:19 -04003581 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003582 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003583
Chris Mason5f39d392007-10-15 16:14:19 -04003584 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003585 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003586
Chris Masonf25956c2008-09-12 15:32:53 -04003587 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003588 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003589 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003590 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003591 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003592 }
Chris Mason5f39d392007-10-15 16:14:19 -04003593
Chris Masonbe0e5c02007-01-26 15:51:26 -05003594 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003595 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003596
Chris Mason5f39d392007-10-15 16:14:19 -04003597 if (old_data < data_end) {
3598 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003599 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003600 slot, old_data, data_end);
3601 BUG_ON(1);
3602 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003603 /*
3604 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3605 */
3606 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003607 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003608 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003609 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003610
Chris Mason5f39d392007-10-15 16:14:19 -04003611 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003612 if (!leaf->map_token) {
3613 map_extent_buffer(leaf, (unsigned long)item,
3614 sizeof(struct btrfs_item),
3615 &leaf->map_token, &leaf->kaddr,
3616 &leaf->map_start, &leaf->map_len,
3617 KM_USER1);
3618 }
3619
Chris Mason5f39d392007-10-15 16:14:19 -04003620 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003621 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003622 }
Chris Masondb945352007-10-15 16:15:53 -04003623 if (leaf->map_token) {
3624 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3625 leaf->map_token = NULL;
3626 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003627
3628 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003629 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003630 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003631 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632
3633 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003634 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003635 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003636 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003637 data_end = old_data;
3638 }
Chris Mason5f39d392007-10-15 16:14:19 -04003639
Chris Mason62e27492007-03-15 12:56:47 -04003640 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003641 for (i = 0; i < nr; i++) {
3642 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3643 btrfs_set_item_key(leaf, &disk_key, slot + i);
3644 item = btrfs_item_nr(leaf, slot + i);
3645 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3646 data_end -= data_size[i];
3647 btrfs_set_item_size(leaf, item, data_size[i]);
3648 }
Chris Mason44871b12009-03-13 10:04:31 -04003649
Chris Mason9c583092008-01-29 15:15:18 -05003650 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003651
3652 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003653 if (slot == 0) {
3654 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003655 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003656 }
Chris Masonb9473432009-03-13 11:00:37 -04003657 btrfs_unlock_up_safe(path, 1);
3658 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003659
Chris Mason5f39d392007-10-15 16:14:19 -04003660 if (btrfs_leaf_free_space(root, leaf) < 0) {
3661 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003662 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003663 }
Chris Mason44871b12009-03-13 10:04:31 -04003664 return ret;
3665}
3666
3667/*
3668 * Given a key and some data, insert items into the tree.
3669 * This does all the path init required, making room in the tree if needed.
3670 */
3671int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3672 struct btrfs_root *root,
3673 struct btrfs_path *path,
3674 struct btrfs_key *cpu_key, u32 *data_size,
3675 int nr)
3676{
Chris Mason44871b12009-03-13 10:04:31 -04003677 int ret = 0;
3678 int slot;
3679 int i;
3680 u32 total_size = 0;
3681 u32 total_data = 0;
3682
3683 for (i = 0; i < nr; i++)
3684 total_data += data_size[i];
3685
3686 total_size = total_data + (nr * sizeof(struct btrfs_item));
3687 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3688 if (ret == 0)
3689 return -EEXIST;
3690 if (ret < 0)
3691 goto out;
3692
Chris Mason44871b12009-03-13 10:04:31 -04003693 slot = path->slots[0];
3694 BUG_ON(slot < 0);
3695
3696 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3697 total_data, total_size, nr);
3698
Chris Masoned2ff2c2007-03-01 18:59:40 -05003699out:
Chris Mason62e27492007-03-15 12:56:47 -04003700 return ret;
3701}
3702
3703/*
3704 * Given a key and some data, insert an item into the tree.
3705 * This does all the path init required, making room in the tree if needed.
3706 */
Chris Masone089f052007-03-16 16:20:31 -04003707int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3708 *root, struct btrfs_key *cpu_key, void *data, u32
3709 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003710{
3711 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003712 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003713 struct extent_buffer *leaf;
3714 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003715
Chris Mason2c90e5d2007-04-02 10:50:19 -04003716 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003717 if (!path)
3718 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003719 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003720 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003721 leaf = path->nodes[0];
3722 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3723 write_extent_buffer(leaf, data, ptr, data_size);
3724 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003725 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003726 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003727 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003728}
3729
Chris Mason74123bd2007-02-02 11:05:29 -05003730/*
Chris Mason5de08d72007-02-24 06:24:44 -05003731 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003732 *
Chris Masond352ac62008-09-29 15:18:18 -04003733 * the tree should have been previously balanced so the deletion does not
3734 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003735 */
Chris Masone089f052007-03-16 16:20:31 -04003736static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3737 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003738{
Chris Mason5f39d392007-10-15 16:14:19 -04003739 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003740 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003741 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003742 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003743
Chris Mason5f39d392007-10-15 16:14:19 -04003744 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003745 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003746 memmove_extent_buffer(parent,
3747 btrfs_node_key_ptr_offset(slot),
3748 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003749 sizeof(struct btrfs_key_ptr) *
3750 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003751 }
Chris Mason7518a232007-03-12 12:01:18 -04003752 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003753 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003754 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003755 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003756 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003757 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003758 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003759 struct btrfs_disk_key disk_key;
3760
3761 btrfs_node_key(parent, &disk_key, 0);
3762 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003763 if (wret)
3764 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003765 }
Chris Masond6025572007-03-30 14:27:56 -04003766 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003767 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003768}
3769
Chris Mason74123bd2007-02-02 11:05:29 -05003770/*
Chris Mason323ac952008-10-01 19:05:46 -04003771 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003772 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003773 *
3774 * This deletes the pointer in path->nodes[1] and frees the leaf
3775 * block extent. zero is returned if it all worked out, < 0 otherwise.
3776 *
3777 * The path must have already been setup for deleting the leaf, including
3778 * all the proper balancing. path->nodes[1] must be locked.
3779 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003780static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3781 struct btrfs_root *root,
3782 struct btrfs_path *path,
3783 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003784{
3785 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003786
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003787 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003788 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3789 if (ret)
3790 return ret;
3791
Chris Mason4d081c42009-02-04 09:31:28 -05003792 /*
3793 * btrfs_free_extent is expensive, we want to make sure we
3794 * aren't holding any locks when we call it
3795 */
3796 btrfs_unlock_up_safe(path, 0);
3797
Yan, Zhengf0486c62010-05-16 10:46:25 -04003798 root_sub_used(root, leaf->len);
3799
3800 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3801 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003802}
3803/*
Chris Mason74123bd2007-02-02 11:05:29 -05003804 * delete the item at the leaf level in path. If that empties
3805 * the leaf, remove it from the tree
3806 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003807int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3808 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003809{
Chris Mason5f39d392007-10-15 16:14:19 -04003810 struct extent_buffer *leaf;
3811 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003812 int last_off;
3813 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003814 int ret = 0;
3815 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003816 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003817 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003818
Chris Mason5f39d392007-10-15 16:14:19 -04003819 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003820 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3821
3822 for (i = 0; i < nr; i++)
3823 dsize += btrfs_item_size_nr(leaf, slot + i);
3824
Chris Mason5f39d392007-10-15 16:14:19 -04003825 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003826
Chris Mason85e21ba2008-01-29 15:11:36 -05003827 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003828 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003829
3830 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003831 data_end + dsize,
3832 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003833 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003834
Chris Mason85e21ba2008-01-29 15:11:36 -05003835 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003836 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003837
Chris Mason5f39d392007-10-15 16:14:19 -04003838 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003839 if (!leaf->map_token) {
3840 map_extent_buffer(leaf, (unsigned long)item,
3841 sizeof(struct btrfs_item),
3842 &leaf->map_token, &leaf->kaddr,
3843 &leaf->map_start, &leaf->map_len,
3844 KM_USER1);
3845 }
Chris Mason5f39d392007-10-15 16:14:19 -04003846 ioff = btrfs_item_offset(leaf, item);
3847 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003848 }
Chris Masondb945352007-10-15 16:15:53 -04003849
3850 if (leaf->map_token) {
3851 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3852 leaf->map_token = NULL;
3853 }
3854
Chris Mason5f39d392007-10-15 16:14:19 -04003855 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003856 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003857 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003858 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003859 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003860 btrfs_set_header_nritems(leaf, nritems - nr);
3861 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003862
Chris Mason74123bd2007-02-02 11:05:29 -05003863 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003864 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003865 if (leaf == root->node) {
3866 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003867 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003868 btrfs_set_path_blocking(path);
3869 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003870 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003871 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003872 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003873 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003874 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003875 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003876 struct btrfs_disk_key disk_key;
3877
3878 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003879 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003880 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003881 if (wret)
3882 ret = wret;
3883 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003884
Chris Mason74123bd2007-02-02 11:05:29 -05003885 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003886 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003887 /* push_leaf_left fixes the path.
3888 * make sure the path still points to our leaf
3889 * for possible call to del_ptr below
3890 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003891 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003892 extent_buffer_get(leaf);
3893
Chris Masonb9473432009-03-13 11:00:37 -04003894 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003895 wret = push_leaf_left(trans, root, path, 1, 1,
3896 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003897 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003898 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003899
3900 if (path->nodes[0] == leaf &&
3901 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003902 wret = push_leaf_right(trans, root, path, 1,
3903 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003904 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003905 ret = wret;
3906 }
Chris Mason5f39d392007-10-15 16:14:19 -04003907
3908 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003909 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003910 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003911 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003912 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003913 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003914 /* if we're still in the path, make sure
3915 * we're dirty. Otherwise, one of the
3916 * push_leaf functions must have already
3917 * dirtied this buffer
3918 */
3919 if (path->nodes[0] == leaf)
3920 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003921 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003922 }
Chris Masond5719762007-03-23 10:01:08 -04003923 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003924 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003925 }
3926 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003927 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003928}
3929
Chris Mason97571fd2007-02-24 13:39:08 -05003930/*
Chris Mason925baed2008-06-25 16:01:30 -04003931 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003932 * returns 0 if it found something or 1 if there are no lesser leaves.
3933 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003934 *
3935 * This may release the path, and so you may lose any locks held at the
3936 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003937 */
3938int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3939{
Chris Mason925baed2008-06-25 16:01:30 -04003940 struct btrfs_key key;
3941 struct btrfs_disk_key found_key;
3942 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003943
Chris Mason925baed2008-06-25 16:01:30 -04003944 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003945
Chris Mason925baed2008-06-25 16:01:30 -04003946 if (key.offset > 0)
3947 key.offset--;
3948 else if (key.type > 0)
3949 key.type--;
3950 else if (key.objectid > 0)
3951 key.objectid--;
3952 else
3953 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003954
David Sterbab3b4aa72011-04-21 01:20:15 +02003955 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003956 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3957 if (ret < 0)
3958 return ret;
3959 btrfs_item_key(path->nodes[0], &found_key, 0);
3960 ret = comp_keys(&found_key, &key);
3961 if (ret < 0)
3962 return 0;
3963 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003964}
3965
Chris Mason3f157a22008-06-25 16:01:31 -04003966/*
3967 * A helper function to walk down the tree starting at min_key, and looking
3968 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003969 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003970 *
3971 * This does not cow, but it does stuff the starting key it finds back
3972 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3973 * key and get a writable path.
3974 *
3975 * This does lock as it descends, and path->keep_locks should be set
3976 * to 1 by the caller.
3977 *
3978 * This honors path->lowest_level to prevent descent past a given level
3979 * of the tree.
3980 *
Chris Masond352ac62008-09-29 15:18:18 -04003981 * min_trans indicates the oldest transaction that you are interested
3982 * in walking through. Any nodes or leaves older than min_trans are
3983 * skipped over (without reading them).
3984 *
Chris Mason3f157a22008-06-25 16:01:31 -04003985 * returns zero if something useful was found, < 0 on error and 1 if there
3986 * was nothing in the tree that matched the search criteria.
3987 */
3988int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003989 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003990 struct btrfs_path *path, int cache_only,
3991 u64 min_trans)
3992{
3993 struct extent_buffer *cur;
3994 struct btrfs_key found_key;
3995 int slot;
Yan96524802008-07-24 12:19:49 -04003996 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003997 u32 nritems;
3998 int level;
3999 int ret = 1;
4000
Chris Mason934d3752008-12-08 16:43:10 -05004001 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004002again:
4003 cur = btrfs_lock_root_node(root);
4004 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004005 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004006 path->nodes[level] = cur;
4007 path->locks[level] = 1;
4008
4009 if (btrfs_header_generation(cur) < min_trans) {
4010 ret = 1;
4011 goto out;
4012 }
Chris Masond3977122009-01-05 21:25:51 -05004013 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004014 nritems = btrfs_header_nritems(cur);
4015 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004016 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004017
Chris Mason323ac952008-10-01 19:05:46 -04004018 /* at the lowest level, we're done, setup the path and exit */
4019 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004020 if (slot >= nritems)
4021 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004022 ret = 0;
4023 path->slots[level] = slot;
4024 btrfs_item_key_to_cpu(cur, &found_key, slot);
4025 goto out;
4026 }
Yan96524802008-07-24 12:19:49 -04004027 if (sret && slot > 0)
4028 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004029 /*
4030 * check this node pointer against the cache_only and
4031 * min_trans parameters. If it isn't in cache or is too
4032 * old, skip to the next one.
4033 */
Chris Masond3977122009-01-05 21:25:51 -05004034 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004035 u64 blockptr;
4036 u64 gen;
4037 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004038 struct btrfs_disk_key disk_key;
4039
Chris Mason3f157a22008-06-25 16:01:31 -04004040 blockptr = btrfs_node_blockptr(cur, slot);
4041 gen = btrfs_node_ptr_generation(cur, slot);
4042 if (gen < min_trans) {
4043 slot++;
4044 continue;
4045 }
4046 if (!cache_only)
4047 break;
4048
Chris Masone02119d2008-09-05 16:13:11 -04004049 if (max_key) {
4050 btrfs_node_key(cur, &disk_key, slot);
4051 if (comp_keys(&disk_key, max_key) >= 0) {
4052 ret = 1;
4053 goto out;
4054 }
4055 }
4056
Chris Mason3f157a22008-06-25 16:01:31 -04004057 tmp = btrfs_find_tree_block(root, blockptr,
4058 btrfs_level_size(root, level - 1));
4059
4060 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4061 free_extent_buffer(tmp);
4062 break;
4063 }
4064 if (tmp)
4065 free_extent_buffer(tmp);
4066 slot++;
4067 }
Chris Masone02119d2008-09-05 16:13:11 -04004068find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004069 /*
4070 * we didn't find a candidate key in this node, walk forward
4071 * and find another one
4072 */
4073 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004074 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004075 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004076 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004077 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004078 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004079 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004080 goto again;
4081 } else {
4082 goto out;
4083 }
4084 }
4085 /* save our key for returning back */
4086 btrfs_node_key_to_cpu(cur, &found_key, slot);
4087 path->slots[level] = slot;
4088 if (level == path->lowest_level) {
4089 ret = 0;
4090 unlock_up(path, level, 1);
4091 goto out;
4092 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004093 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004094 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004095 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004096
4097 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004098
Chris Mason3f157a22008-06-25 16:01:31 -04004099 path->locks[level - 1] = 1;
4100 path->nodes[level - 1] = cur;
4101 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004102 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004103 }
4104out:
4105 if (ret == 0)
4106 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004107 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004108 return ret;
4109}
4110
4111/*
4112 * this is similar to btrfs_next_leaf, but does not try to preserve
4113 * and fixup the path. It looks for and returns the next key in the
4114 * tree based on the current path and the cache_only and min_trans
4115 * parameters.
4116 *
4117 * 0 is returned if another key is found, < 0 if there are any errors
4118 * and 1 is returned if there are no higher keys in the tree
4119 *
4120 * path->keep_locks should be set to 1 on the search made before
4121 * calling this function.
4122 */
Chris Masone7a84562008-06-25 16:01:31 -04004123int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004124 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004125 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004126{
Chris Masone7a84562008-06-25 16:01:31 -04004127 int slot;
4128 struct extent_buffer *c;
4129
Chris Mason934d3752008-12-08 16:43:10 -05004130 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004131 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004132 if (!path->nodes[level])
4133 return 1;
4134
4135 slot = path->slots[level] + 1;
4136 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004137next:
Chris Masone7a84562008-06-25 16:01:31 -04004138 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004139 int ret;
4140 int orig_lowest;
4141 struct btrfs_key cur_key;
4142 if (level + 1 >= BTRFS_MAX_LEVEL ||
4143 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004144 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004145
4146 if (path->locks[level + 1]) {
4147 level++;
4148 continue;
4149 }
4150
4151 slot = btrfs_header_nritems(c) - 1;
4152 if (level == 0)
4153 btrfs_item_key_to_cpu(c, &cur_key, slot);
4154 else
4155 btrfs_node_key_to_cpu(c, &cur_key, slot);
4156
4157 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004158 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004159 path->lowest_level = level;
4160 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4161 0, 0);
4162 path->lowest_level = orig_lowest;
4163 if (ret < 0)
4164 return ret;
4165
4166 c = path->nodes[level];
4167 slot = path->slots[level];
4168 if (ret == 0)
4169 slot++;
4170 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004171 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004172
Chris Masone7a84562008-06-25 16:01:31 -04004173 if (level == 0)
4174 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004175 else {
4176 u64 blockptr = btrfs_node_blockptr(c, slot);
4177 u64 gen = btrfs_node_ptr_generation(c, slot);
4178
4179 if (cache_only) {
4180 struct extent_buffer *cur;
4181 cur = btrfs_find_tree_block(root, blockptr,
4182 btrfs_level_size(root, level - 1));
4183 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4184 slot++;
4185 if (cur)
4186 free_extent_buffer(cur);
4187 goto next;
4188 }
4189 free_extent_buffer(cur);
4190 }
4191 if (gen < min_trans) {
4192 slot++;
4193 goto next;
4194 }
Chris Masone7a84562008-06-25 16:01:31 -04004195 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004196 }
Chris Masone7a84562008-06-25 16:01:31 -04004197 return 0;
4198 }
4199 return 1;
4200}
4201
Chris Mason7bb86312007-12-11 09:25:06 -05004202/*
Chris Mason925baed2008-06-25 16:01:30 -04004203 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004204 * returns 0 if it found something or 1 if there are no greater leaves.
4205 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004206 */
Chris Mason234b63a2007-03-13 10:46:10 -04004207int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004208{
4209 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004210 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004211 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004212 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004213 struct btrfs_key key;
4214 u32 nritems;
4215 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004216 int old_spinning = path->leave_spinning;
4217 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004218
4219 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004220 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004221 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004222
Chris Mason8e73f272009-04-03 10:14:18 -04004223 /*
4224 * we take the blocks in an order that upsets lockdep. Using
4225 * blocking mode is the only way around it.
4226 */
4227#ifdef CONFIG_DEBUG_LOCK_ALLOC
4228 force_blocking = 1;
4229#endif
Chris Mason925baed2008-06-25 16:01:30 -04004230
Chris Mason8e73f272009-04-03 10:14:18 -04004231 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4232again:
4233 level = 1;
4234 next = NULL;
David Sterbab3b4aa72011-04-21 01:20:15 +02004235 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004236
Chris Masona2135012008-06-25 16:01:30 -04004237 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004238
4239 if (!force_blocking)
4240 path->leave_spinning = 1;
4241
Chris Mason925baed2008-06-25 16:01:30 -04004242 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4243 path->keep_locks = 0;
4244
4245 if (ret < 0)
4246 return ret;
4247
Chris Masona2135012008-06-25 16:01:30 -04004248 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004249 /*
4250 * by releasing the path above we dropped all our locks. A balance
4251 * could have added more items next to the key that used to be
4252 * at the very end of the block. So, check again here and
4253 * advance the path if there are now more items available.
4254 */
Chris Masona2135012008-06-25 16:01:30 -04004255 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004256 if (ret == 0)
4257 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004258 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004259 goto done;
4260 }
Chris Masond97e63b2007-02-20 16:40:44 -05004261
Chris Masond3977122009-01-05 21:25:51 -05004262 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004263 if (!path->nodes[level]) {
4264 ret = 1;
4265 goto done;
4266 }
Chris Mason5f39d392007-10-15 16:14:19 -04004267
Chris Masond97e63b2007-02-20 16:40:44 -05004268 slot = path->slots[level] + 1;
4269 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004270 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004271 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004272 if (level == BTRFS_MAX_LEVEL) {
4273 ret = 1;
4274 goto done;
4275 }
Chris Masond97e63b2007-02-20 16:40:44 -05004276 continue;
4277 }
Chris Mason5f39d392007-10-15 16:14:19 -04004278
Chris Mason925baed2008-06-25 16:01:30 -04004279 if (next) {
4280 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004281 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004282 }
Chris Mason5f39d392007-10-15 16:14:19 -04004283
Chris Mason8e73f272009-04-03 10:14:18 -04004284 next = c;
4285 ret = read_block_for_search(NULL, root, path, &next, level,
4286 slot, &key);
4287 if (ret == -EAGAIN)
4288 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004289
Chris Mason76a05b32009-05-14 13:24:30 -04004290 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004291 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004292 goto done;
4293 }
4294
Chris Mason5cd57b22008-06-25 16:01:30 -04004295 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004296 ret = btrfs_try_spin_lock(next);
4297 if (!ret) {
4298 btrfs_set_path_blocking(path);
4299 btrfs_tree_lock(next);
4300 if (!force_blocking)
4301 btrfs_clear_path_blocking(path, next);
4302 }
4303 if (force_blocking)
4304 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004305 }
Chris Masond97e63b2007-02-20 16:40:44 -05004306 break;
4307 }
4308 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004309 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004310 level--;
4311 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004312 if (path->locks[level])
4313 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004314
Chris Mason5f39d392007-10-15 16:14:19 -04004315 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004316 path->nodes[level] = next;
4317 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004318 if (!path->skip_locking)
4319 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004320
Chris Masond97e63b2007-02-20 16:40:44 -05004321 if (!level)
4322 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004323
Chris Mason8e73f272009-04-03 10:14:18 -04004324 ret = read_block_for_search(NULL, root, path, &next, level,
4325 0, &key);
4326 if (ret == -EAGAIN)
4327 goto again;
4328
Chris Mason76a05b32009-05-14 13:24:30 -04004329 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004330 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004331 goto done;
4332 }
4333
Chris Mason5cd57b22008-06-25 16:01:30 -04004334 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004335 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004336 ret = btrfs_try_spin_lock(next);
4337 if (!ret) {
4338 btrfs_set_path_blocking(path);
4339 btrfs_tree_lock(next);
4340 if (!force_blocking)
4341 btrfs_clear_path_blocking(path, next);
4342 }
4343 if (force_blocking)
4344 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004345 }
Chris Masond97e63b2007-02-20 16:40:44 -05004346 }
Chris Mason8e73f272009-04-03 10:14:18 -04004347 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004348done:
4349 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004350 path->leave_spinning = old_spinning;
4351 if (!old_spinning)
4352 btrfs_set_path_blocking(path);
4353
4354 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004355}
Chris Mason0b86a832008-03-24 15:01:56 -04004356
Chris Mason3f157a22008-06-25 16:01:31 -04004357/*
4358 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4359 * searching until it gets past min_objectid or finds an item of 'type'
4360 *
4361 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4362 */
Chris Mason0b86a832008-03-24 15:01:56 -04004363int btrfs_previous_item(struct btrfs_root *root,
4364 struct btrfs_path *path, u64 min_objectid,
4365 int type)
4366{
4367 struct btrfs_key found_key;
4368 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004369 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004370 int ret;
4371
Chris Masond3977122009-01-05 21:25:51 -05004372 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004373 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004374 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004375 ret = btrfs_prev_leaf(root, path);
4376 if (ret != 0)
4377 return ret;
4378 } else {
4379 path->slots[0]--;
4380 }
4381 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004382 nritems = btrfs_header_nritems(leaf);
4383 if (nritems == 0)
4384 return 1;
4385 if (path->slots[0] == nritems)
4386 path->slots[0]--;
4387
Chris Mason0b86a832008-03-24 15:01:56 -04004388 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004389 if (found_key.objectid < min_objectid)
4390 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004391 if (found_key.type == type)
4392 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004393 if (found_key.objectid == min_objectid &&
4394 found_key.type < type)
4395 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004396 }
4397 return 1;
4398}