blob: ec96f3a6d536640919dd25a08c7ed22e4423ef15 [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>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050040
Chris Mason2c90e5d2007-04-02 10:50:19 -040041struct btrfs_path *btrfs_alloc_path(void)
42{
Chris Masondf24a2b2007-04-04 09:36:31 -040043 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050044 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
45 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040046 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040047 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040048}
49
Chris Masonb4ce94d2009-02-04 09:25:08 -050050/*
51 * set all locked nodes in the path to blocking locks. This should
52 * be done before scheduling
53 */
54noinline void btrfs_set_path_blocking(struct btrfs_path *p)
55{
56 int i;
57 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
58 if (p->nodes[i] && p->locks[i])
59 btrfs_set_lock_blocking(p->nodes[i]);
60 }
61}
62
63/*
64 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050065 *
66 * held is used to keep lockdep happy, when lockdep is enabled
67 * we set held to a blocking lock before we go around and
68 * retake all the spinlocks in the path. You can safely use NULL
69 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050070 */
Chris Mason4008c042009-02-12 14:09:45 -050071noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
72 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050073{
74 int i;
Chris Mason4008c042009-02-12 14:09:45 -050075
76#ifdef CONFIG_DEBUG_LOCK_ALLOC
77 /* lockdep really cares that we take all of these spinlocks
78 * in the right order. If any of the locks in the path are not
79 * currently blocking, it is going to complain. So, make really
80 * really sure by forcing the path to blocking before we clear
81 * the path blocking.
82 */
83 if (held)
84 btrfs_set_lock_blocking(held);
85 btrfs_set_path_blocking(p);
86#endif
87
88 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050089 if (p->nodes[i] && p->locks[i])
90 btrfs_clear_lock_blocking(p->nodes[i]);
91 }
Chris Mason4008c042009-02-12 14:09:45 -050092
93#ifdef CONFIG_DEBUG_LOCK_ALLOC
94 if (held)
95 btrfs_clear_lock_blocking(held);
96#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -050097}
98
Chris Masond352ac62008-09-29 15:18:18 -040099/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400100void btrfs_free_path(struct btrfs_path *p)
101{
Chris Masondf24a2b2007-04-04 09:36:31 -0400102 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400103 kmem_cache_free(btrfs_path_cachep, p);
104}
105
Chris Masond352ac62008-09-29 15:18:18 -0400106/*
107 * path release drops references on the extent buffers in the path
108 * and it drops any locks held by this path
109 *
110 * It is safe to call this on paths that no locks or extent buffers held.
111 */
Chris Masond3977122009-01-05 21:25:51 -0500112noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500113{
114 int i;
Chris Masona2135012008-06-25 16:01:30 -0400115
Chris Mason234b63a2007-03-13 10:46:10 -0400116 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400117 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500118 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400119 continue;
120 if (p->locks[i]) {
121 btrfs_tree_unlock(p->nodes[i]);
122 p->locks[i] = 0;
123 }
Chris Mason5f39d392007-10-15 16:14:19 -0400124 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400125 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500126 }
127}
128
Chris Masond352ac62008-09-29 15:18:18 -0400129/*
130 * safely gets a reference on the root node of a tree. A lock
131 * is not taken, so a concurrent writer may put a different node
132 * at the root of the tree. See btrfs_lock_root_node for the
133 * looping required.
134 *
135 * The extent buffer returned by this has a reference taken, so
136 * it won't disappear. It may stop being the root of the tree
137 * at any time because there are no locks held.
138 */
Chris Mason925baed2008-06-25 16:01:30 -0400139struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
140{
141 struct extent_buffer *eb;
142 spin_lock(&root->node_lock);
143 eb = root->node;
144 extent_buffer_get(eb);
145 spin_unlock(&root->node_lock);
146 return eb;
147}
148
Chris Masond352ac62008-09-29 15:18:18 -0400149/* loop around taking references on and locking the root node of the
150 * tree until you end up with a lock on the root. A locked buffer
151 * is returned, with a reference held.
152 */
Chris Mason925baed2008-06-25 16:01:30 -0400153struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
154{
155 struct extent_buffer *eb;
156
Chris Masond3977122009-01-05 21:25:51 -0500157 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400158 eb = btrfs_root_node(root);
159 btrfs_tree_lock(eb);
160
161 spin_lock(&root->node_lock);
162 if (eb == root->node) {
163 spin_unlock(&root->node_lock);
164 break;
165 }
166 spin_unlock(&root->node_lock);
167
168 btrfs_tree_unlock(eb);
169 free_extent_buffer(eb);
170 }
171 return eb;
172}
173
Chris Masond352ac62008-09-29 15:18:18 -0400174/* cowonly root (everything not a reference counted cow subvolume), just get
175 * put onto a simple dirty list. transaction.c walks this to make sure they
176 * get properly updated on disk.
177 */
Chris Mason0b86a832008-03-24 15:01:56 -0400178static void add_root_to_dirty_list(struct btrfs_root *root)
179{
180 if (root->track_dirty && list_empty(&root->dirty_list)) {
181 list_add(&root->dirty_list,
182 &root->fs_info->dirty_cowonly_roots);
183 }
184}
185
Chris Masond352ac62008-09-29 15:18:18 -0400186/*
187 * used by snapshot creation to make a copy of a root for a tree with
188 * a given objectid. The buffer with the new root node is returned in
189 * cow_ret, and this func returns zero on success or a negative error code.
190 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500191int btrfs_copy_root(struct btrfs_trans_handle *trans,
192 struct btrfs_root *root,
193 struct extent_buffer *buf,
194 struct extent_buffer **cow_ret, u64 new_root_objectid)
195{
196 struct extent_buffer *cow;
197 u32 nritems;
198 int ret = 0;
199 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400200 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500201
202 WARN_ON(root->ref_cows && trans->transid !=
203 root->fs_info->running_transaction->transid);
204 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
205
206 level = btrfs_header_level(buf);
207 nritems = btrfs_header_nritems(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400208 if (level == 0)
209 btrfs_item_key(buf, &disk_key, 0);
210 else
211 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400212
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400213 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
214 new_root_objectid, &disk_key, level,
215 buf->start, 0);
216 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500217 return PTR_ERR(cow);
218
219 copy_extent_buffer(cow, buf, 0, 0, cow->len);
220 btrfs_set_header_bytenr(cow, cow->start);
221 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400222 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
223 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
224 BTRFS_HEADER_FLAG_RELOC);
225 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
226 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
227 else
228 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500229
Yan Zheng2b820322008-11-17 21:11:30 -0500230 write_extent_buffer(cow, root->fs_info->fsid,
231 (unsigned long)btrfs_header_fsid(cow),
232 BTRFS_FSID_SIZE);
233
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400235 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
236 ret = btrfs_inc_ref(trans, root, cow, 1);
237 else
238 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500239
Chris Masonbe20aa92007-12-17 20:14:01 -0500240 if (ret)
241 return ret;
242
243 btrfs_mark_buffer_dirty(cow);
244 *cow_ret = cow;
245 return 0;
246}
247
Chris Masond352ac62008-09-29 15:18:18 -0400248/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400249 * check if the tree block can be shared by multiple trees
250 */
251int btrfs_block_can_be_shared(struct btrfs_root *root,
252 struct extent_buffer *buf)
253{
254 /*
255 * Tree blocks not in refernece counted trees and tree roots
256 * are never shared. If a block was allocated after the last
257 * snapshot and the block was not allocated by tree relocation,
258 * we know the block is not shared.
259 */
260 if (root->ref_cows &&
261 buf != root->node && buf != root->commit_root &&
262 (btrfs_header_generation(buf) <=
263 btrfs_root_last_snapshot(&root->root_item) ||
264 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
265 return 1;
266#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
267 if (root->ref_cows &&
268 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
269 return 1;
270#endif
271 return 0;
272}
273
274static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
275 struct btrfs_root *root,
276 struct extent_buffer *buf,
277 struct extent_buffer *cow)
278{
279 u64 refs;
280 u64 owner;
281 u64 flags;
282 u64 new_flags = 0;
283 int ret;
284
285 /*
286 * Backrefs update rules:
287 *
288 * Always use full backrefs for extent pointers in tree block
289 * allocated by tree relocation.
290 *
291 * If a shared tree block is no longer referenced by its owner
292 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
293 * use full backrefs for extent pointers in tree block.
294 *
295 * If a tree block is been relocating
296 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
297 * use full backrefs for extent pointers in tree block.
298 * The reason for this is some operations (such as drop tree)
299 * are only allowed for blocks use full backrefs.
300 */
301
302 if (btrfs_block_can_be_shared(root, buf)) {
303 ret = btrfs_lookup_extent_info(trans, root, buf->start,
304 buf->len, &refs, &flags);
305 BUG_ON(ret);
306 BUG_ON(refs == 0);
307 } else {
308 refs = 1;
309 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
310 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
311 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
312 else
313 flags = 0;
314 }
315
316 owner = btrfs_header_owner(buf);
317 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
318 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
319
320 if (refs > 1) {
321 if ((owner == root->root_key.objectid ||
322 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
323 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
324 ret = btrfs_inc_ref(trans, root, buf, 1);
325 BUG_ON(ret);
326
327 if (root->root_key.objectid ==
328 BTRFS_TREE_RELOC_OBJECTID) {
329 ret = btrfs_dec_ref(trans, root, buf, 0);
330 BUG_ON(ret);
331 ret = btrfs_inc_ref(trans, root, cow, 1);
332 BUG_ON(ret);
333 }
334 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
335 } else {
336
337 if (root->root_key.objectid ==
338 BTRFS_TREE_RELOC_OBJECTID)
339 ret = btrfs_inc_ref(trans, root, cow, 1);
340 else
341 ret = btrfs_inc_ref(trans, root, cow, 0);
342 BUG_ON(ret);
343 }
344 if (new_flags != 0) {
345 ret = btrfs_set_disk_extent_flags(trans, root,
346 buf->start,
347 buf->len,
348 new_flags, 0);
349 BUG_ON(ret);
350 }
351 } else {
352 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
353 if (root->root_key.objectid ==
354 BTRFS_TREE_RELOC_OBJECTID)
355 ret = btrfs_inc_ref(trans, root, cow, 1);
356 else
357 ret = btrfs_inc_ref(trans, root, cow, 0);
358 BUG_ON(ret);
359 ret = btrfs_dec_ref(trans, root, buf, 1);
360 BUG_ON(ret);
361 }
362 clean_tree_block(trans, root, buf);
363 }
364 return 0;
365}
366
367/*
Chris Masond3977122009-01-05 21:25:51 -0500368 * does the dirty work in cow of a single block. The parent block (if
369 * supplied) is updated to point to the new cow copy. The new buffer is marked
370 * dirty and returned locked. If you modify the block it needs to be marked
371 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400372 *
373 * search_start -- an allocation hint for the new block
374 *
Chris Masond3977122009-01-05 21:25:51 -0500375 * empty_size -- a hint that you plan on doing more cow. This is the size in
376 * bytes the allocator should try to find free next to the block it returns.
377 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400378 */
Chris Masond3977122009-01-05 21:25:51 -0500379static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400380 struct btrfs_root *root,
381 struct extent_buffer *buf,
382 struct extent_buffer *parent, int parent_slot,
383 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400384 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400385{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400386 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400387 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500388 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400389 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400390 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400391
Chris Mason925baed2008-06-25 16:01:30 -0400392 if (*cow_ret == buf)
393 unlock_orig = 1;
394
Chris Masonb9447ef2009-03-09 11:45:38 -0400395 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400396
Chris Mason7bb86312007-12-11 09:25:06 -0500397 WARN_ON(root->ref_cows && trans->transid !=
398 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400399 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400400
Chris Mason7bb86312007-12-11 09:25:06 -0500401 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400402
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400403 if (level == 0)
404 btrfs_item_key(buf, &disk_key, 0);
405 else
406 btrfs_node_key(buf, &disk_key, 0);
407
408 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
409 if (parent)
410 parent_start = parent->start;
411 else
412 parent_start = 0;
413 } else
414 parent_start = 0;
415
416 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
417 root->root_key.objectid, &disk_key,
418 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400419 if (IS_ERR(cow))
420 return PTR_ERR(cow);
421
Chris Masonb4ce94d2009-02-04 09:25:08 -0500422 /* cow is set to blocking by btrfs_init_new_buffer */
423
Chris Mason5f39d392007-10-15 16:14:19 -0400424 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400425 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400426 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400427 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
428 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
429 BTRFS_HEADER_FLAG_RELOC);
430 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
431 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
432 else
433 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400434
Yan Zheng2b820322008-11-17 21:11:30 -0500435 write_extent_buffer(cow, root->fs_info->fsid,
436 (unsigned long)btrfs_header_fsid(cow),
437 BTRFS_FSID_SIZE);
438
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400439 update_ref_for_cow(trans, root, buf, cow);
Zheng Yan1a40e232008-09-26 10:09:34 -0400440
Chris Mason6702ed42007-08-07 16:15:09 -0400441 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400442 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400443 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
444 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
445 parent_start = buf->start;
446 else
447 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400448
449 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400450 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400451 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400452 spin_unlock(&root->node_lock);
453
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400454 btrfs_free_extent(trans, root, buf->start, buf->len,
455 parent_start, root->root_key.objectid,
456 level, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400457 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400458 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400459 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400460 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
461 parent_start = parent->start;
462 else
463 parent_start = 0;
464
465 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400466 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400467 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500468 btrfs_set_node_ptr_generation(parent, parent_slot,
469 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400470 btrfs_mark_buffer_dirty(parent);
Chris Mason7bb86312007-12-11 09:25:06 -0500471 btrfs_free_extent(trans, root, buf->start, buf->len,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400472 parent_start, root->root_key.objectid,
473 level, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400474 }
Chris Mason925baed2008-06-25 16:01:30 -0400475 if (unlock_orig)
476 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400477 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400478 btrfs_mark_buffer_dirty(cow);
479 *cow_ret = cow;
480 return 0;
481}
482
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400483static inline int should_cow_block(struct btrfs_trans_handle *trans,
484 struct btrfs_root *root,
485 struct extent_buffer *buf)
486{
487 if (btrfs_header_generation(buf) == trans->transid &&
488 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
489 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
490 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
491 return 0;
492 return 1;
493}
494
Chris Masond352ac62008-09-29 15:18:18 -0400495/*
496 * cows a single block, see __btrfs_cow_block for the real work.
497 * This version of it has extra checks so that a block isn't cow'd more than
498 * once per transaction, as long as it hasn't been written yet
499 */
Chris Masond3977122009-01-05 21:25:51 -0500500noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400501 struct btrfs_root *root, struct extent_buffer *buf,
502 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400503 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500504{
Chris Mason6702ed42007-08-07 16:15:09 -0400505 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400506 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500507
Chris Masonccd467d2007-06-28 15:57:36 -0400508 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500509 printk(KERN_CRIT "trans %llu running %llu\n",
510 (unsigned long long)trans->transid,
511 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400512 root->fs_info->running_transaction->transid);
513 WARN_ON(1);
514 }
515 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500516 printk(KERN_CRIT "trans %llu running %llu\n",
517 (unsigned long long)trans->transid,
518 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400519 WARN_ON(1);
520 }
Chris Masondc17ff82008-01-08 15:46:30 -0500521
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400522 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500523 *cow_ret = buf;
524 return 0;
525 }
Chris Masonc4876852009-02-04 09:24:25 -0500526
Chris Mason0b86a832008-03-24 15:01:56 -0400527 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500528
529 if (parent)
530 btrfs_set_lock_blocking(parent);
531 btrfs_set_lock_blocking(buf);
532
Chris Masonf510cfe2007-10-15 16:14:48 -0400533 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400534 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400535 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400536}
537
Chris Masond352ac62008-09-29 15:18:18 -0400538/*
539 * helper function for defrag to decide if two blocks pointed to by a
540 * node are actually close by
541 */
Chris Mason6b800532007-10-15 16:17:34 -0400542static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400543{
Chris Mason6b800532007-10-15 16:17:34 -0400544 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400545 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400546 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400547 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500548 return 0;
549}
550
Chris Mason081e9572007-11-06 10:26:24 -0500551/*
552 * compare two keys in a memcmp fashion
553 */
554static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
555{
556 struct btrfs_key k1;
557
558 btrfs_disk_key_to_cpu(&k1, disk);
559
Diego Calleja20736ab2009-07-24 11:06:52 -0400560 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500561}
562
Josef Bacikf3465ca2008-11-12 14:19:50 -0500563/*
564 * same as comp_keys only with two btrfs_key's
565 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400566int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500567{
568 if (k1->objectid > k2->objectid)
569 return 1;
570 if (k1->objectid < k2->objectid)
571 return -1;
572 if (k1->type > k2->type)
573 return 1;
574 if (k1->type < k2->type)
575 return -1;
576 if (k1->offset > k2->offset)
577 return 1;
578 if (k1->offset < k2->offset)
579 return -1;
580 return 0;
581}
Chris Mason081e9572007-11-06 10:26:24 -0500582
Chris Masond352ac62008-09-29 15:18:18 -0400583/*
584 * this is used by the defrag code to go through all the
585 * leaves pointed to by a node and reallocate them so that
586 * disk order is close to key order
587 */
Chris Mason6702ed42007-08-07 16:15:09 -0400588int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400589 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400590 int start_slot, int cache_only, u64 *last_ret,
591 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400592{
Chris Mason6b800532007-10-15 16:17:34 -0400593 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400594 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400595 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400596 u64 search_start = *last_ret;
597 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400598 u64 other;
599 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400600 int end_slot;
601 int i;
602 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400603 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400604 int uptodate;
605 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500606 int progress_passed = 0;
607 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400608
Chris Mason5708b952007-10-25 15:43:18 -0400609 parent_level = btrfs_header_level(parent);
610 if (cache_only && parent_level != 1)
611 return 0;
612
Chris Masond3977122009-01-05 21:25:51 -0500613 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400614 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500615 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400616 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400617
Chris Mason6b800532007-10-15 16:17:34 -0400618 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400619 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400620 end_slot = parent_nritems;
621
622 if (parent_nritems == 1)
623 return 0;
624
Chris Masonb4ce94d2009-02-04 09:25:08 -0500625 btrfs_set_lock_blocking(parent);
626
Chris Mason6702ed42007-08-07 16:15:09 -0400627 for (i = start_slot; i < end_slot; i++) {
628 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400629
Chris Mason5708b952007-10-25 15:43:18 -0400630 if (!parent->map_token) {
631 map_extent_buffer(parent,
632 btrfs_node_key_ptr_offset(i),
633 sizeof(struct btrfs_key_ptr),
634 &parent->map_token, &parent->kaddr,
635 &parent->map_start, &parent->map_len,
636 KM_USER1);
637 }
Chris Mason081e9572007-11-06 10:26:24 -0500638 btrfs_node_key(parent, &disk_key, i);
639 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
640 continue;
641
642 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400643 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400644 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400645 if (last_block == 0)
646 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400647
Chris Mason6702ed42007-08-07 16:15:09 -0400648 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400649 other = btrfs_node_blockptr(parent, i - 1);
650 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400651 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400652 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400653 other = btrfs_node_blockptr(parent, i + 1);
654 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400655 }
Chris Masone9d0b132007-08-10 14:06:19 -0400656 if (close) {
657 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400658 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400659 }
Chris Mason5708b952007-10-25 15:43:18 -0400660 if (parent->map_token) {
661 unmap_extent_buffer(parent, parent->map_token,
662 KM_USER1);
663 parent->map_token = NULL;
664 }
Chris Mason6702ed42007-08-07 16:15:09 -0400665
Chris Mason6b800532007-10-15 16:17:34 -0400666 cur = btrfs_find_tree_block(root, blocknr, blocksize);
667 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400668 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400669 else
670 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400671 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400672 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400673 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400674 continue;
675 }
Chris Mason6b800532007-10-15 16:17:34 -0400676 if (!cur) {
677 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400678 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400679 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400680 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400681 }
Chris Mason6702ed42007-08-07 16:15:09 -0400682 }
Chris Masone9d0b132007-08-10 14:06:19 -0400683 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400684 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400685
Chris Masone7a84562008-06-25 16:01:31 -0400686 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500687 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400688 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400689 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400690 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400691 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400692 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400693 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400694 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400695 break;
Yan252c38f2007-08-29 09:11:44 -0400696 }
Chris Masone7a84562008-06-25 16:01:31 -0400697 search_start = cur->start;
698 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400699 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400700 btrfs_tree_unlock(cur);
701 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400702 }
Chris Mason5708b952007-10-25 15:43:18 -0400703 if (parent->map_token) {
704 unmap_extent_buffer(parent, parent->map_token,
705 KM_USER1);
706 parent->map_token = NULL;
707 }
Chris Mason6702ed42007-08-07 16:15:09 -0400708 return err;
709}
710
Chris Mason74123bd2007-02-02 11:05:29 -0500711/*
712 * The leaf data grows from end-to-front in the node.
713 * this returns the address of the start of the last item,
714 * which is the stop of the leaf data stack
715 */
Chris Mason123abc82007-03-14 14:14:43 -0400716static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400717 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500718{
Chris Mason5f39d392007-10-15 16:14:19 -0400719 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500720 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400721 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400722 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500723}
724
Chris Masond352ac62008-09-29 15:18:18 -0400725/*
726 * extra debugging checks to make sure all the items in a key are
727 * well formed and in the proper order
728 */
Chris Mason123abc82007-03-14 14:14:43 -0400729static int check_node(struct btrfs_root *root, struct btrfs_path *path,
730 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500731{
Chris Mason5f39d392007-10-15 16:14:19 -0400732 struct extent_buffer *parent = NULL;
733 struct extent_buffer *node = path->nodes[level];
734 struct btrfs_disk_key parent_key;
735 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500736 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400737 int slot;
738 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400739 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500740
741 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400742 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400743
Chris Mason8d7be552007-05-10 11:24:42 -0400744 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400745 BUG_ON(nritems == 0);
746 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400747 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400748 btrfs_node_key(parent, &parent_key, parent_slot);
749 btrfs_node_key(node, &node_key, 0);
750 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400751 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400752 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400753 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500754 }
Chris Mason123abc82007-03-14 14:14:43 -0400755 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400756 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400757 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
758 btrfs_node_key(node, &node_key, slot);
759 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400760 }
761 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400762 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
763 btrfs_node_key(node, &node_key, slot);
764 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500765 }
766 return 0;
767}
768
Chris Masond352ac62008-09-29 15:18:18 -0400769/*
770 * extra checking to make sure all the items in a leaf are
771 * well formed and in the proper order
772 */
Chris Mason123abc82007-03-14 14:14:43 -0400773static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
774 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500775{
Chris Mason5f39d392007-10-15 16:14:19 -0400776 struct extent_buffer *leaf = path->nodes[level];
777 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500778 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400779 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400780 struct btrfs_disk_key parent_key;
781 struct btrfs_disk_key leaf_key;
782 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400783
Chris Mason5f39d392007-10-15 16:14:19 -0400784 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500785
786 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400787 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400788
789 if (nritems == 0)
790 return 0;
791
792 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400793 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400794 btrfs_node_key(parent, &parent_key, parent_slot);
795 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400796
Chris Mason5f39d392007-10-15 16:14:19 -0400797 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400798 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400799 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400800 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500801 }
Chris Mason5f39d392007-10-15 16:14:19 -0400802 if (slot != 0 && slot < nritems - 1) {
803 btrfs_item_key(leaf, &leaf_key, slot);
804 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
805 if (comp_keys(&leaf_key, &cpukey) <= 0) {
806 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500807 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400808 BUG_ON(1);
809 }
810 if (btrfs_item_offset_nr(leaf, slot - 1) !=
811 btrfs_item_end_nr(leaf, slot)) {
812 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500813 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400814 BUG_ON(1);
815 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500816 }
Chris Mason8d7be552007-05-10 11:24:42 -0400817 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400818 btrfs_item_key(leaf, &leaf_key, slot);
819 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
820 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
821 if (btrfs_item_offset_nr(leaf, slot) !=
822 btrfs_item_end_nr(leaf, slot + 1)) {
823 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500824 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400825 BUG_ON(1);
826 }
Chris Mason8d7be552007-05-10 11:24:42 -0400827 }
Chris Mason5f39d392007-10-15 16:14:19 -0400828 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
829 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500830 return 0;
831}
832
Chris Masond3977122009-01-05 21:25:51 -0500833static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500834 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500835{
Chris Mason85d824c2008-04-10 10:23:19 -0400836 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500837 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400838 return check_leaf(root, path, level);
839 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500840}
841
Chris Mason74123bd2007-02-02 11:05:29 -0500842/*
Chris Mason5f39d392007-10-15 16:14:19 -0400843 * search for key in the extent_buffer. The items start at offset p,
844 * and they are item_size apart. There are 'max' items in p.
845 *
Chris Mason74123bd2007-02-02 11:05:29 -0500846 * the slot in the array is returned via slot, and it points to
847 * the place where you would insert key if it is not found in
848 * the array.
849 *
850 * slot may point to max if the key is bigger than all of the keys
851 */
Chris Masone02119d2008-09-05 16:13:11 -0400852static noinline int generic_bin_search(struct extent_buffer *eb,
853 unsigned long p,
854 int item_size, struct btrfs_key *key,
855 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500856{
857 int low = 0;
858 int high = max;
859 int mid;
860 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400861 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400862 struct btrfs_disk_key unaligned;
863 unsigned long offset;
864 char *map_token = NULL;
865 char *kaddr = NULL;
866 unsigned long map_start = 0;
867 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400868 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500869
Chris Masond3977122009-01-05 21:25:51 -0500870 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500871 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400872 offset = p + mid * item_size;
873
874 if (!map_token || offset < map_start ||
875 (offset + sizeof(struct btrfs_disk_key)) >
876 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400877 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400878 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400879 map_token = NULL;
880 }
Chris Mason934d3752008-12-08 16:43:10 -0500881
882 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400883 sizeof(struct btrfs_disk_key),
884 &map_token, &kaddr,
885 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400886
Chris Mason479965d2007-10-15 16:14:27 -0400887 if (!err) {
888 tmp = (struct btrfs_disk_key *)(kaddr + offset -
889 map_start);
890 } else {
891 read_extent_buffer(eb, &unaligned,
892 offset, sizeof(unaligned));
893 tmp = &unaligned;
894 }
895
Chris Mason5f39d392007-10-15 16:14:19 -0400896 } else {
897 tmp = (struct btrfs_disk_key *)(kaddr + offset -
898 map_start);
899 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500900 ret = comp_keys(tmp, key);
901
902 if (ret < 0)
903 low = mid + 1;
904 else if (ret > 0)
905 high = mid;
906 else {
907 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400908 if (map_token)
909 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500910 return 0;
911 }
912 }
913 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400914 if (map_token)
915 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500916 return 1;
917}
918
Chris Mason97571fd2007-02-24 13:39:08 -0500919/*
920 * simple bin_search frontend that does the right thing for
921 * leaves vs nodes
922 */
Chris Mason5f39d392007-10-15 16:14:19 -0400923static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
924 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500925{
Chris Mason5f39d392007-10-15 16:14:19 -0400926 if (level == 0) {
927 return generic_bin_search(eb,
928 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400929 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400930 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400931 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500932 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400933 return generic_bin_search(eb,
934 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400935 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400936 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400937 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500938 }
939 return -1;
940}
941
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
943 int level, int *slot)
944{
945 return bin_search(eb, key, level, slot);
946}
947
Chris Masond352ac62008-09-29 15:18:18 -0400948/* given a node and slot number, this reads the blocks it points to. The
949 * extent buffer is returned with a reference taken (but unlocked).
950 * NULL is returned on error.
951 */
Chris Masone02119d2008-09-05 16:13:11 -0400952static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400953 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500954{
Chris Masonca7a79a2008-05-12 12:59:19 -0400955 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500956 if (slot < 0)
957 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400958 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500959 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400960
961 BUG_ON(level == 0);
962
Chris Masondb945352007-10-15 16:15:53 -0400963 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400964 btrfs_level_size(root, level - 1),
965 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500966}
967
Chris Masond352ac62008-09-29 15:18:18 -0400968/*
969 * node level balancing, used to make sure nodes are in proper order for
970 * item deletion. We balance from the top down, so we have to make sure
971 * that a deletion won't leave an node completely empty later on.
972 */
Chris Masone02119d2008-09-05 16:13:11 -0400973static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500974 struct btrfs_root *root,
975 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500976{
Chris Mason5f39d392007-10-15 16:14:19 -0400977 struct extent_buffer *right = NULL;
978 struct extent_buffer *mid;
979 struct extent_buffer *left = NULL;
980 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500981 int ret = 0;
982 int wret;
983 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500984 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400985 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500986 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500987
988 if (level == 0)
989 return 0;
990
Chris Mason5f39d392007-10-15 16:14:19 -0400991 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500992
Chris Mason925baed2008-06-25 16:01:30 -0400993 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500994 WARN_ON(btrfs_header_generation(mid) != trans->transid);
995
Chris Mason1d4f8a02007-03-13 09:28:32 -0400996 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500997
Chris Mason234b63a2007-03-13 10:46:10 -0400998 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400999 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001000 pslot = path->slots[level + 1];
1001
Chris Mason40689472007-03-17 14:29:23 -04001002 /*
1003 * deal with the case where there is only one pointer in the root
1004 * by promoting the node below to a root
1005 */
Chris Mason5f39d392007-10-15 16:14:19 -04001006 if (!parent) {
1007 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001008
Chris Mason5f39d392007-10-15 16:14:19 -04001009 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001010 return 0;
1011
1012 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001013 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001014 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001015 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001016 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001017 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan2f375ab2008-02-01 14:58:07 -05001018 BUG_ON(ret);
1019
Chris Mason925baed2008-06-25 16:01:30 -04001020 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001021 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001022 spin_unlock(&root->node_lock);
1023
Chris Mason0b86a832008-03-24 15:01:56 -04001024 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001025 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001026
Chris Mason925baed2008-06-25 16:01:30 -04001027 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001028 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001029 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001030 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001031 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001032 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -05001033 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001034 0, root->root_key.objectid, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001035 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001036 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -04001037 return ret;
Chris Masonbb803952007-03-01 12:04:21 -05001038 }
Chris Mason5f39d392007-10-15 16:14:19 -04001039 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001040 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001041 return 0;
1042
Chris Mason5f39d392007-10-15 16:14:19 -04001043 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001044 err_on_enospc = 1;
1045
Chris Mason5f39d392007-10-15 16:14:19 -04001046 left = read_node_slot(root, parent, pslot - 1);
1047 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001048 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001049 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001050 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001051 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001052 if (wret) {
1053 ret = wret;
1054 goto enospc;
1055 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001056 }
Chris Mason5f39d392007-10-15 16:14:19 -04001057 right = read_node_slot(root, parent, pslot + 1);
1058 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001059 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001060 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001061 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001062 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001063 if (wret) {
1064 ret = wret;
1065 goto enospc;
1066 }
1067 }
1068
1069 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001070 if (left) {
1071 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001072 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001073 if (wret < 0)
1074 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001075 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001076 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001077 }
Chris Mason79f95c82007-03-01 15:16:26 -05001078
1079 /*
1080 * then try to empty the right most buffer into the middle
1081 */
Chris Mason5f39d392007-10-15 16:14:19 -04001082 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001083 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001084 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001085 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001086 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001087 u64 bytenr = right->start;
1088 u32 blocksize = right->len;
1089
Chris Mason5f39d392007-10-15 16:14:19 -04001090 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001091 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001092 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001093 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001094 wret = del_ptr(trans, root, path, level + 1, pslot +
1095 1);
Chris Masonbb803952007-03-01 12:04:21 -05001096 if (wret)
1097 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001098 wret = btrfs_free_extent(trans, root, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001099 blocksize, 0,
1100 root->root_key.objectid,
1101 level, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001102 if (wret)
1103 ret = wret;
1104 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001105 struct btrfs_disk_key right_key;
1106 btrfs_node_key(right, &right_key, 0);
1107 btrfs_set_node_key(parent, &right_key, pslot + 1);
1108 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001109 }
1110 }
Chris Mason5f39d392007-10-15 16:14:19 -04001111 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001112 /*
1113 * we're not allowed to leave a node with one item in the
1114 * tree during a delete. A deletion from lower in the tree
1115 * could try to delete the only pointer in this node.
1116 * So, pull some keys from the left.
1117 * There has to be a left pointer at this point because
1118 * otherwise we would have pulled some pointers from the
1119 * right
1120 */
Chris Mason5f39d392007-10-15 16:14:19 -04001121 BUG_ON(!left);
1122 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001123 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001124 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001125 goto enospc;
1126 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001127 if (wret == 1) {
1128 wret = push_node_left(trans, root, left, mid, 1);
1129 if (wret < 0)
1130 ret = wret;
1131 }
Chris Mason79f95c82007-03-01 15:16:26 -05001132 BUG_ON(wret == 1);
1133 }
Chris Mason5f39d392007-10-15 16:14:19 -04001134 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001135 /* we've managed to empty the middle node, drop it */
Chris Masondb945352007-10-15 16:15:53 -04001136 u64 bytenr = mid->start;
1137 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001138
Chris Mason5f39d392007-10-15 16:14:19 -04001139 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001140 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001141 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001142 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001143 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001144 if (wret)
1145 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001146 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001147 0, root->root_key.objectid,
1148 level, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001149 if (wret)
1150 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001151 } else {
1152 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001153 struct btrfs_disk_key mid_key;
1154 btrfs_node_key(mid, &mid_key, 0);
1155 btrfs_set_node_key(parent, &mid_key, pslot);
1156 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001157 }
Chris Masonbb803952007-03-01 12:04:21 -05001158
Chris Mason79f95c82007-03-01 15:16:26 -05001159 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001160 if (left) {
1161 if (btrfs_header_nritems(left) > orig_slot) {
1162 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001163 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001164 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001165 path->slots[level + 1] -= 1;
1166 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001167 if (mid) {
1168 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001169 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001170 }
Chris Masonbb803952007-03-01 12:04:21 -05001171 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001172 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001173 path->slots[level] = orig_slot;
1174 }
1175 }
Chris Mason79f95c82007-03-01 15:16:26 -05001176 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001177 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001178 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001179 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001180 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001181enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001182 if (right) {
1183 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001184 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001185 }
1186 if (left) {
1187 if (path->nodes[level] != left)
1188 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001189 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001190 }
Chris Masonbb803952007-03-01 12:04:21 -05001191 return ret;
1192}
1193
Chris Masond352ac62008-09-29 15:18:18 -04001194/* Node balancing for insertion. Here we only split or push nodes around
1195 * when they are completely full. This is also done top down, so we
1196 * have to be pessimistic.
1197 */
Chris Masond3977122009-01-05 21:25:51 -05001198static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001199 struct btrfs_root *root,
1200 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001201{
Chris Mason5f39d392007-10-15 16:14:19 -04001202 struct extent_buffer *right = NULL;
1203 struct extent_buffer *mid;
1204 struct extent_buffer *left = NULL;
1205 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001206 int ret = 0;
1207 int wret;
1208 int pslot;
1209 int orig_slot = path->slots[level];
1210 u64 orig_ptr;
1211
1212 if (level == 0)
1213 return 1;
1214
Chris Mason5f39d392007-10-15 16:14:19 -04001215 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001216 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001217 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1218
1219 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001220 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001221 pslot = path->slots[level + 1];
1222
Chris Mason5f39d392007-10-15 16:14:19 -04001223 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001224 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001225
Chris Mason5f39d392007-10-15 16:14:19 -04001226 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001227
1228 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001229 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001230 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001231
1232 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001233 btrfs_set_lock_blocking(left);
1234
Chris Mason5f39d392007-10-15 16:14:19 -04001235 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001236 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1237 wret = 1;
1238 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001239 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001240 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001241 if (ret)
1242 wret = 1;
1243 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001244 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001245 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001246 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001247 }
Chris Masone66f7092007-04-20 13:16:02 -04001248 if (wret < 0)
1249 ret = wret;
1250 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001251 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001252 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001253 btrfs_node_key(mid, &disk_key, 0);
1254 btrfs_set_node_key(parent, &disk_key, pslot);
1255 btrfs_mark_buffer_dirty(parent);
1256 if (btrfs_header_nritems(left) > orig_slot) {
1257 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001258 path->slots[level + 1] -= 1;
1259 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001260 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001261 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001262 } else {
1263 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001264 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001265 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001266 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001267 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001268 }
Chris Masone66f7092007-04-20 13:16:02 -04001269 return 0;
1270 }
Chris Mason925baed2008-06-25 16:01:30 -04001271 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001272 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001273 }
Chris Mason925baed2008-06-25 16:01:30 -04001274 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001275
1276 /*
1277 * then try to empty the right most buffer into the middle
1278 */
Chris Mason5f39d392007-10-15 16:14:19 -04001279 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001280 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001281
Chris Mason925baed2008-06-25 16:01:30 -04001282 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001283 btrfs_set_lock_blocking(right);
1284
Chris Mason5f39d392007-10-15 16:14:19 -04001285 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001286 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1287 wret = 1;
1288 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001289 ret = btrfs_cow_block(trans, root, right,
1290 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001291 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001292 if (ret)
1293 wret = 1;
1294 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001295 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001296 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001297 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001298 }
Chris Masone66f7092007-04-20 13:16:02 -04001299 if (wret < 0)
1300 ret = wret;
1301 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001302 struct btrfs_disk_key disk_key;
1303
1304 btrfs_node_key(right, &disk_key, 0);
1305 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1306 btrfs_mark_buffer_dirty(parent);
1307
1308 if (btrfs_header_nritems(mid) <= orig_slot) {
1309 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001310 path->slots[level + 1] += 1;
1311 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001312 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001313 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001314 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001315 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001316 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001317 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001318 }
Chris Masone66f7092007-04-20 13:16:02 -04001319 return 0;
1320 }
Chris Mason925baed2008-06-25 16:01:30 -04001321 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001322 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001323 }
Chris Masone66f7092007-04-20 13:16:02 -04001324 return 1;
1325}
1326
Chris Mason74123bd2007-02-02 11:05:29 -05001327/*
Chris Masond352ac62008-09-29 15:18:18 -04001328 * readahead one full node of leaves, finding things that are close
1329 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001330 */
Chris Masonc8c42862009-04-03 10:14:18 -04001331static void reada_for_search(struct btrfs_root *root,
1332 struct btrfs_path *path,
1333 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001334{
Chris Mason5f39d392007-10-15 16:14:19 -04001335 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001336 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001337 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001338 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001339 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001340 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001341 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001342 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001343 u32 nr;
1344 u32 blocksize;
1345 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001346
Chris Masona6b6e752007-10-15 16:22:39 -04001347 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001348 return;
1349
Chris Mason6702ed42007-08-07 16:15:09 -04001350 if (!path->nodes[level])
1351 return;
1352
Chris Mason5f39d392007-10-15 16:14:19 -04001353 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001354
Chris Mason3c69fae2007-08-07 15:52:22 -04001355 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001356 blocksize = btrfs_level_size(root, level - 1);
1357 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001358 if (eb) {
1359 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001360 return;
1361 }
1362
Chris Masona7175312009-01-22 09:23:10 -05001363 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001364
Chris Mason5f39d392007-10-15 16:14:19 -04001365 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001366 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001367 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001368 if (direction < 0) {
1369 if (nr == 0)
1370 break;
1371 nr--;
1372 } else if (direction > 0) {
1373 nr++;
1374 if (nr >= nritems)
1375 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001376 }
Chris Mason01f46652007-12-21 16:24:26 -05001377 if (path->reada < 0 && objectid) {
1378 btrfs_node_key(node, &disk_key, nr);
1379 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1380 break;
1381 }
Chris Mason6b800532007-10-15 16:17:34 -04001382 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001383 if ((search <= target && target - search <= 65536) ||
1384 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001385 readahead_tree_block(root, search, blocksize,
1386 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001387 nread += blocksize;
1388 }
1389 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001390 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001391 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001392 }
1393}
Chris Mason925baed2008-06-25 16:01:30 -04001394
Chris Masond352ac62008-09-29 15:18:18 -04001395/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001396 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1397 * cache
1398 */
1399static noinline int reada_for_balance(struct btrfs_root *root,
1400 struct btrfs_path *path, int level)
1401{
1402 int slot;
1403 int nritems;
1404 struct extent_buffer *parent;
1405 struct extent_buffer *eb;
1406 u64 gen;
1407 u64 block1 = 0;
1408 u64 block2 = 0;
1409 int ret = 0;
1410 int blocksize;
1411
Chris Mason8c594ea2009-04-20 15:50:10 -04001412 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001413 if (!parent)
1414 return 0;
1415
1416 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001417 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001418 blocksize = btrfs_level_size(root, level);
1419
1420 if (slot > 0) {
1421 block1 = btrfs_node_blockptr(parent, slot - 1);
1422 gen = btrfs_node_ptr_generation(parent, slot - 1);
1423 eb = btrfs_find_tree_block(root, block1, blocksize);
1424 if (eb && btrfs_buffer_uptodate(eb, gen))
1425 block1 = 0;
1426 free_extent_buffer(eb);
1427 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001428 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001429 block2 = btrfs_node_blockptr(parent, slot + 1);
1430 gen = btrfs_node_ptr_generation(parent, slot + 1);
1431 eb = btrfs_find_tree_block(root, block2, blocksize);
1432 if (eb && btrfs_buffer_uptodate(eb, gen))
1433 block2 = 0;
1434 free_extent_buffer(eb);
1435 }
1436 if (block1 || block2) {
1437 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001438
1439 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001440 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001441
1442 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001443 if (block1)
1444 readahead_tree_block(root, block1, blocksize, 0);
1445 if (block2)
1446 readahead_tree_block(root, block2, blocksize, 0);
1447
1448 if (block1) {
1449 eb = read_tree_block(root, block1, blocksize, 0);
1450 free_extent_buffer(eb);
1451 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001452 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001453 eb = read_tree_block(root, block2, blocksize, 0);
1454 free_extent_buffer(eb);
1455 }
1456 }
1457 return ret;
1458}
1459
1460
1461/*
Chris Masond3977122009-01-05 21:25:51 -05001462 * when we walk down the tree, it is usually safe to unlock the higher layers
1463 * in the tree. The exceptions are when our path goes through slot 0, because
1464 * operations on the tree might require changing key pointers higher up in the
1465 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001466 *
Chris Masond3977122009-01-05 21:25:51 -05001467 * callers might also have set path->keep_locks, which tells this code to keep
1468 * the lock if the path points to the last slot in the block. This is part of
1469 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001470 *
Chris Masond3977122009-01-05 21:25:51 -05001471 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1472 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001473 */
Chris Masone02119d2008-09-05 16:13:11 -04001474static noinline void unlock_up(struct btrfs_path *path, int level,
1475 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001476{
1477 int i;
1478 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001479 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001480 struct extent_buffer *t;
1481
1482 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1483 if (!path->nodes[i])
1484 break;
1485 if (!path->locks[i])
1486 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001487 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001488 skip_level = i + 1;
1489 continue;
1490 }
Chris Mason051e1b92008-06-25 16:01:30 -04001491 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001492 u32 nritems;
1493 t = path->nodes[i];
1494 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001495 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001496 skip_level = i + 1;
1497 continue;
1498 }
1499 }
Chris Mason051e1b92008-06-25 16:01:30 -04001500 if (skip_level < i && i >= lowest_unlock)
1501 no_skips = 1;
1502
Chris Mason925baed2008-06-25 16:01:30 -04001503 t = path->nodes[i];
1504 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1505 btrfs_tree_unlock(t);
1506 path->locks[i] = 0;
1507 }
1508 }
1509}
1510
Chris Mason3c69fae2007-08-07 15:52:22 -04001511/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001512 * This releases any locks held in the path starting at level and
1513 * going all the way up to the root.
1514 *
1515 * btrfs_search_slot will keep the lock held on higher nodes in a few
1516 * corner cases, such as COW of the block at slot zero in the node. This
1517 * ignores those rules, and it should only be called when there are no
1518 * more updates to be done higher up in the tree.
1519 */
1520noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1521{
1522 int i;
1523
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001524 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001525 return;
1526
1527 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1528 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001529 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001530 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001531 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001532 btrfs_tree_unlock(path->nodes[i]);
1533 path->locks[i] = 0;
1534 }
1535}
1536
1537/*
Chris Masonc8c42862009-04-03 10:14:18 -04001538 * helper function for btrfs_search_slot. The goal is to find a block
1539 * in cache without setting the path to blocking. If we find the block
1540 * we return zero and the path is unchanged.
1541 *
1542 * If we can't find the block, we set the path blocking and do some
1543 * reada. -EAGAIN is returned and the search must be repeated.
1544 */
1545static int
1546read_block_for_search(struct btrfs_trans_handle *trans,
1547 struct btrfs_root *root, struct btrfs_path *p,
1548 struct extent_buffer **eb_ret, int level, int slot,
1549 struct btrfs_key *key)
1550{
1551 u64 blocknr;
1552 u64 gen;
1553 u32 blocksize;
1554 struct extent_buffer *b = *eb_ret;
1555 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001556 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001557
1558 blocknr = btrfs_node_blockptr(b, slot);
1559 gen = btrfs_node_ptr_generation(b, slot);
1560 blocksize = btrfs_level_size(root, level - 1);
1561
1562 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1563 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason76a05b32009-05-14 13:24:30 -04001564 /*
1565 * we found an up to date block without sleeping, return
1566 * right away
1567 */
Chris Masonc8c42862009-04-03 10:14:18 -04001568 *eb_ret = tmp;
1569 return 0;
1570 }
1571
1572 /*
1573 * reduce lock contention at high levels
1574 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001575 * we read. Don't release the lock on the current
1576 * level because we need to walk this node to figure
1577 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001578 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001579 btrfs_unlock_up_safe(p, level + 1);
1580 btrfs_set_path_blocking(p);
1581
Chris Masonc8c42862009-04-03 10:14:18 -04001582 if (tmp)
1583 free_extent_buffer(tmp);
1584 if (p->reada)
1585 reada_for_search(root, p, level, slot, key->objectid);
1586
Chris Mason8c594ea2009-04-20 15:50:10 -04001587 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001588
1589 ret = -EAGAIN;
Chris Masonc8c42862009-04-03 10:14:18 -04001590 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Mason76a05b32009-05-14 13:24:30 -04001591 if (tmp) {
1592 /*
1593 * If the read above didn't mark this buffer up to date,
1594 * it will never end up being up to date. Set ret to EIO now
1595 * and give up so that our caller doesn't loop forever
1596 * on our EAGAINs.
1597 */
1598 if (!btrfs_buffer_uptodate(tmp, 0))
1599 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001600 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001601 }
1602 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001603}
1604
1605/*
1606 * helper function for btrfs_search_slot. This does all of the checks
1607 * for node-level blocks and does any balancing required based on
1608 * the ins_len.
1609 *
1610 * If no extra work was required, zero is returned. If we had to
1611 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1612 * start over
1613 */
1614static int
1615setup_nodes_for_search(struct btrfs_trans_handle *trans,
1616 struct btrfs_root *root, struct btrfs_path *p,
1617 struct extent_buffer *b, int level, int ins_len)
1618{
1619 int ret;
1620 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1621 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1622 int sret;
1623
1624 sret = reada_for_balance(root, p, level);
1625 if (sret)
1626 goto again;
1627
1628 btrfs_set_path_blocking(p);
1629 sret = split_node(trans, root, p, level);
1630 btrfs_clear_path_blocking(p, NULL);
1631
1632 BUG_ON(sret > 0);
1633 if (sret) {
1634 ret = sret;
1635 goto done;
1636 }
1637 b = p->nodes[level];
1638 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001639 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001640 int sret;
1641
1642 sret = reada_for_balance(root, p, level);
1643 if (sret)
1644 goto again;
1645
1646 btrfs_set_path_blocking(p);
1647 sret = balance_level(trans, root, p, level);
1648 btrfs_clear_path_blocking(p, NULL);
1649
1650 if (sret) {
1651 ret = sret;
1652 goto done;
1653 }
1654 b = p->nodes[level];
1655 if (!b) {
1656 btrfs_release_path(NULL, p);
1657 goto again;
1658 }
1659 BUG_ON(btrfs_header_nritems(b) == 1);
1660 }
1661 return 0;
1662
1663again:
1664 ret = -EAGAIN;
1665done:
1666 return ret;
1667}
1668
1669/*
Chris Mason74123bd2007-02-02 11:05:29 -05001670 * look for key in the tree. path is filled in with nodes along the way
1671 * if key is found, we return zero and you can find the item in the leaf
1672 * level of the path (level 0)
1673 *
1674 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001675 * be inserted, and 1 is returned. If there are other errors during the
1676 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001677 *
1678 * if ins_len > 0, nodes and leaves will be split as we walk down the
1679 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1680 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001681 */
Chris Masone089f052007-03-16 16:20:31 -04001682int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1683 *root, struct btrfs_key *key, struct btrfs_path *p, int
1684 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001685{
Chris Mason5f39d392007-10-15 16:14:19 -04001686 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001687 int slot;
1688 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001689 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001690 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001691 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001692 u8 lowest_level = 0;
1693
Chris Mason6702ed42007-08-07 16:15:09 -04001694 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001695 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001696 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001697
Chris Mason925baed2008-06-25 16:01:30 -04001698 if (ins_len < 0)
1699 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001700
Chris Masonbb803952007-03-01 12:04:21 -05001701again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001702 if (p->search_commit_root) {
1703 b = root->commit_root;
1704 extent_buffer_get(b);
1705 if (!p->skip_locking)
1706 btrfs_tree_lock(b);
1707 } else {
1708 if (p->skip_locking)
1709 b = btrfs_root_node(root);
1710 else
1711 b = btrfs_lock_root_node(root);
1712 }
Chris Mason925baed2008-06-25 16:01:30 -04001713
Chris Masoneb60cea2007-02-02 09:18:22 -05001714 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001715 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001716
1717 /*
1718 * setup the path here so we can release it under lock
1719 * contention with the cow code
1720 */
1721 p->nodes[level] = b;
1722 if (!p->skip_locking)
1723 p->locks[level] = 1;
1724
Chris Mason02217ed2007-03-02 16:08:05 -05001725 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001726 /*
1727 * if we don't really need to cow this block
1728 * then we don't want to set the path blocking,
1729 * so we test it here
1730 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001731 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001732 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001733
Chris Masonb4ce94d2009-02-04 09:25:08 -05001734 btrfs_set_path_blocking(p);
1735
Yan Zheng33c66f42009-07-22 09:59:00 -04001736 err = btrfs_cow_block(trans, root, b,
1737 p->nodes[level + 1],
1738 p->slots[level + 1], &b);
1739 if (err) {
Chris Mason5f39d392007-10-15 16:14:19 -04001740 free_extent_buffer(b);
Yan Zheng33c66f42009-07-22 09:59:00 -04001741 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001742 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001743 }
Chris Mason02217ed2007-03-02 16:08:05 -05001744 }
Chris Mason65b51a02008-08-01 15:11:20 -04001745cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001746 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001747 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001748 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001749 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001750
Chris Masoneb60cea2007-02-02 09:18:22 -05001751 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001752 if (!p->skip_locking)
1753 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001754
Chris Mason4008c042009-02-12 14:09:45 -05001755 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001756
1757 /*
1758 * we have a lock on b and as long as we aren't changing
1759 * the tree, there is no way to for the items in b to change.
1760 * It is safe to drop the lock on our parent before we
1761 * go through the expensive btree search on b.
1762 *
1763 * If cow is true, then we might be changing slot zero,
1764 * which may require changing the parent. So, we can't
1765 * drop the lock until after we know which slot we're
1766 * operating on.
1767 */
1768 if (!cow)
1769 btrfs_unlock_up_safe(p, level + 1);
1770
Chris Mason123abc82007-03-14 14:14:43 -04001771 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001772 if (ret) {
1773 ret = -1;
1774 goto done;
1775 }
Chris Mason925baed2008-06-25 16:01:30 -04001776
Chris Mason5f39d392007-10-15 16:14:19 -04001777 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001778
Chris Mason5f39d392007-10-15 16:14:19 -04001779 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001780 int dec = 0;
1781 if (ret && slot > 0) {
1782 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001783 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001784 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001785 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001786 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001787 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001788 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001789 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001790 if (err) {
1791 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001792 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001793 }
Chris Masonc8c42862009-04-03 10:14:18 -04001794 b = p->nodes[level];
1795 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001796
Chris Masonf9efa9c2008-06-25 16:14:04 -04001797 unlock_up(p, level, lowest_unlock);
1798
Chris Mason925baed2008-06-25 16:01:30 -04001799 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001800 if (dec)
1801 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001802 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001803 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001804
Yan Zheng33c66f42009-07-22 09:59:00 -04001805 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001806 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001807 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001808 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001809 if (err) {
1810 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001811 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001812 }
Chris Mason76a05b32009-05-14 13:24:30 -04001813
Chris Masonb4ce94d2009-02-04 09:25:08 -05001814 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001815 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001816 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001817
Yan Zheng33c66f42009-07-22 09:59:00 -04001818 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001819 btrfs_set_path_blocking(p);
1820 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001821 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001822 }
1823 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001824 } else {
1825 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001826 if (ins_len > 0 &&
1827 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001828 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001829 err = split_leaf(trans, root, key,
1830 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001831 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001832
Yan Zheng33c66f42009-07-22 09:59:00 -04001833 BUG_ON(err > 0);
1834 if (err) {
1835 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001836 goto done;
1837 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001838 }
Chris Mason459931e2008-12-10 09:10:46 -05001839 if (!p->search_for_split)
1840 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001841 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001842 }
1843 }
Chris Mason65b51a02008-08-01 15:11:20 -04001844 ret = 1;
1845done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001846 /*
1847 * we don't really know what they plan on doing with the path
1848 * from here on, so for now just mark it as blocking
1849 */
Chris Masonb9473432009-03-13 11:00:37 -04001850 if (!p->leave_spinning)
1851 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001852 if (ret < 0)
1853 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001854 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001855}
1856
Chris Mason74123bd2007-02-02 11:05:29 -05001857/*
1858 * adjust the pointers going up the tree, starting at level
1859 * making sure the right key of each node is points to 'key'.
1860 * This is used after shifting pointers to the left, so it stops
1861 * fixing up pointers when a given leaf/node is not in slot 0 of the
1862 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001863 *
1864 * If this fails to write a tree block, it returns -1, but continues
1865 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001866 */
Chris Mason5f39d392007-10-15 16:14:19 -04001867static int fixup_low_keys(struct btrfs_trans_handle *trans,
1868 struct btrfs_root *root, struct btrfs_path *path,
1869 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001870{
1871 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001872 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001873 struct extent_buffer *t;
1874
Chris Mason234b63a2007-03-13 10:46:10 -04001875 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001876 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001877 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001878 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001879 t = path->nodes[i];
1880 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001881 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001882 if (tslot != 0)
1883 break;
1884 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001885 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001886}
1887
Chris Mason74123bd2007-02-02 11:05:29 -05001888/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001889 * update item key.
1890 *
1891 * This function isn't completely safe. It's the caller's responsibility
1892 * that the new key won't break the order
1893 */
1894int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1895 struct btrfs_root *root, struct btrfs_path *path,
1896 struct btrfs_key *new_key)
1897{
1898 struct btrfs_disk_key disk_key;
1899 struct extent_buffer *eb;
1900 int slot;
1901
1902 eb = path->nodes[0];
1903 slot = path->slots[0];
1904 if (slot > 0) {
1905 btrfs_item_key(eb, &disk_key, slot - 1);
1906 if (comp_keys(&disk_key, new_key) >= 0)
1907 return -1;
1908 }
1909 if (slot < btrfs_header_nritems(eb) - 1) {
1910 btrfs_item_key(eb, &disk_key, slot + 1);
1911 if (comp_keys(&disk_key, new_key) <= 0)
1912 return -1;
1913 }
1914
1915 btrfs_cpu_key_to_disk(&disk_key, new_key);
1916 btrfs_set_item_key(eb, &disk_key, slot);
1917 btrfs_mark_buffer_dirty(eb);
1918 if (slot == 0)
1919 fixup_low_keys(trans, root, path, &disk_key, 1);
1920 return 0;
1921}
1922
1923/*
Chris Mason74123bd2007-02-02 11:05:29 -05001924 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001925 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001926 *
1927 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1928 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001929 */
Chris Mason98ed5172008-01-03 10:01:48 -05001930static int push_node_left(struct btrfs_trans_handle *trans,
1931 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001932 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001933{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001934 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001935 int src_nritems;
1936 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001937 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938
Chris Mason5f39d392007-10-15 16:14:19 -04001939 src_nritems = btrfs_header_nritems(src);
1940 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001941 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001942 WARN_ON(btrfs_header_generation(src) != trans->transid);
1943 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001944
Chris Masonbce4eae2008-04-24 14:42:46 -04001945 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001946 return 1;
1947
Chris Masond3977122009-01-05 21:25:51 -05001948 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001949 return 1;
1950
Chris Masonbce4eae2008-04-24 14:42:46 -04001951 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001952 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001953 if (push_items < src_nritems) {
1954 /* leave at least 8 pointers in the node if
1955 * we aren't going to empty it
1956 */
1957 if (src_nritems - push_items < 8) {
1958 if (push_items <= 8)
1959 return 1;
1960 push_items -= 8;
1961 }
1962 }
1963 } else
1964 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001965
Chris Mason5f39d392007-10-15 16:14:19 -04001966 copy_extent_buffer(dst, src,
1967 btrfs_node_key_ptr_offset(dst_nritems),
1968 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001969 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001970
Chris Masonbb803952007-03-01 12:04:21 -05001971 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001972 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1973 btrfs_node_key_ptr_offset(push_items),
1974 (src_nritems - push_items) *
1975 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001976 }
Chris Mason5f39d392007-10-15 16:14:19 -04001977 btrfs_set_header_nritems(src, src_nritems - push_items);
1978 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1979 btrfs_mark_buffer_dirty(src);
1980 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001981
Chris Masonbb803952007-03-01 12:04:21 -05001982 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001983}
1984
Chris Mason97571fd2007-02-24 13:39:08 -05001985/*
Chris Mason79f95c82007-03-01 15:16:26 -05001986 * try to push data from one node into the next node right in the
1987 * tree.
1988 *
1989 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1990 * error, and > 0 if there was no room in the right hand block.
1991 *
1992 * this will only push up to 1/2 the contents of the left node over
1993 */
Chris Mason5f39d392007-10-15 16:14:19 -04001994static int balance_node_right(struct btrfs_trans_handle *trans,
1995 struct btrfs_root *root,
1996 struct extent_buffer *dst,
1997 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001998{
Chris Mason79f95c82007-03-01 15:16:26 -05001999 int push_items = 0;
2000 int max_push;
2001 int src_nritems;
2002 int dst_nritems;
2003 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002004
Chris Mason7bb86312007-12-11 09:25:06 -05002005 WARN_ON(btrfs_header_generation(src) != trans->transid);
2006 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2007
Chris Mason5f39d392007-10-15 16:14:19 -04002008 src_nritems = btrfs_header_nritems(src);
2009 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002010 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002011 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002012 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002013
Chris Masond3977122009-01-05 21:25:51 -05002014 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002015 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002016
2017 max_push = src_nritems / 2 + 1;
2018 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002019 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002020 return 1;
Yan252c38f2007-08-29 09:11:44 -04002021
Chris Mason79f95c82007-03-01 15:16:26 -05002022 if (max_push < push_items)
2023 push_items = max_push;
2024
Chris Mason5f39d392007-10-15 16:14:19 -04002025 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2026 btrfs_node_key_ptr_offset(0),
2027 (dst_nritems) *
2028 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002029
Chris Mason5f39d392007-10-15 16:14:19 -04002030 copy_extent_buffer(dst, src,
2031 btrfs_node_key_ptr_offset(0),
2032 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002033 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002034
Chris Mason5f39d392007-10-15 16:14:19 -04002035 btrfs_set_header_nritems(src, src_nritems - push_items);
2036 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002037
Chris Mason5f39d392007-10-15 16:14:19 -04002038 btrfs_mark_buffer_dirty(src);
2039 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002040
Chris Mason79f95c82007-03-01 15:16:26 -05002041 return ret;
2042}
2043
2044/*
Chris Mason97571fd2007-02-24 13:39:08 -05002045 * helper function to insert a new root level in the tree.
2046 * A new node is allocated, and a single item is inserted to
2047 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002048 *
2049 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002050 */
Chris Masond3977122009-01-05 21:25:51 -05002051static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002052 struct btrfs_root *root,
2053 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002054{
Chris Mason7bb86312007-12-11 09:25:06 -05002055 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002056 struct extent_buffer *lower;
2057 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002058 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002059 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002060
2061 BUG_ON(path->nodes[level]);
2062 BUG_ON(path->nodes[level-1] != root->node);
2063
Chris Mason7bb86312007-12-11 09:25:06 -05002064 lower = path->nodes[level-1];
2065 if (level == 1)
2066 btrfs_item_key(lower, &lower_key, 0);
2067 else
2068 btrfs_node_key(lower, &lower_key, 0);
2069
Zheng Yan31840ae2008-09-23 13:14:14 -04002070 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002071 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002072 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002073 if (IS_ERR(c))
2074 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002075
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002077 btrfs_set_header_nritems(c, 1);
2078 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002079 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002080 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002081 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002082 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002083
Chris Mason5f39d392007-10-15 16:14:19 -04002084 write_extent_buffer(c, root->fs_info->fsid,
2085 (unsigned long)btrfs_header_fsid(c),
2086 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002087
2088 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2089 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2090 BTRFS_UUID_SIZE);
2091
Chris Mason5f39d392007-10-15 16:14:19 -04002092 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002093 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002094 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002095 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002096
2097 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002098
2099 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002100
Chris Mason925baed2008-06-25 16:01:30 -04002101 spin_lock(&root->node_lock);
2102 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002103 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002104 spin_unlock(&root->node_lock);
2105
2106 /* the super has an extra ref to root->node */
2107 free_extent_buffer(old);
2108
Chris Mason0b86a832008-03-24 15:01:56 -04002109 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002110 extent_buffer_get(c);
2111 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002112 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002113 path->slots[level] = 0;
2114 return 0;
2115}
2116
Chris Mason74123bd2007-02-02 11:05:29 -05002117/*
2118 * worker function to insert a single pointer in a node.
2119 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002120 *
Chris Mason74123bd2007-02-02 11:05:29 -05002121 * slot and level indicate where you want the key to go, and
2122 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002123 *
2124 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002125 */
Chris Masone089f052007-03-16 16:20:31 -04002126static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2127 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002128 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002129{
Chris Mason5f39d392007-10-15 16:14:19 -04002130 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002131 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002132
2133 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002134 lower = path->nodes[level];
2135 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002136 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002137 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002138 BUG();
2139 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002140 memmove_extent_buffer(lower,
2141 btrfs_node_key_ptr_offset(slot + 1),
2142 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002143 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002144 }
Chris Mason5f39d392007-10-15 16:14:19 -04002145 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002146 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002147 WARN_ON(trans->transid == 0);
2148 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002149 btrfs_set_header_nritems(lower, nritems + 1);
2150 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002151 return 0;
2152}
2153
Chris Mason97571fd2007-02-24 13:39:08 -05002154/*
2155 * split the node at the specified level in path in two.
2156 * The path is corrected to point to the appropriate node after the split
2157 *
2158 * Before splitting this tries to make some room in the node by pushing
2159 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002160 *
2161 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002162 */
Chris Masone02119d2008-09-05 16:13:11 -04002163static noinline int split_node(struct btrfs_trans_handle *trans,
2164 struct btrfs_root *root,
2165 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002166{
Chris Mason5f39d392007-10-15 16:14:19 -04002167 struct extent_buffer *c;
2168 struct extent_buffer *split;
2169 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002170 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002171 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002172 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002173 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002174
Chris Mason5f39d392007-10-15 16:14:19 -04002175 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002176 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002177 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002178 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002179 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002180 if (ret)
2181 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002182 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002183 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002184 c = path->nodes[level];
2185 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002186 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002187 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002188 if (ret < 0)
2189 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002190 }
Chris Masone66f7092007-04-20 13:16:02 -04002191
Chris Mason5f39d392007-10-15 16:14:19 -04002192 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002193 mid = (c_nritems + 1) / 2;
2194 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002195
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002196 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002197 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002199 if (IS_ERR(split))
2200 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002201
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002203 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002204 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002205 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002206 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002207 btrfs_set_header_owner(split, root->root_key.objectid);
2208 write_extent_buffer(split, root->fs_info->fsid,
2209 (unsigned long)btrfs_header_fsid(split),
2210 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002211 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2212 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2213 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002214
Chris Mason5f39d392007-10-15 16:14:19 -04002215
2216 copy_extent_buffer(split, c,
2217 btrfs_node_key_ptr_offset(0),
2218 btrfs_node_key_ptr_offset(mid),
2219 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2220 btrfs_set_header_nritems(split, c_nritems - mid);
2221 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002222 ret = 0;
2223
Chris Mason5f39d392007-10-15 16:14:19 -04002224 btrfs_mark_buffer_dirty(c);
2225 btrfs_mark_buffer_dirty(split);
2226
Chris Masondb945352007-10-15 16:15:53 -04002227 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002228 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002229 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002230 if (wret)
2231 ret = wret;
2232
Chris Mason5de08d72007-02-24 06:24:44 -05002233 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002234 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002235 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002236 free_extent_buffer(c);
2237 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002238 path->slots[level + 1] += 1;
2239 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002240 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002241 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002242 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002243 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002244}
2245
Chris Mason74123bd2007-02-02 11:05:29 -05002246/*
2247 * how many bytes are required to store the items in a leaf. start
2248 * and nr indicate which items in the leaf to check. This totals up the
2249 * space used both by the item structs and the item data
2250 */
Chris Mason5f39d392007-10-15 16:14:19 -04002251static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002252{
2253 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002254 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002255 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002256
2257 if (!nr)
2258 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002259 data_len = btrfs_item_end_nr(l, start);
2260 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002261 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002262 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002263 return data_len;
2264}
2265
Chris Mason74123bd2007-02-02 11:05:29 -05002266/*
Chris Masond4dbff92007-04-04 14:08:15 -04002267 * The space between the end of the leaf items and
2268 * the start of the leaf data. IOW, how much room
2269 * the leaf has left for both items and data
2270 */
Chris Masond3977122009-01-05 21:25:51 -05002271noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002272 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002273{
Chris Mason5f39d392007-10-15 16:14:19 -04002274 int nritems = btrfs_header_nritems(leaf);
2275 int ret;
2276 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2277 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002278 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2279 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002280 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002281 leaf_space_used(leaf, 0, nritems), nritems);
2282 }
2283 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002284}
2285
Chris Mason44871b12009-03-13 10:04:31 -04002286static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2287 struct btrfs_root *root,
2288 struct btrfs_path *path,
2289 int data_size, int empty,
2290 struct extent_buffer *right,
2291 int free_space, u32 left_nritems)
Chris Mason00ec4c52007-02-24 12:47:20 -05002292{
Chris Mason5f39d392007-10-15 16:14:19 -04002293 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002294 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002295 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002296 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002297 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002298 int push_space = 0;
2299 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002300 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002301 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002302 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002303 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002304 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002305
Chris Mason34a38212007-11-07 13:31:03 -05002306 if (empty)
2307 nr = 0;
2308 else
2309 nr = 1;
2310
Zheng Yan31840ae2008-09-23 13:14:14 -04002311 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002312 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002313
Chris Mason44871b12009-03-13 10:04:31 -04002314 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002315 i = left_nritems - 1;
2316 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002317 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002318
Zheng Yan31840ae2008-09-23 13:14:14 -04002319 if (!empty && push_items > 0) {
2320 if (path->slots[0] > i)
2321 break;
2322 if (path->slots[0] == i) {
2323 int space = btrfs_leaf_free_space(root, left);
2324 if (space + push_space * 2 > free_space)
2325 break;
2326 }
2327 }
2328
Chris Mason00ec4c52007-02-24 12:47:20 -05002329 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002330 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002331
2332 if (!left->map_token) {
2333 map_extent_buffer(left, (unsigned long)item,
2334 sizeof(struct btrfs_item),
2335 &left->map_token, &left->kaddr,
2336 &left->map_start, &left->map_len,
2337 KM_USER1);
2338 }
2339
2340 this_item_size = btrfs_item_size(left, item);
2341 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002342 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002343
Chris Mason00ec4c52007-02-24 12:47:20 -05002344 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002345 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002346 if (i == 0)
2347 break;
2348 i--;
Chris Masondb945352007-10-15 16:15:53 -04002349 }
2350 if (left->map_token) {
2351 unmap_extent_buffer(left, left->map_token, KM_USER1);
2352 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002353 }
Chris Mason5f39d392007-10-15 16:14:19 -04002354
Chris Mason925baed2008-06-25 16:01:30 -04002355 if (push_items == 0)
2356 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002357
Chris Mason34a38212007-11-07 13:31:03 -05002358 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002359 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002360
Chris Mason00ec4c52007-02-24 12:47:20 -05002361 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002362 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002363
Chris Mason5f39d392007-10-15 16:14:19 -04002364 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002365 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002366
Chris Mason00ec4c52007-02-24 12:47:20 -05002367 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002368 data_end = leaf_data_end(root, right);
2369 memmove_extent_buffer(right,
2370 btrfs_leaf_data(right) + data_end - push_space,
2371 btrfs_leaf_data(right) + data_end,
2372 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2373
Chris Mason00ec4c52007-02-24 12:47:20 -05002374 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002375 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002376 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2377 btrfs_leaf_data(left) + leaf_data_end(root, left),
2378 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002379
2380 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2381 btrfs_item_nr_offset(0),
2382 right_nritems * sizeof(struct btrfs_item));
2383
Chris Mason00ec4c52007-02-24 12:47:20 -05002384 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002385 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2386 btrfs_item_nr_offset(left_nritems - push_items),
2387 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002388
2389 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002390 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002391 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002392 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002393 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002394 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002395 if (!right->map_token) {
2396 map_extent_buffer(right, (unsigned long)item,
2397 sizeof(struct btrfs_item),
2398 &right->map_token, &right->kaddr,
2399 &right->map_start, &right->map_len,
2400 KM_USER1);
2401 }
2402 push_space -= btrfs_item_size(right, item);
2403 btrfs_set_item_offset(right, item, push_space);
2404 }
2405
2406 if (right->map_token) {
2407 unmap_extent_buffer(right, right->map_token, KM_USER1);
2408 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002409 }
Chris Mason7518a232007-03-12 12:01:18 -04002410 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002411 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002412
Chris Mason34a38212007-11-07 13:31:03 -05002413 if (left_nritems)
2414 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002415 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002416
Chris Mason5f39d392007-10-15 16:14:19 -04002417 btrfs_item_key(right, &disk_key, 0);
2418 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002419 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002420
Chris Mason00ec4c52007-02-24 12:47:20 -05002421 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002422 if (path->slots[0] >= left_nritems) {
2423 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002424 if (btrfs_header_nritems(path->nodes[0]) == 0)
2425 clean_tree_block(trans, root, path->nodes[0]);
2426 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002427 free_extent_buffer(path->nodes[0]);
2428 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002429 path->slots[1] += 1;
2430 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002431 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002432 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002433 }
2434 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002435
2436out_unlock:
2437 btrfs_tree_unlock(right);
2438 free_extent_buffer(right);
2439 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002440}
Chris Mason925baed2008-06-25 16:01:30 -04002441
Chris Mason00ec4c52007-02-24 12:47:20 -05002442/*
Chris Mason44871b12009-03-13 10:04:31 -04002443 * push some data in the path leaf to the right, trying to free up at
2444 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2445 *
2446 * returns 1 if the push failed because the other node didn't have enough
2447 * room, 0 if everything worked out and < 0 if there were major errors.
2448 */
2449static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2450 *root, struct btrfs_path *path, int data_size,
2451 int empty)
2452{
2453 struct extent_buffer *left = path->nodes[0];
2454 struct extent_buffer *right;
2455 struct extent_buffer *upper;
2456 int slot;
2457 int free_space;
2458 u32 left_nritems;
2459 int ret;
2460
2461 if (!path->nodes[1])
2462 return 1;
2463
2464 slot = path->slots[1];
2465 upper = path->nodes[1];
2466 if (slot >= btrfs_header_nritems(upper) - 1)
2467 return 1;
2468
2469 btrfs_assert_tree_locked(path->nodes[1]);
2470
2471 right = read_node_slot(root, upper, slot + 1);
2472 btrfs_tree_lock(right);
2473 btrfs_set_lock_blocking(right);
2474
2475 free_space = btrfs_leaf_free_space(root, right);
2476 if (free_space < data_size)
2477 goto out_unlock;
2478
2479 /* cow and double check */
2480 ret = btrfs_cow_block(trans, root, right, upper,
2481 slot + 1, &right);
2482 if (ret)
2483 goto out_unlock;
2484
2485 free_space = btrfs_leaf_free_space(root, right);
2486 if (free_space < data_size)
2487 goto out_unlock;
2488
2489 left_nritems = btrfs_header_nritems(left);
2490 if (left_nritems == 0)
2491 goto out_unlock;
2492
2493 return __push_leaf_right(trans, root, path, data_size, empty,
2494 right, free_space, left_nritems);
2495out_unlock:
2496 btrfs_tree_unlock(right);
2497 free_extent_buffer(right);
2498 return 1;
2499}
2500
2501/*
Chris Mason74123bd2007-02-02 11:05:29 -05002502 * push some data in the path leaf to the left, trying to free up at
2503 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2504 */
Chris Mason44871b12009-03-13 10:04:31 -04002505static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2506 struct btrfs_root *root,
2507 struct btrfs_path *path, int data_size,
2508 int empty, struct extent_buffer *left,
2509 int free_space, int right_nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002510{
Chris Mason5f39d392007-10-15 16:14:19 -04002511 struct btrfs_disk_key disk_key;
2512 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002513 int slot;
2514 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515 int push_space = 0;
2516 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002517 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002518 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002519 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002520 int ret = 0;
2521 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002522 u32 this_item_size;
2523 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002524
2525 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002526
Chris Mason34a38212007-11-07 13:31:03 -05002527 if (empty)
2528 nr = right_nritems;
2529 else
2530 nr = right_nritems - 1;
2531
2532 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002533 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002534 if (!right->map_token) {
2535 map_extent_buffer(right, (unsigned long)item,
2536 sizeof(struct btrfs_item),
2537 &right->map_token, &right->kaddr,
2538 &right->map_start, &right->map_len,
2539 KM_USER1);
2540 }
2541
Zheng Yan31840ae2008-09-23 13:14:14 -04002542 if (!empty && push_items > 0) {
2543 if (path->slots[0] < i)
2544 break;
2545 if (path->slots[0] == i) {
2546 int space = btrfs_leaf_free_space(root, right);
2547 if (space + push_space * 2 > free_space)
2548 break;
2549 }
2550 }
2551
Chris Masonbe0e5c02007-01-26 15:51:26 -05002552 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002553 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002554
2555 this_item_size = btrfs_item_size(right, item);
2556 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002557 break;
Chris Masondb945352007-10-15 16:15:53 -04002558
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002560 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561 }
Chris Masondb945352007-10-15 16:15:53 -04002562
2563 if (right->map_token) {
2564 unmap_extent_buffer(right, right->map_token, KM_USER1);
2565 right->map_token = NULL;
2566 }
2567
Chris Masonbe0e5c02007-01-26 15:51:26 -05002568 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002569 ret = 1;
2570 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002571 }
Chris Mason34a38212007-11-07 13:31:03 -05002572 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002573 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002574
Chris Masonbe0e5c02007-01-26 15:51:26 -05002575 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002576 copy_extent_buffer(left, right,
2577 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2578 btrfs_item_nr_offset(0),
2579 push_items * sizeof(struct btrfs_item));
2580
Chris Mason123abc82007-03-14 14:14:43 -04002581 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002582 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002583
2584 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002585 leaf_data_end(root, left) - push_space,
2586 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002587 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002588 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002589 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002590 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002591
Chris Masondb945352007-10-15 16:15:53 -04002592 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002593 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002594 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002595
Chris Mason5f39d392007-10-15 16:14:19 -04002596 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002597 if (!left->map_token) {
2598 map_extent_buffer(left, (unsigned long)item,
2599 sizeof(struct btrfs_item),
2600 &left->map_token, &left->kaddr,
2601 &left->map_start, &left->map_len,
2602 KM_USER1);
2603 }
2604
Chris Mason5f39d392007-10-15 16:14:19 -04002605 ioff = btrfs_item_offset(left, item);
2606 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002607 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002608 }
Chris Mason5f39d392007-10-15 16:14:19 -04002609 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002610 if (left->map_token) {
2611 unmap_extent_buffer(left, left->map_token, KM_USER1);
2612 left->map_token = NULL;
2613 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002614
2615 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002616 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002617 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2618 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002619 WARN_ON(1);
2620 }
Chris Mason5f39d392007-10-15 16:14:19 -04002621
Chris Mason34a38212007-11-07 13:31:03 -05002622 if (push_items < right_nritems) {
2623 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2624 leaf_data_end(root, right);
2625 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2626 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2627 btrfs_leaf_data(right) +
2628 leaf_data_end(root, right), push_space);
2629
2630 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002631 btrfs_item_nr_offset(push_items),
2632 (btrfs_header_nritems(right) - push_items) *
2633 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002634 }
Yaneef1c492007-11-26 10:58:13 -05002635 right_nritems -= push_items;
2636 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002637 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002638 for (i = 0; i < right_nritems; i++) {
2639 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002640
2641 if (!right->map_token) {
2642 map_extent_buffer(right, (unsigned long)item,
2643 sizeof(struct btrfs_item),
2644 &right->map_token, &right->kaddr,
2645 &right->map_start, &right->map_len,
2646 KM_USER1);
2647 }
2648
2649 push_space = push_space - btrfs_item_size(right, item);
2650 btrfs_set_item_offset(right, item, push_space);
2651 }
2652 if (right->map_token) {
2653 unmap_extent_buffer(right, right->map_token, KM_USER1);
2654 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002655 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002656
Chris Mason5f39d392007-10-15 16:14:19 -04002657 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002658 if (right_nritems)
2659 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002660
Chris Mason5f39d392007-10-15 16:14:19 -04002661 btrfs_item_key(right, &disk_key, 0);
2662 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002663 if (wret)
2664 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002665
2666 /* then fixup the leaf pointer in the path */
2667 if (path->slots[0] < push_items) {
2668 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002669 if (btrfs_header_nritems(path->nodes[0]) == 0)
2670 clean_tree_block(trans, root, path->nodes[0]);
2671 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002672 free_extent_buffer(path->nodes[0]);
2673 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002674 path->slots[1] -= 1;
2675 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002676 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002678 path->slots[0] -= push_items;
2679 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002680 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002681 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002682out:
2683 btrfs_tree_unlock(left);
2684 free_extent_buffer(left);
2685 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002686}
2687
Chris Mason74123bd2007-02-02 11:05:29 -05002688/*
Chris Mason44871b12009-03-13 10:04:31 -04002689 * push some data in the path leaf to the left, trying to free up at
2690 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2691 */
2692static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2693 *root, struct btrfs_path *path, int data_size,
2694 int empty)
2695{
2696 struct extent_buffer *right = path->nodes[0];
2697 struct extent_buffer *left;
2698 int slot;
2699 int free_space;
2700 u32 right_nritems;
2701 int ret = 0;
2702
2703 slot = path->slots[1];
2704 if (slot == 0)
2705 return 1;
2706 if (!path->nodes[1])
2707 return 1;
2708
2709 right_nritems = btrfs_header_nritems(right);
2710 if (right_nritems == 0)
2711 return 1;
2712
2713 btrfs_assert_tree_locked(path->nodes[1]);
2714
2715 left = read_node_slot(root, path->nodes[1], slot - 1);
2716 btrfs_tree_lock(left);
2717 btrfs_set_lock_blocking(left);
2718
2719 free_space = btrfs_leaf_free_space(root, left);
2720 if (free_space < data_size) {
2721 ret = 1;
2722 goto out;
2723 }
2724
2725 /* cow and double check */
2726 ret = btrfs_cow_block(trans, root, left,
2727 path->nodes[1], slot - 1, &left);
2728 if (ret) {
2729 /* we hit -ENOSPC, but it isn't fatal here */
2730 ret = 1;
2731 goto out;
2732 }
2733
2734 free_space = btrfs_leaf_free_space(root, left);
2735 if (free_space < data_size) {
2736 ret = 1;
2737 goto out;
2738 }
2739
2740 return __push_leaf_left(trans, root, path, data_size,
2741 empty, left, free_space, right_nritems);
2742out:
2743 btrfs_tree_unlock(left);
2744 free_extent_buffer(left);
2745 return ret;
2746}
2747
2748/*
Chris Mason74123bd2007-02-02 11:05:29 -05002749 * split the path's leaf in two, making sure there is at least data_size
2750 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002751 *
2752 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002753 */
Chris Mason44871b12009-03-13 10:04:31 -04002754static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002755 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002756 struct btrfs_path *path,
2757 struct extent_buffer *l,
2758 struct extent_buffer *right,
2759 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002760{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002761 int data_copy_size;
2762 int rt_data_off;
2763 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002764 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002765 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002766 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002767
Chris Mason5f39d392007-10-15 16:14:19 -04002768 nritems = nritems - mid;
2769 btrfs_set_header_nritems(right, nritems);
2770 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2771
2772 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2773 btrfs_item_nr_offset(mid),
2774 nritems * sizeof(struct btrfs_item));
2775
2776 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002777 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2778 data_copy_size, btrfs_leaf_data(l) +
2779 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002780
Chris Mason5f39d392007-10-15 16:14:19 -04002781 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2782 btrfs_item_end_nr(l, mid);
2783
2784 for (i = 0; i < nritems; i++) {
2785 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002786 u32 ioff;
2787
2788 if (!right->map_token) {
2789 map_extent_buffer(right, (unsigned long)item,
2790 sizeof(struct btrfs_item),
2791 &right->map_token, &right->kaddr,
2792 &right->map_start, &right->map_len,
2793 KM_USER1);
2794 }
2795
2796 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002797 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002798 }
Chris Mason74123bd2007-02-02 11:05:29 -05002799
Chris Masondb945352007-10-15 16:15:53 -04002800 if (right->map_token) {
2801 unmap_extent_buffer(right, right->map_token, KM_USER1);
2802 right->map_token = NULL;
2803 }
2804
Chris Mason5f39d392007-10-15 16:14:19 -04002805 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002806 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002807 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002808 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2809 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002810 if (wret)
2811 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002812
2813 btrfs_mark_buffer_dirty(right);
2814 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002815 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002816
Chris Masonbe0e5c02007-01-26 15:51:26 -05002817 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002818 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002819 free_extent_buffer(path->nodes[0]);
2820 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002821 path->slots[0] -= mid;
2822 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002823 } else {
2824 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002825 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002826 }
Chris Mason5f39d392007-10-15 16:14:19 -04002827
Chris Masoneb60cea2007-02-02 09:18:22 -05002828 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002829
Chris Mason44871b12009-03-13 10:04:31 -04002830 return ret;
2831}
2832
2833/*
2834 * split the path's leaf in two, making sure there is at least data_size
2835 * available for the resulting leaf level of the path.
2836 *
2837 * returns 0 if all went well and < 0 on failure.
2838 */
2839static noinline int split_leaf(struct btrfs_trans_handle *trans,
2840 struct btrfs_root *root,
2841 struct btrfs_key *ins_key,
2842 struct btrfs_path *path, int data_size,
2843 int extend)
2844{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002845 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002846 struct extent_buffer *l;
2847 u32 nritems;
2848 int mid;
2849 int slot;
2850 struct extent_buffer *right;
2851 int ret = 0;
2852 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002853 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002854 int num_doubles = 0;
2855
Yan, Zhenga5719522009-09-24 09:17:31 -04002856 l = path->nodes[0];
2857 slot = path->slots[0];
2858 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2859 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2860 return -EOVERFLOW;
2861
Chris Mason44871b12009-03-13 10:04:31 -04002862 /* first try to make some room by pushing left and right */
Chris Masonb3612422009-05-13 19:12:15 -04002863 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason44871b12009-03-13 10:04:31 -04002864 wret = push_leaf_right(trans, root, path, data_size, 0);
2865 if (wret < 0)
2866 return wret;
2867 if (wret) {
2868 wret = push_leaf_left(trans, root, path, data_size, 0);
2869 if (wret < 0)
2870 return wret;
2871 }
2872 l = path->nodes[0];
2873
2874 /* did the pushes work? */
2875 if (btrfs_leaf_free_space(root, l) >= data_size)
2876 return 0;
2877 }
2878
2879 if (!path->nodes[1]) {
2880 ret = insert_new_root(trans, root, path, 1);
2881 if (ret)
2882 return ret;
2883 }
2884again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002885 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002886 l = path->nodes[0];
2887 slot = path->slots[0];
2888 nritems = btrfs_header_nritems(l);
2889 mid = (nritems + 1) / 2;
2890
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002891 if (mid <= slot) {
2892 if (nritems == 1 ||
2893 leaf_space_used(l, mid, nritems - mid) + data_size >
2894 BTRFS_LEAF_DATA_SIZE(root)) {
2895 if (slot >= nritems) {
2896 split = 0;
2897 } else {
2898 mid = slot;
2899 if (mid != nritems &&
2900 leaf_space_used(l, mid, nritems - mid) +
2901 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2902 split = 2;
2903 }
2904 }
2905 }
2906 } else {
2907 if (leaf_space_used(l, 0, mid) + data_size >
2908 BTRFS_LEAF_DATA_SIZE(root)) {
2909 if (!extend && data_size && slot == 0) {
2910 split = 0;
2911 } else if ((extend || !data_size) && slot == 0) {
2912 mid = 1;
2913 } else {
2914 mid = slot;
2915 if (mid != nritems &&
2916 leaf_space_used(l, mid, nritems - mid) +
2917 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2918 split = 2 ;
2919 }
2920 }
2921 }
2922 }
2923
2924 if (split == 0)
2925 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2926 else
2927 btrfs_item_key(l, &disk_key, mid);
2928
2929 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002930 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002931 &disk_key, 0, l->start, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002932 if (IS_ERR(right)) {
2933 BUG_ON(1);
2934 return PTR_ERR(right);
2935 }
2936
2937 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2938 btrfs_set_header_bytenr(right, right->start);
2939 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002940 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002941 btrfs_set_header_owner(right, root->root_key.objectid);
2942 btrfs_set_header_level(right, 0);
2943 write_extent_buffer(right, root->fs_info->fsid,
2944 (unsigned long)btrfs_header_fsid(right),
2945 BTRFS_FSID_SIZE);
2946
2947 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2948 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2949 BTRFS_UUID_SIZE);
2950
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002951 if (split == 0) {
2952 if (mid <= slot) {
2953 btrfs_set_header_nritems(right, 0);
2954 wret = insert_ptr(trans, root, path,
2955 &disk_key, right->start,
2956 path->slots[1] + 1, 1);
2957 if (wret)
2958 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002959
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002960 btrfs_tree_unlock(path->nodes[0]);
2961 free_extent_buffer(path->nodes[0]);
2962 path->nodes[0] = right;
2963 path->slots[0] = 0;
2964 path->slots[1] += 1;
2965 } else {
2966 btrfs_set_header_nritems(right, 0);
2967 wret = insert_ptr(trans, root, path,
2968 &disk_key,
2969 right->start,
2970 path->slots[1], 1);
2971 if (wret)
2972 ret = wret;
2973 btrfs_tree_unlock(path->nodes[0]);
2974 free_extent_buffer(path->nodes[0]);
2975 path->nodes[0] = right;
2976 path->slots[0] = 0;
2977 if (path->slots[1] == 0) {
2978 wret = fixup_low_keys(trans, root,
2979 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002980 if (wret)
2981 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002982 }
2983 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002984 btrfs_mark_buffer_dirty(right);
2985 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002986 }
2987
2988 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2989 BUG_ON(ret);
2990
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002991 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002992 BUG_ON(num_doubles != 0);
2993 num_doubles++;
2994 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002995 }
Chris Mason44871b12009-03-13 10:04:31 -04002996
Chris Masonbe0e5c02007-01-26 15:51:26 -05002997 return ret;
2998}
2999
Chris Masond352ac62008-09-29 15:18:18 -04003000/*
Chris Mason459931e2008-12-10 09:10:46 -05003001 * This function splits a single item into two items,
3002 * giving 'new_key' to the new item and splitting the
3003 * old one at split_offset (from the start of the item).
3004 *
3005 * The path may be released by this operation. After
3006 * the split, the path is pointing to the old item. The
3007 * new item is going to be in the same node as the old one.
3008 *
3009 * Note, the item being split must be smaller enough to live alone on
3010 * a tree block with room for one extra struct btrfs_item
3011 *
3012 * This allows us to split the item in place, keeping a lock on the
3013 * leaf the entire time.
3014 */
3015int btrfs_split_item(struct btrfs_trans_handle *trans,
3016 struct btrfs_root *root,
3017 struct btrfs_path *path,
3018 struct btrfs_key *new_key,
3019 unsigned long split_offset)
3020{
3021 u32 item_size;
3022 struct extent_buffer *leaf;
3023 struct btrfs_key orig_key;
3024 struct btrfs_item *item;
3025 struct btrfs_item *new_item;
3026 int ret = 0;
3027 int slot;
3028 u32 nritems;
3029 u32 orig_offset;
3030 struct btrfs_disk_key disk_key;
3031 char *buf;
3032
3033 leaf = path->nodes[0];
3034 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
3035 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
3036 goto split;
3037
3038 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3039 btrfs_release_path(root, path);
3040
3041 path->search_for_split = 1;
3042 path->keep_locks = 1;
3043
3044 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
3045 path->search_for_split = 0;
3046
3047 /* if our item isn't there or got smaller, return now */
3048 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
3049 path->slots[0])) {
3050 path->keep_locks = 0;
3051 return -EAGAIN;
3052 }
3053
Chris Masonb9473432009-03-13 11:00:37 -04003054 btrfs_set_path_blocking(path);
Yan Zheng87b29b22008-12-17 10:21:48 -05003055 ret = split_leaf(trans, root, &orig_key, path,
3056 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05003057 path->keep_locks = 0;
3058 BUG_ON(ret);
3059
Chris Masonb9473432009-03-13 11:00:37 -04003060 btrfs_unlock_up_safe(path, 1);
3061 leaf = path->nodes[0];
3062 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3063
3064split:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003065 /*
3066 * make sure any changes to the path from split_leaf leave it
3067 * in a blocking state
3068 */
3069 btrfs_set_path_blocking(path);
3070
Chris Mason459931e2008-12-10 09:10:46 -05003071 item = btrfs_item_nr(leaf, path->slots[0]);
3072 orig_offset = btrfs_item_offset(leaf, item);
3073 item_size = btrfs_item_size(leaf, item);
3074
Chris Mason459931e2008-12-10 09:10:46 -05003075 buf = kmalloc(item_size, GFP_NOFS);
3076 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3077 path->slots[0]), item_size);
3078 slot = path->slots[0] + 1;
3079 leaf = path->nodes[0];
3080
3081 nritems = btrfs_header_nritems(leaf);
3082
3083 if (slot != nritems) {
3084 /* shift the items */
3085 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3086 btrfs_item_nr_offset(slot),
3087 (nritems - slot) * sizeof(struct btrfs_item));
3088
3089 }
3090
3091 btrfs_cpu_key_to_disk(&disk_key, new_key);
3092 btrfs_set_item_key(leaf, &disk_key, slot);
3093
3094 new_item = btrfs_item_nr(leaf, slot);
3095
3096 btrfs_set_item_offset(leaf, new_item, orig_offset);
3097 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3098
3099 btrfs_set_item_offset(leaf, item,
3100 orig_offset + item_size - split_offset);
3101 btrfs_set_item_size(leaf, item, split_offset);
3102
3103 btrfs_set_header_nritems(leaf, nritems + 1);
3104
3105 /* write the data for the start of the original item */
3106 write_extent_buffer(leaf, buf,
3107 btrfs_item_ptr_offset(leaf, path->slots[0]),
3108 split_offset);
3109
3110 /* write the data for the new item */
3111 write_extent_buffer(leaf, buf + split_offset,
3112 btrfs_item_ptr_offset(leaf, slot),
3113 item_size - split_offset);
3114 btrfs_mark_buffer_dirty(leaf);
3115
3116 ret = 0;
3117 if (btrfs_leaf_free_space(root, leaf) < 0) {
3118 btrfs_print_leaf(root, leaf);
3119 BUG();
3120 }
3121 kfree(buf);
3122 return ret;
3123}
3124
3125/*
Chris Masond352ac62008-09-29 15:18:18 -04003126 * make the item pointed to by the path smaller. new_size indicates
3127 * how small to make it, and from_end tells us if we just chop bytes
3128 * off the end of the item or if we shift the item to chop bytes off
3129 * the front.
3130 */
Chris Masonb18c6682007-04-17 13:26:50 -04003131int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root,
3133 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003134 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003135{
3136 int ret = 0;
3137 int slot;
3138 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003139 struct extent_buffer *leaf;
3140 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003141 u32 nritems;
3142 unsigned int data_end;
3143 unsigned int old_data_start;
3144 unsigned int old_size;
3145 unsigned int size_diff;
3146 int i;
3147
3148 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003149 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003150 slot = path->slots[0];
3151
3152 old_size = btrfs_item_size_nr(leaf, slot);
3153 if (old_size == new_size)
3154 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003155
Chris Mason5f39d392007-10-15 16:14:19 -04003156 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003157 data_end = leaf_data_end(root, leaf);
3158
Chris Mason5f39d392007-10-15 16:14:19 -04003159 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003160
Chris Masonb18c6682007-04-17 13:26:50 -04003161 size_diff = old_size - new_size;
3162
3163 BUG_ON(slot < 0);
3164 BUG_ON(slot >= nritems);
3165
3166 /*
3167 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3168 */
3169 /* first correct the data pointers */
3170 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003171 u32 ioff;
3172 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003173
3174 if (!leaf->map_token) {
3175 map_extent_buffer(leaf, (unsigned long)item,
3176 sizeof(struct btrfs_item),
3177 &leaf->map_token, &leaf->kaddr,
3178 &leaf->map_start, &leaf->map_len,
3179 KM_USER1);
3180 }
3181
Chris Mason5f39d392007-10-15 16:14:19 -04003182 ioff = btrfs_item_offset(leaf, item);
3183 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003184 }
Chris Masondb945352007-10-15 16:15:53 -04003185
3186 if (leaf->map_token) {
3187 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3188 leaf->map_token = NULL;
3189 }
3190
Chris Masonb18c6682007-04-17 13:26:50 -04003191 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003192 if (from_end) {
3193 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3194 data_end + size_diff, btrfs_leaf_data(leaf) +
3195 data_end, old_data_start + new_size - data_end);
3196 } else {
3197 struct btrfs_disk_key disk_key;
3198 u64 offset;
3199
3200 btrfs_item_key(leaf, &disk_key, slot);
3201
3202 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3203 unsigned long ptr;
3204 struct btrfs_file_extent_item *fi;
3205
3206 fi = btrfs_item_ptr(leaf, slot,
3207 struct btrfs_file_extent_item);
3208 fi = (struct btrfs_file_extent_item *)(
3209 (unsigned long)fi - size_diff);
3210
3211 if (btrfs_file_extent_type(leaf, fi) ==
3212 BTRFS_FILE_EXTENT_INLINE) {
3213 ptr = btrfs_item_ptr_offset(leaf, slot);
3214 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003215 (unsigned long)fi,
3216 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003217 disk_bytenr));
3218 }
3219 }
3220
3221 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3222 data_end + size_diff, btrfs_leaf_data(leaf) +
3223 data_end, old_data_start - data_end);
3224
3225 offset = btrfs_disk_key_offset(&disk_key);
3226 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3227 btrfs_set_item_key(leaf, &disk_key, slot);
3228 if (slot == 0)
3229 fixup_low_keys(trans, root, path, &disk_key, 1);
3230 }
Chris Mason5f39d392007-10-15 16:14:19 -04003231
3232 item = btrfs_item_nr(leaf, slot);
3233 btrfs_set_item_size(leaf, item, new_size);
3234 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003235
3236 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003237 if (btrfs_leaf_free_space(root, leaf) < 0) {
3238 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003239 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003240 }
Chris Masonb18c6682007-04-17 13:26:50 -04003241 return ret;
3242}
3243
Chris Masond352ac62008-09-29 15:18:18 -04003244/*
3245 * make the item pointed to by the path bigger, data_size is the new size.
3246 */
Chris Mason5f39d392007-10-15 16:14:19 -04003247int btrfs_extend_item(struct btrfs_trans_handle *trans,
3248 struct btrfs_root *root, struct btrfs_path *path,
3249 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003250{
3251 int ret = 0;
3252 int slot;
3253 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003254 struct extent_buffer *leaf;
3255 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003256 u32 nritems;
3257 unsigned int data_end;
3258 unsigned int old_data;
3259 unsigned int old_size;
3260 int i;
3261
3262 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003263 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003264
Chris Mason5f39d392007-10-15 16:14:19 -04003265 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003266 data_end = leaf_data_end(root, leaf);
3267
Chris Mason5f39d392007-10-15 16:14:19 -04003268 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3269 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003270 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003271 }
Chris Mason6567e832007-04-16 09:22:45 -04003272 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003273 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003274
3275 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003276 if (slot >= nritems) {
3277 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003278 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3279 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003280 BUG_ON(1);
3281 }
Chris Mason6567e832007-04-16 09:22:45 -04003282
3283 /*
3284 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3285 */
3286 /* first correct the data pointers */
3287 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003288 u32 ioff;
3289 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003290
3291 if (!leaf->map_token) {
3292 map_extent_buffer(leaf, (unsigned long)item,
3293 sizeof(struct btrfs_item),
3294 &leaf->map_token, &leaf->kaddr,
3295 &leaf->map_start, &leaf->map_len,
3296 KM_USER1);
3297 }
Chris Mason5f39d392007-10-15 16:14:19 -04003298 ioff = btrfs_item_offset(leaf, item);
3299 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003300 }
Chris Mason5f39d392007-10-15 16:14:19 -04003301
Chris Masondb945352007-10-15 16:15:53 -04003302 if (leaf->map_token) {
3303 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3304 leaf->map_token = NULL;
3305 }
3306
Chris Mason6567e832007-04-16 09:22:45 -04003307 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003308 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003309 data_end - data_size, btrfs_leaf_data(leaf) +
3310 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003311
Chris Mason6567e832007-04-16 09:22:45 -04003312 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003313 old_size = btrfs_item_size_nr(leaf, slot);
3314 item = btrfs_item_nr(leaf, slot);
3315 btrfs_set_item_size(leaf, item, old_size + data_size);
3316 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003317
3318 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003319 if (btrfs_leaf_free_space(root, leaf) < 0) {
3320 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003321 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003322 }
Chris Mason6567e832007-04-16 09:22:45 -04003323 return ret;
3324}
3325
Chris Mason74123bd2007-02-02 11:05:29 -05003326/*
Chris Masond352ac62008-09-29 15:18:18 -04003327 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003328 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003329 * Returns the number of keys that were inserted.
3330 */
3331int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3332 struct btrfs_root *root,
3333 struct btrfs_path *path,
3334 struct btrfs_key *cpu_key, u32 *data_size,
3335 int nr)
3336{
3337 struct extent_buffer *leaf;
3338 struct btrfs_item *item;
3339 int ret = 0;
3340 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003341 int i;
3342 u32 nritems;
3343 u32 total_data = 0;
3344 u32 total_size = 0;
3345 unsigned int data_end;
3346 struct btrfs_disk_key disk_key;
3347 struct btrfs_key found_key;
3348
Yan Zheng87b29b22008-12-17 10:21:48 -05003349 for (i = 0; i < nr; i++) {
3350 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3351 BTRFS_LEAF_DATA_SIZE(root)) {
3352 break;
3353 nr = i;
3354 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003355 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003356 total_size += data_size[i] + sizeof(struct btrfs_item);
3357 }
3358 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003359
Josef Bacikf3465ca2008-11-12 14:19:50 -05003360 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3361 if (ret == 0)
3362 return -EEXIST;
3363 if (ret < 0)
3364 goto out;
3365
Josef Bacikf3465ca2008-11-12 14:19:50 -05003366 leaf = path->nodes[0];
3367
3368 nritems = btrfs_header_nritems(leaf);
3369 data_end = leaf_data_end(root, leaf);
3370
3371 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3372 for (i = nr; i >= 0; i--) {
3373 total_data -= data_size[i];
3374 total_size -= data_size[i] + sizeof(struct btrfs_item);
3375 if (total_size < btrfs_leaf_free_space(root, leaf))
3376 break;
3377 }
3378 nr = i;
3379 }
3380
3381 slot = path->slots[0];
3382 BUG_ON(slot < 0);
3383
3384 if (slot != nritems) {
3385 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3386
3387 item = btrfs_item_nr(leaf, slot);
3388 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3389
3390 /* figure out how many keys we can insert in here */
3391 total_data = data_size[0];
3392 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003393 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003394 break;
3395 total_data += data_size[i];
3396 }
3397 nr = i;
3398
3399 if (old_data < data_end) {
3400 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003401 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003402 slot, old_data, data_end);
3403 BUG_ON(1);
3404 }
3405 /*
3406 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3407 */
3408 /* first correct the data pointers */
3409 WARN_ON(leaf->map_token);
3410 for (i = slot; i < nritems; i++) {
3411 u32 ioff;
3412
3413 item = btrfs_item_nr(leaf, i);
3414 if (!leaf->map_token) {
3415 map_extent_buffer(leaf, (unsigned long)item,
3416 sizeof(struct btrfs_item),
3417 &leaf->map_token, &leaf->kaddr,
3418 &leaf->map_start, &leaf->map_len,
3419 KM_USER1);
3420 }
3421
3422 ioff = btrfs_item_offset(leaf, item);
3423 btrfs_set_item_offset(leaf, item, ioff - total_data);
3424 }
3425 if (leaf->map_token) {
3426 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3427 leaf->map_token = NULL;
3428 }
3429
3430 /* shift the items */
3431 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3432 btrfs_item_nr_offset(slot),
3433 (nritems - slot) * sizeof(struct btrfs_item));
3434
3435 /* shift the data */
3436 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3437 data_end - total_data, btrfs_leaf_data(leaf) +
3438 data_end, old_data - data_end);
3439 data_end = old_data;
3440 } else {
3441 /*
3442 * this sucks but it has to be done, if we are inserting at
3443 * the end of the leaf only insert 1 of the items, since we
3444 * have no way of knowing whats on the next leaf and we'd have
3445 * to drop our current locks to figure it out
3446 */
3447 nr = 1;
3448 }
3449
3450 /* setup the item for the new data */
3451 for (i = 0; i < nr; i++) {
3452 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3453 btrfs_set_item_key(leaf, &disk_key, slot + i);
3454 item = btrfs_item_nr(leaf, slot + i);
3455 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3456 data_end -= data_size[i];
3457 btrfs_set_item_size(leaf, item, data_size[i]);
3458 }
3459 btrfs_set_header_nritems(leaf, nritems + nr);
3460 btrfs_mark_buffer_dirty(leaf);
3461
3462 ret = 0;
3463 if (slot == 0) {
3464 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3465 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3466 }
3467
3468 if (btrfs_leaf_free_space(root, leaf) < 0) {
3469 btrfs_print_leaf(root, leaf);
3470 BUG();
3471 }
3472out:
3473 if (!ret)
3474 ret = nr;
3475 return ret;
3476}
3477
3478/*
Chris Mason44871b12009-03-13 10:04:31 -04003479 * this is a helper for btrfs_insert_empty_items, the main goal here is
3480 * to save stack depth by doing the bulk of the work in a function
3481 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003482 */
Chris Mason44871b12009-03-13 10:04:31 -04003483static noinline_for_stack int
3484setup_items_for_insert(struct btrfs_trans_handle *trans,
3485 struct btrfs_root *root, struct btrfs_path *path,
3486 struct btrfs_key *cpu_key, u32 *data_size,
3487 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003488{
Chris Mason5f39d392007-10-15 16:14:19 -04003489 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003490 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003491 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003492 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003493 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003494 int ret;
3495 struct extent_buffer *leaf;
3496 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003497
Chris Mason5f39d392007-10-15 16:14:19 -04003498 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003499 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003500
Chris Mason5f39d392007-10-15 16:14:19 -04003501 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003502 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003503
Chris Masonf25956c2008-09-12 15:32:53 -04003504 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003505 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003506 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003507 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003508 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003509 }
Chris Mason5f39d392007-10-15 16:14:19 -04003510
Chris Masonbe0e5c02007-01-26 15:51:26 -05003511 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003512 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003513
Chris Mason5f39d392007-10-15 16:14:19 -04003514 if (old_data < data_end) {
3515 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003516 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003517 slot, old_data, data_end);
3518 BUG_ON(1);
3519 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003520 /*
3521 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3522 */
3523 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003524 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003525 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003526 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003527
Chris Mason5f39d392007-10-15 16:14:19 -04003528 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003529 if (!leaf->map_token) {
3530 map_extent_buffer(leaf, (unsigned long)item,
3531 sizeof(struct btrfs_item),
3532 &leaf->map_token, &leaf->kaddr,
3533 &leaf->map_start, &leaf->map_len,
3534 KM_USER1);
3535 }
3536
Chris Mason5f39d392007-10-15 16:14:19 -04003537 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003538 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003539 }
Chris Masondb945352007-10-15 16:15:53 -04003540 if (leaf->map_token) {
3541 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3542 leaf->map_token = NULL;
3543 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003544
3545 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003546 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003547 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003548 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003549
3550 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003551 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003552 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003553 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003554 data_end = old_data;
3555 }
Chris Mason5f39d392007-10-15 16:14:19 -04003556
Chris Mason62e27492007-03-15 12:56:47 -04003557 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003558 for (i = 0; i < nr; i++) {
3559 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3560 btrfs_set_item_key(leaf, &disk_key, slot + i);
3561 item = btrfs_item_nr(leaf, slot + i);
3562 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3563 data_end -= data_size[i];
3564 btrfs_set_item_size(leaf, item, data_size[i]);
3565 }
Chris Mason44871b12009-03-13 10:04:31 -04003566
Chris Mason9c583092008-01-29 15:15:18 -05003567 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003568
3569 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003570 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003571 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003572 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003573 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003574 }
Chris Masonb9473432009-03-13 11:00:37 -04003575 btrfs_unlock_up_safe(path, 1);
3576 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003577
Chris Mason5f39d392007-10-15 16:14:19 -04003578 if (btrfs_leaf_free_space(root, leaf) < 0) {
3579 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003580 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003581 }
Chris Mason44871b12009-03-13 10:04:31 -04003582 return ret;
3583}
3584
3585/*
3586 * Given a key and some data, insert items into the tree.
3587 * This does all the path init required, making room in the tree if needed.
3588 */
3589int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3590 struct btrfs_root *root,
3591 struct btrfs_path *path,
3592 struct btrfs_key *cpu_key, u32 *data_size,
3593 int nr)
3594{
3595 struct extent_buffer *leaf;
3596 int ret = 0;
3597 int slot;
3598 int i;
3599 u32 total_size = 0;
3600 u32 total_data = 0;
3601
3602 for (i = 0; i < nr; i++)
3603 total_data += data_size[i];
3604
3605 total_size = total_data + (nr * sizeof(struct btrfs_item));
3606 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3607 if (ret == 0)
3608 return -EEXIST;
3609 if (ret < 0)
3610 goto out;
3611
3612 leaf = path->nodes[0];
3613 slot = path->slots[0];
3614 BUG_ON(slot < 0);
3615
3616 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3617 total_data, total_size, nr);
3618
Chris Masoned2ff2c2007-03-01 18:59:40 -05003619out:
Chris Mason62e27492007-03-15 12:56:47 -04003620 return ret;
3621}
3622
3623/*
3624 * Given a key and some data, insert an item into the tree.
3625 * This does all the path init required, making room in the tree if needed.
3626 */
Chris Masone089f052007-03-16 16:20:31 -04003627int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3628 *root, struct btrfs_key *cpu_key, void *data, u32
3629 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003630{
3631 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003632 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003633 struct extent_buffer *leaf;
3634 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003635
Chris Mason2c90e5d2007-04-02 10:50:19 -04003636 path = btrfs_alloc_path();
3637 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003638 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003639 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003640 leaf = path->nodes[0];
3641 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3642 write_extent_buffer(leaf, data, ptr, data_size);
3643 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003644 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003645 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003646 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003647}
3648
Chris Mason74123bd2007-02-02 11:05:29 -05003649/*
Chris Mason5de08d72007-02-24 06:24:44 -05003650 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003651 *
Chris Masond352ac62008-09-29 15:18:18 -04003652 * the tree should have been previously balanced so the deletion does not
3653 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003654 */
Chris Masone089f052007-03-16 16:20:31 -04003655static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3656 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003657{
Chris Mason5f39d392007-10-15 16:14:19 -04003658 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003659 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003660 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003661 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003662
Chris Mason5f39d392007-10-15 16:14:19 -04003663 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003664 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003665 memmove_extent_buffer(parent,
3666 btrfs_node_key_ptr_offset(slot),
3667 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003668 sizeof(struct btrfs_key_ptr) *
3669 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003670 }
Chris Mason7518a232007-03-12 12:01:18 -04003671 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003672 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003673 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003674 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003675 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003676 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003677 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003678 struct btrfs_disk_key disk_key;
3679
3680 btrfs_node_key(parent, &disk_key, 0);
3681 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003682 if (wret)
3683 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003684 }
Chris Masond6025572007-03-30 14:27:56 -04003685 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003686 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003687}
3688
Chris Mason74123bd2007-02-02 11:05:29 -05003689/*
Chris Mason323ac952008-10-01 19:05:46 -04003690 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003691 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003692 *
3693 * This deletes the pointer in path->nodes[1] and frees the leaf
3694 * block extent. zero is returned if it all worked out, < 0 otherwise.
3695 *
3696 * The path must have already been setup for deleting the leaf, including
3697 * all the proper balancing. path->nodes[1] must be locked.
3698 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003699static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3700 struct btrfs_root *root,
3701 struct btrfs_path *path,
3702 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003703{
3704 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003705
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003706 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003707 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3708 if (ret)
3709 return ret;
3710
Chris Mason4d081c42009-02-04 09:31:28 -05003711 /*
3712 * btrfs_free_extent is expensive, we want to make sure we
3713 * aren't holding any locks when we call it
3714 */
3715 btrfs_unlock_up_safe(path, 0);
3716
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003717 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
3718 0, root->root_key.objectid, 0, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003719 return ret;
3720}
3721/*
Chris Mason74123bd2007-02-02 11:05:29 -05003722 * delete the item at the leaf level in path. If that empties
3723 * the leaf, remove it from the tree
3724 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003725int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3726 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003727{
Chris Mason5f39d392007-10-15 16:14:19 -04003728 struct extent_buffer *leaf;
3729 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003730 int last_off;
3731 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003732 int ret = 0;
3733 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003734 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003735 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003736
Chris Mason5f39d392007-10-15 16:14:19 -04003737 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003738 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3739
3740 for (i = 0; i < nr; i++)
3741 dsize += btrfs_item_size_nr(leaf, slot + i);
3742
Chris Mason5f39d392007-10-15 16:14:19 -04003743 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003744
Chris Mason85e21ba2008-01-29 15:11:36 -05003745 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003746 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003747
3748 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003749 data_end + dsize,
3750 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003751 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003752
Chris Mason85e21ba2008-01-29 15:11:36 -05003753 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003754 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003755
Chris Mason5f39d392007-10-15 16:14:19 -04003756 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003757 if (!leaf->map_token) {
3758 map_extent_buffer(leaf, (unsigned long)item,
3759 sizeof(struct btrfs_item),
3760 &leaf->map_token, &leaf->kaddr,
3761 &leaf->map_start, &leaf->map_len,
3762 KM_USER1);
3763 }
Chris Mason5f39d392007-10-15 16:14:19 -04003764 ioff = btrfs_item_offset(leaf, item);
3765 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003766 }
Chris Masondb945352007-10-15 16:15:53 -04003767
3768 if (leaf->map_token) {
3769 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3770 leaf->map_token = NULL;
3771 }
3772
Chris Mason5f39d392007-10-15 16:14:19 -04003773 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003774 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003775 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003776 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003777 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003778 btrfs_set_header_nritems(leaf, nritems - nr);
3779 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003780
Chris Mason74123bd2007-02-02 11:05:29 -05003781 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003782 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003783 if (leaf == root->node) {
3784 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003785 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003786 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003787 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003788 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003789 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003790 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003791 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003792 struct btrfs_disk_key disk_key;
3793
3794 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003795 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003796 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003797 if (wret)
3798 ret = wret;
3799 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003800
Chris Mason74123bd2007-02-02 11:05:29 -05003801 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04003802 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003803 /* push_leaf_left fixes the path.
3804 * make sure the path still points to our leaf
3805 * for possible call to del_ptr below
3806 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003807 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003808 extent_buffer_get(leaf);
3809
Chris Masonb9473432009-03-13 11:00:37 -04003810 btrfs_set_path_blocking(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003811 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003812 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003813 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003814
3815 if (path->nodes[0] == leaf &&
3816 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003817 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003818 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003819 ret = wret;
3820 }
Chris Mason5f39d392007-10-15 16:14:19 -04003821
3822 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003823 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003824 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003825 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003826 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003827 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003828 /* if we're still in the path, make sure
3829 * we're dirty. Otherwise, one of the
3830 * push_leaf functions must have already
3831 * dirtied this buffer
3832 */
3833 if (path->nodes[0] == leaf)
3834 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003835 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003836 }
Chris Masond5719762007-03-23 10:01:08 -04003837 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003838 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003839 }
3840 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003841 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003842}
3843
Chris Mason97571fd2007-02-24 13:39:08 -05003844/*
Chris Mason925baed2008-06-25 16:01:30 -04003845 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003846 * returns 0 if it found something or 1 if there are no lesser leaves.
3847 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003848 *
3849 * This may release the path, and so you may lose any locks held at the
3850 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003851 */
3852int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3853{
Chris Mason925baed2008-06-25 16:01:30 -04003854 struct btrfs_key key;
3855 struct btrfs_disk_key found_key;
3856 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003857
Chris Mason925baed2008-06-25 16:01:30 -04003858 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003859
Chris Mason925baed2008-06-25 16:01:30 -04003860 if (key.offset > 0)
3861 key.offset--;
3862 else if (key.type > 0)
3863 key.type--;
3864 else if (key.objectid > 0)
3865 key.objectid--;
3866 else
3867 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003868
Chris Mason925baed2008-06-25 16:01:30 -04003869 btrfs_release_path(root, path);
3870 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3871 if (ret < 0)
3872 return ret;
3873 btrfs_item_key(path->nodes[0], &found_key, 0);
3874 ret = comp_keys(&found_key, &key);
3875 if (ret < 0)
3876 return 0;
3877 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003878}
3879
Chris Mason3f157a22008-06-25 16:01:31 -04003880/*
3881 * A helper function to walk down the tree starting at min_key, and looking
3882 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003883 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003884 *
3885 * This does not cow, but it does stuff the starting key it finds back
3886 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3887 * key and get a writable path.
3888 *
3889 * This does lock as it descends, and path->keep_locks should be set
3890 * to 1 by the caller.
3891 *
3892 * This honors path->lowest_level to prevent descent past a given level
3893 * of the tree.
3894 *
Chris Masond352ac62008-09-29 15:18:18 -04003895 * min_trans indicates the oldest transaction that you are interested
3896 * in walking through. Any nodes or leaves older than min_trans are
3897 * skipped over (without reading them).
3898 *
Chris Mason3f157a22008-06-25 16:01:31 -04003899 * returns zero if something useful was found, < 0 on error and 1 if there
3900 * was nothing in the tree that matched the search criteria.
3901 */
3902int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003903 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003904 struct btrfs_path *path, int cache_only,
3905 u64 min_trans)
3906{
3907 struct extent_buffer *cur;
3908 struct btrfs_key found_key;
3909 int slot;
Yan96524802008-07-24 12:19:49 -04003910 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003911 u32 nritems;
3912 int level;
3913 int ret = 1;
3914
Chris Mason934d3752008-12-08 16:43:10 -05003915 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003916again:
3917 cur = btrfs_lock_root_node(root);
3918 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003919 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003920 path->nodes[level] = cur;
3921 path->locks[level] = 1;
3922
3923 if (btrfs_header_generation(cur) < min_trans) {
3924 ret = 1;
3925 goto out;
3926 }
Chris Masond3977122009-01-05 21:25:51 -05003927 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003928 nritems = btrfs_header_nritems(cur);
3929 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003930 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003931
Chris Mason323ac952008-10-01 19:05:46 -04003932 /* at the lowest level, we're done, setup the path and exit */
3933 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003934 if (slot >= nritems)
3935 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003936 ret = 0;
3937 path->slots[level] = slot;
3938 btrfs_item_key_to_cpu(cur, &found_key, slot);
3939 goto out;
3940 }
Yan96524802008-07-24 12:19:49 -04003941 if (sret && slot > 0)
3942 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003943 /*
3944 * check this node pointer against the cache_only and
3945 * min_trans parameters. If it isn't in cache or is too
3946 * old, skip to the next one.
3947 */
Chris Masond3977122009-01-05 21:25:51 -05003948 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003949 u64 blockptr;
3950 u64 gen;
3951 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003952 struct btrfs_disk_key disk_key;
3953
Chris Mason3f157a22008-06-25 16:01:31 -04003954 blockptr = btrfs_node_blockptr(cur, slot);
3955 gen = btrfs_node_ptr_generation(cur, slot);
3956 if (gen < min_trans) {
3957 slot++;
3958 continue;
3959 }
3960 if (!cache_only)
3961 break;
3962
Chris Masone02119d2008-09-05 16:13:11 -04003963 if (max_key) {
3964 btrfs_node_key(cur, &disk_key, slot);
3965 if (comp_keys(&disk_key, max_key) >= 0) {
3966 ret = 1;
3967 goto out;
3968 }
3969 }
3970
Chris Mason3f157a22008-06-25 16:01:31 -04003971 tmp = btrfs_find_tree_block(root, blockptr,
3972 btrfs_level_size(root, level - 1));
3973
3974 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3975 free_extent_buffer(tmp);
3976 break;
3977 }
3978 if (tmp)
3979 free_extent_buffer(tmp);
3980 slot++;
3981 }
Chris Masone02119d2008-09-05 16:13:11 -04003982find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003983 /*
3984 * we didn't find a candidate key in this node, walk forward
3985 * and find another one
3986 */
3987 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003988 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003989 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003990 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003991 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003992 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003993 btrfs_release_path(root, path);
3994 goto again;
3995 } else {
3996 goto out;
3997 }
3998 }
3999 /* save our key for returning back */
4000 btrfs_node_key_to_cpu(cur, &found_key, slot);
4001 path->slots[level] = slot;
4002 if (level == path->lowest_level) {
4003 ret = 0;
4004 unlock_up(path, level, 1);
4005 goto out;
4006 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004007 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004008 cur = read_node_slot(root, cur, slot);
4009
4010 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004011
Chris Mason3f157a22008-06-25 16:01:31 -04004012 path->locks[level - 1] = 1;
4013 path->nodes[level - 1] = cur;
4014 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004015 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004016 }
4017out:
4018 if (ret == 0)
4019 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004020 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004021 return ret;
4022}
4023
4024/*
4025 * this is similar to btrfs_next_leaf, but does not try to preserve
4026 * and fixup the path. It looks for and returns the next key in the
4027 * tree based on the current path and the cache_only and min_trans
4028 * parameters.
4029 *
4030 * 0 is returned if another key is found, < 0 if there are any errors
4031 * and 1 is returned if there are no higher keys in the tree
4032 *
4033 * path->keep_locks should be set to 1 on the search made before
4034 * calling this function.
4035 */
Chris Masone7a84562008-06-25 16:01:31 -04004036int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004037 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004038 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004039{
Chris Masone7a84562008-06-25 16:01:31 -04004040 int slot;
4041 struct extent_buffer *c;
4042
Chris Mason934d3752008-12-08 16:43:10 -05004043 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004044 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004045 if (!path->nodes[level])
4046 return 1;
4047
4048 slot = path->slots[level] + 1;
4049 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004050next:
Chris Masone7a84562008-06-25 16:01:31 -04004051 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004052 int ret;
4053 int orig_lowest;
4054 struct btrfs_key cur_key;
4055 if (level + 1 >= BTRFS_MAX_LEVEL ||
4056 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004057 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004058
4059 if (path->locks[level + 1]) {
4060 level++;
4061 continue;
4062 }
4063
4064 slot = btrfs_header_nritems(c) - 1;
4065 if (level == 0)
4066 btrfs_item_key_to_cpu(c, &cur_key, slot);
4067 else
4068 btrfs_node_key_to_cpu(c, &cur_key, slot);
4069
4070 orig_lowest = path->lowest_level;
4071 btrfs_release_path(root, path);
4072 path->lowest_level = level;
4073 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4074 0, 0);
4075 path->lowest_level = orig_lowest;
4076 if (ret < 0)
4077 return ret;
4078
4079 c = path->nodes[level];
4080 slot = path->slots[level];
4081 if (ret == 0)
4082 slot++;
4083 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004084 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004085
Chris Masone7a84562008-06-25 16:01:31 -04004086 if (level == 0)
4087 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004088 else {
4089 u64 blockptr = btrfs_node_blockptr(c, slot);
4090 u64 gen = btrfs_node_ptr_generation(c, slot);
4091
4092 if (cache_only) {
4093 struct extent_buffer *cur;
4094 cur = btrfs_find_tree_block(root, blockptr,
4095 btrfs_level_size(root, level - 1));
4096 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4097 slot++;
4098 if (cur)
4099 free_extent_buffer(cur);
4100 goto next;
4101 }
4102 free_extent_buffer(cur);
4103 }
4104 if (gen < min_trans) {
4105 slot++;
4106 goto next;
4107 }
Chris Masone7a84562008-06-25 16:01:31 -04004108 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004109 }
Chris Masone7a84562008-06-25 16:01:31 -04004110 return 0;
4111 }
4112 return 1;
4113}
4114
Chris Mason7bb86312007-12-11 09:25:06 -05004115/*
Chris Mason925baed2008-06-25 16:01:30 -04004116 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004117 * returns 0 if it found something or 1 if there are no greater leaves.
4118 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004119 */
Chris Mason234b63a2007-03-13 10:46:10 -04004120int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004121{
4122 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004123 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004124 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004125 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004126 struct btrfs_key key;
4127 u32 nritems;
4128 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004129 int old_spinning = path->leave_spinning;
4130 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004131
4132 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004133 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004134 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004135
Chris Mason8e73f272009-04-03 10:14:18 -04004136 /*
4137 * we take the blocks in an order that upsets lockdep. Using
4138 * blocking mode is the only way around it.
4139 */
4140#ifdef CONFIG_DEBUG_LOCK_ALLOC
4141 force_blocking = 1;
4142#endif
Chris Mason925baed2008-06-25 16:01:30 -04004143
Chris Mason8e73f272009-04-03 10:14:18 -04004144 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4145again:
4146 level = 1;
4147 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004148 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004149
Chris Masona2135012008-06-25 16:01:30 -04004150 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004151
4152 if (!force_blocking)
4153 path->leave_spinning = 1;
4154
Chris Mason925baed2008-06-25 16:01:30 -04004155 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4156 path->keep_locks = 0;
4157
4158 if (ret < 0)
4159 return ret;
4160
Chris Masona2135012008-06-25 16:01:30 -04004161 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004162 /*
4163 * by releasing the path above we dropped all our locks. A balance
4164 * could have added more items next to the key that used to be
4165 * at the very end of the block. So, check again here and
4166 * advance the path if there are now more items available.
4167 */
Chris Masona2135012008-06-25 16:01:30 -04004168 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004169 if (ret == 0)
4170 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004171 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004172 goto done;
4173 }
Chris Masond97e63b2007-02-20 16:40:44 -05004174
Chris Masond3977122009-01-05 21:25:51 -05004175 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004176 if (!path->nodes[level]) {
4177 ret = 1;
4178 goto done;
4179 }
Chris Mason5f39d392007-10-15 16:14:19 -04004180
Chris Masond97e63b2007-02-20 16:40:44 -05004181 slot = path->slots[level] + 1;
4182 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004183 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004184 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004185 if (level == BTRFS_MAX_LEVEL) {
4186 ret = 1;
4187 goto done;
4188 }
Chris Masond97e63b2007-02-20 16:40:44 -05004189 continue;
4190 }
Chris Mason5f39d392007-10-15 16:14:19 -04004191
Chris Mason925baed2008-06-25 16:01:30 -04004192 if (next) {
4193 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004194 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004195 }
Chris Mason5f39d392007-10-15 16:14:19 -04004196
Chris Mason8e73f272009-04-03 10:14:18 -04004197 next = c;
4198 ret = read_block_for_search(NULL, root, path, &next, level,
4199 slot, &key);
4200 if (ret == -EAGAIN)
4201 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004202
Chris Mason76a05b32009-05-14 13:24:30 -04004203 if (ret < 0) {
4204 btrfs_release_path(root, path);
4205 goto done;
4206 }
4207
Chris Mason5cd57b22008-06-25 16:01:30 -04004208 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004209 ret = btrfs_try_spin_lock(next);
4210 if (!ret) {
4211 btrfs_set_path_blocking(path);
4212 btrfs_tree_lock(next);
4213 if (!force_blocking)
4214 btrfs_clear_path_blocking(path, next);
4215 }
4216 if (force_blocking)
4217 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004218 }
Chris Masond97e63b2007-02-20 16:40:44 -05004219 break;
4220 }
4221 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004222 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004223 level--;
4224 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004225 if (path->locks[level])
4226 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004227
Chris Mason5f39d392007-10-15 16:14:19 -04004228 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004229 path->nodes[level] = next;
4230 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004231 if (!path->skip_locking)
4232 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004233
Chris Masond97e63b2007-02-20 16:40:44 -05004234 if (!level)
4235 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004236
Chris Mason8e73f272009-04-03 10:14:18 -04004237 ret = read_block_for_search(NULL, root, path, &next, level,
4238 0, &key);
4239 if (ret == -EAGAIN)
4240 goto again;
4241
Chris Mason76a05b32009-05-14 13:24:30 -04004242 if (ret < 0) {
4243 btrfs_release_path(root, path);
4244 goto done;
4245 }
4246
Chris Mason5cd57b22008-06-25 16:01:30 -04004247 if (!path->skip_locking) {
Chris Masonb9447ef2009-03-09 11:45:38 -04004248 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004249 ret = btrfs_try_spin_lock(next);
4250 if (!ret) {
4251 btrfs_set_path_blocking(path);
4252 btrfs_tree_lock(next);
4253 if (!force_blocking)
4254 btrfs_clear_path_blocking(path, next);
4255 }
4256 if (force_blocking)
4257 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004258 }
Chris Masond97e63b2007-02-20 16:40:44 -05004259 }
Chris Mason8e73f272009-04-03 10:14:18 -04004260 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004261done:
4262 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004263 path->leave_spinning = old_spinning;
4264 if (!old_spinning)
4265 btrfs_set_path_blocking(path);
4266
4267 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004268}
Chris Mason0b86a832008-03-24 15:01:56 -04004269
Chris Mason3f157a22008-06-25 16:01:31 -04004270/*
4271 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4272 * searching until it gets past min_objectid or finds an item of 'type'
4273 *
4274 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4275 */
Chris Mason0b86a832008-03-24 15:01:56 -04004276int btrfs_previous_item(struct btrfs_root *root,
4277 struct btrfs_path *path, u64 min_objectid,
4278 int type)
4279{
4280 struct btrfs_key found_key;
4281 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004282 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004283 int ret;
4284
Chris Masond3977122009-01-05 21:25:51 -05004285 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004286 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004287 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004288 ret = btrfs_prev_leaf(root, path);
4289 if (ret != 0)
4290 return ret;
4291 } else {
4292 path->slots[0]--;
4293 }
4294 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004295 nritems = btrfs_header_nritems(leaf);
4296 if (nritems == 0)
4297 return 1;
4298 if (path->slots[0] == nritems)
4299 path->slots[0]--;
4300
Chris Mason0b86a832008-03-24 15:01:56 -04004301 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004302 if (found_key.objectid < min_objectid)
4303 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004304 if (found_key.type == type)
4305 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004306 if (found_key.objectid == min_objectid &&
4307 found_key.type < type)
4308 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004309 }
4310 return 1;
4311}