blob: 2736b6b2ff5f67484526936cb724104872770aa9 [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);
46 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040047 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040048 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040049}
50
Chris Masonb4ce94d2009-02-04 09:25:08 -050051/*
52 * set all locked nodes in the path to blocking locks. This should
53 * be done before scheduling
54 */
55noinline void btrfs_set_path_blocking(struct btrfs_path *p)
56{
57 int i;
58 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
59 if (p->nodes[i] && p->locks[i])
60 btrfs_set_lock_blocking(p->nodes[i]);
61 }
62}
63
64/*
65 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050066 *
67 * held is used to keep lockdep happy, when lockdep is enabled
68 * we set held to a blocking lock before we go around and
69 * retake all the spinlocks in the path. You can safely use NULL
70 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050071 */
Chris Mason4008c042009-02-12 14:09:45 -050072noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
73 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050074{
75 int i;
Chris Mason4008c042009-02-12 14:09:45 -050076
77#ifdef CONFIG_DEBUG_LOCK_ALLOC
78 /* lockdep really cares that we take all of these spinlocks
79 * in the right order. If any of the locks in the path are not
80 * currently blocking, it is going to complain. So, make really
81 * really sure by forcing the path to blocking before we clear
82 * the path blocking.
83 */
84 if (held)
85 btrfs_set_lock_blocking(held);
86 btrfs_set_path_blocking(p);
87#endif
88
89 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050090 if (p->nodes[i] && p->locks[i])
91 btrfs_clear_lock_blocking(p->nodes[i]);
92 }
Chris Mason4008c042009-02-12 14:09:45 -050093
94#ifdef CONFIG_DEBUG_LOCK_ALLOC
95 if (held)
96 btrfs_clear_lock_blocking(held);
97#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -050098}
99
Chris Masond352ac62008-09-29 15:18:18 -0400100/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400101void btrfs_free_path(struct btrfs_path *p)
102{
Jesper Juhlff175d52010-12-25 21:22:30 +0000103 if (!p)
104 return;
Chris Masondf24a2b2007-04-04 09:36:31 -0400105 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400106 kmem_cache_free(btrfs_path_cachep, p);
107}
108
Chris Masond352ac62008-09-29 15:18:18 -0400109/*
110 * path release drops references on the extent buffers in the path
111 * and it drops any locks held by this path
112 *
113 * It is safe to call this on paths that no locks or extent buffers held.
114 */
Chris Masond3977122009-01-05 21:25:51 -0500115noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500116{
117 int i;
Chris Masona2135012008-06-25 16:01:30 -0400118
Chris Mason234b63a2007-03-13 10:46:10 -0400119 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400120 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500121 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400122 continue;
123 if (p->locks[i]) {
124 btrfs_tree_unlock(p->nodes[i]);
125 p->locks[i] = 0;
126 }
Chris Mason5f39d392007-10-15 16:14:19 -0400127 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400128 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500129 }
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
133 * safely gets a reference on the root node of a tree. A lock
134 * is not taken, so a concurrent writer may put a different node
135 * at the root of the tree. See btrfs_lock_root_node for the
136 * looping required.
137 *
138 * The extent buffer returned by this has a reference taken, so
139 * it won't disappear. It may stop being the root of the tree
140 * at any time because there are no locks held.
141 */
Chris Mason925baed2008-06-25 16:01:30 -0400142struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
143{
144 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400145
146 rcu_read_lock();
147 eb = rcu_dereference(root->node);
Chris Mason925baed2008-06-25 16:01:30 -0400148 extent_buffer_get(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400149 rcu_read_unlock();
Chris Mason925baed2008-06-25 16:01:30 -0400150 return eb;
151}
152
Chris Masond352ac62008-09-29 15:18:18 -0400153/* loop around taking references on and locking the root node of the
154 * tree until you end up with a lock on the root. A locked buffer
155 * is returned, with a reference held.
156 */
Chris Mason925baed2008-06-25 16:01:30 -0400157struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
158{
159 struct extent_buffer *eb;
160
Chris Masond3977122009-01-05 21:25:51 -0500161 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400162 eb = btrfs_root_node(root);
163 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400164 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400165 break;
Chris Mason925baed2008-06-25 16:01:30 -0400166 btrfs_tree_unlock(eb);
167 free_extent_buffer(eb);
168 }
169 return eb;
170}
171
Chris Masond352ac62008-09-29 15:18:18 -0400172/* cowonly root (everything not a reference counted cow subvolume), just get
173 * put onto a simple dirty list. transaction.c walks this to make sure they
174 * get properly updated on disk.
175 */
Chris Mason0b86a832008-03-24 15:01:56 -0400176static void add_root_to_dirty_list(struct btrfs_root *root)
177{
178 if (root->track_dirty && list_empty(&root->dirty_list)) {
179 list_add(&root->dirty_list,
180 &root->fs_info->dirty_cowonly_roots);
181 }
182}
183
Chris Masond352ac62008-09-29 15:18:18 -0400184/*
185 * used by snapshot creation to make a copy of a root for a tree with
186 * a given objectid. The buffer with the new root node is returned in
187 * cow_ret, and this func returns zero on success or a negative error code.
188 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500189int btrfs_copy_root(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root,
191 struct extent_buffer *buf,
192 struct extent_buffer **cow_ret, u64 new_root_objectid)
193{
194 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500195 int ret = 0;
196 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400197 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500198
199 WARN_ON(root->ref_cows && trans->transid !=
200 root->fs_info->running_transaction->transid);
201 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
202
203 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400204 if (level == 0)
205 btrfs_item_key(buf, &disk_key, 0);
206 else
207 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400208
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400209 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
210 new_root_objectid, &disk_key, level,
211 buf->start, 0);
212 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 return PTR_ERR(cow);
214
215 copy_extent_buffer(cow, buf, 0, 0, cow->len);
216 btrfs_set_header_bytenr(cow, cow->start);
217 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400218 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
219 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
220 BTRFS_HEADER_FLAG_RELOC);
221 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
222 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
223 else
224 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500225
Yan Zheng2b820322008-11-17 21:11:30 -0500226 write_extent_buffer(cow, root->fs_info->fsid,
227 (unsigned long)btrfs_header_fsid(cow),
228 BTRFS_FSID_SIZE);
229
Chris Masonbe20aa92007-12-17 20:14:01 -0500230 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400231 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
232 ret = btrfs_inc_ref(trans, root, cow, 1);
233 else
234 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500235
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 if (ret)
237 return ret;
238
239 btrfs_mark_buffer_dirty(cow);
240 *cow_ret = cow;
241 return 0;
242}
243
Chris Masond352ac62008-09-29 15:18:18 -0400244/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400245 * check if the tree block can be shared by multiple trees
246 */
247int btrfs_block_can_be_shared(struct btrfs_root *root,
248 struct extent_buffer *buf)
249{
250 /*
251 * Tree blocks not in refernece counted trees and tree roots
252 * are never shared. If a block was allocated after the last
253 * snapshot and the block was not allocated by tree relocation,
254 * we know the block is not shared.
255 */
256 if (root->ref_cows &&
257 buf != root->node && buf != root->commit_root &&
258 (btrfs_header_generation(buf) <=
259 btrfs_root_last_snapshot(&root->root_item) ||
260 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
261 return 1;
262#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
263 if (root->ref_cows &&
264 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
265 return 1;
266#endif
267 return 0;
268}
269
270static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
271 struct btrfs_root *root,
272 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400273 struct extent_buffer *cow,
274 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275{
276 u64 refs;
277 u64 owner;
278 u64 flags;
279 u64 new_flags = 0;
280 int ret;
281
282 /*
283 * Backrefs update rules:
284 *
285 * Always use full backrefs for extent pointers in tree block
286 * allocated by tree relocation.
287 *
288 * If a shared tree block is no longer referenced by its owner
289 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
290 * use full backrefs for extent pointers in tree block.
291 *
292 * If a tree block is been relocating
293 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
294 * use full backrefs for extent pointers in tree block.
295 * The reason for this is some operations (such as drop tree)
296 * are only allowed for blocks use full backrefs.
297 */
298
299 if (btrfs_block_can_be_shared(root, buf)) {
300 ret = btrfs_lookup_extent_info(trans, root, buf->start,
301 buf->len, &refs, &flags);
302 BUG_ON(ret);
303 BUG_ON(refs == 0);
304 } else {
305 refs = 1;
306 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
307 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
308 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
309 else
310 flags = 0;
311 }
312
313 owner = btrfs_header_owner(buf);
314 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
315 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
316
317 if (refs > 1) {
318 if ((owner == root->root_key.objectid ||
319 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
320 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
321 ret = btrfs_inc_ref(trans, root, buf, 1);
322 BUG_ON(ret);
323
324 if (root->root_key.objectid ==
325 BTRFS_TREE_RELOC_OBJECTID) {
326 ret = btrfs_dec_ref(trans, root, buf, 0);
327 BUG_ON(ret);
328 ret = btrfs_inc_ref(trans, root, cow, 1);
329 BUG_ON(ret);
330 }
331 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
332 } else {
333
334 if (root->root_key.objectid ==
335 BTRFS_TREE_RELOC_OBJECTID)
336 ret = btrfs_inc_ref(trans, root, cow, 1);
337 else
338 ret = btrfs_inc_ref(trans, root, cow, 0);
339 BUG_ON(ret);
340 }
341 if (new_flags != 0) {
342 ret = btrfs_set_disk_extent_flags(trans, root,
343 buf->start,
344 buf->len,
345 new_flags, 0);
346 BUG_ON(ret);
347 }
348 } else {
349 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
350 if (root->root_key.objectid ==
351 BTRFS_TREE_RELOC_OBJECTID)
352 ret = btrfs_inc_ref(trans, root, cow, 1);
353 else
354 ret = btrfs_inc_ref(trans, root, cow, 0);
355 BUG_ON(ret);
356 ret = btrfs_dec_ref(trans, root, buf, 1);
357 BUG_ON(ret);
358 }
359 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400360 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400361 }
362 return 0;
363}
364
365/*
Chris Masond3977122009-01-05 21:25:51 -0500366 * does the dirty work in cow of a single block. The parent block (if
367 * supplied) is updated to point to the new cow copy. The new buffer is marked
368 * dirty and returned locked. If you modify the block it needs to be marked
369 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400370 *
371 * search_start -- an allocation hint for the new block
372 *
Chris Masond3977122009-01-05 21:25:51 -0500373 * empty_size -- a hint that you plan on doing more cow. This is the size in
374 * bytes the allocator should try to find free next to the block it returns.
375 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400376 */
Chris Masond3977122009-01-05 21:25:51 -0500377static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400378 struct btrfs_root *root,
379 struct extent_buffer *buf,
380 struct extent_buffer *parent, int parent_slot,
381 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400382 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400383{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400384 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400385 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500386 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400387 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400388 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400389 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400390
Chris Mason925baed2008-06-25 16:01:30 -0400391 if (*cow_ret == buf)
392 unlock_orig = 1;
393
Chris Masonb9447ef2009-03-09 11:45:38 -0400394 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400395
Chris Mason7bb86312007-12-11 09:25:06 -0500396 WARN_ON(root->ref_cows && trans->transid !=
397 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400398 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400399
Chris Mason7bb86312007-12-11 09:25:06 -0500400 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400401
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400402 if (level == 0)
403 btrfs_item_key(buf, &disk_key, 0);
404 else
405 btrfs_node_key(buf, &disk_key, 0);
406
407 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
408 if (parent)
409 parent_start = parent->start;
410 else
411 parent_start = 0;
412 } else
413 parent_start = 0;
414
415 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
416 root->root_key.objectid, &disk_key,
417 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400418 if (IS_ERR(cow))
419 return PTR_ERR(cow);
420
Chris Masonb4ce94d2009-02-04 09:25:08 -0500421 /* cow is set to blocking by btrfs_init_new_buffer */
422
Chris Mason5f39d392007-10-15 16:14:19 -0400423 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400424 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400425 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400426 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
427 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
428 BTRFS_HEADER_FLAG_RELOC);
429 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
430 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
431 else
432 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400433
Yan Zheng2b820322008-11-17 21:11:30 -0500434 write_extent_buffer(cow, root->fs_info->fsid,
435 (unsigned long)btrfs_header_fsid(cow),
436 BTRFS_FSID_SIZE);
437
Yan, Zhengf0486c62010-05-16 10:46:25 -0400438 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400439
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400440 if (root->ref_cows)
441 btrfs_reloc_cow_block(trans, root, buf, cow);
442
Chris Mason6702ed42007-08-07 16:15:09 -0400443 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400444 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400445 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
446 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
447 parent_start = buf->start;
448 else
449 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400450
Chris Mason5f39d392007-10-15 16:14:19 -0400451 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400452 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400453
Yan, Zhengf0486c62010-05-16 10:46:25 -0400454 btrfs_free_tree_block(trans, root, buf, parent_start,
455 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400456 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400457 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400458 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400459 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
460 parent_start = parent->start;
461 else
462 parent_start = 0;
463
464 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400465 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400466 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500467 btrfs_set_node_ptr_generation(parent, parent_slot,
468 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400469 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400470 btrfs_free_tree_block(trans, root, buf, parent_start,
471 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400472 }
Chris Mason925baed2008-06-25 16:01:30 -0400473 if (unlock_orig)
474 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400475 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400476 btrfs_mark_buffer_dirty(cow);
477 *cow_ret = cow;
478 return 0;
479}
480
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400481static inline int should_cow_block(struct btrfs_trans_handle *trans,
482 struct btrfs_root *root,
483 struct extent_buffer *buf)
484{
485 if (btrfs_header_generation(buf) == trans->transid &&
486 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
487 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
488 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
489 return 0;
490 return 1;
491}
492
Chris Masond352ac62008-09-29 15:18:18 -0400493/*
494 * cows a single block, see __btrfs_cow_block for the real work.
495 * This version of it has extra checks so that a block isn't cow'd more than
496 * once per transaction, as long as it hasn't been written yet
497 */
Chris Masond3977122009-01-05 21:25:51 -0500498noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400499 struct btrfs_root *root, struct extent_buffer *buf,
500 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400501 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500502{
Chris Mason6702ed42007-08-07 16:15:09 -0400503 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400504 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500505
Chris Masonccd467d2007-06-28 15:57:36 -0400506 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500507 printk(KERN_CRIT "trans %llu running %llu\n",
508 (unsigned long long)trans->transid,
509 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400510 root->fs_info->running_transaction->transid);
511 WARN_ON(1);
512 }
513 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500514 printk(KERN_CRIT "trans %llu running %llu\n",
515 (unsigned long long)trans->transid,
516 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400517 WARN_ON(1);
518 }
Chris Masondc17ff82008-01-08 15:46:30 -0500519
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400520 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500521 *cow_ret = buf;
522 return 0;
523 }
Chris Masonc4876852009-02-04 09:24:25 -0500524
Chris Mason0b86a832008-03-24 15:01:56 -0400525 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500526
527 if (parent)
528 btrfs_set_lock_blocking(parent);
529 btrfs_set_lock_blocking(buf);
530
Chris Masonf510cfe2007-10-15 16:14:48 -0400531 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400532 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +0000533
534 trace_btrfs_cow_block(root, buf, *cow_ret);
535
Chris Masonf510cfe2007-10-15 16:14:48 -0400536 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400537}
538
Chris Masond352ac62008-09-29 15:18:18 -0400539/*
540 * helper function for defrag to decide if two blocks pointed to by a
541 * node are actually close by
542 */
Chris Mason6b800532007-10-15 16:17:34 -0400543static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400544{
Chris Mason6b800532007-10-15 16:17:34 -0400545 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400546 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400547 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400548 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500549 return 0;
550}
551
Chris Mason081e9572007-11-06 10:26:24 -0500552/*
553 * compare two keys in a memcmp fashion
554 */
555static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
556{
557 struct btrfs_key k1;
558
559 btrfs_disk_key_to_cpu(&k1, disk);
560
Diego Calleja20736ab2009-07-24 11:06:52 -0400561 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500562}
563
Josef Bacikf3465ca2008-11-12 14:19:50 -0500564/*
565 * same as comp_keys only with two btrfs_key's
566 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400567int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500568{
569 if (k1->objectid > k2->objectid)
570 return 1;
571 if (k1->objectid < k2->objectid)
572 return -1;
573 if (k1->type > k2->type)
574 return 1;
575 if (k1->type < k2->type)
576 return -1;
577 if (k1->offset > k2->offset)
578 return 1;
579 if (k1->offset < k2->offset)
580 return -1;
581 return 0;
582}
Chris Mason081e9572007-11-06 10:26:24 -0500583
Chris Masond352ac62008-09-29 15:18:18 -0400584/*
585 * this is used by the defrag code to go through all the
586 * leaves pointed to by a node and reallocate them so that
587 * disk order is close to key order
588 */
Chris Mason6702ed42007-08-07 16:15:09 -0400589int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400590 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400591 int start_slot, int cache_only, u64 *last_ret,
592 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400593{
Chris Mason6b800532007-10-15 16:17:34 -0400594 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400595 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400596 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400597 u64 search_start = *last_ret;
598 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400599 u64 other;
600 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400601 int end_slot;
602 int i;
603 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400604 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400605 int uptodate;
606 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500607 int progress_passed = 0;
608 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400609
Chris Mason5708b952007-10-25 15:43:18 -0400610 parent_level = btrfs_header_level(parent);
611 if (cache_only && parent_level != 1)
612 return 0;
613
Chris Masond3977122009-01-05 21:25:51 -0500614 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400615 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500616 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400617 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400618
Chris Mason6b800532007-10-15 16:17:34 -0400619 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400620 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400621 end_slot = parent_nritems;
622
623 if (parent_nritems == 1)
624 return 0;
625
Chris Masonb4ce94d2009-02-04 09:25:08 -0500626 btrfs_set_lock_blocking(parent);
627
Chris Mason6702ed42007-08-07 16:15:09 -0400628 for (i = start_slot; i < end_slot; i++) {
629 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400630
Chris Mason5708b952007-10-25 15:43:18 -0400631 if (!parent->map_token) {
632 map_extent_buffer(parent,
633 btrfs_node_key_ptr_offset(i),
634 sizeof(struct btrfs_key_ptr),
635 &parent->map_token, &parent->kaddr,
636 &parent->map_start, &parent->map_len,
637 KM_USER1);
638 }
Chris Mason081e9572007-11-06 10:26:24 -0500639 btrfs_node_key(parent, &disk_key, i);
640 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
641 continue;
642
643 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400644 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400645 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400646 if (last_block == 0)
647 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400648
Chris Mason6702ed42007-08-07 16:15:09 -0400649 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400650 other = btrfs_node_blockptr(parent, i - 1);
651 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400652 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400653 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400654 other = btrfs_node_blockptr(parent, i + 1);
655 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400656 }
Chris Masone9d0b132007-08-10 14:06:19 -0400657 if (close) {
658 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400659 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400660 }
Chris Mason5708b952007-10-25 15:43:18 -0400661 if (parent->map_token) {
662 unmap_extent_buffer(parent, parent->map_token,
663 KM_USER1);
664 parent->map_token = NULL;
665 }
Chris Mason6702ed42007-08-07 16:15:09 -0400666
Chris Mason6b800532007-10-15 16:17:34 -0400667 cur = btrfs_find_tree_block(root, blocknr, blocksize);
668 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400669 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400670 else
671 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400672 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400673 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400674 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400675 continue;
676 }
Chris Mason6b800532007-10-15 16:17:34 -0400677 if (!cur) {
678 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400679 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +0000680 if (!cur)
681 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -0400682 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400683 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400684 }
Chris Mason6702ed42007-08-07 16:15:09 -0400685 }
Chris Masone9d0b132007-08-10 14:06:19 -0400686 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400687 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400688
Chris Masone7a84562008-06-25 16:01:31 -0400689 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500690 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400691 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400692 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400693 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400694 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400695 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400696 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400697 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400698 break;
Yan252c38f2007-08-29 09:11:44 -0400699 }
Chris Masone7a84562008-06-25 16:01:31 -0400700 search_start = cur->start;
701 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400702 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400703 btrfs_tree_unlock(cur);
704 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400705 }
Chris Mason5708b952007-10-25 15:43:18 -0400706 if (parent->map_token) {
707 unmap_extent_buffer(parent, parent->map_token,
708 KM_USER1);
709 parent->map_token = NULL;
710 }
Chris Mason6702ed42007-08-07 16:15:09 -0400711 return err;
712}
713
Chris Mason74123bd2007-02-02 11:05:29 -0500714/*
715 * The leaf data grows from end-to-front in the node.
716 * this returns the address of the start of the last item,
717 * which is the stop of the leaf data stack
718 */
Chris Mason123abc82007-03-14 14:14:43 -0400719static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400720 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500721{
Chris Mason5f39d392007-10-15 16:14:19 -0400722 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400724 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400725 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500726}
727
Chris Masonaa5d6be2007-02-28 16:35:06 -0500728
Chris Mason74123bd2007-02-02 11:05:29 -0500729/*
Chris Mason5f39d392007-10-15 16:14:19 -0400730 * search for key in the extent_buffer. The items start at offset p,
731 * and they are item_size apart. There are 'max' items in p.
732 *
Chris Mason74123bd2007-02-02 11:05:29 -0500733 * the slot in the array is returned via slot, and it points to
734 * the place where you would insert key if it is not found in
735 * the array.
736 *
737 * slot may point to max if the key is bigger than all of the keys
738 */
Chris Masone02119d2008-09-05 16:13:11 -0400739static noinline int generic_bin_search(struct extent_buffer *eb,
740 unsigned long p,
741 int item_size, struct btrfs_key *key,
742 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500743{
744 int low = 0;
745 int high = max;
746 int mid;
747 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400748 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400749 struct btrfs_disk_key unaligned;
750 unsigned long offset;
751 char *map_token = NULL;
752 char *kaddr = NULL;
753 unsigned long map_start = 0;
754 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400755 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500756
Chris Masond3977122009-01-05 21:25:51 -0500757 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500758 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400759 offset = p + mid * item_size;
760
761 if (!map_token || offset < map_start ||
762 (offset + sizeof(struct btrfs_disk_key)) >
763 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400764 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400765 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400766 map_token = NULL;
767 }
Chris Mason934d3752008-12-08 16:43:10 -0500768
769 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400770 sizeof(struct btrfs_disk_key),
771 &map_token, &kaddr,
772 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400773
Chris Mason479965d2007-10-15 16:14:27 -0400774 if (!err) {
775 tmp = (struct btrfs_disk_key *)(kaddr + offset -
776 map_start);
777 } else {
778 read_extent_buffer(eb, &unaligned,
779 offset, sizeof(unaligned));
780 tmp = &unaligned;
781 }
782
Chris Mason5f39d392007-10-15 16:14:19 -0400783 } else {
784 tmp = (struct btrfs_disk_key *)(kaddr + offset -
785 map_start);
786 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500787 ret = comp_keys(tmp, key);
788
789 if (ret < 0)
790 low = mid + 1;
791 else if (ret > 0)
792 high = mid;
793 else {
794 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400795 if (map_token)
796 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500797 return 0;
798 }
799 }
800 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400801 if (map_token)
802 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500803 return 1;
804}
805
Chris Mason97571fd2007-02-24 13:39:08 -0500806/*
807 * simple bin_search frontend that does the right thing for
808 * leaves vs nodes
809 */
Chris Mason5f39d392007-10-15 16:14:19 -0400810static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
811 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500812{
Chris Mason5f39d392007-10-15 16:14:19 -0400813 if (level == 0) {
814 return generic_bin_search(eb,
815 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400816 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400817 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400818 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500819 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400820 return generic_bin_search(eb,
821 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400822 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400823 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400824 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500825 }
826 return -1;
827}
828
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400829int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
830 int level, int *slot)
831{
832 return bin_search(eb, key, level, slot);
833}
834
Yan, Zhengf0486c62010-05-16 10:46:25 -0400835static void root_add_used(struct btrfs_root *root, u32 size)
836{
837 spin_lock(&root->accounting_lock);
838 btrfs_set_root_used(&root->root_item,
839 btrfs_root_used(&root->root_item) + size);
840 spin_unlock(&root->accounting_lock);
841}
842
843static void root_sub_used(struct btrfs_root *root, u32 size)
844{
845 spin_lock(&root->accounting_lock);
846 btrfs_set_root_used(&root->root_item,
847 btrfs_root_used(&root->root_item) - size);
848 spin_unlock(&root->accounting_lock);
849}
850
Chris Masond352ac62008-09-29 15:18:18 -0400851/* given a node and slot number, this reads the blocks it points to. The
852 * extent buffer is returned with a reference taken (but unlocked).
853 * NULL is returned on error.
854 */
Chris Masone02119d2008-09-05 16:13:11 -0400855static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400856 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500857{
Chris Masonca7a79a2008-05-12 12:59:19 -0400858 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500859 if (slot < 0)
860 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400861 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500862 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400863
864 BUG_ON(level == 0);
865
Chris Masondb945352007-10-15 16:15:53 -0400866 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400867 btrfs_level_size(root, level - 1),
868 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500869}
870
Chris Masond352ac62008-09-29 15:18:18 -0400871/*
872 * node level balancing, used to make sure nodes are in proper order for
873 * item deletion. We balance from the top down, so we have to make sure
874 * that a deletion won't leave an node completely empty later on.
875 */
Chris Masone02119d2008-09-05 16:13:11 -0400876static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500877 struct btrfs_root *root,
878 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500879{
Chris Mason5f39d392007-10-15 16:14:19 -0400880 struct extent_buffer *right = NULL;
881 struct extent_buffer *mid;
882 struct extent_buffer *left = NULL;
883 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500884 int ret = 0;
885 int wret;
886 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500887 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500888 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500889
890 if (level == 0)
891 return 0;
892
Chris Mason5f39d392007-10-15 16:14:19 -0400893 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500894
Chris Mason925baed2008-06-25 16:01:30 -0400895 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500896 WARN_ON(btrfs_header_generation(mid) != trans->transid);
897
Chris Mason1d4f8a02007-03-13 09:28:32 -0400898 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500899
Chris Mason234b63a2007-03-13 10:46:10 -0400900 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400901 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500902 pslot = path->slots[level + 1];
903
Chris Mason40689472007-03-17 14:29:23 -0400904 /*
905 * deal with the case where there is only one pointer in the root
906 * by promoting the node below to a root
907 */
Chris Mason5f39d392007-10-15 16:14:19 -0400908 if (!parent) {
909 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500910
Chris Mason5f39d392007-10-15 16:14:19 -0400911 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500912 return 0;
913
914 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400915 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500916 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400917 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500918 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400919 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400920 if (ret) {
921 btrfs_tree_unlock(child);
922 free_extent_buffer(child);
923 goto enospc;
924 }
Yan2f375ab2008-02-01 14:58:07 -0500925
Chris Mason240f62c2011-03-23 14:54:42 -0400926 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400927
Chris Mason0b86a832008-03-24 15:01:56 -0400928 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400929 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500930
Chris Mason925baed2008-06-25 16:01:30 -0400931 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500932 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400933 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400934 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500935 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400936 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400937
938 root_sub_used(root, mid->len);
939 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500940 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400941 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400942 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500943 }
Chris Mason5f39d392007-10-15 16:14:19 -0400944 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400945 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500946 return 0;
947
Andi Kleen559af822010-10-29 15:14:37 -0400948 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400949
Chris Mason5f39d392007-10-15 16:14:19 -0400950 left = read_node_slot(root, parent, pslot - 1);
951 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400952 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500953 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400954 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400955 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400956 if (wret) {
957 ret = wret;
958 goto enospc;
959 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400960 }
Chris Mason5f39d392007-10-15 16:14:19 -0400961 right = read_node_slot(root, parent, pslot + 1);
962 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400963 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500964 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400965 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400966 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400967 if (wret) {
968 ret = wret;
969 goto enospc;
970 }
971 }
972
973 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400974 if (left) {
975 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400976 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500977 if (wret < 0)
978 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -0400979 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500980 }
Chris Mason79f95c82007-03-01 15:16:26 -0500981
982 /*
983 * then try to empty the right most buffer into the middle
984 */
Chris Mason5f39d392007-10-15 16:14:19 -0400985 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400986 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500988 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400989 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400990 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400991 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -0400992 wret = del_ptr(trans, root, path, level + 1, pslot +
993 1);
Chris Masonbb803952007-03-01 12:04:21 -0500994 if (wret)
995 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400996 root_sub_used(root, right->len);
997 btrfs_free_tree_block(trans, root, right, 0, 1);
998 free_extent_buffer(right);
999 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001000 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001001 struct btrfs_disk_key right_key;
1002 btrfs_node_key(right, &right_key, 0);
1003 btrfs_set_node_key(parent, &right_key, pslot + 1);
1004 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001005 }
1006 }
Chris Mason5f39d392007-10-15 16:14:19 -04001007 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001008 /*
1009 * we're not allowed to leave a node with one item in the
1010 * tree during a delete. A deletion from lower in the tree
1011 * could try to delete the only pointer in this node.
1012 * So, pull some keys from the left.
1013 * There has to be a left pointer at this point because
1014 * otherwise we would have pulled some pointers from the
1015 * right
1016 */
Chris Mason5f39d392007-10-15 16:14:19 -04001017 BUG_ON(!left);
1018 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001019 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001020 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001021 goto enospc;
1022 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001023 if (wret == 1) {
1024 wret = push_node_left(trans, root, left, mid, 1);
1025 if (wret < 0)
1026 ret = wret;
1027 }
Chris Mason79f95c82007-03-01 15:16:26 -05001028 BUG_ON(wret == 1);
1029 }
Chris Mason5f39d392007-10-15 16:14:19 -04001030 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001031 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001032 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001033 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001034 if (wret)
1035 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001036 root_sub_used(root, mid->len);
1037 btrfs_free_tree_block(trans, root, mid, 0, 1);
1038 free_extent_buffer(mid);
1039 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001040 } else {
1041 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001042 struct btrfs_disk_key mid_key;
1043 btrfs_node_key(mid, &mid_key, 0);
1044 btrfs_set_node_key(parent, &mid_key, pslot);
1045 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001046 }
Chris Masonbb803952007-03-01 12:04:21 -05001047
Chris Mason79f95c82007-03-01 15:16:26 -05001048 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001049 if (left) {
1050 if (btrfs_header_nritems(left) > orig_slot) {
1051 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001052 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001053 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001054 path->slots[level + 1] -= 1;
1055 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001056 if (mid) {
1057 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001058 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001059 }
Chris Masonbb803952007-03-01 12:04:21 -05001060 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001061 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001062 path->slots[level] = orig_slot;
1063 }
1064 }
Chris Mason79f95c82007-03-01 15:16:26 -05001065 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001066 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001067 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001068 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001069enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001070 if (right) {
1071 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001072 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001073 }
1074 if (left) {
1075 if (path->nodes[level] != left)
1076 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001077 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001078 }
Chris Masonbb803952007-03-01 12:04:21 -05001079 return ret;
1080}
1081
Chris Masond352ac62008-09-29 15:18:18 -04001082/* Node balancing for insertion. Here we only split or push nodes around
1083 * when they are completely full. This is also done top down, so we
1084 * have to be pessimistic.
1085 */
Chris Masond3977122009-01-05 21:25:51 -05001086static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001087 struct btrfs_root *root,
1088 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001089{
Chris Mason5f39d392007-10-15 16:14:19 -04001090 struct extent_buffer *right = NULL;
1091 struct extent_buffer *mid;
1092 struct extent_buffer *left = NULL;
1093 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001094 int ret = 0;
1095 int wret;
1096 int pslot;
1097 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001098
1099 if (level == 0)
1100 return 1;
1101
Chris Mason5f39d392007-10-15 16:14:19 -04001102 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001103 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001104
1105 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001106 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001107 pslot = path->slots[level + 1];
1108
Chris Mason5f39d392007-10-15 16:14:19 -04001109 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001110 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001111
Chris Mason5f39d392007-10-15 16:14:19 -04001112 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001113
1114 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001115 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001116 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001117
1118 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001119 btrfs_set_lock_blocking(left);
1120
Chris Mason5f39d392007-10-15 16:14:19 -04001121 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001122 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1123 wret = 1;
1124 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001125 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001126 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001127 if (ret)
1128 wret = 1;
1129 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001130 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001131 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001132 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001133 }
Chris Masone66f7092007-04-20 13:16:02 -04001134 if (wret < 0)
1135 ret = wret;
1136 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001137 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001138 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001139 btrfs_node_key(mid, &disk_key, 0);
1140 btrfs_set_node_key(parent, &disk_key, pslot);
1141 btrfs_mark_buffer_dirty(parent);
1142 if (btrfs_header_nritems(left) > orig_slot) {
1143 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001144 path->slots[level + 1] -= 1;
1145 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001146 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001147 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001148 } else {
1149 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001150 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001151 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001152 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001153 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001154 }
Chris Masone66f7092007-04-20 13:16:02 -04001155 return 0;
1156 }
Chris Mason925baed2008-06-25 16:01:30 -04001157 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001158 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001159 }
Chris Mason925baed2008-06-25 16:01:30 -04001160 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001161
1162 /*
1163 * then try to empty the right most buffer into the middle
1164 */
Chris Mason5f39d392007-10-15 16:14:19 -04001165 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001166 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001167
Chris Mason925baed2008-06-25 16:01:30 -04001168 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001169 btrfs_set_lock_blocking(right);
1170
Chris Mason5f39d392007-10-15 16:14:19 -04001171 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001172 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1173 wret = 1;
1174 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001175 ret = btrfs_cow_block(trans, root, right,
1176 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001177 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001178 if (ret)
1179 wret = 1;
1180 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001182 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001183 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001184 }
Chris Masone66f7092007-04-20 13:16:02 -04001185 if (wret < 0)
1186 ret = wret;
1187 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001188 struct btrfs_disk_key disk_key;
1189
1190 btrfs_node_key(right, &disk_key, 0);
1191 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1192 btrfs_mark_buffer_dirty(parent);
1193
1194 if (btrfs_header_nritems(mid) <= orig_slot) {
1195 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001196 path->slots[level + 1] += 1;
1197 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001198 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001199 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001200 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001201 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001202 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001203 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001204 }
Chris Masone66f7092007-04-20 13:16:02 -04001205 return 0;
1206 }
Chris Mason925baed2008-06-25 16:01:30 -04001207 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001208 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001209 }
Chris Masone66f7092007-04-20 13:16:02 -04001210 return 1;
1211}
1212
Chris Mason74123bd2007-02-02 11:05:29 -05001213/*
Chris Masond352ac62008-09-29 15:18:18 -04001214 * readahead one full node of leaves, finding things that are close
1215 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001216 */
Chris Masonc8c42862009-04-03 10:14:18 -04001217static void reada_for_search(struct btrfs_root *root,
1218 struct btrfs_path *path,
1219 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001220{
Chris Mason5f39d392007-10-15 16:14:19 -04001221 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001222 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001223 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001224 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001225 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001226 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001227 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001228 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001229 u32 nr;
1230 u32 blocksize;
1231 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001232
Chris Masona6b6e752007-10-15 16:22:39 -04001233 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001234 return;
1235
Chris Mason6702ed42007-08-07 16:15:09 -04001236 if (!path->nodes[level])
1237 return;
1238
Chris Mason5f39d392007-10-15 16:14:19 -04001239 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001240
Chris Mason3c69fae2007-08-07 15:52:22 -04001241 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001242 blocksize = btrfs_level_size(root, level - 1);
1243 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001244 if (eb) {
1245 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001246 return;
1247 }
1248
Chris Masona7175312009-01-22 09:23:10 -05001249 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001250
Chris Mason5f39d392007-10-15 16:14:19 -04001251 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001252 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001253 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001254 if (direction < 0) {
1255 if (nr == 0)
1256 break;
1257 nr--;
1258 } else if (direction > 0) {
1259 nr++;
1260 if (nr >= nritems)
1261 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001262 }
Chris Mason01f46652007-12-21 16:24:26 -05001263 if (path->reada < 0 && objectid) {
1264 btrfs_node_key(node, &disk_key, nr);
1265 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1266 break;
1267 }
Chris Mason6b800532007-10-15 16:17:34 -04001268 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001269 if ((search <= target && target - search <= 65536) ||
1270 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001271 readahead_tree_block(root, search, blocksize,
1272 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001273 nread += blocksize;
1274 }
1275 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001276 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001277 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001278 }
1279}
Chris Mason925baed2008-06-25 16:01:30 -04001280
Chris Masond352ac62008-09-29 15:18:18 -04001281/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001282 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1283 * cache
1284 */
1285static noinline int reada_for_balance(struct btrfs_root *root,
1286 struct btrfs_path *path, int level)
1287{
1288 int slot;
1289 int nritems;
1290 struct extent_buffer *parent;
1291 struct extent_buffer *eb;
1292 u64 gen;
1293 u64 block1 = 0;
1294 u64 block2 = 0;
1295 int ret = 0;
1296 int blocksize;
1297
Chris Mason8c594ea2009-04-20 15:50:10 -04001298 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001299 if (!parent)
1300 return 0;
1301
1302 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001303 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001304 blocksize = btrfs_level_size(root, level);
1305
1306 if (slot > 0) {
1307 block1 = btrfs_node_blockptr(parent, slot - 1);
1308 gen = btrfs_node_ptr_generation(parent, slot - 1);
1309 eb = btrfs_find_tree_block(root, block1, blocksize);
1310 if (eb && btrfs_buffer_uptodate(eb, gen))
1311 block1 = 0;
1312 free_extent_buffer(eb);
1313 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001314 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001315 block2 = btrfs_node_blockptr(parent, slot + 1);
1316 gen = btrfs_node_ptr_generation(parent, slot + 1);
1317 eb = btrfs_find_tree_block(root, block2, blocksize);
1318 if (eb && btrfs_buffer_uptodate(eb, gen))
1319 block2 = 0;
1320 free_extent_buffer(eb);
1321 }
1322 if (block1 || block2) {
1323 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001324
1325 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001326 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001327
1328 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001329 if (block1)
1330 readahead_tree_block(root, block1, blocksize, 0);
1331 if (block2)
1332 readahead_tree_block(root, block2, blocksize, 0);
1333
1334 if (block1) {
1335 eb = read_tree_block(root, block1, blocksize, 0);
1336 free_extent_buffer(eb);
1337 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001338 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001339 eb = read_tree_block(root, block2, blocksize, 0);
1340 free_extent_buffer(eb);
1341 }
1342 }
1343 return ret;
1344}
1345
1346
1347/*
Chris Masond3977122009-01-05 21:25:51 -05001348 * when we walk down the tree, it is usually safe to unlock the higher layers
1349 * in the tree. The exceptions are when our path goes through slot 0, because
1350 * operations on the tree might require changing key pointers higher up in the
1351 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001352 *
Chris Masond3977122009-01-05 21:25:51 -05001353 * callers might also have set path->keep_locks, which tells this code to keep
1354 * the lock if the path points to the last slot in the block. This is part of
1355 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001356 *
Chris Masond3977122009-01-05 21:25:51 -05001357 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1358 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001359 */
Chris Masone02119d2008-09-05 16:13:11 -04001360static noinline void unlock_up(struct btrfs_path *path, int level,
1361 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001362{
1363 int i;
1364 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001365 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001366 struct extent_buffer *t;
1367
1368 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1369 if (!path->nodes[i])
1370 break;
1371 if (!path->locks[i])
1372 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001373 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001374 skip_level = i + 1;
1375 continue;
1376 }
Chris Mason051e1b92008-06-25 16:01:30 -04001377 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001378 u32 nritems;
1379 t = path->nodes[i];
1380 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001381 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001382 skip_level = i + 1;
1383 continue;
1384 }
1385 }
Chris Mason051e1b92008-06-25 16:01:30 -04001386 if (skip_level < i && i >= lowest_unlock)
1387 no_skips = 1;
1388
Chris Mason925baed2008-06-25 16:01:30 -04001389 t = path->nodes[i];
1390 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1391 btrfs_tree_unlock(t);
1392 path->locks[i] = 0;
1393 }
1394 }
1395}
1396
Chris Mason3c69fae2007-08-07 15:52:22 -04001397/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001398 * This releases any locks held in the path starting at level and
1399 * going all the way up to the root.
1400 *
1401 * btrfs_search_slot will keep the lock held on higher nodes in a few
1402 * corner cases, such as COW of the block at slot zero in the node. This
1403 * ignores those rules, and it should only be called when there are no
1404 * more updates to be done higher up in the tree.
1405 */
1406noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1407{
1408 int i;
1409
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001410 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001411 return;
1412
1413 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1414 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001415 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001416 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001417 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001418 btrfs_tree_unlock(path->nodes[i]);
1419 path->locks[i] = 0;
1420 }
1421}
1422
1423/*
Chris Masonc8c42862009-04-03 10:14:18 -04001424 * helper function for btrfs_search_slot. The goal is to find a block
1425 * in cache without setting the path to blocking. If we find the block
1426 * we return zero and the path is unchanged.
1427 *
1428 * If we can't find the block, we set the path blocking and do some
1429 * reada. -EAGAIN is returned and the search must be repeated.
1430 */
1431static int
1432read_block_for_search(struct btrfs_trans_handle *trans,
1433 struct btrfs_root *root, struct btrfs_path *p,
1434 struct extent_buffer **eb_ret, int level, int slot,
1435 struct btrfs_key *key)
1436{
1437 u64 blocknr;
1438 u64 gen;
1439 u32 blocksize;
1440 struct extent_buffer *b = *eb_ret;
1441 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001442 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001443
1444 blocknr = btrfs_node_blockptr(b, slot);
1445 gen = btrfs_node_ptr_generation(b, slot);
1446 blocksize = btrfs_level_size(root, level - 1);
1447
1448 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001449 if (tmp) {
1450 if (btrfs_buffer_uptodate(tmp, 0)) {
1451 if (btrfs_buffer_uptodate(tmp, gen)) {
1452 /*
1453 * we found an up to date block without
1454 * sleeping, return
1455 * right away
1456 */
1457 *eb_ret = tmp;
1458 return 0;
1459 }
1460 /* the pages were up to date, but we failed
1461 * the generation number check. Do a full
1462 * read for the generation number that is correct.
1463 * We must do this without dropping locks so
1464 * we can trust our generation number
1465 */
1466 free_extent_buffer(tmp);
1467 tmp = read_tree_block(root, blocknr, blocksize, gen);
1468 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1469 *eb_ret = tmp;
1470 return 0;
1471 }
1472 free_extent_buffer(tmp);
1473 btrfs_release_path(NULL, p);
1474 return -EIO;
1475 }
Chris Masonc8c42862009-04-03 10:14:18 -04001476 }
1477
1478 /*
1479 * reduce lock contention at high levels
1480 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001481 * we read. Don't release the lock on the current
1482 * level because we need to walk this node to figure
1483 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001484 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001485 btrfs_unlock_up_safe(p, level + 1);
1486 btrfs_set_path_blocking(p);
1487
Chris Masoncb449212010-10-24 11:01:27 -04001488 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001489 if (p->reada)
1490 reada_for_search(root, p, level, slot, key->objectid);
1491
Chris Mason8c594ea2009-04-20 15:50:10 -04001492 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001493
1494 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001495 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001496 if (tmp) {
1497 /*
1498 * If the read above didn't mark this buffer up to date,
1499 * it will never end up being up to date. Set ret to EIO now
1500 * and give up so that our caller doesn't loop forever
1501 * on our EAGAINs.
1502 */
1503 if (!btrfs_buffer_uptodate(tmp, 0))
1504 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001505 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001506 }
1507 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001508}
1509
1510/*
1511 * helper function for btrfs_search_slot. This does all of the checks
1512 * for node-level blocks and does any balancing required based on
1513 * the ins_len.
1514 *
1515 * If no extra work was required, zero is returned. If we had to
1516 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1517 * start over
1518 */
1519static int
1520setup_nodes_for_search(struct btrfs_trans_handle *trans,
1521 struct btrfs_root *root, struct btrfs_path *p,
1522 struct extent_buffer *b, int level, int ins_len)
1523{
1524 int ret;
1525 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1526 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1527 int sret;
1528
1529 sret = reada_for_balance(root, p, level);
1530 if (sret)
1531 goto again;
1532
1533 btrfs_set_path_blocking(p);
1534 sret = split_node(trans, root, p, level);
1535 btrfs_clear_path_blocking(p, NULL);
1536
1537 BUG_ON(sret > 0);
1538 if (sret) {
1539 ret = sret;
1540 goto done;
1541 }
1542 b = p->nodes[level];
1543 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001544 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001545 int sret;
1546
1547 sret = reada_for_balance(root, p, level);
1548 if (sret)
1549 goto again;
1550
1551 btrfs_set_path_blocking(p);
1552 sret = balance_level(trans, root, p, level);
1553 btrfs_clear_path_blocking(p, NULL);
1554
1555 if (sret) {
1556 ret = sret;
1557 goto done;
1558 }
1559 b = p->nodes[level];
1560 if (!b) {
1561 btrfs_release_path(NULL, p);
1562 goto again;
1563 }
1564 BUG_ON(btrfs_header_nritems(b) == 1);
1565 }
1566 return 0;
1567
1568again:
1569 ret = -EAGAIN;
1570done:
1571 return ret;
1572}
1573
1574/*
Chris Mason74123bd2007-02-02 11:05:29 -05001575 * look for key in the tree. path is filled in with nodes along the way
1576 * if key is found, we return zero and you can find the item in the leaf
1577 * level of the path (level 0)
1578 *
1579 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001580 * be inserted, and 1 is returned. If there are other errors during the
1581 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001582 *
1583 * if ins_len > 0, nodes and leaves will be split as we walk down the
1584 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1585 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001586 */
Chris Masone089f052007-03-16 16:20:31 -04001587int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1588 *root, struct btrfs_key *key, struct btrfs_path *p, int
1589 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001590{
Chris Mason5f39d392007-10-15 16:14:19 -04001591 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001592 int slot;
1593 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001594 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001595 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001596 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001597 u8 lowest_level = 0;
1598
Chris Mason6702ed42007-08-07 16:15:09 -04001599 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001600 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001601 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001602
Chris Mason925baed2008-06-25 16:01:30 -04001603 if (ins_len < 0)
1604 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001605
Chris Masonbb803952007-03-01 12:04:21 -05001606again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001607 if (p->search_commit_root) {
1608 b = root->commit_root;
1609 extent_buffer_get(b);
1610 if (!p->skip_locking)
1611 btrfs_tree_lock(b);
1612 } else {
1613 if (p->skip_locking)
1614 b = btrfs_root_node(root);
1615 else
1616 b = btrfs_lock_root_node(root);
1617 }
Chris Mason925baed2008-06-25 16:01:30 -04001618
Chris Masoneb60cea2007-02-02 09:18:22 -05001619 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001620 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001621
1622 /*
1623 * setup the path here so we can release it under lock
1624 * contention with the cow code
1625 */
1626 p->nodes[level] = b;
1627 if (!p->skip_locking)
1628 p->locks[level] = 1;
1629
Chris Mason02217ed2007-03-02 16:08:05 -05001630 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001631 /*
1632 * if we don't really need to cow this block
1633 * then we don't want to set the path blocking,
1634 * so we test it here
1635 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001636 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001637 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001638
Chris Masonb4ce94d2009-02-04 09:25:08 -05001639 btrfs_set_path_blocking(p);
1640
Yan Zheng33c66f42009-07-22 09:59:00 -04001641 err = btrfs_cow_block(trans, root, b,
1642 p->nodes[level + 1],
1643 p->slots[level + 1], &b);
1644 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001645 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001646 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001647 }
Chris Mason02217ed2007-03-02 16:08:05 -05001648 }
Chris Mason65b51a02008-08-01 15:11:20 -04001649cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001650 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001651 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001652 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001653 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001654
Chris Masoneb60cea2007-02-02 09:18:22 -05001655 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001656 if (!p->skip_locking)
1657 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001658
Chris Mason4008c042009-02-12 14:09:45 -05001659 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001660
1661 /*
1662 * we have a lock on b and as long as we aren't changing
1663 * the tree, there is no way to for the items in b to change.
1664 * It is safe to drop the lock on our parent before we
1665 * go through the expensive btree search on b.
1666 *
1667 * If cow is true, then we might be changing slot zero,
1668 * which may require changing the parent. So, we can't
1669 * drop the lock until after we know which slot we're
1670 * operating on.
1671 */
1672 if (!cow)
1673 btrfs_unlock_up_safe(p, level + 1);
1674
Chris Mason5f39d392007-10-15 16:14:19 -04001675 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001676
Chris Mason5f39d392007-10-15 16:14:19 -04001677 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001678 int dec = 0;
1679 if (ret && slot > 0) {
1680 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001681 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001682 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001683 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001684 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001685 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001686 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001687 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001688 if (err) {
1689 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001690 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001691 }
Chris Masonc8c42862009-04-03 10:14:18 -04001692 b = p->nodes[level];
1693 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001694
Chris Masonf9efa9c2008-06-25 16:14:04 -04001695 unlock_up(p, level, lowest_unlock);
1696
Chris Mason925baed2008-06-25 16:01:30 -04001697 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001698 if (dec)
1699 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001700 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001701 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001702
Yan Zheng33c66f42009-07-22 09:59:00 -04001703 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001704 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001705 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001706 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001707 if (err) {
1708 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001709 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001710 }
Chris Mason76a05b32009-05-14 13:24:30 -04001711
Chris Masonb4ce94d2009-02-04 09:25:08 -05001712 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001713 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001714 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001715
Yan Zheng33c66f42009-07-22 09:59:00 -04001716 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001717 btrfs_set_path_blocking(p);
1718 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001719 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001720 }
1721 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001722 } else {
1723 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001724 if (ins_len > 0 &&
1725 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001726 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001727 err = split_leaf(trans, root, key,
1728 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001729 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001730
Yan Zheng33c66f42009-07-22 09:59:00 -04001731 BUG_ON(err > 0);
1732 if (err) {
1733 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001734 goto done;
1735 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001736 }
Chris Mason459931e2008-12-10 09:10:46 -05001737 if (!p->search_for_split)
1738 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001739 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001740 }
1741 }
Chris Mason65b51a02008-08-01 15:11:20 -04001742 ret = 1;
1743done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001744 /*
1745 * we don't really know what they plan on doing with the path
1746 * from here on, so for now just mark it as blocking
1747 */
Chris Masonb9473432009-03-13 11:00:37 -04001748 if (!p->leave_spinning)
1749 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001750 if (ret < 0)
1751 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001752 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001753}
1754
Chris Mason74123bd2007-02-02 11:05:29 -05001755/*
1756 * adjust the pointers going up the tree, starting at level
1757 * making sure the right key of each node is points to 'key'.
1758 * This is used after shifting pointers to the left, so it stops
1759 * fixing up pointers when a given leaf/node is not in slot 0 of the
1760 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001761 *
1762 * If this fails to write a tree block, it returns -1, but continues
1763 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001764 */
Chris Mason5f39d392007-10-15 16:14:19 -04001765static int fixup_low_keys(struct btrfs_trans_handle *trans,
1766 struct btrfs_root *root, struct btrfs_path *path,
1767 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001768{
1769 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001770 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001771 struct extent_buffer *t;
1772
Chris Mason234b63a2007-03-13 10:46:10 -04001773 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001774 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001775 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001776 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001777 t = path->nodes[i];
1778 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001779 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001780 if (tslot != 0)
1781 break;
1782 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001783 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001784}
1785
Chris Mason74123bd2007-02-02 11:05:29 -05001786/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001787 * update item key.
1788 *
1789 * This function isn't completely safe. It's the caller's responsibility
1790 * that the new key won't break the order
1791 */
1792int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1793 struct btrfs_root *root, struct btrfs_path *path,
1794 struct btrfs_key *new_key)
1795{
1796 struct btrfs_disk_key disk_key;
1797 struct extent_buffer *eb;
1798 int slot;
1799
1800 eb = path->nodes[0];
1801 slot = path->slots[0];
1802 if (slot > 0) {
1803 btrfs_item_key(eb, &disk_key, slot - 1);
1804 if (comp_keys(&disk_key, new_key) >= 0)
1805 return -1;
1806 }
1807 if (slot < btrfs_header_nritems(eb) - 1) {
1808 btrfs_item_key(eb, &disk_key, slot + 1);
1809 if (comp_keys(&disk_key, new_key) <= 0)
1810 return -1;
1811 }
1812
1813 btrfs_cpu_key_to_disk(&disk_key, new_key);
1814 btrfs_set_item_key(eb, &disk_key, slot);
1815 btrfs_mark_buffer_dirty(eb);
1816 if (slot == 0)
1817 fixup_low_keys(trans, root, path, &disk_key, 1);
1818 return 0;
1819}
1820
1821/*
Chris Mason74123bd2007-02-02 11:05:29 -05001822 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001823 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001824 *
1825 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1826 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001827 */
Chris Mason98ed5172008-01-03 10:01:48 -05001828static int push_node_left(struct btrfs_trans_handle *trans,
1829 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001830 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001831{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001832 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001833 int src_nritems;
1834 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001835 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001836
Chris Mason5f39d392007-10-15 16:14:19 -04001837 src_nritems = btrfs_header_nritems(src);
1838 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001839 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001840 WARN_ON(btrfs_header_generation(src) != trans->transid);
1841 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001842
Chris Masonbce4eae2008-04-24 14:42:46 -04001843 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001844 return 1;
1845
Chris Masond3977122009-01-05 21:25:51 -05001846 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001847 return 1;
1848
Chris Masonbce4eae2008-04-24 14:42:46 -04001849 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001850 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001851 if (push_items < src_nritems) {
1852 /* leave at least 8 pointers in the node if
1853 * we aren't going to empty it
1854 */
1855 if (src_nritems - push_items < 8) {
1856 if (push_items <= 8)
1857 return 1;
1858 push_items -= 8;
1859 }
1860 }
1861 } else
1862 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001863
Chris Mason5f39d392007-10-15 16:14:19 -04001864 copy_extent_buffer(dst, src,
1865 btrfs_node_key_ptr_offset(dst_nritems),
1866 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001867 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001868
Chris Masonbb803952007-03-01 12:04:21 -05001869 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001870 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1871 btrfs_node_key_ptr_offset(push_items),
1872 (src_nritems - push_items) *
1873 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001874 }
Chris Mason5f39d392007-10-15 16:14:19 -04001875 btrfs_set_header_nritems(src, src_nritems - push_items);
1876 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1877 btrfs_mark_buffer_dirty(src);
1878 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001879
Chris Masonbb803952007-03-01 12:04:21 -05001880 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001881}
1882
Chris Mason97571fd2007-02-24 13:39:08 -05001883/*
Chris Mason79f95c82007-03-01 15:16:26 -05001884 * try to push data from one node into the next node right in the
1885 * tree.
1886 *
1887 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1888 * error, and > 0 if there was no room in the right hand block.
1889 *
1890 * this will only push up to 1/2 the contents of the left node over
1891 */
Chris Mason5f39d392007-10-15 16:14:19 -04001892static int balance_node_right(struct btrfs_trans_handle *trans,
1893 struct btrfs_root *root,
1894 struct extent_buffer *dst,
1895 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001896{
Chris Mason79f95c82007-03-01 15:16:26 -05001897 int push_items = 0;
1898 int max_push;
1899 int src_nritems;
1900 int dst_nritems;
1901 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001902
Chris Mason7bb86312007-12-11 09:25:06 -05001903 WARN_ON(btrfs_header_generation(src) != trans->transid);
1904 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1905
Chris Mason5f39d392007-10-15 16:14:19 -04001906 src_nritems = btrfs_header_nritems(src);
1907 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001908 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05001909 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001910 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001911
Chris Masond3977122009-01-05 21:25:51 -05001912 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04001913 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001914
1915 max_push = src_nritems / 2 + 1;
1916 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05001917 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001918 return 1;
Yan252c38f2007-08-29 09:11:44 -04001919
Chris Mason79f95c82007-03-01 15:16:26 -05001920 if (max_push < push_items)
1921 push_items = max_push;
1922
Chris Mason5f39d392007-10-15 16:14:19 -04001923 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1924 btrfs_node_key_ptr_offset(0),
1925 (dst_nritems) *
1926 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001927
Chris Mason5f39d392007-10-15 16:14:19 -04001928 copy_extent_buffer(dst, src,
1929 btrfs_node_key_ptr_offset(0),
1930 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05001931 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001932
Chris Mason5f39d392007-10-15 16:14:19 -04001933 btrfs_set_header_nritems(src, src_nritems - push_items);
1934 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001935
Chris Mason5f39d392007-10-15 16:14:19 -04001936 btrfs_mark_buffer_dirty(src);
1937 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001938
Chris Mason79f95c82007-03-01 15:16:26 -05001939 return ret;
1940}
1941
1942/*
Chris Mason97571fd2007-02-24 13:39:08 -05001943 * helper function to insert a new root level in the tree.
1944 * A new node is allocated, and a single item is inserted to
1945 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001946 *
1947 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001948 */
Chris Masond3977122009-01-05 21:25:51 -05001949static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001950 struct btrfs_root *root,
1951 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001952{
Chris Mason7bb86312007-12-11 09:25:06 -05001953 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001954 struct extent_buffer *lower;
1955 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001956 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001957 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001958
1959 BUG_ON(path->nodes[level]);
1960 BUG_ON(path->nodes[level-1] != root->node);
1961
Chris Mason7bb86312007-12-11 09:25:06 -05001962 lower = path->nodes[level-1];
1963 if (level == 1)
1964 btrfs_item_key(lower, &lower_key, 0);
1965 else
1966 btrfs_node_key(lower, &lower_key, 0);
1967
Zheng Yan31840ae2008-09-23 13:14:14 -04001968 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001969 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001970 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001971 if (IS_ERR(c))
1972 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001973
Yan, Zhengf0486c62010-05-16 10:46:25 -04001974 root_add_used(root, root->nodesize);
1975
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001976 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04001977 btrfs_set_header_nritems(c, 1);
1978 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001979 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001980 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001981 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04001982 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001983
Chris Mason5f39d392007-10-15 16:14:19 -04001984 write_extent_buffer(c, root->fs_info->fsid,
1985 (unsigned long)btrfs_header_fsid(c),
1986 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001987
1988 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1989 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1990 BTRFS_UUID_SIZE);
1991
Chris Mason5f39d392007-10-15 16:14:19 -04001992 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001993 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001994 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001995 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001996
1997 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001998
1999 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002000
Chris Mason925baed2008-06-25 16:01:30 -04002001 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002002 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002003
2004 /* the super has an extra ref to root->node */
2005 free_extent_buffer(old);
2006
Chris Mason0b86a832008-03-24 15:01:56 -04002007 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002008 extent_buffer_get(c);
2009 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002010 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002011 path->slots[level] = 0;
2012 return 0;
2013}
2014
Chris Mason74123bd2007-02-02 11:05:29 -05002015/*
2016 * worker function to insert a single pointer in a node.
2017 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002018 *
Chris Mason74123bd2007-02-02 11:05:29 -05002019 * slot and level indicate where you want the key to go, and
2020 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002021 *
2022 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002023 */
Chris Masone089f052007-03-16 16:20:31 -04002024static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2025 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002026 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002027{
Chris Mason5f39d392007-10-15 16:14:19 -04002028 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002029 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002030
2031 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002032 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002033 lower = path->nodes[level];
2034 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002035 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002036 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002037 BUG();
2038 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002039 memmove_extent_buffer(lower,
2040 btrfs_node_key_ptr_offset(slot + 1),
2041 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002042 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002043 }
Chris Mason5f39d392007-10-15 16:14:19 -04002044 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002045 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002046 WARN_ON(trans->transid == 0);
2047 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002048 btrfs_set_header_nritems(lower, nritems + 1);
2049 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002050 return 0;
2051}
2052
Chris Mason97571fd2007-02-24 13:39:08 -05002053/*
2054 * split the node at the specified level in path in two.
2055 * The path is corrected to point to the appropriate node after the split
2056 *
2057 * Before splitting this tries to make some room in the node by pushing
2058 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002059 *
2060 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002061 */
Chris Masone02119d2008-09-05 16:13:11 -04002062static noinline int split_node(struct btrfs_trans_handle *trans,
2063 struct btrfs_root *root,
2064 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002065{
Chris Mason5f39d392007-10-15 16:14:19 -04002066 struct extent_buffer *c;
2067 struct extent_buffer *split;
2068 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002069 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002070 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002071 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002072 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002073
Chris Mason5f39d392007-10-15 16:14:19 -04002074 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002075 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002076 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002077 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002078 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002079 if (ret)
2080 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002081 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002082 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002083 c = path->nodes[level];
2084 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002085 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002086 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002087 if (ret < 0)
2088 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002089 }
Chris Masone66f7092007-04-20 13:16:02 -04002090
Chris Mason5f39d392007-10-15 16:14:19 -04002091 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002092 mid = (c_nritems + 1) / 2;
2093 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002094
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002095 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002096 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002097 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002098 if (IS_ERR(split))
2099 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002100
Yan, Zhengf0486c62010-05-16 10:46:25 -04002101 root_add_used(root, root->nodesize);
2102
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002103 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002104 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002105 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002107 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002108 btrfs_set_header_owner(split, root->root_key.objectid);
2109 write_extent_buffer(split, root->fs_info->fsid,
2110 (unsigned long)btrfs_header_fsid(split),
2111 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002112 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2113 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2114 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002115
Chris Mason5f39d392007-10-15 16:14:19 -04002116
2117 copy_extent_buffer(split, c,
2118 btrfs_node_key_ptr_offset(0),
2119 btrfs_node_key_ptr_offset(mid),
2120 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2121 btrfs_set_header_nritems(split, c_nritems - mid);
2122 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002123 ret = 0;
2124
Chris Mason5f39d392007-10-15 16:14:19 -04002125 btrfs_mark_buffer_dirty(c);
2126 btrfs_mark_buffer_dirty(split);
2127
Chris Masondb945352007-10-15 16:15:53 -04002128 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002129 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002130 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002131 if (wret)
2132 ret = wret;
2133
Chris Mason5de08d72007-02-24 06:24:44 -05002134 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002135 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002136 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002137 free_extent_buffer(c);
2138 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002139 path->slots[level + 1] += 1;
2140 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002141 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002143 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002144 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002145}
2146
Chris Mason74123bd2007-02-02 11:05:29 -05002147/*
2148 * how many bytes are required to store the items in a leaf. start
2149 * and nr indicate which items in the leaf to check. This totals up the
2150 * space used both by the item structs and the item data
2151 */
Chris Mason5f39d392007-10-15 16:14:19 -04002152static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002153{
2154 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002155 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002156 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002157
2158 if (!nr)
2159 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002160 data_len = btrfs_item_end_nr(l, start);
2161 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002162 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002163 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002164 return data_len;
2165}
2166
Chris Mason74123bd2007-02-02 11:05:29 -05002167/*
Chris Masond4dbff92007-04-04 14:08:15 -04002168 * The space between the end of the leaf items and
2169 * the start of the leaf data. IOW, how much room
2170 * the leaf has left for both items and data
2171 */
Chris Masond3977122009-01-05 21:25:51 -05002172noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002173 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002174{
Chris Mason5f39d392007-10-15 16:14:19 -04002175 int nritems = btrfs_header_nritems(leaf);
2176 int ret;
2177 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2178 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002179 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2180 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002181 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002182 leaf_space_used(leaf, 0, nritems), nritems);
2183 }
2184 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002185}
2186
Chris Mason99d8f832010-07-07 10:51:48 -04002187/*
2188 * min slot controls the lowest index we're willing to push to the
2189 * right. We'll push up to and including min_slot, but no lower
2190 */
Chris Mason44871b12009-03-13 10:04:31 -04002191static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2192 struct btrfs_root *root,
2193 struct btrfs_path *path,
2194 int data_size, int empty,
2195 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002196 int free_space, u32 left_nritems,
2197 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002198{
Chris Mason5f39d392007-10-15 16:14:19 -04002199 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002200 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002201 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002202 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002203 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002204 int push_space = 0;
2205 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002206 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002207 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002208 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002209 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002210 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002211
Chris Mason34a38212007-11-07 13:31:03 -05002212 if (empty)
2213 nr = 0;
2214 else
Chris Mason99d8f832010-07-07 10:51:48 -04002215 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002216
Zheng Yan31840ae2008-09-23 13:14:14 -04002217 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002218 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002219
Chris Mason44871b12009-03-13 10:04:31 -04002220 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002221 i = left_nritems - 1;
2222 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002223 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002224
Zheng Yan31840ae2008-09-23 13:14:14 -04002225 if (!empty && push_items > 0) {
2226 if (path->slots[0] > i)
2227 break;
2228 if (path->slots[0] == i) {
2229 int space = btrfs_leaf_free_space(root, left);
2230 if (space + push_space * 2 > free_space)
2231 break;
2232 }
2233 }
2234
Chris Mason00ec4c52007-02-24 12:47:20 -05002235 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002236 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002237
2238 if (!left->map_token) {
2239 map_extent_buffer(left, (unsigned long)item,
2240 sizeof(struct btrfs_item),
2241 &left->map_token, &left->kaddr,
2242 &left->map_start, &left->map_len,
2243 KM_USER1);
2244 }
2245
2246 this_item_size = btrfs_item_size(left, item);
2247 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002248 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002249
Chris Mason00ec4c52007-02-24 12:47:20 -05002250 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002251 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002252 if (i == 0)
2253 break;
2254 i--;
Chris Masondb945352007-10-15 16:15:53 -04002255 }
2256 if (left->map_token) {
2257 unmap_extent_buffer(left, left->map_token, KM_USER1);
2258 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002259 }
Chris Mason5f39d392007-10-15 16:14:19 -04002260
Chris Mason925baed2008-06-25 16:01:30 -04002261 if (push_items == 0)
2262 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002263
Chris Mason34a38212007-11-07 13:31:03 -05002264 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002265 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002266
Chris Mason00ec4c52007-02-24 12:47:20 -05002267 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002268 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002269
Chris Mason5f39d392007-10-15 16:14:19 -04002270 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002271 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002272
Chris Mason00ec4c52007-02-24 12:47:20 -05002273 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002274 data_end = leaf_data_end(root, right);
2275 memmove_extent_buffer(right,
2276 btrfs_leaf_data(right) + data_end - push_space,
2277 btrfs_leaf_data(right) + data_end,
2278 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2279
Chris Mason00ec4c52007-02-24 12:47:20 -05002280 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002281 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002282 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2283 btrfs_leaf_data(left) + leaf_data_end(root, left),
2284 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002285
2286 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2287 btrfs_item_nr_offset(0),
2288 right_nritems * sizeof(struct btrfs_item));
2289
Chris Mason00ec4c52007-02-24 12:47:20 -05002290 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002291 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2292 btrfs_item_nr_offset(left_nritems - push_items),
2293 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002294
2295 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002296 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002297 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002298 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002299 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002300 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002301 if (!right->map_token) {
2302 map_extent_buffer(right, (unsigned long)item,
2303 sizeof(struct btrfs_item),
2304 &right->map_token, &right->kaddr,
2305 &right->map_start, &right->map_len,
2306 KM_USER1);
2307 }
2308 push_space -= btrfs_item_size(right, item);
2309 btrfs_set_item_offset(right, item, push_space);
2310 }
2311
2312 if (right->map_token) {
2313 unmap_extent_buffer(right, right->map_token, KM_USER1);
2314 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002315 }
Chris Mason7518a232007-03-12 12:01:18 -04002316 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002317 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002318
Chris Mason34a38212007-11-07 13:31:03 -05002319 if (left_nritems)
2320 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002321 else
2322 clean_tree_block(trans, root, left);
2323
Chris Mason5f39d392007-10-15 16:14:19 -04002324 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002325
Chris Mason5f39d392007-10-15 16:14:19 -04002326 btrfs_item_key(right, &disk_key, 0);
2327 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002328 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002329
Chris Mason00ec4c52007-02-24 12:47:20 -05002330 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002331 if (path->slots[0] >= left_nritems) {
2332 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002333 if (btrfs_header_nritems(path->nodes[0]) == 0)
2334 clean_tree_block(trans, root, path->nodes[0]);
2335 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002336 free_extent_buffer(path->nodes[0]);
2337 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002338 path->slots[1] += 1;
2339 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002340 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002341 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002342 }
2343 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002344
2345out_unlock:
2346 btrfs_tree_unlock(right);
2347 free_extent_buffer(right);
2348 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002349}
Chris Mason925baed2008-06-25 16:01:30 -04002350
Chris Mason00ec4c52007-02-24 12:47:20 -05002351/*
Chris Mason44871b12009-03-13 10:04:31 -04002352 * push some data in the path leaf to the right, trying to free up at
2353 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2354 *
2355 * returns 1 if the push failed because the other node didn't have enough
2356 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002357 *
2358 * this will push starting from min_slot to the end of the leaf. It won't
2359 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002360 */
2361static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002362 *root, struct btrfs_path *path,
2363 int min_data_size, int data_size,
2364 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002365{
2366 struct extent_buffer *left = path->nodes[0];
2367 struct extent_buffer *right;
2368 struct extent_buffer *upper;
2369 int slot;
2370 int free_space;
2371 u32 left_nritems;
2372 int ret;
2373
2374 if (!path->nodes[1])
2375 return 1;
2376
2377 slot = path->slots[1];
2378 upper = path->nodes[1];
2379 if (slot >= btrfs_header_nritems(upper) - 1)
2380 return 1;
2381
2382 btrfs_assert_tree_locked(path->nodes[1]);
2383
2384 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002385 if (right == NULL)
2386 return 1;
2387
Chris Mason44871b12009-03-13 10:04:31 -04002388 btrfs_tree_lock(right);
2389 btrfs_set_lock_blocking(right);
2390
2391 free_space = btrfs_leaf_free_space(root, right);
2392 if (free_space < data_size)
2393 goto out_unlock;
2394
2395 /* cow and double check */
2396 ret = btrfs_cow_block(trans, root, right, upper,
2397 slot + 1, &right);
2398 if (ret)
2399 goto out_unlock;
2400
2401 free_space = btrfs_leaf_free_space(root, right);
2402 if (free_space < data_size)
2403 goto out_unlock;
2404
2405 left_nritems = btrfs_header_nritems(left);
2406 if (left_nritems == 0)
2407 goto out_unlock;
2408
Chris Mason99d8f832010-07-07 10:51:48 -04002409 return __push_leaf_right(trans, root, path, min_data_size, empty,
2410 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002411out_unlock:
2412 btrfs_tree_unlock(right);
2413 free_extent_buffer(right);
2414 return 1;
2415}
2416
2417/*
Chris Mason74123bd2007-02-02 11:05:29 -05002418 * push some data in the path leaf to the left, trying to free up at
2419 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002420 *
2421 * max_slot can put a limit on how far into the leaf we'll push items. The
2422 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2423 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002424 */
Chris Mason44871b12009-03-13 10:04:31 -04002425static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2426 struct btrfs_root *root,
2427 struct btrfs_path *path, int data_size,
2428 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002429 int free_space, u32 right_nritems,
2430 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002431{
Chris Mason5f39d392007-10-15 16:14:19 -04002432 struct btrfs_disk_key disk_key;
2433 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002434 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002435 int push_space = 0;
2436 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002437 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002438 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002439 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002440 int ret = 0;
2441 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002442 u32 this_item_size;
2443 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002444
Chris Mason34a38212007-11-07 13:31:03 -05002445 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002446 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002447 else
Chris Mason99d8f832010-07-07 10:51:48 -04002448 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002449
2450 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002451 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002452 if (!right->map_token) {
2453 map_extent_buffer(right, (unsigned long)item,
2454 sizeof(struct btrfs_item),
2455 &right->map_token, &right->kaddr,
2456 &right->map_start, &right->map_len,
2457 KM_USER1);
2458 }
2459
Zheng Yan31840ae2008-09-23 13:14:14 -04002460 if (!empty && push_items > 0) {
2461 if (path->slots[0] < i)
2462 break;
2463 if (path->slots[0] == i) {
2464 int space = btrfs_leaf_free_space(root, right);
2465 if (space + push_space * 2 > free_space)
2466 break;
2467 }
2468 }
2469
Chris Masonbe0e5c02007-01-26 15:51:26 -05002470 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002471 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002472
2473 this_item_size = btrfs_item_size(right, item);
2474 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002475 break;
Chris Masondb945352007-10-15 16:15:53 -04002476
Chris Masonbe0e5c02007-01-26 15:51:26 -05002477 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002478 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002479 }
Chris Masondb945352007-10-15 16:15:53 -04002480
2481 if (right->map_token) {
2482 unmap_extent_buffer(right, right->map_token, KM_USER1);
2483 right->map_token = NULL;
2484 }
2485
Chris Masonbe0e5c02007-01-26 15:51:26 -05002486 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002487 ret = 1;
2488 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002489 }
Chris Mason34a38212007-11-07 13:31:03 -05002490 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002491 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002492
Chris Masonbe0e5c02007-01-26 15:51:26 -05002493 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002494 copy_extent_buffer(left, right,
2495 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2496 btrfs_item_nr_offset(0),
2497 push_items * sizeof(struct btrfs_item));
2498
Chris Mason123abc82007-03-14 14:14:43 -04002499 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002500 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002501
2502 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002503 leaf_data_end(root, left) - push_space,
2504 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002505 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002506 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002507 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002508 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002509
Chris Masondb945352007-10-15 16:15:53 -04002510 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002511 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002512 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002513
Chris Mason5f39d392007-10-15 16:14:19 -04002514 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002515 if (!left->map_token) {
2516 map_extent_buffer(left, (unsigned long)item,
2517 sizeof(struct btrfs_item),
2518 &left->map_token, &left->kaddr,
2519 &left->map_start, &left->map_len,
2520 KM_USER1);
2521 }
2522
Chris Mason5f39d392007-10-15 16:14:19 -04002523 ioff = btrfs_item_offset(left, item);
2524 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002525 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002526 }
Chris Mason5f39d392007-10-15 16:14:19 -04002527 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002528 if (left->map_token) {
2529 unmap_extent_buffer(left, left->map_token, KM_USER1);
2530 left->map_token = NULL;
2531 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002532
2533 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002534 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002535 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2536 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002537 WARN_ON(1);
2538 }
Chris Mason5f39d392007-10-15 16:14:19 -04002539
Chris Mason34a38212007-11-07 13:31:03 -05002540 if (push_items < right_nritems) {
2541 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2542 leaf_data_end(root, right);
2543 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2544 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2545 btrfs_leaf_data(right) +
2546 leaf_data_end(root, right), push_space);
2547
2548 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002549 btrfs_item_nr_offset(push_items),
2550 (btrfs_header_nritems(right) - push_items) *
2551 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002552 }
Yaneef1c492007-11-26 10:58:13 -05002553 right_nritems -= push_items;
2554 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002555 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002556 for (i = 0; i < right_nritems; i++) {
2557 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002558
2559 if (!right->map_token) {
2560 map_extent_buffer(right, (unsigned long)item,
2561 sizeof(struct btrfs_item),
2562 &right->map_token, &right->kaddr,
2563 &right->map_start, &right->map_len,
2564 KM_USER1);
2565 }
2566
2567 push_space = push_space - btrfs_item_size(right, item);
2568 btrfs_set_item_offset(right, item, push_space);
2569 }
2570 if (right->map_token) {
2571 unmap_extent_buffer(right, right->map_token, KM_USER1);
2572 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002573 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002574
Chris Mason5f39d392007-10-15 16:14:19 -04002575 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002576 if (right_nritems)
2577 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002578 else
2579 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002580
Chris Mason5f39d392007-10-15 16:14:19 -04002581 btrfs_item_key(right, &disk_key, 0);
2582 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002583 if (wret)
2584 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002585
2586 /* then fixup the leaf pointer in the path */
2587 if (path->slots[0] < push_items) {
2588 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002589 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002590 free_extent_buffer(path->nodes[0]);
2591 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002592 path->slots[1] -= 1;
2593 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002594 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002595 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002596 path->slots[0] -= push_items;
2597 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002598 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002599 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002600out:
2601 btrfs_tree_unlock(left);
2602 free_extent_buffer(left);
2603 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002604}
2605
Chris Mason74123bd2007-02-02 11:05:29 -05002606/*
Chris Mason44871b12009-03-13 10:04:31 -04002607 * push some data in the path leaf to the left, trying to free up at
2608 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002609 *
2610 * max_slot can put a limit on how far into the leaf we'll push items. The
2611 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2612 * items
Chris Mason44871b12009-03-13 10:04:31 -04002613 */
2614static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002615 *root, struct btrfs_path *path, int min_data_size,
2616 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002617{
2618 struct extent_buffer *right = path->nodes[0];
2619 struct extent_buffer *left;
2620 int slot;
2621 int free_space;
2622 u32 right_nritems;
2623 int ret = 0;
2624
2625 slot = path->slots[1];
2626 if (slot == 0)
2627 return 1;
2628 if (!path->nodes[1])
2629 return 1;
2630
2631 right_nritems = btrfs_header_nritems(right);
2632 if (right_nritems == 0)
2633 return 1;
2634
2635 btrfs_assert_tree_locked(path->nodes[1]);
2636
2637 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002638 if (left == NULL)
2639 return 1;
2640
Chris Mason44871b12009-03-13 10:04:31 -04002641 btrfs_tree_lock(left);
2642 btrfs_set_lock_blocking(left);
2643
2644 free_space = btrfs_leaf_free_space(root, left);
2645 if (free_space < data_size) {
2646 ret = 1;
2647 goto out;
2648 }
2649
2650 /* cow and double check */
2651 ret = btrfs_cow_block(trans, root, left,
2652 path->nodes[1], slot - 1, &left);
2653 if (ret) {
2654 /* we hit -ENOSPC, but it isn't fatal here */
2655 ret = 1;
2656 goto out;
2657 }
2658
2659 free_space = btrfs_leaf_free_space(root, left);
2660 if (free_space < data_size) {
2661 ret = 1;
2662 goto out;
2663 }
2664
Chris Mason99d8f832010-07-07 10:51:48 -04002665 return __push_leaf_left(trans, root, path, min_data_size,
2666 empty, left, free_space, right_nritems,
2667 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002668out:
2669 btrfs_tree_unlock(left);
2670 free_extent_buffer(left);
2671 return ret;
2672}
2673
2674/*
Chris Mason74123bd2007-02-02 11:05:29 -05002675 * split the path's leaf in two, making sure there is at least data_size
2676 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002677 *
2678 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002679 */
Chris Mason44871b12009-03-13 10:04:31 -04002680static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002681 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002682 struct btrfs_path *path,
2683 struct extent_buffer *l,
2684 struct extent_buffer *right,
2685 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002686{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002687 int data_copy_size;
2688 int rt_data_off;
2689 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002690 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002691 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002692 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002693
Chris Mason5f39d392007-10-15 16:14:19 -04002694 nritems = nritems - mid;
2695 btrfs_set_header_nritems(right, nritems);
2696 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2697
2698 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2699 btrfs_item_nr_offset(mid),
2700 nritems * sizeof(struct btrfs_item));
2701
2702 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002703 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2704 data_copy_size, btrfs_leaf_data(l) +
2705 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002706
Chris Mason5f39d392007-10-15 16:14:19 -04002707 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2708 btrfs_item_end_nr(l, mid);
2709
2710 for (i = 0; i < nritems; i++) {
2711 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002712 u32 ioff;
2713
2714 if (!right->map_token) {
2715 map_extent_buffer(right, (unsigned long)item,
2716 sizeof(struct btrfs_item),
2717 &right->map_token, &right->kaddr,
2718 &right->map_start, &right->map_len,
2719 KM_USER1);
2720 }
2721
2722 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002723 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002724 }
Chris Mason74123bd2007-02-02 11:05:29 -05002725
Chris Masondb945352007-10-15 16:15:53 -04002726 if (right->map_token) {
2727 unmap_extent_buffer(right, right->map_token, KM_USER1);
2728 right->map_token = NULL;
2729 }
2730
Chris Mason5f39d392007-10-15 16:14:19 -04002731 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002732 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002733 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002734 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2735 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002736 if (wret)
2737 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002738
2739 btrfs_mark_buffer_dirty(right);
2740 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002741 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002742
Chris Masonbe0e5c02007-01-26 15:51:26 -05002743 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002744 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002745 free_extent_buffer(path->nodes[0]);
2746 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002747 path->slots[0] -= mid;
2748 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002749 } else {
2750 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002751 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002752 }
Chris Mason5f39d392007-10-15 16:14:19 -04002753
Chris Masoneb60cea2007-02-02 09:18:22 -05002754 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002755
Chris Mason44871b12009-03-13 10:04:31 -04002756 return ret;
2757}
2758
2759/*
Chris Mason99d8f832010-07-07 10:51:48 -04002760 * double splits happen when we need to insert a big item in the middle
2761 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2762 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2763 * A B C
2764 *
2765 * We avoid this by trying to push the items on either side of our target
2766 * into the adjacent leaves. If all goes well we can avoid the double split
2767 * completely.
2768 */
2769static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2770 struct btrfs_root *root,
2771 struct btrfs_path *path,
2772 int data_size)
2773{
2774 int ret;
2775 int progress = 0;
2776 int slot;
2777 u32 nritems;
2778
2779 slot = path->slots[0];
2780
2781 /*
2782 * try to push all the items after our slot into the
2783 * right leaf
2784 */
2785 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2786 if (ret < 0)
2787 return ret;
2788
2789 if (ret == 0)
2790 progress++;
2791
2792 nritems = btrfs_header_nritems(path->nodes[0]);
2793 /*
2794 * our goal is to get our slot at the start or end of a leaf. If
2795 * we've done so we're done
2796 */
2797 if (path->slots[0] == 0 || path->slots[0] == nritems)
2798 return 0;
2799
2800 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2801 return 0;
2802
2803 /* try to push all the items before our slot into the next leaf */
2804 slot = path->slots[0];
2805 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2806 if (ret < 0)
2807 return ret;
2808
2809 if (ret == 0)
2810 progress++;
2811
2812 if (progress)
2813 return 0;
2814 return 1;
2815}
2816
2817/*
Chris Mason44871b12009-03-13 10:04:31 -04002818 * split the path's leaf in two, making sure there is at least data_size
2819 * available for the resulting leaf level of the path.
2820 *
2821 * returns 0 if all went well and < 0 on failure.
2822 */
2823static noinline int split_leaf(struct btrfs_trans_handle *trans,
2824 struct btrfs_root *root,
2825 struct btrfs_key *ins_key,
2826 struct btrfs_path *path, int data_size,
2827 int extend)
2828{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002829 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002830 struct extent_buffer *l;
2831 u32 nritems;
2832 int mid;
2833 int slot;
2834 struct extent_buffer *right;
2835 int ret = 0;
2836 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002837 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002838 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002839 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002840
Yan, Zhenga5719522009-09-24 09:17:31 -04002841 l = path->nodes[0];
2842 slot = path->slots[0];
2843 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2844 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2845 return -EOVERFLOW;
2846
Chris Mason44871b12009-03-13 10:04:31 -04002847 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002848 if (data_size) {
2849 wret = push_leaf_right(trans, root, path, data_size,
2850 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002851 if (wret < 0)
2852 return wret;
2853 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002854 wret = push_leaf_left(trans, root, path, data_size,
2855 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002856 if (wret < 0)
2857 return wret;
2858 }
2859 l = path->nodes[0];
2860
2861 /* did the pushes work? */
2862 if (btrfs_leaf_free_space(root, l) >= data_size)
2863 return 0;
2864 }
2865
2866 if (!path->nodes[1]) {
2867 ret = insert_new_root(trans, root, path, 1);
2868 if (ret)
2869 return ret;
2870 }
2871again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002872 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002873 l = path->nodes[0];
2874 slot = path->slots[0];
2875 nritems = btrfs_header_nritems(l);
2876 mid = (nritems + 1) / 2;
2877
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002878 if (mid <= slot) {
2879 if (nritems == 1 ||
2880 leaf_space_used(l, mid, nritems - mid) + data_size >
2881 BTRFS_LEAF_DATA_SIZE(root)) {
2882 if (slot >= nritems) {
2883 split = 0;
2884 } else {
2885 mid = slot;
2886 if (mid != nritems &&
2887 leaf_space_used(l, mid, nritems - mid) +
2888 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002889 if (data_size && !tried_avoid_double)
2890 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002891 split = 2;
2892 }
2893 }
2894 }
2895 } else {
2896 if (leaf_space_used(l, 0, mid) + data_size >
2897 BTRFS_LEAF_DATA_SIZE(root)) {
2898 if (!extend && data_size && slot == 0) {
2899 split = 0;
2900 } else if ((extend || !data_size) && slot == 0) {
2901 mid = 1;
2902 } else {
2903 mid = slot;
2904 if (mid != nritems &&
2905 leaf_space_used(l, mid, nritems - mid) +
2906 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04002907 if (data_size && !tried_avoid_double)
2908 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002909 split = 2 ;
2910 }
2911 }
2912 }
2913 }
2914
2915 if (split == 0)
2916 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2917 else
2918 btrfs_item_key(l, &disk_key, mid);
2919
2920 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002921 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002922 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002923 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04002924 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002925
2926 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04002927
2928 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2929 btrfs_set_header_bytenr(right, right->start);
2930 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002931 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002932 btrfs_set_header_owner(right, root->root_key.objectid);
2933 btrfs_set_header_level(right, 0);
2934 write_extent_buffer(right, root->fs_info->fsid,
2935 (unsigned long)btrfs_header_fsid(right),
2936 BTRFS_FSID_SIZE);
2937
2938 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2939 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2940 BTRFS_UUID_SIZE);
2941
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002942 if (split == 0) {
2943 if (mid <= slot) {
2944 btrfs_set_header_nritems(right, 0);
2945 wret = insert_ptr(trans, root, path,
2946 &disk_key, right->start,
2947 path->slots[1] + 1, 1);
2948 if (wret)
2949 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002950
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002951 btrfs_tree_unlock(path->nodes[0]);
2952 free_extent_buffer(path->nodes[0]);
2953 path->nodes[0] = right;
2954 path->slots[0] = 0;
2955 path->slots[1] += 1;
2956 } else {
2957 btrfs_set_header_nritems(right, 0);
2958 wret = insert_ptr(trans, root, path,
2959 &disk_key,
2960 right->start,
2961 path->slots[1], 1);
2962 if (wret)
2963 ret = wret;
2964 btrfs_tree_unlock(path->nodes[0]);
2965 free_extent_buffer(path->nodes[0]);
2966 path->nodes[0] = right;
2967 path->slots[0] = 0;
2968 if (path->slots[1] == 0) {
2969 wret = fixup_low_keys(trans, root,
2970 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002971 if (wret)
2972 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002973 }
2974 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002975 btrfs_mark_buffer_dirty(right);
2976 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002977 }
2978
2979 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2980 BUG_ON(ret);
2981
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002982 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002983 BUG_ON(num_doubles != 0);
2984 num_doubles++;
2985 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002986 }
Chris Mason44871b12009-03-13 10:04:31 -04002987
Chris Masonbe0e5c02007-01-26 15:51:26 -05002988 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04002989
2990push_for_double:
2991 push_for_double_split(trans, root, path, data_size);
2992 tried_avoid_double = 1;
2993 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2994 return 0;
2995 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002996}
2997
Yan, Zhengad48fd752009-11-12 09:33:58 +00002998static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
2999 struct btrfs_root *root,
3000 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003001{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003002 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003003 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003004 struct btrfs_file_extent_item *fi;
3005 u64 extent_len = 0;
3006 u32 item_size;
3007 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003008
3009 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003010 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3011
3012 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3013 key.type != BTRFS_EXTENT_CSUM_KEY);
3014
3015 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3016 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003017
3018 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003019 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3020 fi = btrfs_item_ptr(leaf, path->slots[0],
3021 struct btrfs_file_extent_item);
3022 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3023 }
Chris Mason459931e2008-12-10 09:10:46 -05003024 btrfs_release_path(root, path);
3025
Chris Mason459931e2008-12-10 09:10:46 -05003026 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003027 path->search_for_split = 1;
3028 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003029 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003030 if (ret < 0)
3031 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003032
Yan, Zhengad48fd752009-11-12 09:33:58 +00003033 ret = -EAGAIN;
3034 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003035 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003036 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3037 goto err;
3038
Chris Mason109f6ae2010-04-02 09:20:18 -04003039 /* the leaf has changed, it now has room. return now */
3040 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3041 goto err;
3042
Yan, Zhengad48fd752009-11-12 09:33:58 +00003043 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3044 fi = btrfs_item_ptr(leaf, path->slots[0],
3045 struct btrfs_file_extent_item);
3046 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3047 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003048 }
3049
Chris Masonb9473432009-03-13 11:00:37 -04003050 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003051 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003052 if (ret)
3053 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003054
Yan, Zhengad48fd752009-11-12 09:33:58 +00003055 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003056 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003057 return 0;
3058err:
3059 path->keep_locks = 0;
3060 return ret;
3061}
3062
3063static noinline int split_item(struct btrfs_trans_handle *trans,
3064 struct btrfs_root *root,
3065 struct btrfs_path *path,
3066 struct btrfs_key *new_key,
3067 unsigned long split_offset)
3068{
3069 struct extent_buffer *leaf;
3070 struct btrfs_item *item;
3071 struct btrfs_item *new_item;
3072 int slot;
3073 char *buf;
3074 u32 nritems;
3075 u32 item_size;
3076 u32 orig_offset;
3077 struct btrfs_disk_key disk_key;
3078
Chris Masonb9473432009-03-13 11:00:37 -04003079 leaf = path->nodes[0];
3080 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3081
Chris Masonb4ce94d2009-02-04 09:25:08 -05003082 btrfs_set_path_blocking(path);
3083
Chris Mason459931e2008-12-10 09:10:46 -05003084 item = btrfs_item_nr(leaf, path->slots[0]);
3085 orig_offset = btrfs_item_offset(leaf, item);
3086 item_size = btrfs_item_size(leaf, item);
3087
Chris Mason459931e2008-12-10 09:10:46 -05003088 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003089 if (!buf)
3090 return -ENOMEM;
3091
Chris Mason459931e2008-12-10 09:10:46 -05003092 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3093 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003094
Chris Mason459931e2008-12-10 09:10:46 -05003095 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003096 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003097 if (slot != nritems) {
3098 /* shift the items */
3099 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003100 btrfs_item_nr_offset(slot),
3101 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003102 }
3103
3104 btrfs_cpu_key_to_disk(&disk_key, new_key);
3105 btrfs_set_item_key(leaf, &disk_key, slot);
3106
3107 new_item = btrfs_item_nr(leaf, slot);
3108
3109 btrfs_set_item_offset(leaf, new_item, orig_offset);
3110 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3111
3112 btrfs_set_item_offset(leaf, item,
3113 orig_offset + item_size - split_offset);
3114 btrfs_set_item_size(leaf, item, split_offset);
3115
3116 btrfs_set_header_nritems(leaf, nritems + 1);
3117
3118 /* write the data for the start of the original item */
3119 write_extent_buffer(leaf, buf,
3120 btrfs_item_ptr_offset(leaf, path->slots[0]),
3121 split_offset);
3122
3123 /* write the data for the new item */
3124 write_extent_buffer(leaf, buf + split_offset,
3125 btrfs_item_ptr_offset(leaf, slot),
3126 item_size - split_offset);
3127 btrfs_mark_buffer_dirty(leaf);
3128
Yan, Zhengad48fd752009-11-12 09:33:58 +00003129 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003130 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003131 return 0;
3132}
3133
3134/*
3135 * This function splits a single item into two items,
3136 * giving 'new_key' to the new item and splitting the
3137 * old one at split_offset (from the start of the item).
3138 *
3139 * The path may be released by this operation. After
3140 * the split, the path is pointing to the old item. The
3141 * new item is going to be in the same node as the old one.
3142 *
3143 * Note, the item being split must be smaller enough to live alone on
3144 * a tree block with room for one extra struct btrfs_item
3145 *
3146 * This allows us to split the item in place, keeping a lock on the
3147 * leaf the entire time.
3148 */
3149int btrfs_split_item(struct btrfs_trans_handle *trans,
3150 struct btrfs_root *root,
3151 struct btrfs_path *path,
3152 struct btrfs_key *new_key,
3153 unsigned long split_offset)
3154{
3155 int ret;
3156 ret = setup_leaf_for_split(trans, root, path,
3157 sizeof(struct btrfs_item));
3158 if (ret)
3159 return ret;
3160
3161 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003162 return ret;
3163}
3164
3165/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003166 * This function duplicate a item, giving 'new_key' to the new item.
3167 * It guarantees both items live in the same tree leaf and the new item
3168 * is contiguous with the original item.
3169 *
3170 * This allows us to split file extent in place, keeping a lock on the
3171 * leaf the entire time.
3172 */
3173int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3174 struct btrfs_root *root,
3175 struct btrfs_path *path,
3176 struct btrfs_key *new_key)
3177{
3178 struct extent_buffer *leaf;
3179 int ret;
3180 u32 item_size;
3181
3182 leaf = path->nodes[0];
3183 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3184 ret = setup_leaf_for_split(trans, root, path,
3185 item_size + sizeof(struct btrfs_item));
3186 if (ret)
3187 return ret;
3188
3189 path->slots[0]++;
3190 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3191 item_size, item_size +
3192 sizeof(struct btrfs_item), 1);
3193 BUG_ON(ret);
3194
3195 leaf = path->nodes[0];
3196 memcpy_extent_buffer(leaf,
3197 btrfs_item_ptr_offset(leaf, path->slots[0]),
3198 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3199 item_size);
3200 return 0;
3201}
3202
3203/*
Chris Masond352ac62008-09-29 15:18:18 -04003204 * make the item pointed to by the path smaller. new_size indicates
3205 * how small to make it, and from_end tells us if we just chop bytes
3206 * off the end of the item or if we shift the item to chop bytes off
3207 * the front.
3208 */
Chris Masonb18c6682007-04-17 13:26:50 -04003209int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3210 struct btrfs_root *root,
3211 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003212 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003213{
3214 int ret = 0;
3215 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003216 struct extent_buffer *leaf;
3217 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003218 u32 nritems;
3219 unsigned int data_end;
3220 unsigned int old_data_start;
3221 unsigned int old_size;
3222 unsigned int size_diff;
3223 int i;
3224
Chris Mason5f39d392007-10-15 16:14:19 -04003225 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003226 slot = path->slots[0];
3227
3228 old_size = btrfs_item_size_nr(leaf, slot);
3229 if (old_size == new_size)
3230 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003231
Chris Mason5f39d392007-10-15 16:14:19 -04003232 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003233 data_end = leaf_data_end(root, leaf);
3234
Chris Mason5f39d392007-10-15 16:14:19 -04003235 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003236
Chris Masonb18c6682007-04-17 13:26:50 -04003237 size_diff = old_size - new_size;
3238
3239 BUG_ON(slot < 0);
3240 BUG_ON(slot >= nritems);
3241
3242 /*
3243 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3244 */
3245 /* first correct the data pointers */
3246 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003247 u32 ioff;
3248 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003249
3250 if (!leaf->map_token) {
3251 map_extent_buffer(leaf, (unsigned long)item,
3252 sizeof(struct btrfs_item),
3253 &leaf->map_token, &leaf->kaddr,
3254 &leaf->map_start, &leaf->map_len,
3255 KM_USER1);
3256 }
3257
Chris Mason5f39d392007-10-15 16:14:19 -04003258 ioff = btrfs_item_offset(leaf, item);
3259 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003260 }
Chris Masondb945352007-10-15 16:15:53 -04003261
3262 if (leaf->map_token) {
3263 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3264 leaf->map_token = NULL;
3265 }
3266
Chris Masonb18c6682007-04-17 13:26:50 -04003267 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003268 if (from_end) {
3269 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3270 data_end + size_diff, btrfs_leaf_data(leaf) +
3271 data_end, old_data_start + new_size - data_end);
3272 } else {
3273 struct btrfs_disk_key disk_key;
3274 u64 offset;
3275
3276 btrfs_item_key(leaf, &disk_key, slot);
3277
3278 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3279 unsigned long ptr;
3280 struct btrfs_file_extent_item *fi;
3281
3282 fi = btrfs_item_ptr(leaf, slot,
3283 struct btrfs_file_extent_item);
3284 fi = (struct btrfs_file_extent_item *)(
3285 (unsigned long)fi - size_diff);
3286
3287 if (btrfs_file_extent_type(leaf, fi) ==
3288 BTRFS_FILE_EXTENT_INLINE) {
3289 ptr = btrfs_item_ptr_offset(leaf, slot);
3290 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003291 (unsigned long)fi,
3292 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003293 disk_bytenr));
3294 }
3295 }
3296
3297 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3298 data_end + size_diff, btrfs_leaf_data(leaf) +
3299 data_end, old_data_start - data_end);
3300
3301 offset = btrfs_disk_key_offset(&disk_key);
3302 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3303 btrfs_set_item_key(leaf, &disk_key, slot);
3304 if (slot == 0)
3305 fixup_low_keys(trans, root, path, &disk_key, 1);
3306 }
Chris Mason5f39d392007-10-15 16:14:19 -04003307
3308 item = btrfs_item_nr(leaf, slot);
3309 btrfs_set_item_size(leaf, item, new_size);
3310 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003311
3312 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003313 if (btrfs_leaf_free_space(root, leaf) < 0) {
3314 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003315 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003316 }
Chris Masonb18c6682007-04-17 13:26:50 -04003317 return ret;
3318}
3319
Chris Masond352ac62008-09-29 15:18:18 -04003320/*
3321 * make the item pointed to by the path bigger, data_size is the new size.
3322 */
Chris Mason5f39d392007-10-15 16:14:19 -04003323int btrfs_extend_item(struct btrfs_trans_handle *trans,
3324 struct btrfs_root *root, struct btrfs_path *path,
3325 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003326{
3327 int ret = 0;
3328 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003329 struct extent_buffer *leaf;
3330 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003331 u32 nritems;
3332 unsigned int data_end;
3333 unsigned int old_data;
3334 unsigned int old_size;
3335 int i;
3336
Chris Mason5f39d392007-10-15 16:14:19 -04003337 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003338
Chris Mason5f39d392007-10-15 16:14:19 -04003339 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003340 data_end = leaf_data_end(root, leaf);
3341
Chris Mason5f39d392007-10-15 16:14:19 -04003342 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3343 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003344 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003345 }
Chris Mason6567e832007-04-16 09:22:45 -04003346 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003347 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003348
3349 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003350 if (slot >= nritems) {
3351 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003352 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3353 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003354 BUG_ON(1);
3355 }
Chris Mason6567e832007-04-16 09:22:45 -04003356
3357 /*
3358 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3359 */
3360 /* first correct the data pointers */
3361 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003362 u32 ioff;
3363 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003364
3365 if (!leaf->map_token) {
3366 map_extent_buffer(leaf, (unsigned long)item,
3367 sizeof(struct btrfs_item),
3368 &leaf->map_token, &leaf->kaddr,
3369 &leaf->map_start, &leaf->map_len,
3370 KM_USER1);
3371 }
Chris Mason5f39d392007-10-15 16:14:19 -04003372 ioff = btrfs_item_offset(leaf, item);
3373 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003374 }
Chris Mason5f39d392007-10-15 16:14:19 -04003375
Chris Masondb945352007-10-15 16:15:53 -04003376 if (leaf->map_token) {
3377 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3378 leaf->map_token = NULL;
3379 }
3380
Chris Mason6567e832007-04-16 09:22:45 -04003381 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003382 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003383 data_end - data_size, btrfs_leaf_data(leaf) +
3384 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003385
Chris Mason6567e832007-04-16 09:22:45 -04003386 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003387 old_size = btrfs_item_size_nr(leaf, slot);
3388 item = btrfs_item_nr(leaf, slot);
3389 btrfs_set_item_size(leaf, item, old_size + data_size);
3390 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003391
3392 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003393 if (btrfs_leaf_free_space(root, leaf) < 0) {
3394 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003395 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003396 }
Chris Mason6567e832007-04-16 09:22:45 -04003397 return ret;
3398}
3399
Chris Mason74123bd2007-02-02 11:05:29 -05003400/*
Chris Masond352ac62008-09-29 15:18:18 -04003401 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003402 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003403 * Returns the number of keys that were inserted.
3404 */
3405int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3406 struct btrfs_root *root,
3407 struct btrfs_path *path,
3408 struct btrfs_key *cpu_key, u32 *data_size,
3409 int nr)
3410{
3411 struct extent_buffer *leaf;
3412 struct btrfs_item *item;
3413 int ret = 0;
3414 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003415 int i;
3416 u32 nritems;
3417 u32 total_data = 0;
3418 u32 total_size = 0;
3419 unsigned int data_end;
3420 struct btrfs_disk_key disk_key;
3421 struct btrfs_key found_key;
3422
Yan Zheng87b29b22008-12-17 10:21:48 -05003423 for (i = 0; i < nr; i++) {
3424 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3425 BTRFS_LEAF_DATA_SIZE(root)) {
3426 break;
3427 nr = i;
3428 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003429 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003430 total_size += data_size[i] + sizeof(struct btrfs_item);
3431 }
3432 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003433
Josef Bacikf3465ca2008-11-12 14:19:50 -05003434 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3435 if (ret == 0)
3436 return -EEXIST;
3437 if (ret < 0)
3438 goto out;
3439
Josef Bacikf3465ca2008-11-12 14:19:50 -05003440 leaf = path->nodes[0];
3441
3442 nritems = btrfs_header_nritems(leaf);
3443 data_end = leaf_data_end(root, leaf);
3444
3445 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3446 for (i = nr; i >= 0; i--) {
3447 total_data -= data_size[i];
3448 total_size -= data_size[i] + sizeof(struct btrfs_item);
3449 if (total_size < btrfs_leaf_free_space(root, leaf))
3450 break;
3451 }
3452 nr = i;
3453 }
3454
3455 slot = path->slots[0];
3456 BUG_ON(slot < 0);
3457
3458 if (slot != nritems) {
3459 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3460
3461 item = btrfs_item_nr(leaf, slot);
3462 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3463
3464 /* figure out how many keys we can insert in here */
3465 total_data = data_size[0];
3466 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003467 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003468 break;
3469 total_data += data_size[i];
3470 }
3471 nr = i;
3472
3473 if (old_data < data_end) {
3474 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003475 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003476 slot, old_data, data_end);
3477 BUG_ON(1);
3478 }
3479 /*
3480 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3481 */
3482 /* first correct the data pointers */
3483 WARN_ON(leaf->map_token);
3484 for (i = slot; i < nritems; i++) {
3485 u32 ioff;
3486
3487 item = btrfs_item_nr(leaf, i);
3488 if (!leaf->map_token) {
3489 map_extent_buffer(leaf, (unsigned long)item,
3490 sizeof(struct btrfs_item),
3491 &leaf->map_token, &leaf->kaddr,
3492 &leaf->map_start, &leaf->map_len,
3493 KM_USER1);
3494 }
3495
3496 ioff = btrfs_item_offset(leaf, item);
3497 btrfs_set_item_offset(leaf, item, ioff - total_data);
3498 }
3499 if (leaf->map_token) {
3500 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3501 leaf->map_token = NULL;
3502 }
3503
3504 /* shift the items */
3505 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3506 btrfs_item_nr_offset(slot),
3507 (nritems - slot) * sizeof(struct btrfs_item));
3508
3509 /* shift the data */
3510 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3511 data_end - total_data, btrfs_leaf_data(leaf) +
3512 data_end, old_data - data_end);
3513 data_end = old_data;
3514 } else {
3515 /*
3516 * this sucks but it has to be done, if we are inserting at
3517 * the end of the leaf only insert 1 of the items, since we
3518 * have no way of knowing whats on the next leaf and we'd have
3519 * to drop our current locks to figure it out
3520 */
3521 nr = 1;
3522 }
3523
3524 /* setup the item for the new data */
3525 for (i = 0; i < nr; i++) {
3526 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3527 btrfs_set_item_key(leaf, &disk_key, slot + i);
3528 item = btrfs_item_nr(leaf, slot + i);
3529 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3530 data_end -= data_size[i];
3531 btrfs_set_item_size(leaf, item, data_size[i]);
3532 }
3533 btrfs_set_header_nritems(leaf, nritems + nr);
3534 btrfs_mark_buffer_dirty(leaf);
3535
3536 ret = 0;
3537 if (slot == 0) {
3538 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3539 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3540 }
3541
3542 if (btrfs_leaf_free_space(root, leaf) < 0) {
3543 btrfs_print_leaf(root, leaf);
3544 BUG();
3545 }
3546out:
3547 if (!ret)
3548 ret = nr;
3549 return ret;
3550}
3551
3552/*
Chris Mason44871b12009-03-13 10:04:31 -04003553 * this is a helper for btrfs_insert_empty_items, the main goal here is
3554 * to save stack depth by doing the bulk of the work in a function
3555 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003556 */
Miao Xie16cdcec2011-04-22 18:12:22 +08003557int setup_items_for_insert(struct btrfs_trans_handle *trans,
3558 struct btrfs_root *root, struct btrfs_path *path,
3559 struct btrfs_key *cpu_key, u32 *data_size,
3560 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003561{
Chris Mason5f39d392007-10-15 16:14:19 -04003562 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003563 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003564 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003565 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003566 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003567 int ret;
3568 struct extent_buffer *leaf;
3569 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003570
Chris Mason5f39d392007-10-15 16:14:19 -04003571 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003572 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003573
Chris Mason5f39d392007-10-15 16:14:19 -04003574 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003575 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003576
Chris Masonf25956c2008-09-12 15:32:53 -04003577 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003578 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003579 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003580 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003581 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003582 }
Chris Mason5f39d392007-10-15 16:14:19 -04003583
Chris Masonbe0e5c02007-01-26 15:51:26 -05003584 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003585 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003586
Chris Mason5f39d392007-10-15 16:14:19 -04003587 if (old_data < data_end) {
3588 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003589 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003590 slot, old_data, data_end);
3591 BUG_ON(1);
3592 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593 /*
3594 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3595 */
3596 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003597 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003598 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003599 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003600
Chris Mason5f39d392007-10-15 16:14:19 -04003601 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003602 if (!leaf->map_token) {
3603 map_extent_buffer(leaf, (unsigned long)item,
3604 sizeof(struct btrfs_item),
3605 &leaf->map_token, &leaf->kaddr,
3606 &leaf->map_start, &leaf->map_len,
3607 KM_USER1);
3608 }
3609
Chris Mason5f39d392007-10-15 16:14:19 -04003610 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003611 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003612 }
Chris Masondb945352007-10-15 16:15:53 -04003613 if (leaf->map_token) {
3614 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3615 leaf->map_token = NULL;
3616 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003617
3618 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003619 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003620 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003621 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003622
3623 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003624 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003625 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003626 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003627 data_end = old_data;
3628 }
Chris Mason5f39d392007-10-15 16:14:19 -04003629
Chris Mason62e27492007-03-15 12:56:47 -04003630 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003631 for (i = 0; i < nr; i++) {
3632 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3633 btrfs_set_item_key(leaf, &disk_key, slot + i);
3634 item = btrfs_item_nr(leaf, slot + i);
3635 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3636 data_end -= data_size[i];
3637 btrfs_set_item_size(leaf, item, data_size[i]);
3638 }
Chris Mason44871b12009-03-13 10:04:31 -04003639
Chris Mason9c583092008-01-29 15:15:18 -05003640 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003641
3642 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003643 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003644 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003645 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003646 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003647 }
Chris Masonb9473432009-03-13 11:00:37 -04003648 btrfs_unlock_up_safe(path, 1);
3649 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003650
Chris Mason5f39d392007-10-15 16:14:19 -04003651 if (btrfs_leaf_free_space(root, leaf) < 0) {
3652 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003653 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003654 }
Chris Mason44871b12009-03-13 10:04:31 -04003655 return ret;
3656}
3657
3658/*
3659 * Given a key and some data, insert items into the tree.
3660 * This does all the path init required, making room in the tree if needed.
3661 */
3662int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3663 struct btrfs_root *root,
3664 struct btrfs_path *path,
3665 struct btrfs_key *cpu_key, u32 *data_size,
3666 int nr)
3667{
Chris Mason44871b12009-03-13 10:04:31 -04003668 int ret = 0;
3669 int slot;
3670 int i;
3671 u32 total_size = 0;
3672 u32 total_data = 0;
3673
3674 for (i = 0; i < nr; i++)
3675 total_data += data_size[i];
3676
3677 total_size = total_data + (nr * sizeof(struct btrfs_item));
3678 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3679 if (ret == 0)
3680 return -EEXIST;
3681 if (ret < 0)
3682 goto out;
3683
Chris Mason44871b12009-03-13 10:04:31 -04003684 slot = path->slots[0];
3685 BUG_ON(slot < 0);
3686
3687 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3688 total_data, total_size, nr);
3689
Chris Masoned2ff2c2007-03-01 18:59:40 -05003690out:
Chris Mason62e27492007-03-15 12:56:47 -04003691 return ret;
3692}
3693
3694/*
3695 * Given a key and some data, insert an item into the tree.
3696 * This does all the path init required, making room in the tree if needed.
3697 */
Chris Masone089f052007-03-16 16:20:31 -04003698int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3699 *root, struct btrfs_key *cpu_key, void *data, u32
3700 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003701{
3702 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003703 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003704 struct extent_buffer *leaf;
3705 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003706
Chris Mason2c90e5d2007-04-02 10:50:19 -04003707 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003708 if (!path)
3709 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003710 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003711 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003712 leaf = path->nodes[0];
3713 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3714 write_extent_buffer(leaf, data, ptr, data_size);
3715 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003716 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003717 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003718 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003719}
3720
Chris Mason74123bd2007-02-02 11:05:29 -05003721/*
Chris Mason5de08d72007-02-24 06:24:44 -05003722 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003723 *
Chris Masond352ac62008-09-29 15:18:18 -04003724 * the tree should have been previously balanced so the deletion does not
3725 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003726 */
Chris Masone089f052007-03-16 16:20:31 -04003727static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3728 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003729{
Chris Mason5f39d392007-10-15 16:14:19 -04003730 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003731 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003732 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003733 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003734
Chris Mason5f39d392007-10-15 16:14:19 -04003735 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003736 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003737 memmove_extent_buffer(parent,
3738 btrfs_node_key_ptr_offset(slot),
3739 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003740 sizeof(struct btrfs_key_ptr) *
3741 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003742 }
Chris Mason7518a232007-03-12 12:01:18 -04003743 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003744 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003745 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003746 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003747 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003748 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003749 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003750 struct btrfs_disk_key disk_key;
3751
3752 btrfs_node_key(parent, &disk_key, 0);
3753 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003754 if (wret)
3755 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003756 }
Chris Masond6025572007-03-30 14:27:56 -04003757 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003758 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003759}
3760
Chris Mason74123bd2007-02-02 11:05:29 -05003761/*
Chris Mason323ac952008-10-01 19:05:46 -04003762 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003763 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003764 *
3765 * This deletes the pointer in path->nodes[1] and frees the leaf
3766 * block extent. zero is returned if it all worked out, < 0 otherwise.
3767 *
3768 * The path must have already been setup for deleting the leaf, including
3769 * all the proper balancing. path->nodes[1] must be locked.
3770 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003771static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3772 struct btrfs_root *root,
3773 struct btrfs_path *path,
3774 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003775{
3776 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003777
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003778 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003779 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3780 if (ret)
3781 return ret;
3782
Chris Mason4d081c42009-02-04 09:31:28 -05003783 /*
3784 * btrfs_free_extent is expensive, we want to make sure we
3785 * aren't holding any locks when we call it
3786 */
3787 btrfs_unlock_up_safe(path, 0);
3788
Yan, Zhengf0486c62010-05-16 10:46:25 -04003789 root_sub_used(root, leaf->len);
3790
3791 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3792 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003793}
3794/*
Chris Mason74123bd2007-02-02 11:05:29 -05003795 * delete the item at the leaf level in path. If that empties
3796 * the leaf, remove it from the tree
3797 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003798int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3799 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003800{
Chris Mason5f39d392007-10-15 16:14:19 -04003801 struct extent_buffer *leaf;
3802 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003803 int last_off;
3804 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003805 int ret = 0;
3806 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003807 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003808 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003809
Chris Mason5f39d392007-10-15 16:14:19 -04003810 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003811 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3812
3813 for (i = 0; i < nr; i++)
3814 dsize += btrfs_item_size_nr(leaf, slot + i);
3815
Chris Mason5f39d392007-10-15 16:14:19 -04003816 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003817
Chris Mason85e21ba2008-01-29 15:11:36 -05003818 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003819 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003820
3821 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003822 data_end + dsize,
3823 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003824 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003825
Chris Mason85e21ba2008-01-29 15:11:36 -05003826 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003827 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003828
Chris Mason5f39d392007-10-15 16:14:19 -04003829 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003830 if (!leaf->map_token) {
3831 map_extent_buffer(leaf, (unsigned long)item,
3832 sizeof(struct btrfs_item),
3833 &leaf->map_token, &leaf->kaddr,
3834 &leaf->map_start, &leaf->map_len,
3835 KM_USER1);
3836 }
Chris Mason5f39d392007-10-15 16:14:19 -04003837 ioff = btrfs_item_offset(leaf, item);
3838 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003839 }
Chris Masondb945352007-10-15 16:15:53 -04003840
3841 if (leaf->map_token) {
3842 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3843 leaf->map_token = NULL;
3844 }
3845
Chris Mason5f39d392007-10-15 16:14:19 -04003846 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003847 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003848 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003849 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003850 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003851 btrfs_set_header_nritems(leaf, nritems - nr);
3852 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003853
Chris Mason74123bd2007-02-02 11:05:29 -05003854 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003855 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003856 if (leaf == root->node) {
3857 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003858 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04003859 btrfs_set_path_blocking(path);
3860 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003861 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003862 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003863 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003864 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003865 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003866 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003867 struct btrfs_disk_key disk_key;
3868
3869 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003870 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003871 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003872 if (wret)
3873 ret = wret;
3874 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003875
Chris Mason74123bd2007-02-02 11:05:29 -05003876 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003877 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003878 /* push_leaf_left fixes the path.
3879 * make sure the path still points to our leaf
3880 * for possible call to del_ptr below
3881 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003882 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003883 extent_buffer_get(leaf);
3884
Chris Masonb9473432009-03-13 11:00:37 -04003885 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04003886 wret = push_leaf_left(trans, root, path, 1, 1,
3887 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003888 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003889 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003890
3891 if (path->nodes[0] == leaf &&
3892 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003893 wret = push_leaf_right(trans, root, path, 1,
3894 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04003895 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003896 ret = wret;
3897 }
Chris Mason5f39d392007-10-15 16:14:19 -04003898
3899 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003900 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003901 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003902 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003903 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003904 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003905 /* if we're still in the path, make sure
3906 * we're dirty. Otherwise, one of the
3907 * push_leaf functions must have already
3908 * dirtied this buffer
3909 */
3910 if (path->nodes[0] == leaf)
3911 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003912 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003913 }
Chris Masond5719762007-03-23 10:01:08 -04003914 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003915 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003916 }
3917 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003918 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003919}
3920
Chris Mason97571fd2007-02-24 13:39:08 -05003921/*
Chris Mason925baed2008-06-25 16:01:30 -04003922 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003923 * returns 0 if it found something or 1 if there are no lesser leaves.
3924 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003925 *
3926 * This may release the path, and so you may lose any locks held at the
3927 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003928 */
3929int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3930{
Chris Mason925baed2008-06-25 16:01:30 -04003931 struct btrfs_key key;
3932 struct btrfs_disk_key found_key;
3933 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003934
Chris Mason925baed2008-06-25 16:01:30 -04003935 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003936
Chris Mason925baed2008-06-25 16:01:30 -04003937 if (key.offset > 0)
3938 key.offset--;
3939 else if (key.type > 0)
3940 key.type--;
3941 else if (key.objectid > 0)
3942 key.objectid--;
3943 else
3944 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003945
Chris Mason925baed2008-06-25 16:01:30 -04003946 btrfs_release_path(root, path);
3947 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3948 if (ret < 0)
3949 return ret;
3950 btrfs_item_key(path->nodes[0], &found_key, 0);
3951 ret = comp_keys(&found_key, &key);
3952 if (ret < 0)
3953 return 0;
3954 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003955}
3956
Chris Mason3f157a22008-06-25 16:01:31 -04003957/*
3958 * A helper function to walk down the tree starting at min_key, and looking
3959 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003960 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003961 *
3962 * This does not cow, but it does stuff the starting key it finds back
3963 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3964 * key and get a writable path.
3965 *
3966 * This does lock as it descends, and path->keep_locks should be set
3967 * to 1 by the caller.
3968 *
3969 * This honors path->lowest_level to prevent descent past a given level
3970 * of the tree.
3971 *
Chris Masond352ac62008-09-29 15:18:18 -04003972 * min_trans indicates the oldest transaction that you are interested
3973 * in walking through. Any nodes or leaves older than min_trans are
3974 * skipped over (without reading them).
3975 *
Chris Mason3f157a22008-06-25 16:01:31 -04003976 * returns zero if something useful was found, < 0 on error and 1 if there
3977 * was nothing in the tree that matched the search criteria.
3978 */
3979int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003980 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003981 struct btrfs_path *path, int cache_only,
3982 u64 min_trans)
3983{
3984 struct extent_buffer *cur;
3985 struct btrfs_key found_key;
3986 int slot;
Yan96524802008-07-24 12:19:49 -04003987 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003988 u32 nritems;
3989 int level;
3990 int ret = 1;
3991
Chris Mason934d3752008-12-08 16:43:10 -05003992 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003993again:
3994 cur = btrfs_lock_root_node(root);
3995 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003996 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003997 path->nodes[level] = cur;
3998 path->locks[level] = 1;
3999
4000 if (btrfs_header_generation(cur) < min_trans) {
4001 ret = 1;
4002 goto out;
4003 }
Chris Masond3977122009-01-05 21:25:51 -05004004 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004005 nritems = btrfs_header_nritems(cur);
4006 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004007 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004008
Chris Mason323ac952008-10-01 19:05:46 -04004009 /* at the lowest level, we're done, setup the path and exit */
4010 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004011 if (slot >= nritems)
4012 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004013 ret = 0;
4014 path->slots[level] = slot;
4015 btrfs_item_key_to_cpu(cur, &found_key, slot);
4016 goto out;
4017 }
Yan96524802008-07-24 12:19:49 -04004018 if (sret && slot > 0)
4019 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004020 /*
4021 * check this node pointer against the cache_only and
4022 * min_trans parameters. If it isn't in cache or is too
4023 * old, skip to the next one.
4024 */
Chris Masond3977122009-01-05 21:25:51 -05004025 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004026 u64 blockptr;
4027 u64 gen;
4028 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004029 struct btrfs_disk_key disk_key;
4030
Chris Mason3f157a22008-06-25 16:01:31 -04004031 blockptr = btrfs_node_blockptr(cur, slot);
4032 gen = btrfs_node_ptr_generation(cur, slot);
4033 if (gen < min_trans) {
4034 slot++;
4035 continue;
4036 }
4037 if (!cache_only)
4038 break;
4039
Chris Masone02119d2008-09-05 16:13:11 -04004040 if (max_key) {
4041 btrfs_node_key(cur, &disk_key, slot);
4042 if (comp_keys(&disk_key, max_key) >= 0) {
4043 ret = 1;
4044 goto out;
4045 }
4046 }
4047
Chris Mason3f157a22008-06-25 16:01:31 -04004048 tmp = btrfs_find_tree_block(root, blockptr,
4049 btrfs_level_size(root, level - 1));
4050
4051 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4052 free_extent_buffer(tmp);
4053 break;
4054 }
4055 if (tmp)
4056 free_extent_buffer(tmp);
4057 slot++;
4058 }
Chris Masone02119d2008-09-05 16:13:11 -04004059find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004060 /*
4061 * we didn't find a candidate key in this node, walk forward
4062 * and find another one
4063 */
4064 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004065 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004066 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004067 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004068 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004069 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004070 btrfs_release_path(root, path);
4071 goto again;
4072 } else {
4073 goto out;
4074 }
4075 }
4076 /* save our key for returning back */
4077 btrfs_node_key_to_cpu(cur, &found_key, slot);
4078 path->slots[level] = slot;
4079 if (level == path->lowest_level) {
4080 ret = 0;
4081 unlock_up(path, level, 1);
4082 goto out;
4083 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004084 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004085 cur = read_node_slot(root, cur, slot);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00004086 BUG_ON(!cur);
Chris Mason3f157a22008-06-25 16:01:31 -04004087
4088 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004089
Chris Mason3f157a22008-06-25 16:01:31 -04004090 path->locks[level - 1] = 1;
4091 path->nodes[level - 1] = cur;
4092 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004093 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004094 }
4095out:
4096 if (ret == 0)
4097 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004098 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004099 return ret;
4100}
4101
4102/*
4103 * this is similar to btrfs_next_leaf, but does not try to preserve
4104 * and fixup the path. It looks for and returns the next key in the
4105 * tree based on the current path and the cache_only and min_trans
4106 * parameters.
4107 *
4108 * 0 is returned if another key is found, < 0 if there are any errors
4109 * and 1 is returned if there are no higher keys in the tree
4110 *
4111 * path->keep_locks should be set to 1 on the search made before
4112 * calling this function.
4113 */
Chris Masone7a84562008-06-25 16:01:31 -04004114int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004115 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004116 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004117{
Chris Masone7a84562008-06-25 16:01:31 -04004118 int slot;
4119 struct extent_buffer *c;
4120
Chris Mason934d3752008-12-08 16:43:10 -05004121 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004122 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004123 if (!path->nodes[level])
4124 return 1;
4125
4126 slot = path->slots[level] + 1;
4127 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004128next:
Chris Masone7a84562008-06-25 16:01:31 -04004129 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004130 int ret;
4131 int orig_lowest;
4132 struct btrfs_key cur_key;
4133 if (level + 1 >= BTRFS_MAX_LEVEL ||
4134 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004135 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004136
4137 if (path->locks[level + 1]) {
4138 level++;
4139 continue;
4140 }
4141
4142 slot = btrfs_header_nritems(c) - 1;
4143 if (level == 0)
4144 btrfs_item_key_to_cpu(c, &cur_key, slot);
4145 else
4146 btrfs_node_key_to_cpu(c, &cur_key, slot);
4147
4148 orig_lowest = path->lowest_level;
4149 btrfs_release_path(root, path);
4150 path->lowest_level = level;
4151 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4152 0, 0);
4153 path->lowest_level = orig_lowest;
4154 if (ret < 0)
4155 return ret;
4156
4157 c = path->nodes[level];
4158 slot = path->slots[level];
4159 if (ret == 0)
4160 slot++;
4161 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004162 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004163
Chris Masone7a84562008-06-25 16:01:31 -04004164 if (level == 0)
4165 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004166 else {
4167 u64 blockptr = btrfs_node_blockptr(c, slot);
4168 u64 gen = btrfs_node_ptr_generation(c, slot);
4169
4170 if (cache_only) {
4171 struct extent_buffer *cur;
4172 cur = btrfs_find_tree_block(root, blockptr,
4173 btrfs_level_size(root, level - 1));
4174 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4175 slot++;
4176 if (cur)
4177 free_extent_buffer(cur);
4178 goto next;
4179 }
4180 free_extent_buffer(cur);
4181 }
4182 if (gen < min_trans) {
4183 slot++;
4184 goto next;
4185 }
Chris Masone7a84562008-06-25 16:01:31 -04004186 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004187 }
Chris Masone7a84562008-06-25 16:01:31 -04004188 return 0;
4189 }
4190 return 1;
4191}
4192
Chris Mason7bb86312007-12-11 09:25:06 -05004193/*
Chris Mason925baed2008-06-25 16:01:30 -04004194 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004195 * returns 0 if it found something or 1 if there are no greater leaves.
4196 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004197 */
Chris Mason234b63a2007-03-13 10:46:10 -04004198int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004199{
4200 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004201 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004202 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004203 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004204 struct btrfs_key key;
4205 u32 nritems;
4206 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004207 int old_spinning = path->leave_spinning;
4208 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004209
4210 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004211 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004212 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004213
Chris Mason8e73f272009-04-03 10:14:18 -04004214 /*
4215 * we take the blocks in an order that upsets lockdep. Using
4216 * blocking mode is the only way around it.
4217 */
4218#ifdef CONFIG_DEBUG_LOCK_ALLOC
4219 force_blocking = 1;
4220#endif
Chris Mason925baed2008-06-25 16:01:30 -04004221
Chris Mason8e73f272009-04-03 10:14:18 -04004222 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4223again:
4224 level = 1;
4225 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004226 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004227
Chris Masona2135012008-06-25 16:01:30 -04004228 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004229
4230 if (!force_blocking)
4231 path->leave_spinning = 1;
4232
Chris Mason925baed2008-06-25 16:01:30 -04004233 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4234 path->keep_locks = 0;
4235
4236 if (ret < 0)
4237 return ret;
4238
Chris Masona2135012008-06-25 16:01:30 -04004239 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004240 /*
4241 * by releasing the path above we dropped all our locks. A balance
4242 * could have added more items next to the key that used to be
4243 * at the very end of the block. So, check again here and
4244 * advance the path if there are now more items available.
4245 */
Chris Masona2135012008-06-25 16:01:30 -04004246 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004247 if (ret == 0)
4248 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004249 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004250 goto done;
4251 }
Chris Masond97e63b2007-02-20 16:40:44 -05004252
Chris Masond3977122009-01-05 21:25:51 -05004253 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004254 if (!path->nodes[level]) {
4255 ret = 1;
4256 goto done;
4257 }
Chris Mason5f39d392007-10-15 16:14:19 -04004258
Chris Masond97e63b2007-02-20 16:40:44 -05004259 slot = path->slots[level] + 1;
4260 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004261 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004262 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004263 if (level == BTRFS_MAX_LEVEL) {
4264 ret = 1;
4265 goto done;
4266 }
Chris Masond97e63b2007-02-20 16:40:44 -05004267 continue;
4268 }
Chris Mason5f39d392007-10-15 16:14:19 -04004269
Chris Mason925baed2008-06-25 16:01:30 -04004270 if (next) {
4271 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004272 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004273 }
Chris Mason5f39d392007-10-15 16:14:19 -04004274
Chris Mason8e73f272009-04-03 10:14:18 -04004275 next = c;
4276 ret = read_block_for_search(NULL, root, path, &next, level,
4277 slot, &key);
4278 if (ret == -EAGAIN)
4279 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004280
Chris Mason76a05b32009-05-14 13:24:30 -04004281 if (ret < 0) {
4282 btrfs_release_path(root, path);
4283 goto done;
4284 }
4285
Chris Mason5cd57b22008-06-25 16:01:30 -04004286 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004287 ret = btrfs_try_spin_lock(next);
4288 if (!ret) {
4289 btrfs_set_path_blocking(path);
4290 btrfs_tree_lock(next);
4291 if (!force_blocking)
4292 btrfs_clear_path_blocking(path, next);
4293 }
4294 if (force_blocking)
4295 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004296 }
Chris Masond97e63b2007-02-20 16:40:44 -05004297 break;
4298 }
4299 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004300 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004301 level--;
4302 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004303 if (path->locks[level])
4304 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004305
Chris Mason5f39d392007-10-15 16:14:19 -04004306 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004307 path->nodes[level] = next;
4308 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004309 if (!path->skip_locking)
4310 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004311
Chris Masond97e63b2007-02-20 16:40:44 -05004312 if (!level)
4313 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004314
Chris Mason8e73f272009-04-03 10:14:18 -04004315 ret = read_block_for_search(NULL, root, path, &next, level,
4316 0, &key);
4317 if (ret == -EAGAIN)
4318 goto again;
4319
Chris Mason76a05b32009-05-14 13:24:30 -04004320 if (ret < 0) {
4321 btrfs_release_path(root, path);
4322 goto done;
4323 }
4324
Chris Mason5cd57b22008-06-25 16:01:30 -04004325 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004326 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004327 ret = btrfs_try_spin_lock(next);
4328 if (!ret) {
4329 btrfs_set_path_blocking(path);
4330 btrfs_tree_lock(next);
4331 if (!force_blocking)
4332 btrfs_clear_path_blocking(path, next);
4333 }
4334 if (force_blocking)
4335 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004336 }
Chris Masond97e63b2007-02-20 16:40:44 -05004337 }
Chris Mason8e73f272009-04-03 10:14:18 -04004338 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004339done:
4340 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004341 path->leave_spinning = old_spinning;
4342 if (!old_spinning)
4343 btrfs_set_path_blocking(path);
4344
4345 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004346}
Chris Mason0b86a832008-03-24 15:01:56 -04004347
Chris Mason3f157a22008-06-25 16:01:31 -04004348/*
4349 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4350 * searching until it gets past min_objectid or finds an item of 'type'
4351 *
4352 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4353 */
Chris Mason0b86a832008-03-24 15:01:56 -04004354int btrfs_previous_item(struct btrfs_root *root,
4355 struct btrfs_path *path, u64 min_objectid,
4356 int type)
4357{
4358 struct btrfs_key found_key;
4359 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004360 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004361 int ret;
4362
Chris Masond3977122009-01-05 21:25:51 -05004363 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004364 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004365 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004366 ret = btrfs_prev_leaf(root, path);
4367 if (ret != 0)
4368 return ret;
4369 } else {
4370 path->slots[0]--;
4371 }
4372 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004373 nritems = btrfs_header_nritems(leaf);
4374 if (nritems == 0)
4375 return 1;
4376 if (path->slots[0] == nritems)
4377 path->slots[0]--;
4378
Chris Mason0b86a832008-03-24 15:01:56 -04004379 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004380 if (found_key.objectid < min_objectid)
4381 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004382 if (found_key.type == type)
4383 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004384 if (found_key.objectid == min_objectid &&
4385 found_key.type < type)
4386 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004387 }
4388 return 1;
4389}