blob: fdd423a550d64c2534edd1d90813f2d5be569ea0 [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 Masonb9447ef82009-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
560 if (k1.objectid > k2->objectid)
561 return 1;
562 if (k1.objectid < k2->objectid)
563 return -1;
564 if (k1.type > k2->type)
565 return 1;
566 if (k1.type < k2->type)
567 return -1;
568 if (k1.offset > k2->offset)
569 return 1;
570 if (k1.offset < k2->offset)
571 return -1;
572 return 0;
573}
574
Josef Bacikf3465ca2008-11-12 14:19:50 -0500575/*
576 * same as comp_keys only with two btrfs_key's
577 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400578int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500579{
580 if (k1->objectid > k2->objectid)
581 return 1;
582 if (k1->objectid < k2->objectid)
583 return -1;
584 if (k1->type > k2->type)
585 return 1;
586 if (k1->type < k2->type)
587 return -1;
588 if (k1->offset > k2->offset)
589 return 1;
590 if (k1->offset < k2->offset)
591 return -1;
592 return 0;
593}
Chris Mason081e9572007-11-06 10:26:24 -0500594
Chris Masond352ac62008-09-29 15:18:18 -0400595/*
596 * this is used by the defrag code to go through all the
597 * leaves pointed to by a node and reallocate them so that
598 * disk order is close to key order
599 */
Chris Mason6702ed42007-08-07 16:15:09 -0400600int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400601 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400602 int start_slot, int cache_only, u64 *last_ret,
603 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400604{
Chris Mason6b800532007-10-15 16:17:34 -0400605 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400606 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400607 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400608 u64 search_start = *last_ret;
609 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400610 u64 other;
611 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400612 int end_slot;
613 int i;
614 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400615 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400616 int uptodate;
617 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500618 int progress_passed = 0;
619 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400620
Chris Mason5708b952007-10-25 15:43:18 -0400621 parent_level = btrfs_header_level(parent);
622 if (cache_only && parent_level != 1)
623 return 0;
624
Chris Masond3977122009-01-05 21:25:51 -0500625 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400626 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500627 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400628 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400629
Chris Mason6b800532007-10-15 16:17:34 -0400630 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400631 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400632 end_slot = parent_nritems;
633
634 if (parent_nritems == 1)
635 return 0;
636
Chris Masonb4ce94d2009-02-04 09:25:08 -0500637 btrfs_set_lock_blocking(parent);
638
Chris Mason6702ed42007-08-07 16:15:09 -0400639 for (i = start_slot; i < end_slot; i++) {
640 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400641
Chris Mason5708b952007-10-25 15:43:18 -0400642 if (!parent->map_token) {
643 map_extent_buffer(parent,
644 btrfs_node_key_ptr_offset(i),
645 sizeof(struct btrfs_key_ptr),
646 &parent->map_token, &parent->kaddr,
647 &parent->map_start, &parent->map_len,
648 KM_USER1);
649 }
Chris Mason081e9572007-11-06 10:26:24 -0500650 btrfs_node_key(parent, &disk_key, i);
651 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
652 continue;
653
654 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400655 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400656 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400657 if (last_block == 0)
658 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400659
Chris Mason6702ed42007-08-07 16:15:09 -0400660 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400661 other = btrfs_node_blockptr(parent, i - 1);
662 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400663 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400664 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400665 other = btrfs_node_blockptr(parent, i + 1);
666 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400667 }
Chris Masone9d0b132007-08-10 14:06:19 -0400668 if (close) {
669 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400670 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400671 }
Chris Mason5708b952007-10-25 15:43:18 -0400672 if (parent->map_token) {
673 unmap_extent_buffer(parent, parent->map_token,
674 KM_USER1);
675 parent->map_token = NULL;
676 }
Chris Mason6702ed42007-08-07 16:15:09 -0400677
Chris Mason6b800532007-10-15 16:17:34 -0400678 cur = btrfs_find_tree_block(root, blocknr, blocksize);
679 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400680 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400681 else
682 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400683 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400684 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400685 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400686 continue;
687 }
Chris Mason6b800532007-10-15 16:17:34 -0400688 if (!cur) {
689 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400690 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400691 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400692 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400693 }
Chris Mason6702ed42007-08-07 16:15:09 -0400694 }
Chris Masone9d0b132007-08-10 14:06:19 -0400695 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400696 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400697
Chris Masone7a84562008-06-25 16:01:31 -0400698 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500699 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400700 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400701 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400702 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400703 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400704 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400705 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400706 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400707 break;
Yan252c38f2007-08-29 09:11:44 -0400708 }
Chris Masone7a84562008-06-25 16:01:31 -0400709 search_start = cur->start;
710 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400711 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400712 btrfs_tree_unlock(cur);
713 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400714 }
Chris Mason5708b952007-10-25 15:43:18 -0400715 if (parent->map_token) {
716 unmap_extent_buffer(parent, parent->map_token,
717 KM_USER1);
718 parent->map_token = NULL;
719 }
Chris Mason6702ed42007-08-07 16:15:09 -0400720 return err;
721}
722
Chris Mason74123bd2007-02-02 11:05:29 -0500723/*
724 * The leaf data grows from end-to-front in the node.
725 * this returns the address of the start of the last item,
726 * which is the stop of the leaf data stack
727 */
Chris Mason123abc82007-03-14 14:14:43 -0400728static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400729 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500730{
Chris Mason5f39d392007-10-15 16:14:19 -0400731 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500732 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400733 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400734 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500735}
736
Chris Masond352ac62008-09-29 15:18:18 -0400737/*
738 * extra debugging checks to make sure all the items in a key are
739 * well formed and in the proper order
740 */
Chris Mason123abc82007-03-14 14:14:43 -0400741static int check_node(struct btrfs_root *root, struct btrfs_path *path,
742 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500743{
Chris Mason5f39d392007-10-15 16:14:19 -0400744 struct extent_buffer *parent = NULL;
745 struct extent_buffer *node = path->nodes[level];
746 struct btrfs_disk_key parent_key;
747 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500748 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400749 int slot;
750 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400751 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500752
753 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400754 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400755
Chris Mason8d7be552007-05-10 11:24:42 -0400756 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400757 BUG_ON(nritems == 0);
758 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400759 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400760 btrfs_node_key(parent, &parent_key, parent_slot);
761 btrfs_node_key(node, &node_key, 0);
762 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400763 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400764 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400765 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500766 }
Chris Mason123abc82007-03-14 14:14:43 -0400767 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400768 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400769 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
770 btrfs_node_key(node, &node_key, slot);
771 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400772 }
773 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400774 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
775 btrfs_node_key(node, &node_key, slot);
776 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500777 }
778 return 0;
779}
780
Chris Masond352ac62008-09-29 15:18:18 -0400781/*
782 * extra checking to make sure all the items in a leaf are
783 * well formed and in the proper order
784 */
Chris Mason123abc82007-03-14 14:14:43 -0400785static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
786 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500787{
Chris Mason5f39d392007-10-15 16:14:19 -0400788 struct extent_buffer *leaf = path->nodes[level];
789 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500790 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400791 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400792 struct btrfs_disk_key parent_key;
793 struct btrfs_disk_key leaf_key;
794 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400795
Chris Mason5f39d392007-10-15 16:14:19 -0400796 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500797
798 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400799 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400800
801 if (nritems == 0)
802 return 0;
803
804 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400805 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400806 btrfs_node_key(parent, &parent_key, parent_slot);
807 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400808
Chris Mason5f39d392007-10-15 16:14:19 -0400809 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400810 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400811 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400812 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500813 }
Chris Mason5f39d392007-10-15 16:14:19 -0400814 if (slot != 0 && slot < nritems - 1) {
815 btrfs_item_key(leaf, &leaf_key, slot);
816 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
817 if (comp_keys(&leaf_key, &cpukey) <= 0) {
818 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500819 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400820 BUG_ON(1);
821 }
822 if (btrfs_item_offset_nr(leaf, slot - 1) !=
823 btrfs_item_end_nr(leaf, slot)) {
824 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500825 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400826 BUG_ON(1);
827 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500828 }
Chris Mason8d7be552007-05-10 11:24:42 -0400829 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400830 btrfs_item_key(leaf, &leaf_key, slot);
831 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
832 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
833 if (btrfs_item_offset_nr(leaf, slot) !=
834 btrfs_item_end_nr(leaf, slot + 1)) {
835 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500836 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400837 BUG_ON(1);
838 }
Chris Mason8d7be552007-05-10 11:24:42 -0400839 }
Chris Mason5f39d392007-10-15 16:14:19 -0400840 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
841 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500842 return 0;
843}
844
Chris Masond3977122009-01-05 21:25:51 -0500845static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500846 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500847{
Chris Mason85d824c2008-04-10 10:23:19 -0400848 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500849 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400850 return check_leaf(root, path, level);
851 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500852}
853
Chris Mason74123bd2007-02-02 11:05:29 -0500854/*
Chris Mason5f39d392007-10-15 16:14:19 -0400855 * search for key in the extent_buffer. The items start at offset p,
856 * and they are item_size apart. There are 'max' items in p.
857 *
Chris Mason74123bd2007-02-02 11:05:29 -0500858 * the slot in the array is returned via slot, and it points to
859 * the place where you would insert key if it is not found in
860 * the array.
861 *
862 * slot may point to max if the key is bigger than all of the keys
863 */
Chris Masone02119d2008-09-05 16:13:11 -0400864static noinline int generic_bin_search(struct extent_buffer *eb,
865 unsigned long p,
866 int item_size, struct btrfs_key *key,
867 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500868{
869 int low = 0;
870 int high = max;
871 int mid;
872 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400873 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400874 struct btrfs_disk_key unaligned;
875 unsigned long offset;
876 char *map_token = NULL;
877 char *kaddr = NULL;
878 unsigned long map_start = 0;
879 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400880 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500881
Chris Masond3977122009-01-05 21:25:51 -0500882 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500883 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400884 offset = p + mid * item_size;
885
886 if (!map_token || offset < map_start ||
887 (offset + sizeof(struct btrfs_disk_key)) >
888 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400889 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400890 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400891 map_token = NULL;
892 }
Chris Mason934d3752008-12-08 16:43:10 -0500893
894 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400895 sizeof(struct btrfs_disk_key),
896 &map_token, &kaddr,
897 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400898
Chris Mason479965d2007-10-15 16:14:27 -0400899 if (!err) {
900 tmp = (struct btrfs_disk_key *)(kaddr + offset -
901 map_start);
902 } else {
903 read_extent_buffer(eb, &unaligned,
904 offset, sizeof(unaligned));
905 tmp = &unaligned;
906 }
907
Chris Mason5f39d392007-10-15 16:14:19 -0400908 } else {
909 tmp = (struct btrfs_disk_key *)(kaddr + offset -
910 map_start);
911 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500912 ret = comp_keys(tmp, key);
913
914 if (ret < 0)
915 low = mid + 1;
916 else if (ret > 0)
917 high = mid;
918 else {
919 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400920 if (map_token)
921 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500922 return 0;
923 }
924 }
925 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400926 if (map_token)
927 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500928 return 1;
929}
930
Chris Mason97571fd2007-02-24 13:39:08 -0500931/*
932 * simple bin_search frontend that does the right thing for
933 * leaves vs nodes
934 */
Chris Mason5f39d392007-10-15 16:14:19 -0400935static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
936 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500937{
Chris Mason5f39d392007-10-15 16:14:19 -0400938 if (level == 0) {
939 return generic_bin_search(eb,
940 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400941 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400942 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400943 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500944 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400945 return generic_bin_search(eb,
946 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400947 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400948 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400949 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500950 }
951 return -1;
952}
953
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400954int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
955 int level, int *slot)
956{
957 return bin_search(eb, key, level, slot);
958}
959
Chris Masond352ac62008-09-29 15:18:18 -0400960/* given a node and slot number, this reads the blocks it points to. The
961 * extent buffer is returned with a reference taken (but unlocked).
962 * NULL is returned on error.
963 */
Chris Masone02119d2008-09-05 16:13:11 -0400964static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400965 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500966{
Chris Masonca7a79a2008-05-12 12:59:19 -0400967 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500968 if (slot < 0)
969 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400970 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500971 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400972
973 BUG_ON(level == 0);
974
Chris Masondb945352007-10-15 16:15:53 -0400975 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400976 btrfs_level_size(root, level - 1),
977 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500978}
979
Chris Masond352ac62008-09-29 15:18:18 -0400980/*
981 * node level balancing, used to make sure nodes are in proper order for
982 * item deletion. We balance from the top down, so we have to make sure
983 * that a deletion won't leave an node completely empty later on.
984 */
Chris Masone02119d2008-09-05 16:13:11 -0400985static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500986 struct btrfs_root *root,
987 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500988{
Chris Mason5f39d392007-10-15 16:14:19 -0400989 struct extent_buffer *right = NULL;
990 struct extent_buffer *mid;
991 struct extent_buffer *left = NULL;
992 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500993 int ret = 0;
994 int wret;
995 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500996 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400997 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500998 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500999
1000 if (level == 0)
1001 return 0;
1002
Chris Mason5f39d392007-10-15 16:14:19 -04001003 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001004
Chris Mason925baed2008-06-25 16:01:30 -04001005 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -05001006 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1007
Chris Mason1d4f8a02007-03-13 09:28:32 -04001008 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001009
Chris Mason234b63a2007-03-13 10:46:10 -04001010 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001011 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001012 pslot = path->slots[level + 1];
1013
Chris Mason40689472007-03-17 14:29:23 -04001014 /*
1015 * deal with the case where there is only one pointer in the root
1016 * by promoting the node below to a root
1017 */
Chris Mason5f39d392007-10-15 16:14:19 -04001018 if (!parent) {
1019 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001020
Chris Mason5f39d392007-10-15 16:14:19 -04001021 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001022 return 0;
1023
1024 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001025 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001026 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001027 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001028 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001029 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan2f375ab2008-02-01 14:58:07 -05001030 BUG_ON(ret);
1031
Chris Mason925baed2008-06-25 16:01:30 -04001032 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001033 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001034 spin_unlock(&root->node_lock);
1035
Chris Mason0b86a832008-03-24 15:01:56 -04001036 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001037 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001038
Chris Mason925baed2008-06-25 16:01:30 -04001039 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001040 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001041 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001042 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001043 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001044 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -05001045 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001046 0, root->root_key.objectid, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001047 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001048 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -04001049 return ret;
Chris Masonbb803952007-03-01 12:04:21 -05001050 }
Chris Mason5f39d392007-10-15 16:14:19 -04001051 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001052 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001053 return 0;
1054
Chris Masonb3612422009-05-13 19:12:15 -04001055 if (btrfs_header_nritems(mid) > 2)
Chris Masona4b6e072009-03-16 10:59:57 -04001056 return 0;
1057
Chris Mason5f39d392007-10-15 16:14:19 -04001058 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001059 err_on_enospc = 1;
1060
Chris Mason5f39d392007-10-15 16:14:19 -04001061 left = read_node_slot(root, parent, pslot - 1);
1062 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001063 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001064 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001065 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001066 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001067 if (wret) {
1068 ret = wret;
1069 goto enospc;
1070 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001071 }
Chris Mason5f39d392007-10-15 16:14:19 -04001072 right = read_node_slot(root, parent, pslot + 1);
1073 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001074 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001075 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001076 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001077 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001078 if (wret) {
1079 ret = wret;
1080 goto enospc;
1081 }
1082 }
1083
1084 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001085 if (left) {
1086 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001087 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001088 if (wret < 0)
1089 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001090 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001091 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001092 }
Chris Mason79f95c82007-03-01 15:16:26 -05001093
1094 /*
1095 * then try to empty the right most buffer into the middle
1096 */
Chris Mason5f39d392007-10-15 16:14:19 -04001097 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001098 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001099 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001100 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001101 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001102 u64 bytenr = right->start;
1103 u32 blocksize = right->len;
1104
Chris Mason5f39d392007-10-15 16:14:19 -04001105 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001106 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001107 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001108 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001109 wret = del_ptr(trans, root, path, level + 1, pslot +
1110 1);
Chris Masonbb803952007-03-01 12:04:21 -05001111 if (wret)
1112 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001113 wret = btrfs_free_extent(trans, root, bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001114 blocksize, 0,
1115 root->root_key.objectid,
1116 level, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001117 if (wret)
1118 ret = wret;
1119 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001120 struct btrfs_disk_key right_key;
1121 btrfs_node_key(right, &right_key, 0);
1122 btrfs_set_node_key(parent, &right_key, pslot + 1);
1123 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001124 }
1125 }
Chris Mason5f39d392007-10-15 16:14:19 -04001126 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001127 /*
1128 * we're not allowed to leave a node with one item in the
1129 * tree during a delete. A deletion from lower in the tree
1130 * could try to delete the only pointer in this node.
1131 * So, pull some keys from the left.
1132 * There has to be a left pointer at this point because
1133 * otherwise we would have pulled some pointers from the
1134 * right
1135 */
Chris Mason5f39d392007-10-15 16:14:19 -04001136 BUG_ON(!left);
1137 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001138 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001139 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001140 goto enospc;
1141 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001142 if (wret == 1) {
1143 wret = push_node_left(trans, root, left, mid, 1);
1144 if (wret < 0)
1145 ret = wret;
1146 }
Chris Mason79f95c82007-03-01 15:16:26 -05001147 BUG_ON(wret == 1);
1148 }
Chris Mason5f39d392007-10-15 16:14:19 -04001149 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001150 /* we've managed to empty the middle node, drop it */
Chris Masondb945352007-10-15 16:15:53 -04001151 u64 bytenr = mid->start;
1152 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001153
Chris Mason5f39d392007-10-15 16:14:19 -04001154 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001155 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001156 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001157 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001158 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001159 if (wret)
1160 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001161 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001162 0, root->root_key.objectid,
1163 level, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001164 if (wret)
1165 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001166 } else {
1167 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001168 struct btrfs_disk_key mid_key;
1169 btrfs_node_key(mid, &mid_key, 0);
1170 btrfs_set_node_key(parent, &mid_key, pslot);
1171 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001172 }
Chris Masonbb803952007-03-01 12:04:21 -05001173
Chris Mason79f95c82007-03-01 15:16:26 -05001174 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001175 if (left) {
1176 if (btrfs_header_nritems(left) > orig_slot) {
1177 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001178 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001179 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001180 path->slots[level + 1] -= 1;
1181 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001182 if (mid) {
1183 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001184 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001185 }
Chris Masonbb803952007-03-01 12:04:21 -05001186 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001187 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001188 path->slots[level] = orig_slot;
1189 }
1190 }
Chris Mason79f95c82007-03-01 15:16:26 -05001191 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001192 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001193 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001194 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001195 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001196enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001197 if (right) {
1198 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001199 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001200 }
1201 if (left) {
1202 if (path->nodes[level] != left)
1203 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001204 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001205 }
Chris Masonbb803952007-03-01 12:04:21 -05001206 return ret;
1207}
1208
Chris Masond352ac62008-09-29 15:18:18 -04001209/* Node balancing for insertion. Here we only split or push nodes around
1210 * when they are completely full. This is also done top down, so we
1211 * have to be pessimistic.
1212 */
Chris Masond3977122009-01-05 21:25:51 -05001213static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001214 struct btrfs_root *root,
1215 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001216{
Chris Mason5f39d392007-10-15 16:14:19 -04001217 struct extent_buffer *right = NULL;
1218 struct extent_buffer *mid;
1219 struct extent_buffer *left = NULL;
1220 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001221 int ret = 0;
1222 int wret;
1223 int pslot;
1224 int orig_slot = path->slots[level];
1225 u64 orig_ptr;
1226
1227 if (level == 0)
1228 return 1;
1229
Chris Mason5f39d392007-10-15 16:14:19 -04001230 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001231 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001232 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1233
1234 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001235 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001236 pslot = path->slots[level + 1];
1237
Chris Mason5f39d392007-10-15 16:14:19 -04001238 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001239 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001240
Chris Mason5f39d392007-10-15 16:14:19 -04001241 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001242
1243 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001244 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001245 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001246
1247 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001248 btrfs_set_lock_blocking(left);
1249
Chris Mason5f39d392007-10-15 16:14:19 -04001250 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001251 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1252 wret = 1;
1253 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001254 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001255 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001256 if (ret)
1257 wret = 1;
1258 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001259 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001260 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001261 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001262 }
Chris Masone66f7092007-04-20 13:16:02 -04001263 if (wret < 0)
1264 ret = wret;
1265 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001266 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001267 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001268 btrfs_node_key(mid, &disk_key, 0);
1269 btrfs_set_node_key(parent, &disk_key, pslot);
1270 btrfs_mark_buffer_dirty(parent);
1271 if (btrfs_header_nritems(left) > orig_slot) {
1272 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001273 path->slots[level + 1] -= 1;
1274 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001275 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001276 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001277 } else {
1278 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001279 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001280 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001281 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001282 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001283 }
Chris Masone66f7092007-04-20 13:16:02 -04001284 return 0;
1285 }
Chris Mason925baed2008-06-25 16:01:30 -04001286 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001287 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001288 }
Chris Mason925baed2008-06-25 16:01:30 -04001289 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001290
1291 /*
1292 * then try to empty the right most buffer into the middle
1293 */
Chris Mason5f39d392007-10-15 16:14:19 -04001294 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001295 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001296
Chris Mason925baed2008-06-25 16:01:30 -04001297 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001298 btrfs_set_lock_blocking(right);
1299
Chris Mason5f39d392007-10-15 16:14:19 -04001300 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001301 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1302 wret = 1;
1303 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001304 ret = btrfs_cow_block(trans, root, right,
1305 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001306 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001307 if (ret)
1308 wret = 1;
1309 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001310 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001311 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001312 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001313 }
Chris Masone66f7092007-04-20 13:16:02 -04001314 if (wret < 0)
1315 ret = wret;
1316 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001317 struct btrfs_disk_key disk_key;
1318
1319 btrfs_node_key(right, &disk_key, 0);
1320 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1321 btrfs_mark_buffer_dirty(parent);
1322
1323 if (btrfs_header_nritems(mid) <= orig_slot) {
1324 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001325 path->slots[level + 1] += 1;
1326 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001327 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001328 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001329 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001330 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001331 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001332 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001333 }
Chris Masone66f7092007-04-20 13:16:02 -04001334 return 0;
1335 }
Chris Mason925baed2008-06-25 16:01:30 -04001336 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001337 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001338 }
Chris Masone66f7092007-04-20 13:16:02 -04001339 return 1;
1340}
1341
Chris Mason74123bd2007-02-02 11:05:29 -05001342/*
Chris Masond352ac62008-09-29 15:18:18 -04001343 * readahead one full node of leaves, finding things that are close
1344 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001345 */
Chris Masonc8c42862009-04-03 10:14:18 -04001346static void reada_for_search(struct btrfs_root *root,
1347 struct btrfs_path *path,
1348 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001349{
Chris Mason5f39d392007-10-15 16:14:19 -04001350 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001351 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001352 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001353 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001354 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001355 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001356 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001357 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001358 u32 nr;
1359 u32 blocksize;
1360 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001361
Chris Masona6b6e752007-10-15 16:22:39 -04001362 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001363 return;
1364
Chris Mason6702ed42007-08-07 16:15:09 -04001365 if (!path->nodes[level])
1366 return;
1367
Chris Mason5f39d392007-10-15 16:14:19 -04001368 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001369
Chris Mason3c69fae2007-08-07 15:52:22 -04001370 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001371 blocksize = btrfs_level_size(root, level - 1);
1372 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001373 if (eb) {
1374 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001375 return;
1376 }
1377
Chris Masona7175312009-01-22 09:23:10 -05001378 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001379
Chris Mason5f39d392007-10-15 16:14:19 -04001380 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001381 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001382 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001383 if (direction < 0) {
1384 if (nr == 0)
1385 break;
1386 nr--;
1387 } else if (direction > 0) {
1388 nr++;
1389 if (nr >= nritems)
1390 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001391 }
Chris Mason01f46652007-12-21 16:24:26 -05001392 if (path->reada < 0 && objectid) {
1393 btrfs_node_key(node, &disk_key, nr);
1394 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1395 break;
1396 }
Chris Mason6b800532007-10-15 16:17:34 -04001397 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001398 if ((search <= target && target - search <= 65536) ||
1399 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001400 readahead_tree_block(root, search, blocksize,
1401 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001402 nread += blocksize;
1403 }
1404 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001405 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001406 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001407 }
1408}
Chris Mason925baed2008-06-25 16:01:30 -04001409
Chris Masond352ac62008-09-29 15:18:18 -04001410/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001411 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1412 * cache
1413 */
1414static noinline int reada_for_balance(struct btrfs_root *root,
1415 struct btrfs_path *path, int level)
1416{
1417 int slot;
1418 int nritems;
1419 struct extent_buffer *parent;
1420 struct extent_buffer *eb;
1421 u64 gen;
1422 u64 block1 = 0;
1423 u64 block2 = 0;
1424 int ret = 0;
1425 int blocksize;
1426
Chris Mason8c594ea2009-04-20 15:50:10 -04001427 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001428 if (!parent)
1429 return 0;
1430
1431 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001432 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001433 blocksize = btrfs_level_size(root, level);
1434
1435 if (slot > 0) {
1436 block1 = btrfs_node_blockptr(parent, slot - 1);
1437 gen = btrfs_node_ptr_generation(parent, slot - 1);
1438 eb = btrfs_find_tree_block(root, block1, blocksize);
1439 if (eb && btrfs_buffer_uptodate(eb, gen))
1440 block1 = 0;
1441 free_extent_buffer(eb);
1442 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001443 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001444 block2 = btrfs_node_blockptr(parent, slot + 1);
1445 gen = btrfs_node_ptr_generation(parent, slot + 1);
1446 eb = btrfs_find_tree_block(root, block2, blocksize);
1447 if (eb && btrfs_buffer_uptodate(eb, gen))
1448 block2 = 0;
1449 free_extent_buffer(eb);
1450 }
1451 if (block1 || block2) {
1452 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001453
1454 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001455 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001456
1457 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001458 if (block1)
1459 readahead_tree_block(root, block1, blocksize, 0);
1460 if (block2)
1461 readahead_tree_block(root, block2, blocksize, 0);
1462
1463 if (block1) {
1464 eb = read_tree_block(root, block1, blocksize, 0);
1465 free_extent_buffer(eb);
1466 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001467 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001468 eb = read_tree_block(root, block2, blocksize, 0);
1469 free_extent_buffer(eb);
1470 }
1471 }
1472 return ret;
1473}
1474
1475
1476/*
Chris Masond3977122009-01-05 21:25:51 -05001477 * when we walk down the tree, it is usually safe to unlock the higher layers
1478 * in the tree. The exceptions are when our path goes through slot 0, because
1479 * operations on the tree might require changing key pointers higher up in the
1480 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001481 *
Chris Masond3977122009-01-05 21:25:51 -05001482 * callers might also have set path->keep_locks, which tells this code to keep
1483 * the lock if the path points to the last slot in the block. This is part of
1484 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001485 *
Chris Masond3977122009-01-05 21:25:51 -05001486 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1487 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001488 */
Chris Masone02119d2008-09-05 16:13:11 -04001489static noinline void unlock_up(struct btrfs_path *path, int level,
1490 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001491{
1492 int i;
1493 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001494 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001495 struct extent_buffer *t;
1496
1497 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1498 if (!path->nodes[i])
1499 break;
1500 if (!path->locks[i])
1501 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001502 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001503 skip_level = i + 1;
1504 continue;
1505 }
Chris Mason051e1b92008-06-25 16:01:30 -04001506 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001507 u32 nritems;
1508 t = path->nodes[i];
1509 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001510 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001511 skip_level = i + 1;
1512 continue;
1513 }
1514 }
Chris Mason051e1b92008-06-25 16:01:30 -04001515 if (skip_level < i && i >= lowest_unlock)
1516 no_skips = 1;
1517
Chris Mason925baed2008-06-25 16:01:30 -04001518 t = path->nodes[i];
1519 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1520 btrfs_tree_unlock(t);
1521 path->locks[i] = 0;
1522 }
1523 }
1524}
1525
Chris Mason3c69fae2007-08-07 15:52:22 -04001526/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001527 * This releases any locks held in the path starting at level and
1528 * going all the way up to the root.
1529 *
1530 * btrfs_search_slot will keep the lock held on higher nodes in a few
1531 * corner cases, such as COW of the block at slot zero in the node. This
1532 * ignores those rules, and it should only be called when there are no
1533 * more updates to be done higher up in the tree.
1534 */
1535noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1536{
1537 int i;
1538
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001539 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001540 return;
1541
1542 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1543 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001544 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001545 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001546 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001547 btrfs_tree_unlock(path->nodes[i]);
1548 path->locks[i] = 0;
1549 }
1550}
1551
1552/*
Chris Masonc8c42862009-04-03 10:14:18 -04001553 * helper function for btrfs_search_slot. The goal is to find a block
1554 * in cache without setting the path to blocking. If we find the block
1555 * we return zero and the path is unchanged.
1556 *
1557 * If we can't find the block, we set the path blocking and do some
1558 * reada. -EAGAIN is returned and the search must be repeated.
1559 */
1560static int
1561read_block_for_search(struct btrfs_trans_handle *trans,
1562 struct btrfs_root *root, struct btrfs_path *p,
1563 struct extent_buffer **eb_ret, int level, int slot,
1564 struct btrfs_key *key)
1565{
1566 u64 blocknr;
1567 u64 gen;
1568 u32 blocksize;
1569 struct extent_buffer *b = *eb_ret;
1570 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001571 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001572
1573 blocknr = btrfs_node_blockptr(b, slot);
1574 gen = btrfs_node_ptr_generation(b, slot);
1575 blocksize = btrfs_level_size(root, level - 1);
1576
1577 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1578 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason76a05b32009-05-14 13:24:30 -04001579 /*
1580 * we found an up to date block without sleeping, return
1581 * right away
1582 */
Chris Masonc8c42862009-04-03 10:14:18 -04001583 *eb_ret = tmp;
1584 return 0;
1585 }
1586
1587 /*
1588 * reduce lock contention at high levels
1589 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001590 * we read. Don't release the lock on the current
1591 * level because we need to walk this node to figure
1592 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001593 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001594 btrfs_unlock_up_safe(p, level + 1);
1595 btrfs_set_path_blocking(p);
1596
Chris Masonc8c42862009-04-03 10:14:18 -04001597 if (tmp)
1598 free_extent_buffer(tmp);
1599 if (p->reada)
1600 reada_for_search(root, p, level, slot, key->objectid);
1601
Chris Mason8c594ea2009-04-20 15:50:10 -04001602 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001603
1604 ret = -EAGAIN;
Chris Masonc8c42862009-04-03 10:14:18 -04001605 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Mason76a05b32009-05-14 13:24:30 -04001606 if (tmp) {
1607 /*
1608 * If the read above didn't mark this buffer up to date,
1609 * it will never end up being up to date. Set ret to EIO now
1610 * and give up so that our caller doesn't loop forever
1611 * on our EAGAINs.
1612 */
1613 if (!btrfs_buffer_uptodate(tmp, 0))
1614 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001615 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001616 }
1617 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001618}
1619
1620/*
1621 * helper function for btrfs_search_slot. This does all of the checks
1622 * for node-level blocks and does any balancing required based on
1623 * the ins_len.
1624 *
1625 * If no extra work was required, zero is returned. If we had to
1626 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1627 * start over
1628 */
1629static int
1630setup_nodes_for_search(struct btrfs_trans_handle *trans,
1631 struct btrfs_root *root, struct btrfs_path *p,
1632 struct extent_buffer *b, int level, int ins_len)
1633{
1634 int ret;
1635 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1636 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1637 int sret;
1638
1639 sret = reada_for_balance(root, p, level);
1640 if (sret)
1641 goto again;
1642
1643 btrfs_set_path_blocking(p);
1644 sret = split_node(trans, root, p, level);
1645 btrfs_clear_path_blocking(p, NULL);
1646
1647 BUG_ON(sret > 0);
1648 if (sret) {
1649 ret = sret;
1650 goto done;
1651 }
1652 b = p->nodes[level];
1653 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001654 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001655 int sret;
1656
1657 sret = reada_for_balance(root, p, level);
1658 if (sret)
1659 goto again;
1660
1661 btrfs_set_path_blocking(p);
1662 sret = balance_level(trans, root, p, level);
1663 btrfs_clear_path_blocking(p, NULL);
1664
1665 if (sret) {
1666 ret = sret;
1667 goto done;
1668 }
1669 b = p->nodes[level];
1670 if (!b) {
1671 btrfs_release_path(NULL, p);
1672 goto again;
1673 }
1674 BUG_ON(btrfs_header_nritems(b) == 1);
1675 }
1676 return 0;
1677
1678again:
1679 ret = -EAGAIN;
1680done:
1681 return ret;
1682}
1683
1684/*
Chris Mason74123bd2007-02-02 11:05:29 -05001685 * look for key in the tree. path is filled in with nodes along the way
1686 * if key is found, we return zero and you can find the item in the leaf
1687 * level of the path (level 0)
1688 *
1689 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001690 * be inserted, and 1 is returned. If there are other errors during the
1691 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001692 *
1693 * if ins_len > 0, nodes and leaves will be split as we walk down the
1694 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1695 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001696 */
Chris Masone089f052007-03-16 16:20:31 -04001697int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1698 *root, struct btrfs_key *key, struct btrfs_path *p, int
1699 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001700{
Chris Mason5f39d392007-10-15 16:14:19 -04001701 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001702 int slot;
1703 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001704 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001705 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001706 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001707 u8 lowest_level = 0;
1708
Chris Mason6702ed42007-08-07 16:15:09 -04001709 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001710 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001711 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001712
Chris Mason925baed2008-06-25 16:01:30 -04001713 if (ins_len < 0)
1714 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001715
Chris Masonbb803952007-03-01 12:04:21 -05001716again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001717 if (p->search_commit_root) {
1718 b = root->commit_root;
1719 extent_buffer_get(b);
1720 if (!p->skip_locking)
1721 btrfs_tree_lock(b);
1722 } else {
1723 if (p->skip_locking)
1724 b = btrfs_root_node(root);
1725 else
1726 b = btrfs_lock_root_node(root);
1727 }
Chris Mason925baed2008-06-25 16:01:30 -04001728
Chris Masoneb60cea2007-02-02 09:18:22 -05001729 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001730 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001731
1732 /*
1733 * setup the path here so we can release it under lock
1734 * contention with the cow code
1735 */
1736 p->nodes[level] = b;
1737 if (!p->skip_locking)
1738 p->locks[level] = 1;
1739
Chris Mason02217ed2007-03-02 16:08:05 -05001740 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001741 /*
1742 * if we don't really need to cow this block
1743 * then we don't want to set the path blocking,
1744 * so we test it here
1745 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001746 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001747 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001748
Chris Masonb4ce94d2009-02-04 09:25:08 -05001749 btrfs_set_path_blocking(p);
1750
Yan Zheng33c66f42009-07-22 09:59:00 -04001751 err = btrfs_cow_block(trans, root, b,
1752 p->nodes[level + 1],
1753 p->slots[level + 1], &b);
1754 if (err) {
Chris Mason5f39d392007-10-15 16:14:19 -04001755 free_extent_buffer(b);
Yan Zheng33c66f42009-07-22 09:59:00 -04001756 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001757 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001758 }
Chris Mason02217ed2007-03-02 16:08:05 -05001759 }
Chris Mason65b51a02008-08-01 15:11:20 -04001760cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001761 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001762 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001763 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001764 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001765
Chris Masoneb60cea2007-02-02 09:18:22 -05001766 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001767 if (!p->skip_locking)
1768 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001769
Chris Mason4008c042009-02-12 14:09:45 -05001770 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001771
1772 /*
1773 * we have a lock on b and as long as we aren't changing
1774 * the tree, there is no way to for the items in b to change.
1775 * It is safe to drop the lock on our parent before we
1776 * go through the expensive btree search on b.
1777 *
1778 * If cow is true, then we might be changing slot zero,
1779 * which may require changing the parent. So, we can't
1780 * drop the lock until after we know which slot we're
1781 * operating on.
1782 */
1783 if (!cow)
1784 btrfs_unlock_up_safe(p, level + 1);
1785
Chris Mason123abc82007-03-14 14:14:43 -04001786 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001787 if (ret) {
1788 ret = -1;
1789 goto done;
1790 }
Chris Mason925baed2008-06-25 16:01:30 -04001791
Chris Mason5f39d392007-10-15 16:14:19 -04001792 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001793
Chris Mason5f39d392007-10-15 16:14:19 -04001794 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001795 int dec = 0;
1796 if (ret && slot > 0) {
1797 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001798 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001799 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001800 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001801 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001802 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001803 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001804 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001805 if (err) {
1806 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001807 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001808 }
Chris Masonc8c42862009-04-03 10:14:18 -04001809 b = p->nodes[level];
1810 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001811
Chris Masonf9efa9c2008-06-25 16:14:04 -04001812 unlock_up(p, level, lowest_unlock);
1813
Chris Mason925baed2008-06-25 16:01:30 -04001814 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001815 if (dec)
1816 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001817 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001818 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001819
Yan Zheng33c66f42009-07-22 09:59:00 -04001820 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001821 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001822 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001823 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001824 if (err) {
1825 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001826 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001827 }
Chris Mason76a05b32009-05-14 13:24:30 -04001828
Chris Masonb4ce94d2009-02-04 09:25:08 -05001829 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001830 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001831 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001832
Yan Zheng33c66f42009-07-22 09:59:00 -04001833 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001834 btrfs_set_path_blocking(p);
1835 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001836 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001837 }
1838 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001839 } else {
1840 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001841 if (ins_len > 0 &&
1842 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001843 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001844 err = split_leaf(trans, root, key,
1845 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001846 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001847
Yan Zheng33c66f42009-07-22 09:59:00 -04001848 BUG_ON(err > 0);
1849 if (err) {
1850 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001851 goto done;
1852 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001853 }
Chris Mason459931e2008-12-10 09:10:46 -05001854 if (!p->search_for_split)
1855 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001856 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001857 }
1858 }
Chris Mason65b51a02008-08-01 15:11:20 -04001859 ret = 1;
1860done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001861 /*
1862 * we don't really know what they plan on doing with the path
1863 * from here on, so for now just mark it as blocking
1864 */
Chris Masonb9473432009-03-13 11:00:37 -04001865 if (!p->leave_spinning)
1866 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001867 if (ret < 0)
1868 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001869 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001870}
1871
Chris Mason74123bd2007-02-02 11:05:29 -05001872/*
1873 * adjust the pointers going up the tree, starting at level
1874 * making sure the right key of each node is points to 'key'.
1875 * This is used after shifting pointers to the left, so it stops
1876 * fixing up pointers when a given leaf/node is not in slot 0 of the
1877 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001878 *
1879 * If this fails to write a tree block, it returns -1, but continues
1880 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001881 */
Chris Mason5f39d392007-10-15 16:14:19 -04001882static int fixup_low_keys(struct btrfs_trans_handle *trans,
1883 struct btrfs_root *root, struct btrfs_path *path,
1884 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001885{
1886 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001887 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001888 struct extent_buffer *t;
1889
Chris Mason234b63a2007-03-13 10:46:10 -04001890 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001891 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001892 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001893 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001894 t = path->nodes[i];
1895 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001896 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001897 if (tslot != 0)
1898 break;
1899 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001900 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001901}
1902
Chris Mason74123bd2007-02-02 11:05:29 -05001903/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001904 * update item key.
1905 *
1906 * This function isn't completely safe. It's the caller's responsibility
1907 * that the new key won't break the order
1908 */
1909int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1910 struct btrfs_root *root, struct btrfs_path *path,
1911 struct btrfs_key *new_key)
1912{
1913 struct btrfs_disk_key disk_key;
1914 struct extent_buffer *eb;
1915 int slot;
1916
1917 eb = path->nodes[0];
1918 slot = path->slots[0];
1919 if (slot > 0) {
1920 btrfs_item_key(eb, &disk_key, slot - 1);
1921 if (comp_keys(&disk_key, new_key) >= 0)
1922 return -1;
1923 }
1924 if (slot < btrfs_header_nritems(eb) - 1) {
1925 btrfs_item_key(eb, &disk_key, slot + 1);
1926 if (comp_keys(&disk_key, new_key) <= 0)
1927 return -1;
1928 }
1929
1930 btrfs_cpu_key_to_disk(&disk_key, new_key);
1931 btrfs_set_item_key(eb, &disk_key, slot);
1932 btrfs_mark_buffer_dirty(eb);
1933 if (slot == 0)
1934 fixup_low_keys(trans, root, path, &disk_key, 1);
1935 return 0;
1936}
1937
1938/*
Chris Mason74123bd2007-02-02 11:05:29 -05001939 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001940 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001941 *
1942 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1943 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001944 */
Chris Mason98ed5172008-01-03 10:01:48 -05001945static int push_node_left(struct btrfs_trans_handle *trans,
1946 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001947 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001948{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001949 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001950 int src_nritems;
1951 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001952 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001953
Chris Mason5f39d392007-10-15 16:14:19 -04001954 src_nritems = btrfs_header_nritems(src);
1955 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001956 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001957 WARN_ON(btrfs_header_generation(src) != trans->transid);
1958 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001959
Chris Masonbce4eae2008-04-24 14:42:46 -04001960 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001961 return 1;
1962
Chris Masond3977122009-01-05 21:25:51 -05001963 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001964 return 1;
1965
Chris Masonbce4eae2008-04-24 14:42:46 -04001966 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001967 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001968 if (push_items < src_nritems) {
1969 /* leave at least 8 pointers in the node if
1970 * we aren't going to empty it
1971 */
1972 if (src_nritems - push_items < 8) {
1973 if (push_items <= 8)
1974 return 1;
1975 push_items -= 8;
1976 }
1977 }
1978 } else
1979 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001980
Chris Mason5f39d392007-10-15 16:14:19 -04001981 copy_extent_buffer(dst, src,
1982 btrfs_node_key_ptr_offset(dst_nritems),
1983 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001984 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001985
Chris Masonbb803952007-03-01 12:04:21 -05001986 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001987 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1988 btrfs_node_key_ptr_offset(push_items),
1989 (src_nritems - push_items) *
1990 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001991 }
Chris Mason5f39d392007-10-15 16:14:19 -04001992 btrfs_set_header_nritems(src, src_nritems - push_items);
1993 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1994 btrfs_mark_buffer_dirty(src);
1995 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001996
Chris Masonbb803952007-03-01 12:04:21 -05001997 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001998}
1999
Chris Mason97571fd2007-02-24 13:39:08 -05002000/*
Chris Mason79f95c82007-03-01 15:16:26 -05002001 * try to push data from one node into the next node right in the
2002 * tree.
2003 *
2004 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2005 * error, and > 0 if there was no room in the right hand block.
2006 *
2007 * this will only push up to 1/2 the contents of the left node over
2008 */
Chris Mason5f39d392007-10-15 16:14:19 -04002009static int balance_node_right(struct btrfs_trans_handle *trans,
2010 struct btrfs_root *root,
2011 struct extent_buffer *dst,
2012 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002013{
Chris Mason79f95c82007-03-01 15:16:26 -05002014 int push_items = 0;
2015 int max_push;
2016 int src_nritems;
2017 int dst_nritems;
2018 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002019
Chris Mason7bb86312007-12-11 09:25:06 -05002020 WARN_ON(btrfs_header_generation(src) != trans->transid);
2021 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2022
Chris Mason5f39d392007-10-15 16:14:19 -04002023 src_nritems = btrfs_header_nritems(src);
2024 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002025 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002026 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002027 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002028
Chris Masond3977122009-01-05 21:25:51 -05002029 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002030 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002031
2032 max_push = src_nritems / 2 + 1;
2033 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002034 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002035 return 1;
Yan252c38f2007-08-29 09:11:44 -04002036
Chris Mason79f95c82007-03-01 15:16:26 -05002037 if (max_push < push_items)
2038 push_items = max_push;
2039
Chris Mason5f39d392007-10-15 16:14:19 -04002040 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2041 btrfs_node_key_ptr_offset(0),
2042 (dst_nritems) *
2043 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002044
Chris Mason5f39d392007-10-15 16:14:19 -04002045 copy_extent_buffer(dst, src,
2046 btrfs_node_key_ptr_offset(0),
2047 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002048 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002049
Chris Mason5f39d392007-10-15 16:14:19 -04002050 btrfs_set_header_nritems(src, src_nritems - push_items);
2051 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002052
Chris Mason5f39d392007-10-15 16:14:19 -04002053 btrfs_mark_buffer_dirty(src);
2054 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002055
Chris Mason79f95c82007-03-01 15:16:26 -05002056 return ret;
2057}
2058
2059/*
Chris Mason97571fd2007-02-24 13:39:08 -05002060 * helper function to insert a new root level in the tree.
2061 * A new node is allocated, and a single item is inserted to
2062 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002063 *
2064 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002065 */
Chris Masond3977122009-01-05 21:25:51 -05002066static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002067 struct btrfs_root *root,
2068 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002069{
Chris Mason7bb86312007-12-11 09:25:06 -05002070 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002071 struct extent_buffer *lower;
2072 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002073 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002074 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002075
2076 BUG_ON(path->nodes[level]);
2077 BUG_ON(path->nodes[level-1] != root->node);
2078
Chris Mason7bb86312007-12-11 09:25:06 -05002079 lower = path->nodes[level-1];
2080 if (level == 1)
2081 btrfs_item_key(lower, &lower_key, 0);
2082 else
2083 btrfs_node_key(lower, &lower_key, 0);
2084
Zheng Yan31840ae2008-09-23 13:14:14 -04002085 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002087 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002088 if (IS_ERR(c))
2089 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002090
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002091 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002092 btrfs_set_header_nritems(c, 1);
2093 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002094 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002095 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002098
Chris Mason5f39d392007-10-15 16:14:19 -04002099 write_extent_buffer(c, root->fs_info->fsid,
2100 (unsigned long)btrfs_header_fsid(c),
2101 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002102
2103 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2104 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2105 BTRFS_UUID_SIZE);
2106
Chris Mason5f39d392007-10-15 16:14:19 -04002107 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002108 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002109 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002110 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002111
2112 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002113
2114 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002115
Chris Mason925baed2008-06-25 16:01:30 -04002116 spin_lock(&root->node_lock);
2117 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002118 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002119 spin_unlock(&root->node_lock);
2120
2121 /* the super has an extra ref to root->node */
2122 free_extent_buffer(old);
2123
Chris Mason0b86a832008-03-24 15:01:56 -04002124 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002125 extent_buffer_get(c);
2126 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002127 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002128 path->slots[level] = 0;
2129 return 0;
2130}
2131
Chris Mason74123bd2007-02-02 11:05:29 -05002132/*
2133 * worker function to insert a single pointer in a node.
2134 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002135 *
Chris Mason74123bd2007-02-02 11:05:29 -05002136 * slot and level indicate where you want the key to go, and
2137 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002138 *
2139 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002140 */
Chris Masone089f052007-03-16 16:20:31 -04002141static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2142 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002143 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002144{
Chris Mason5f39d392007-10-15 16:14:19 -04002145 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002146 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002147
2148 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002149 lower = path->nodes[level];
2150 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002151 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002152 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002153 BUG();
2154 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002155 memmove_extent_buffer(lower,
2156 btrfs_node_key_ptr_offset(slot + 1),
2157 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002158 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002159 }
Chris Mason5f39d392007-10-15 16:14:19 -04002160 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002161 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002162 WARN_ON(trans->transid == 0);
2163 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002164 btrfs_set_header_nritems(lower, nritems + 1);
2165 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002166 return 0;
2167}
2168
Chris Mason97571fd2007-02-24 13:39:08 -05002169/*
2170 * split the node at the specified level in path in two.
2171 * The path is corrected to point to the appropriate node after the split
2172 *
2173 * Before splitting this tries to make some room in the node by pushing
2174 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002175 *
2176 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002177 */
Chris Masone02119d2008-09-05 16:13:11 -04002178static noinline int split_node(struct btrfs_trans_handle *trans,
2179 struct btrfs_root *root,
2180 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002181{
Chris Mason5f39d392007-10-15 16:14:19 -04002182 struct extent_buffer *c;
2183 struct extent_buffer *split;
2184 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002185 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002186 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002187 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002188 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002189
Chris Mason5f39d392007-10-15 16:14:19 -04002190 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002191 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002192 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002193 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002194 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002195 if (ret)
2196 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002197 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002198 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002199 c = path->nodes[level];
2200 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002201 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002202 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002203 if (ret < 0)
2204 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002205 }
Chris Masone66f7092007-04-20 13:16:02 -04002206
Chris Mason5f39d392007-10-15 16:14:19 -04002207 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002208 mid = (c_nritems + 1) / 2;
2209 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002210
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002211 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002212 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002213 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002214 if (IS_ERR(split))
2215 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002216
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002217 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002218 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002219 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002220 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002221 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002222 btrfs_set_header_owner(split, root->root_key.objectid);
2223 write_extent_buffer(split, root->fs_info->fsid,
2224 (unsigned long)btrfs_header_fsid(split),
2225 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002226 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2227 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2228 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002229
Chris Mason5f39d392007-10-15 16:14:19 -04002230
2231 copy_extent_buffer(split, c,
2232 btrfs_node_key_ptr_offset(0),
2233 btrfs_node_key_ptr_offset(mid),
2234 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2235 btrfs_set_header_nritems(split, c_nritems - mid);
2236 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002237 ret = 0;
2238
Chris Mason5f39d392007-10-15 16:14:19 -04002239 btrfs_mark_buffer_dirty(c);
2240 btrfs_mark_buffer_dirty(split);
2241
Chris Masondb945352007-10-15 16:15:53 -04002242 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002243 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002244 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002245 if (wret)
2246 ret = wret;
2247
Chris Mason5de08d72007-02-24 06:24:44 -05002248 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002249 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002250 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002251 free_extent_buffer(c);
2252 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002253 path->slots[level + 1] += 1;
2254 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002255 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002256 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002257 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002258 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002259}
2260
Chris Mason74123bd2007-02-02 11:05:29 -05002261/*
2262 * how many bytes are required to store the items in a leaf. start
2263 * and nr indicate which items in the leaf to check. This totals up the
2264 * space used both by the item structs and the item data
2265 */
Chris Mason5f39d392007-10-15 16:14:19 -04002266static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002267{
2268 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002269 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002270 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002271
2272 if (!nr)
2273 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002274 data_len = btrfs_item_end_nr(l, start);
2275 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002276 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002277 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002278 return data_len;
2279}
2280
Chris Mason74123bd2007-02-02 11:05:29 -05002281/*
Chris Masond4dbff92007-04-04 14:08:15 -04002282 * The space between the end of the leaf items and
2283 * the start of the leaf data. IOW, how much room
2284 * the leaf has left for both items and data
2285 */
Chris Masond3977122009-01-05 21:25:51 -05002286noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002287 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002288{
Chris Mason5f39d392007-10-15 16:14:19 -04002289 int nritems = btrfs_header_nritems(leaf);
2290 int ret;
2291 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2292 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002293 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2294 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002295 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002296 leaf_space_used(leaf, 0, nritems), nritems);
2297 }
2298 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002299}
2300
Chris Mason44871b12009-03-13 10:04:31 -04002301static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2302 struct btrfs_root *root,
2303 struct btrfs_path *path,
2304 int data_size, int empty,
2305 struct extent_buffer *right,
2306 int free_space, u32 left_nritems)
Chris Mason00ec4c52007-02-24 12:47:20 -05002307{
Chris Mason5f39d392007-10-15 16:14:19 -04002308 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002309 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002310 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002311 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002312 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002313 int push_space = 0;
2314 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002315 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002316 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002317 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002318 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002319 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002320
Chris Mason34a38212007-11-07 13:31:03 -05002321 if (empty)
2322 nr = 0;
2323 else
2324 nr = 1;
2325
Zheng Yan31840ae2008-09-23 13:14:14 -04002326 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002327 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002328
Chris Mason44871b12009-03-13 10:04:31 -04002329 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002330 i = left_nritems - 1;
2331 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002332 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002333
Zheng Yan31840ae2008-09-23 13:14:14 -04002334 if (!empty && push_items > 0) {
2335 if (path->slots[0] > i)
2336 break;
2337 if (path->slots[0] == i) {
2338 int space = btrfs_leaf_free_space(root, left);
2339 if (space + push_space * 2 > free_space)
2340 break;
2341 }
2342 }
2343
Chris Mason00ec4c52007-02-24 12:47:20 -05002344 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002345 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002346
2347 if (!left->map_token) {
2348 map_extent_buffer(left, (unsigned long)item,
2349 sizeof(struct btrfs_item),
2350 &left->map_token, &left->kaddr,
2351 &left->map_start, &left->map_len,
2352 KM_USER1);
2353 }
2354
2355 this_item_size = btrfs_item_size(left, item);
2356 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002357 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002358
Chris Mason00ec4c52007-02-24 12:47:20 -05002359 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002360 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002361 if (i == 0)
2362 break;
2363 i--;
Chris Masondb945352007-10-15 16:15:53 -04002364 }
2365 if (left->map_token) {
2366 unmap_extent_buffer(left, left->map_token, KM_USER1);
2367 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002368 }
Chris Mason5f39d392007-10-15 16:14:19 -04002369
Chris Mason925baed2008-06-25 16:01:30 -04002370 if (push_items == 0)
2371 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002372
Chris Mason34a38212007-11-07 13:31:03 -05002373 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002374 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002375
Chris Mason00ec4c52007-02-24 12:47:20 -05002376 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002377 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002378
Chris Mason5f39d392007-10-15 16:14:19 -04002379 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002380 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002381
Chris Mason00ec4c52007-02-24 12:47:20 -05002382 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002383 data_end = leaf_data_end(root, right);
2384 memmove_extent_buffer(right,
2385 btrfs_leaf_data(right) + data_end - push_space,
2386 btrfs_leaf_data(right) + data_end,
2387 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2388
Chris Mason00ec4c52007-02-24 12:47:20 -05002389 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002390 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002391 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2392 btrfs_leaf_data(left) + leaf_data_end(root, left),
2393 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002394
2395 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2396 btrfs_item_nr_offset(0),
2397 right_nritems * sizeof(struct btrfs_item));
2398
Chris Mason00ec4c52007-02-24 12:47:20 -05002399 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002400 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2401 btrfs_item_nr_offset(left_nritems - push_items),
2402 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002403
2404 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002405 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002406 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002407 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002408 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002409 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002410 if (!right->map_token) {
2411 map_extent_buffer(right, (unsigned long)item,
2412 sizeof(struct btrfs_item),
2413 &right->map_token, &right->kaddr,
2414 &right->map_start, &right->map_len,
2415 KM_USER1);
2416 }
2417 push_space -= btrfs_item_size(right, item);
2418 btrfs_set_item_offset(right, item, push_space);
2419 }
2420
2421 if (right->map_token) {
2422 unmap_extent_buffer(right, right->map_token, KM_USER1);
2423 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002424 }
Chris Mason7518a232007-03-12 12:01:18 -04002425 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002426 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002427
Chris Mason34a38212007-11-07 13:31:03 -05002428 if (left_nritems)
2429 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002430 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002431
Chris Mason5f39d392007-10-15 16:14:19 -04002432 btrfs_item_key(right, &disk_key, 0);
2433 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002434 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002435
Chris Mason00ec4c52007-02-24 12:47:20 -05002436 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002437 if (path->slots[0] >= left_nritems) {
2438 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002439 if (btrfs_header_nritems(path->nodes[0]) == 0)
2440 clean_tree_block(trans, root, path->nodes[0]);
2441 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002442 free_extent_buffer(path->nodes[0]);
2443 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002444 path->slots[1] += 1;
2445 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002446 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002447 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002448 }
2449 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002450
2451out_unlock:
2452 btrfs_tree_unlock(right);
2453 free_extent_buffer(right);
2454 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002455}
Chris Mason925baed2008-06-25 16:01:30 -04002456
Chris Mason00ec4c52007-02-24 12:47:20 -05002457/*
Chris Mason44871b12009-03-13 10:04:31 -04002458 * push some data in the path leaf to the right, trying to free up at
2459 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2460 *
2461 * returns 1 if the push failed because the other node didn't have enough
2462 * room, 0 if everything worked out and < 0 if there were major errors.
2463 */
2464static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2465 *root, struct btrfs_path *path, int data_size,
2466 int empty)
2467{
2468 struct extent_buffer *left = path->nodes[0];
2469 struct extent_buffer *right;
2470 struct extent_buffer *upper;
2471 int slot;
2472 int free_space;
2473 u32 left_nritems;
2474 int ret;
2475
2476 if (!path->nodes[1])
2477 return 1;
2478
2479 slot = path->slots[1];
2480 upper = path->nodes[1];
2481 if (slot >= btrfs_header_nritems(upper) - 1)
2482 return 1;
2483
2484 btrfs_assert_tree_locked(path->nodes[1]);
2485
2486 right = read_node_slot(root, upper, slot + 1);
2487 btrfs_tree_lock(right);
2488 btrfs_set_lock_blocking(right);
2489
2490 free_space = btrfs_leaf_free_space(root, right);
2491 if (free_space < data_size)
2492 goto out_unlock;
2493
2494 /* cow and double check */
2495 ret = btrfs_cow_block(trans, root, right, upper,
2496 slot + 1, &right);
2497 if (ret)
2498 goto out_unlock;
2499
2500 free_space = btrfs_leaf_free_space(root, right);
2501 if (free_space < data_size)
2502 goto out_unlock;
2503
2504 left_nritems = btrfs_header_nritems(left);
2505 if (left_nritems == 0)
2506 goto out_unlock;
2507
2508 return __push_leaf_right(trans, root, path, data_size, empty,
2509 right, free_space, left_nritems);
2510out_unlock:
2511 btrfs_tree_unlock(right);
2512 free_extent_buffer(right);
2513 return 1;
2514}
2515
2516/*
Chris Mason74123bd2007-02-02 11:05:29 -05002517 * push some data in the path leaf to the left, trying to free up at
2518 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2519 */
Chris Mason44871b12009-03-13 10:04:31 -04002520static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2521 struct btrfs_root *root,
2522 struct btrfs_path *path, int data_size,
2523 int empty, struct extent_buffer *left,
2524 int free_space, int right_nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002525{
Chris Mason5f39d392007-10-15 16:14:19 -04002526 struct btrfs_disk_key disk_key;
2527 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002528 int slot;
2529 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002530 int push_space = 0;
2531 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002532 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002533 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002534 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002535 int ret = 0;
2536 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002537 u32 this_item_size;
2538 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002539
2540 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002541
Chris Mason34a38212007-11-07 13:31:03 -05002542 if (empty)
2543 nr = right_nritems;
2544 else
2545 nr = right_nritems - 1;
2546
2547 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002548 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002549 if (!right->map_token) {
2550 map_extent_buffer(right, (unsigned long)item,
2551 sizeof(struct btrfs_item),
2552 &right->map_token, &right->kaddr,
2553 &right->map_start, &right->map_len,
2554 KM_USER1);
2555 }
2556
Zheng Yan31840ae2008-09-23 13:14:14 -04002557 if (!empty && push_items > 0) {
2558 if (path->slots[0] < i)
2559 break;
2560 if (path->slots[0] == i) {
2561 int space = btrfs_leaf_free_space(root, right);
2562 if (space + push_space * 2 > free_space)
2563 break;
2564 }
2565 }
2566
Chris Masonbe0e5c02007-01-26 15:51:26 -05002567 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002568 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002569
2570 this_item_size = btrfs_item_size(right, item);
2571 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002572 break;
Chris Masondb945352007-10-15 16:15:53 -04002573
Chris Masonbe0e5c02007-01-26 15:51:26 -05002574 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002575 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002576 }
Chris Masondb945352007-10-15 16:15:53 -04002577
2578 if (right->map_token) {
2579 unmap_extent_buffer(right, right->map_token, KM_USER1);
2580 right->map_token = NULL;
2581 }
2582
Chris Masonbe0e5c02007-01-26 15:51:26 -05002583 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002584 ret = 1;
2585 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002586 }
Chris Mason34a38212007-11-07 13:31:03 -05002587 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002588 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002589
Chris Masonbe0e5c02007-01-26 15:51:26 -05002590 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002591 copy_extent_buffer(left, right,
2592 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2593 btrfs_item_nr_offset(0),
2594 push_items * sizeof(struct btrfs_item));
2595
Chris Mason123abc82007-03-14 14:14:43 -04002596 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002597 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002598
2599 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002600 leaf_data_end(root, left) - push_space,
2601 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002602 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002603 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002604 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002605 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002606
Chris Masondb945352007-10-15 16:15:53 -04002607 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002608 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002609 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002610
Chris Mason5f39d392007-10-15 16:14:19 -04002611 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002612 if (!left->map_token) {
2613 map_extent_buffer(left, (unsigned long)item,
2614 sizeof(struct btrfs_item),
2615 &left->map_token, &left->kaddr,
2616 &left->map_start, &left->map_len,
2617 KM_USER1);
2618 }
2619
Chris Mason5f39d392007-10-15 16:14:19 -04002620 ioff = btrfs_item_offset(left, item);
2621 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002622 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002623 }
Chris Mason5f39d392007-10-15 16:14:19 -04002624 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002625 if (left->map_token) {
2626 unmap_extent_buffer(left, left->map_token, KM_USER1);
2627 left->map_token = NULL;
2628 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002629
2630 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002631 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002632 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2633 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002634 WARN_ON(1);
2635 }
Chris Mason5f39d392007-10-15 16:14:19 -04002636
Chris Mason34a38212007-11-07 13:31:03 -05002637 if (push_items < right_nritems) {
2638 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2639 leaf_data_end(root, right);
2640 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2641 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2642 btrfs_leaf_data(right) +
2643 leaf_data_end(root, right), push_space);
2644
2645 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002646 btrfs_item_nr_offset(push_items),
2647 (btrfs_header_nritems(right) - push_items) *
2648 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002649 }
Yaneef1c492007-11-26 10:58:13 -05002650 right_nritems -= push_items;
2651 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002652 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002653 for (i = 0; i < right_nritems; i++) {
2654 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002655
2656 if (!right->map_token) {
2657 map_extent_buffer(right, (unsigned long)item,
2658 sizeof(struct btrfs_item),
2659 &right->map_token, &right->kaddr,
2660 &right->map_start, &right->map_len,
2661 KM_USER1);
2662 }
2663
2664 push_space = push_space - btrfs_item_size(right, item);
2665 btrfs_set_item_offset(right, item, push_space);
2666 }
2667 if (right->map_token) {
2668 unmap_extent_buffer(right, right->map_token, KM_USER1);
2669 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002670 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002671
Chris Mason5f39d392007-10-15 16:14:19 -04002672 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002673 if (right_nritems)
2674 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002675
Chris Mason5f39d392007-10-15 16:14:19 -04002676 btrfs_item_key(right, &disk_key, 0);
2677 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002678 if (wret)
2679 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002680
2681 /* then fixup the leaf pointer in the path */
2682 if (path->slots[0] < push_items) {
2683 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002684 if (btrfs_header_nritems(path->nodes[0]) == 0)
2685 clean_tree_block(trans, root, path->nodes[0]);
2686 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002687 free_extent_buffer(path->nodes[0]);
2688 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689 path->slots[1] -= 1;
2690 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002691 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002692 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002693 path->slots[0] -= push_items;
2694 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002695 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002696 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002697out:
2698 btrfs_tree_unlock(left);
2699 free_extent_buffer(left);
2700 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002701}
2702
Chris Mason74123bd2007-02-02 11:05:29 -05002703/*
Chris Mason44871b12009-03-13 10:04:31 -04002704 * push some data in the path leaf to the left, trying to free up at
2705 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2706 */
2707static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2708 *root, struct btrfs_path *path, int data_size,
2709 int empty)
2710{
2711 struct extent_buffer *right = path->nodes[0];
2712 struct extent_buffer *left;
2713 int slot;
2714 int free_space;
2715 u32 right_nritems;
2716 int ret = 0;
2717
2718 slot = path->slots[1];
2719 if (slot == 0)
2720 return 1;
2721 if (!path->nodes[1])
2722 return 1;
2723
2724 right_nritems = btrfs_header_nritems(right);
2725 if (right_nritems == 0)
2726 return 1;
2727
2728 btrfs_assert_tree_locked(path->nodes[1]);
2729
2730 left = read_node_slot(root, path->nodes[1], slot - 1);
2731 btrfs_tree_lock(left);
2732 btrfs_set_lock_blocking(left);
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 /* cow and double check */
2741 ret = btrfs_cow_block(trans, root, left,
2742 path->nodes[1], slot - 1, &left);
2743 if (ret) {
2744 /* we hit -ENOSPC, but it isn't fatal here */
2745 ret = 1;
2746 goto out;
2747 }
2748
2749 free_space = btrfs_leaf_free_space(root, left);
2750 if (free_space < data_size) {
2751 ret = 1;
2752 goto out;
2753 }
2754
2755 return __push_leaf_left(trans, root, path, data_size,
2756 empty, left, free_space, right_nritems);
2757out:
2758 btrfs_tree_unlock(left);
2759 free_extent_buffer(left);
2760 return ret;
2761}
2762
2763/*
Chris Mason74123bd2007-02-02 11:05:29 -05002764 * split the path's leaf in two, making sure there is at least data_size
2765 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002766 *
2767 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002768 */
Chris Mason44871b12009-03-13 10:04:31 -04002769static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002770 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002771 struct btrfs_path *path,
2772 struct extent_buffer *l,
2773 struct extent_buffer *right,
2774 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002775{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002776 int data_copy_size;
2777 int rt_data_off;
2778 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002779 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002780 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002781 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002782
Chris Mason5f39d392007-10-15 16:14:19 -04002783 nritems = nritems - mid;
2784 btrfs_set_header_nritems(right, nritems);
2785 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2786
2787 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2788 btrfs_item_nr_offset(mid),
2789 nritems * sizeof(struct btrfs_item));
2790
2791 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002792 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2793 data_copy_size, btrfs_leaf_data(l) +
2794 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002795
Chris Mason5f39d392007-10-15 16:14:19 -04002796 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2797 btrfs_item_end_nr(l, mid);
2798
2799 for (i = 0; i < nritems; i++) {
2800 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002801 u32 ioff;
2802
2803 if (!right->map_token) {
2804 map_extent_buffer(right, (unsigned long)item,
2805 sizeof(struct btrfs_item),
2806 &right->map_token, &right->kaddr,
2807 &right->map_start, &right->map_len,
2808 KM_USER1);
2809 }
2810
2811 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002812 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002813 }
Chris Mason74123bd2007-02-02 11:05:29 -05002814
Chris Masondb945352007-10-15 16:15:53 -04002815 if (right->map_token) {
2816 unmap_extent_buffer(right, right->map_token, KM_USER1);
2817 right->map_token = NULL;
2818 }
2819
Chris Mason5f39d392007-10-15 16:14:19 -04002820 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002821 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002822 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002823 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2824 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002825 if (wret)
2826 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002827
2828 btrfs_mark_buffer_dirty(right);
2829 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002830 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002831
Chris Masonbe0e5c02007-01-26 15:51:26 -05002832 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002833 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002834 free_extent_buffer(path->nodes[0]);
2835 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002836 path->slots[0] -= mid;
2837 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002838 } else {
2839 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002840 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002841 }
Chris Mason5f39d392007-10-15 16:14:19 -04002842
Chris Masoneb60cea2007-02-02 09:18:22 -05002843 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002844
Chris Mason44871b12009-03-13 10:04:31 -04002845 return ret;
2846}
2847
2848/*
2849 * split the path's leaf in two, making sure there is at least data_size
2850 * available for the resulting leaf level of the path.
2851 *
2852 * returns 0 if all went well and < 0 on failure.
2853 */
2854static noinline int split_leaf(struct btrfs_trans_handle *trans,
2855 struct btrfs_root *root,
2856 struct btrfs_key *ins_key,
2857 struct btrfs_path *path, int data_size,
2858 int extend)
2859{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002860 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002861 struct extent_buffer *l;
2862 u32 nritems;
2863 int mid;
2864 int slot;
2865 struct extent_buffer *right;
2866 int ret = 0;
2867 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002868 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002869 int num_doubles = 0;
2870
2871 /* first try to make some room by pushing left and right */
Chris Masonb3612422009-05-13 19:12:15 -04002872 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason44871b12009-03-13 10:04:31 -04002873 wret = push_leaf_right(trans, root, path, data_size, 0);
2874 if (wret < 0)
2875 return wret;
2876 if (wret) {
2877 wret = push_leaf_left(trans, root, path, data_size, 0);
2878 if (wret < 0)
2879 return wret;
2880 }
2881 l = path->nodes[0];
2882
2883 /* did the pushes work? */
2884 if (btrfs_leaf_free_space(root, l) >= data_size)
2885 return 0;
2886 }
2887
2888 if (!path->nodes[1]) {
2889 ret = insert_new_root(trans, root, path, 1);
2890 if (ret)
2891 return ret;
2892 }
2893again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002894 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04002895 l = path->nodes[0];
2896 slot = path->slots[0];
2897 nritems = btrfs_header_nritems(l);
2898 mid = (nritems + 1) / 2;
2899
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002900 if (mid <= slot) {
2901 if (nritems == 1 ||
2902 leaf_space_used(l, mid, nritems - mid) + data_size >
2903 BTRFS_LEAF_DATA_SIZE(root)) {
2904 if (slot >= nritems) {
2905 split = 0;
2906 } else {
2907 mid = slot;
2908 if (mid != nritems &&
2909 leaf_space_used(l, mid, nritems - mid) +
2910 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2911 split = 2;
2912 }
2913 }
2914 }
2915 } else {
2916 if (leaf_space_used(l, 0, mid) + data_size >
2917 BTRFS_LEAF_DATA_SIZE(root)) {
2918 if (!extend && data_size && slot == 0) {
2919 split = 0;
2920 } else if ((extend || !data_size) && slot == 0) {
2921 mid = 1;
2922 } else {
2923 mid = slot;
2924 if (mid != nritems &&
2925 leaf_space_used(l, mid, nritems - mid) +
2926 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2927 split = 2 ;
2928 }
2929 }
2930 }
2931 }
2932
2933 if (split == 0)
2934 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2935 else
2936 btrfs_item_key(l, &disk_key, mid);
2937
2938 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04002939 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002940 &disk_key, 0, l->start, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002941 if (IS_ERR(right)) {
2942 BUG_ON(1);
2943 return PTR_ERR(right);
2944 }
2945
2946 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2947 btrfs_set_header_bytenr(right, right->start);
2948 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002949 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04002950 btrfs_set_header_owner(right, root->root_key.objectid);
2951 btrfs_set_header_level(right, 0);
2952 write_extent_buffer(right, root->fs_info->fsid,
2953 (unsigned long)btrfs_header_fsid(right),
2954 BTRFS_FSID_SIZE);
2955
2956 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2957 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2958 BTRFS_UUID_SIZE);
2959
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002960 if (split == 0) {
2961 if (mid <= slot) {
2962 btrfs_set_header_nritems(right, 0);
2963 wret = insert_ptr(trans, root, path,
2964 &disk_key, right->start,
2965 path->slots[1] + 1, 1);
2966 if (wret)
2967 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002968
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002969 btrfs_tree_unlock(path->nodes[0]);
2970 free_extent_buffer(path->nodes[0]);
2971 path->nodes[0] = right;
2972 path->slots[0] = 0;
2973 path->slots[1] += 1;
2974 } else {
2975 btrfs_set_header_nritems(right, 0);
2976 wret = insert_ptr(trans, root, path,
2977 &disk_key,
2978 right->start,
2979 path->slots[1], 1);
2980 if (wret)
2981 ret = wret;
2982 btrfs_tree_unlock(path->nodes[0]);
2983 free_extent_buffer(path->nodes[0]);
2984 path->nodes[0] = right;
2985 path->slots[0] = 0;
2986 if (path->slots[1] == 0) {
2987 wret = fixup_low_keys(trans, root,
2988 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04002989 if (wret)
2990 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04002991 }
2992 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002993 btrfs_mark_buffer_dirty(right);
2994 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04002995 }
2996
2997 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2998 BUG_ON(ret);
2999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003001 BUG_ON(num_doubles != 0);
3002 num_doubles++;
3003 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003004 }
Chris Mason44871b12009-03-13 10:04:31 -04003005
Chris Masonbe0e5c02007-01-26 15:51:26 -05003006 return ret;
3007}
3008
Chris Masond352ac62008-09-29 15:18:18 -04003009/*
Chris Mason459931e2008-12-10 09:10:46 -05003010 * This function splits a single item into two items,
3011 * giving 'new_key' to the new item and splitting the
3012 * old one at split_offset (from the start of the item).
3013 *
3014 * The path may be released by this operation. After
3015 * the split, the path is pointing to the old item. The
3016 * new item is going to be in the same node as the old one.
3017 *
3018 * Note, the item being split must be smaller enough to live alone on
3019 * a tree block with room for one extra struct btrfs_item
3020 *
3021 * This allows us to split the item in place, keeping a lock on the
3022 * leaf the entire time.
3023 */
3024int btrfs_split_item(struct btrfs_trans_handle *trans,
3025 struct btrfs_root *root,
3026 struct btrfs_path *path,
3027 struct btrfs_key *new_key,
3028 unsigned long split_offset)
3029{
3030 u32 item_size;
3031 struct extent_buffer *leaf;
3032 struct btrfs_key orig_key;
3033 struct btrfs_item *item;
3034 struct btrfs_item *new_item;
3035 int ret = 0;
3036 int slot;
3037 u32 nritems;
3038 u32 orig_offset;
3039 struct btrfs_disk_key disk_key;
3040 char *buf;
3041
3042 leaf = path->nodes[0];
3043 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
3044 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
3045 goto split;
3046
3047 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3048 btrfs_release_path(root, path);
3049
3050 path->search_for_split = 1;
3051 path->keep_locks = 1;
3052
3053 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
3054 path->search_for_split = 0;
3055
3056 /* if our item isn't there or got smaller, return now */
3057 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
3058 path->slots[0])) {
3059 path->keep_locks = 0;
3060 return -EAGAIN;
3061 }
3062
Chris Masonb9473432009-03-13 11:00:37 -04003063 btrfs_set_path_blocking(path);
Yan Zheng87b29b22008-12-17 10:21:48 -05003064 ret = split_leaf(trans, root, &orig_key, path,
3065 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05003066 path->keep_locks = 0;
3067 BUG_ON(ret);
3068
Chris Masonb9473432009-03-13 11:00:37 -04003069 btrfs_unlock_up_safe(path, 1);
3070 leaf = path->nodes[0];
3071 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3072
3073split:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003074 /*
3075 * make sure any changes to the path from split_leaf leave it
3076 * in a blocking state
3077 */
3078 btrfs_set_path_blocking(path);
3079
Chris Mason459931e2008-12-10 09:10:46 -05003080 item = btrfs_item_nr(leaf, path->slots[0]);
3081 orig_offset = btrfs_item_offset(leaf, item);
3082 item_size = btrfs_item_size(leaf, item);
3083
Chris Mason459931e2008-12-10 09:10:46 -05003084 buf = kmalloc(item_size, GFP_NOFS);
3085 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3086 path->slots[0]), item_size);
3087 slot = path->slots[0] + 1;
3088 leaf = path->nodes[0];
3089
3090 nritems = btrfs_header_nritems(leaf);
3091
3092 if (slot != nritems) {
3093 /* shift the items */
3094 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3095 btrfs_item_nr_offset(slot),
3096 (nritems - slot) * sizeof(struct btrfs_item));
3097
3098 }
3099
3100 btrfs_cpu_key_to_disk(&disk_key, new_key);
3101 btrfs_set_item_key(leaf, &disk_key, slot);
3102
3103 new_item = btrfs_item_nr(leaf, slot);
3104
3105 btrfs_set_item_offset(leaf, new_item, orig_offset);
3106 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3107
3108 btrfs_set_item_offset(leaf, item,
3109 orig_offset + item_size - split_offset);
3110 btrfs_set_item_size(leaf, item, split_offset);
3111
3112 btrfs_set_header_nritems(leaf, nritems + 1);
3113
3114 /* write the data for the start of the original item */
3115 write_extent_buffer(leaf, buf,
3116 btrfs_item_ptr_offset(leaf, path->slots[0]),
3117 split_offset);
3118
3119 /* write the data for the new item */
3120 write_extent_buffer(leaf, buf + split_offset,
3121 btrfs_item_ptr_offset(leaf, slot),
3122 item_size - split_offset);
3123 btrfs_mark_buffer_dirty(leaf);
3124
3125 ret = 0;
3126 if (btrfs_leaf_free_space(root, leaf) < 0) {
3127 btrfs_print_leaf(root, leaf);
3128 BUG();
3129 }
3130 kfree(buf);
3131 return ret;
3132}
3133
3134/*
Chris Masond352ac62008-09-29 15:18:18 -04003135 * make the item pointed to by the path smaller. new_size indicates
3136 * how small to make it, and from_end tells us if we just chop bytes
3137 * off the end of the item or if we shift the item to chop bytes off
3138 * the front.
3139 */
Chris Masonb18c6682007-04-17 13:26:50 -04003140int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3141 struct btrfs_root *root,
3142 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003143 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003144{
3145 int ret = 0;
3146 int slot;
3147 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003148 struct extent_buffer *leaf;
3149 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003150 u32 nritems;
3151 unsigned int data_end;
3152 unsigned int old_data_start;
3153 unsigned int old_size;
3154 unsigned int size_diff;
3155 int i;
3156
3157 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003158 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003159 slot = path->slots[0];
3160
3161 old_size = btrfs_item_size_nr(leaf, slot);
3162 if (old_size == new_size)
3163 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003164
Chris Mason5f39d392007-10-15 16:14:19 -04003165 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003166 data_end = leaf_data_end(root, leaf);
3167
Chris Mason5f39d392007-10-15 16:14:19 -04003168 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003169
Chris Masonb18c6682007-04-17 13:26:50 -04003170 size_diff = old_size - new_size;
3171
3172 BUG_ON(slot < 0);
3173 BUG_ON(slot >= nritems);
3174
3175 /*
3176 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3177 */
3178 /* first correct the data pointers */
3179 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003180 u32 ioff;
3181 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003182
3183 if (!leaf->map_token) {
3184 map_extent_buffer(leaf, (unsigned long)item,
3185 sizeof(struct btrfs_item),
3186 &leaf->map_token, &leaf->kaddr,
3187 &leaf->map_start, &leaf->map_len,
3188 KM_USER1);
3189 }
3190
Chris Mason5f39d392007-10-15 16:14:19 -04003191 ioff = btrfs_item_offset(leaf, item);
3192 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003193 }
Chris Masondb945352007-10-15 16:15:53 -04003194
3195 if (leaf->map_token) {
3196 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3197 leaf->map_token = NULL;
3198 }
3199
Chris Masonb18c6682007-04-17 13:26:50 -04003200 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003201 if (from_end) {
3202 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3203 data_end + size_diff, btrfs_leaf_data(leaf) +
3204 data_end, old_data_start + new_size - data_end);
3205 } else {
3206 struct btrfs_disk_key disk_key;
3207 u64 offset;
3208
3209 btrfs_item_key(leaf, &disk_key, slot);
3210
3211 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3212 unsigned long ptr;
3213 struct btrfs_file_extent_item *fi;
3214
3215 fi = btrfs_item_ptr(leaf, slot,
3216 struct btrfs_file_extent_item);
3217 fi = (struct btrfs_file_extent_item *)(
3218 (unsigned long)fi - size_diff);
3219
3220 if (btrfs_file_extent_type(leaf, fi) ==
3221 BTRFS_FILE_EXTENT_INLINE) {
3222 ptr = btrfs_item_ptr_offset(leaf, slot);
3223 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003224 (unsigned long)fi,
3225 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003226 disk_bytenr));
3227 }
3228 }
3229
3230 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3231 data_end + size_diff, btrfs_leaf_data(leaf) +
3232 data_end, old_data_start - data_end);
3233
3234 offset = btrfs_disk_key_offset(&disk_key);
3235 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3236 btrfs_set_item_key(leaf, &disk_key, slot);
3237 if (slot == 0)
3238 fixup_low_keys(trans, root, path, &disk_key, 1);
3239 }
Chris Mason5f39d392007-10-15 16:14:19 -04003240
3241 item = btrfs_item_nr(leaf, slot);
3242 btrfs_set_item_size(leaf, item, new_size);
3243 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003244
3245 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003246 if (btrfs_leaf_free_space(root, leaf) < 0) {
3247 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003248 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003249 }
Chris Masonb18c6682007-04-17 13:26:50 -04003250 return ret;
3251}
3252
Chris Masond352ac62008-09-29 15:18:18 -04003253/*
3254 * make the item pointed to by the path bigger, data_size is the new size.
3255 */
Chris Mason5f39d392007-10-15 16:14:19 -04003256int btrfs_extend_item(struct btrfs_trans_handle *trans,
3257 struct btrfs_root *root, struct btrfs_path *path,
3258 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003259{
3260 int ret = 0;
3261 int slot;
3262 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003263 struct extent_buffer *leaf;
3264 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003265 u32 nritems;
3266 unsigned int data_end;
3267 unsigned int old_data;
3268 unsigned int old_size;
3269 int i;
3270
3271 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003272 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003273
Chris Mason5f39d392007-10-15 16:14:19 -04003274 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003275 data_end = leaf_data_end(root, leaf);
3276
Chris Mason5f39d392007-10-15 16:14:19 -04003277 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3278 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003279 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003280 }
Chris Mason6567e832007-04-16 09:22:45 -04003281 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003282 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003283
3284 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003285 if (slot >= nritems) {
3286 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003287 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3288 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003289 BUG_ON(1);
3290 }
Chris Mason6567e832007-04-16 09:22:45 -04003291
3292 /*
3293 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3294 */
3295 /* first correct the data pointers */
3296 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003297 u32 ioff;
3298 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003299
3300 if (!leaf->map_token) {
3301 map_extent_buffer(leaf, (unsigned long)item,
3302 sizeof(struct btrfs_item),
3303 &leaf->map_token, &leaf->kaddr,
3304 &leaf->map_start, &leaf->map_len,
3305 KM_USER1);
3306 }
Chris Mason5f39d392007-10-15 16:14:19 -04003307 ioff = btrfs_item_offset(leaf, item);
3308 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003309 }
Chris Mason5f39d392007-10-15 16:14:19 -04003310
Chris Masondb945352007-10-15 16:15:53 -04003311 if (leaf->map_token) {
3312 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3313 leaf->map_token = NULL;
3314 }
3315
Chris Mason6567e832007-04-16 09:22:45 -04003316 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003317 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003318 data_end - data_size, btrfs_leaf_data(leaf) +
3319 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003320
Chris Mason6567e832007-04-16 09:22:45 -04003321 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003322 old_size = btrfs_item_size_nr(leaf, slot);
3323 item = btrfs_item_nr(leaf, slot);
3324 btrfs_set_item_size(leaf, item, old_size + data_size);
3325 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003326
3327 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003328 if (btrfs_leaf_free_space(root, leaf) < 0) {
3329 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003330 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003331 }
Chris Mason6567e832007-04-16 09:22:45 -04003332 return ret;
3333}
3334
Chris Mason74123bd2007-02-02 11:05:29 -05003335/*
Chris Masond352ac62008-09-29 15:18:18 -04003336 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003337 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003338 * Returns the number of keys that were inserted.
3339 */
3340int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3341 struct btrfs_root *root,
3342 struct btrfs_path *path,
3343 struct btrfs_key *cpu_key, u32 *data_size,
3344 int nr)
3345{
3346 struct extent_buffer *leaf;
3347 struct btrfs_item *item;
3348 int ret = 0;
3349 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003350 int i;
3351 u32 nritems;
3352 u32 total_data = 0;
3353 u32 total_size = 0;
3354 unsigned int data_end;
3355 struct btrfs_disk_key disk_key;
3356 struct btrfs_key found_key;
3357
Yan Zheng87b29b22008-12-17 10:21:48 -05003358 for (i = 0; i < nr; i++) {
3359 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3360 BTRFS_LEAF_DATA_SIZE(root)) {
3361 break;
3362 nr = i;
3363 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003364 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003365 total_size += data_size[i] + sizeof(struct btrfs_item);
3366 }
3367 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003368
Josef Bacikf3465ca2008-11-12 14:19:50 -05003369 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3370 if (ret == 0)
3371 return -EEXIST;
3372 if (ret < 0)
3373 goto out;
3374
Josef Bacikf3465ca2008-11-12 14:19:50 -05003375 leaf = path->nodes[0];
3376
3377 nritems = btrfs_header_nritems(leaf);
3378 data_end = leaf_data_end(root, leaf);
3379
3380 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3381 for (i = nr; i >= 0; i--) {
3382 total_data -= data_size[i];
3383 total_size -= data_size[i] + sizeof(struct btrfs_item);
3384 if (total_size < btrfs_leaf_free_space(root, leaf))
3385 break;
3386 }
3387 nr = i;
3388 }
3389
3390 slot = path->slots[0];
3391 BUG_ON(slot < 0);
3392
3393 if (slot != nritems) {
3394 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3395
3396 item = btrfs_item_nr(leaf, slot);
3397 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3398
3399 /* figure out how many keys we can insert in here */
3400 total_data = data_size[0];
3401 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003402 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003403 break;
3404 total_data += data_size[i];
3405 }
3406 nr = i;
3407
3408 if (old_data < data_end) {
3409 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003410 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003411 slot, old_data, data_end);
3412 BUG_ON(1);
3413 }
3414 /*
3415 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3416 */
3417 /* first correct the data pointers */
3418 WARN_ON(leaf->map_token);
3419 for (i = slot; i < nritems; i++) {
3420 u32 ioff;
3421
3422 item = btrfs_item_nr(leaf, i);
3423 if (!leaf->map_token) {
3424 map_extent_buffer(leaf, (unsigned long)item,
3425 sizeof(struct btrfs_item),
3426 &leaf->map_token, &leaf->kaddr,
3427 &leaf->map_start, &leaf->map_len,
3428 KM_USER1);
3429 }
3430
3431 ioff = btrfs_item_offset(leaf, item);
3432 btrfs_set_item_offset(leaf, item, ioff - total_data);
3433 }
3434 if (leaf->map_token) {
3435 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3436 leaf->map_token = NULL;
3437 }
3438
3439 /* shift the items */
3440 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3441 btrfs_item_nr_offset(slot),
3442 (nritems - slot) * sizeof(struct btrfs_item));
3443
3444 /* shift the data */
3445 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3446 data_end - total_data, btrfs_leaf_data(leaf) +
3447 data_end, old_data - data_end);
3448 data_end = old_data;
3449 } else {
3450 /*
3451 * this sucks but it has to be done, if we are inserting at
3452 * the end of the leaf only insert 1 of the items, since we
3453 * have no way of knowing whats on the next leaf and we'd have
3454 * to drop our current locks to figure it out
3455 */
3456 nr = 1;
3457 }
3458
3459 /* setup the item for the new data */
3460 for (i = 0; i < nr; i++) {
3461 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3462 btrfs_set_item_key(leaf, &disk_key, slot + i);
3463 item = btrfs_item_nr(leaf, slot + i);
3464 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3465 data_end -= data_size[i];
3466 btrfs_set_item_size(leaf, item, data_size[i]);
3467 }
3468 btrfs_set_header_nritems(leaf, nritems + nr);
3469 btrfs_mark_buffer_dirty(leaf);
3470
3471 ret = 0;
3472 if (slot == 0) {
3473 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3474 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3475 }
3476
3477 if (btrfs_leaf_free_space(root, leaf) < 0) {
3478 btrfs_print_leaf(root, leaf);
3479 BUG();
3480 }
3481out:
3482 if (!ret)
3483 ret = nr;
3484 return ret;
3485}
3486
3487/*
Chris Mason44871b12009-03-13 10:04:31 -04003488 * this is a helper for btrfs_insert_empty_items, the main goal here is
3489 * to save stack depth by doing the bulk of the work in a function
3490 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003491 */
Chris Mason44871b12009-03-13 10:04:31 -04003492static noinline_for_stack int
3493setup_items_for_insert(struct btrfs_trans_handle *trans,
3494 struct btrfs_root *root, struct btrfs_path *path,
3495 struct btrfs_key *cpu_key, u32 *data_size,
3496 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003497{
Chris Mason5f39d392007-10-15 16:14:19 -04003498 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003499 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003500 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003501 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003502 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003503 int ret;
3504 struct extent_buffer *leaf;
3505 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003506
Chris Mason5f39d392007-10-15 16:14:19 -04003507 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003508 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003509
Chris Mason5f39d392007-10-15 16:14:19 -04003510 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003511 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003512
Chris Masonf25956c2008-09-12 15:32:53 -04003513 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003514 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003515 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003516 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003517 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003518 }
Chris Mason5f39d392007-10-15 16:14:19 -04003519
Chris Masonbe0e5c02007-01-26 15:51:26 -05003520 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003521 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003522
Chris Mason5f39d392007-10-15 16:14:19 -04003523 if (old_data < data_end) {
3524 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003525 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003526 slot, old_data, data_end);
3527 BUG_ON(1);
3528 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003529 /*
3530 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3531 */
3532 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003533 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003534 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003535 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003536
Chris Mason5f39d392007-10-15 16:14:19 -04003537 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003538 if (!leaf->map_token) {
3539 map_extent_buffer(leaf, (unsigned long)item,
3540 sizeof(struct btrfs_item),
3541 &leaf->map_token, &leaf->kaddr,
3542 &leaf->map_start, &leaf->map_len,
3543 KM_USER1);
3544 }
3545
Chris Mason5f39d392007-10-15 16:14:19 -04003546 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003547 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003548 }
Chris Masondb945352007-10-15 16:15:53 -04003549 if (leaf->map_token) {
3550 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3551 leaf->map_token = NULL;
3552 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003553
3554 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003555 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003556 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003557 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003558
3559 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003560 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003561 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003562 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003563 data_end = old_data;
3564 }
Chris Mason5f39d392007-10-15 16:14:19 -04003565
Chris Mason62e27492007-03-15 12:56:47 -04003566 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003567 for (i = 0; i < nr; i++) {
3568 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3569 btrfs_set_item_key(leaf, &disk_key, slot + i);
3570 item = btrfs_item_nr(leaf, slot + i);
3571 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3572 data_end -= data_size[i];
3573 btrfs_set_item_size(leaf, item, data_size[i]);
3574 }
Chris Mason44871b12009-03-13 10:04:31 -04003575
Chris Mason9c583092008-01-29 15:15:18 -05003576 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003577
3578 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003579 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003580 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003581 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003582 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003583 }
Chris Masonb9473432009-03-13 11:00:37 -04003584 btrfs_unlock_up_safe(path, 1);
3585 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003586
Chris Mason5f39d392007-10-15 16:14:19 -04003587 if (btrfs_leaf_free_space(root, leaf) < 0) {
3588 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003589 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003590 }
Chris Mason44871b12009-03-13 10:04:31 -04003591 return ret;
3592}
3593
3594/*
3595 * Given a key and some data, insert items into the tree.
3596 * This does all the path init required, making room in the tree if needed.
3597 */
3598int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3599 struct btrfs_root *root,
3600 struct btrfs_path *path,
3601 struct btrfs_key *cpu_key, u32 *data_size,
3602 int nr)
3603{
3604 struct extent_buffer *leaf;
3605 int ret = 0;
3606 int slot;
3607 int i;
3608 u32 total_size = 0;
3609 u32 total_data = 0;
3610
3611 for (i = 0; i < nr; i++)
3612 total_data += data_size[i];
3613
3614 total_size = total_data + (nr * sizeof(struct btrfs_item));
3615 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3616 if (ret == 0)
3617 return -EEXIST;
3618 if (ret < 0)
3619 goto out;
3620
3621 leaf = path->nodes[0];
3622 slot = path->slots[0];
3623 BUG_ON(slot < 0);
3624
3625 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3626 total_data, total_size, nr);
3627
Chris Masoned2ff2c2007-03-01 18:59:40 -05003628out:
Chris Mason62e27492007-03-15 12:56:47 -04003629 return ret;
3630}
3631
3632/*
3633 * Given a key and some data, insert an item into the tree.
3634 * This does all the path init required, making room in the tree if needed.
3635 */
Chris Masone089f052007-03-16 16:20:31 -04003636int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3637 *root, struct btrfs_key *cpu_key, void *data, u32
3638 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003639{
3640 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003641 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003642 struct extent_buffer *leaf;
3643 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003644
Chris Mason2c90e5d2007-04-02 10:50:19 -04003645 path = btrfs_alloc_path();
3646 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003647 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003648 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003649 leaf = path->nodes[0];
3650 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3651 write_extent_buffer(leaf, data, ptr, data_size);
3652 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003653 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003654 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003655 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003656}
3657
Chris Mason74123bd2007-02-02 11:05:29 -05003658/*
Chris Mason5de08d72007-02-24 06:24:44 -05003659 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003660 *
Chris Masond352ac62008-09-29 15:18:18 -04003661 * the tree should have been previously balanced so the deletion does not
3662 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003663 */
Chris Masone089f052007-03-16 16:20:31 -04003664static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3665 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003666{
Chris Mason5f39d392007-10-15 16:14:19 -04003667 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003668 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003669 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003670 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003671
Chris Mason5f39d392007-10-15 16:14:19 -04003672 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003673 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003674 memmove_extent_buffer(parent,
3675 btrfs_node_key_ptr_offset(slot),
3676 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003677 sizeof(struct btrfs_key_ptr) *
3678 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003679 }
Chris Mason7518a232007-03-12 12:01:18 -04003680 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003681 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003682 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003683 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003684 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003685 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003686 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003687 struct btrfs_disk_key disk_key;
3688
3689 btrfs_node_key(parent, &disk_key, 0);
3690 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003691 if (wret)
3692 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003693 }
Chris Masond6025572007-03-30 14:27:56 -04003694 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003695 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003696}
3697
Chris Mason74123bd2007-02-02 11:05:29 -05003698/*
Chris Mason323ac952008-10-01 19:05:46 -04003699 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003700 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003701 *
3702 * This deletes the pointer in path->nodes[1] and frees the leaf
3703 * block extent. zero is returned if it all worked out, < 0 otherwise.
3704 *
3705 * The path must have already been setup for deleting the leaf, including
3706 * all the proper balancing. path->nodes[1] must be locked.
3707 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003708static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3709 struct btrfs_root *root,
3710 struct btrfs_path *path,
3711 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003712{
3713 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003714
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003715 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003716 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3717 if (ret)
3718 return ret;
3719
Chris Mason4d081c42009-02-04 09:31:28 -05003720 /*
3721 * btrfs_free_extent is expensive, we want to make sure we
3722 * aren't holding any locks when we call it
3723 */
3724 btrfs_unlock_up_safe(path, 0);
3725
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003726 ret = btrfs_free_extent(trans, root, leaf->start, leaf->len,
3727 0, root->root_key.objectid, 0, 0);
Chris Mason323ac952008-10-01 19:05:46 -04003728 return ret;
3729}
3730/*
Chris Mason74123bd2007-02-02 11:05:29 -05003731 * delete the item at the leaf level in path. If that empties
3732 * the leaf, remove it from the tree
3733 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003734int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3735 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003736{
Chris Mason5f39d392007-10-15 16:14:19 -04003737 struct extent_buffer *leaf;
3738 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003739 int last_off;
3740 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003741 int ret = 0;
3742 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003743 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003744 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003745
Chris Mason5f39d392007-10-15 16:14:19 -04003746 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003747 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3748
3749 for (i = 0; i < nr; i++)
3750 dsize += btrfs_item_size_nr(leaf, slot + i);
3751
Chris Mason5f39d392007-10-15 16:14:19 -04003752 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003753
Chris Mason85e21ba2008-01-29 15:11:36 -05003754 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003755 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
3757 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003758 data_end + dsize,
3759 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003760 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003761
Chris Mason85e21ba2008-01-29 15:11:36 -05003762 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003763 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003764
Chris Mason5f39d392007-10-15 16:14:19 -04003765 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003766 if (!leaf->map_token) {
3767 map_extent_buffer(leaf, (unsigned long)item,
3768 sizeof(struct btrfs_item),
3769 &leaf->map_token, &leaf->kaddr,
3770 &leaf->map_start, &leaf->map_len,
3771 KM_USER1);
3772 }
Chris Mason5f39d392007-10-15 16:14:19 -04003773 ioff = btrfs_item_offset(leaf, item);
3774 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003775 }
Chris Masondb945352007-10-15 16:15:53 -04003776
3777 if (leaf->map_token) {
3778 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3779 leaf->map_token = NULL;
3780 }
3781
Chris Mason5f39d392007-10-15 16:14:19 -04003782 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003783 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003784 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003785 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003786 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003787 btrfs_set_header_nritems(leaf, nritems - nr);
3788 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003789
Chris Mason74123bd2007-02-02 11:05:29 -05003790 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003791 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003792 if (leaf == root->node) {
3793 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003794 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003795 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003796 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003797 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003798 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003799 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003800 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003801 struct btrfs_disk_key disk_key;
3802
3803 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003804 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003805 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003806 if (wret)
3807 ret = wret;
3808 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003809
Chris Mason74123bd2007-02-02 11:05:29 -05003810 /* delete the leaf if it is mostly empty */
Chris Masoncfbb9302009-05-18 10:41:58 -04003811 if (used < BTRFS_LEAF_DATA_SIZE(root) / 2) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003812 /* push_leaf_left fixes the path.
3813 * make sure the path still points to our leaf
3814 * for possible call to del_ptr below
3815 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003816 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003817 extent_buffer_get(leaf);
3818
Chris Masonb9473432009-03-13 11:00:37 -04003819 btrfs_set_path_blocking(path);
Chris Mason85e21ba2008-01-29 15:11:36 -05003820 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003821 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003822 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003823
3824 if (path->nodes[0] == leaf &&
3825 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003826 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003827 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003828 ret = wret;
3829 }
Chris Mason5f39d392007-10-15 16:14:19 -04003830
3831 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003832 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003833 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04003834 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003835 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003836 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003837 /* if we're still in the path, make sure
3838 * we're dirty. Otherwise, one of the
3839 * push_leaf functions must have already
3840 * dirtied this buffer
3841 */
3842 if (path->nodes[0] == leaf)
3843 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003844 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003845 }
Chris Masond5719762007-03-23 10:01:08 -04003846 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003847 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003848 }
3849 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003850 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003851}
3852
Chris Mason97571fd2007-02-24 13:39:08 -05003853/*
Chris Mason925baed2008-06-25 16:01:30 -04003854 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003855 * returns 0 if it found something or 1 if there are no lesser leaves.
3856 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003857 *
3858 * This may release the path, and so you may lose any locks held at the
3859 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003860 */
3861int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3862{
Chris Mason925baed2008-06-25 16:01:30 -04003863 struct btrfs_key key;
3864 struct btrfs_disk_key found_key;
3865 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003866
Chris Mason925baed2008-06-25 16:01:30 -04003867 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003868
Chris Mason925baed2008-06-25 16:01:30 -04003869 if (key.offset > 0)
3870 key.offset--;
3871 else if (key.type > 0)
3872 key.type--;
3873 else if (key.objectid > 0)
3874 key.objectid--;
3875 else
3876 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003877
Chris Mason925baed2008-06-25 16:01:30 -04003878 btrfs_release_path(root, path);
3879 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3880 if (ret < 0)
3881 return ret;
3882 btrfs_item_key(path->nodes[0], &found_key, 0);
3883 ret = comp_keys(&found_key, &key);
3884 if (ret < 0)
3885 return 0;
3886 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003887}
3888
Chris Mason3f157a22008-06-25 16:01:31 -04003889/*
3890 * A helper function to walk down the tree starting at min_key, and looking
3891 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003892 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003893 *
3894 * This does not cow, but it does stuff the starting key it finds back
3895 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3896 * key and get a writable path.
3897 *
3898 * This does lock as it descends, and path->keep_locks should be set
3899 * to 1 by the caller.
3900 *
3901 * This honors path->lowest_level to prevent descent past a given level
3902 * of the tree.
3903 *
Chris Masond352ac62008-09-29 15:18:18 -04003904 * min_trans indicates the oldest transaction that you are interested
3905 * in walking through. Any nodes or leaves older than min_trans are
3906 * skipped over (without reading them).
3907 *
Chris Mason3f157a22008-06-25 16:01:31 -04003908 * returns zero if something useful was found, < 0 on error and 1 if there
3909 * was nothing in the tree that matched the search criteria.
3910 */
3911int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003912 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003913 struct btrfs_path *path, int cache_only,
3914 u64 min_trans)
3915{
3916 struct extent_buffer *cur;
3917 struct btrfs_key found_key;
3918 int slot;
Yan96524802008-07-24 12:19:49 -04003919 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003920 u32 nritems;
3921 int level;
3922 int ret = 1;
3923
Chris Mason934d3752008-12-08 16:43:10 -05003924 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003925again:
3926 cur = btrfs_lock_root_node(root);
3927 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003928 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003929 path->nodes[level] = cur;
3930 path->locks[level] = 1;
3931
3932 if (btrfs_header_generation(cur) < min_trans) {
3933 ret = 1;
3934 goto out;
3935 }
Chris Masond3977122009-01-05 21:25:51 -05003936 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003937 nritems = btrfs_header_nritems(cur);
3938 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003939 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003940
Chris Mason323ac952008-10-01 19:05:46 -04003941 /* at the lowest level, we're done, setup the path and exit */
3942 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003943 if (slot >= nritems)
3944 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003945 ret = 0;
3946 path->slots[level] = slot;
3947 btrfs_item_key_to_cpu(cur, &found_key, slot);
3948 goto out;
3949 }
Yan96524802008-07-24 12:19:49 -04003950 if (sret && slot > 0)
3951 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003952 /*
3953 * check this node pointer against the cache_only and
3954 * min_trans parameters. If it isn't in cache or is too
3955 * old, skip to the next one.
3956 */
Chris Masond3977122009-01-05 21:25:51 -05003957 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003958 u64 blockptr;
3959 u64 gen;
3960 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003961 struct btrfs_disk_key disk_key;
3962
Chris Mason3f157a22008-06-25 16:01:31 -04003963 blockptr = btrfs_node_blockptr(cur, slot);
3964 gen = btrfs_node_ptr_generation(cur, slot);
3965 if (gen < min_trans) {
3966 slot++;
3967 continue;
3968 }
3969 if (!cache_only)
3970 break;
3971
Chris Masone02119d2008-09-05 16:13:11 -04003972 if (max_key) {
3973 btrfs_node_key(cur, &disk_key, slot);
3974 if (comp_keys(&disk_key, max_key) >= 0) {
3975 ret = 1;
3976 goto out;
3977 }
3978 }
3979
Chris Mason3f157a22008-06-25 16:01:31 -04003980 tmp = btrfs_find_tree_block(root, blockptr,
3981 btrfs_level_size(root, level - 1));
3982
3983 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3984 free_extent_buffer(tmp);
3985 break;
3986 }
3987 if (tmp)
3988 free_extent_buffer(tmp);
3989 slot++;
3990 }
Chris Masone02119d2008-09-05 16:13:11 -04003991find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003992 /*
3993 * we didn't find a candidate key in this node, walk forward
3994 * and find another one
3995 */
3996 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003997 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003998 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003999 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004000 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004001 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004002 btrfs_release_path(root, path);
4003 goto again;
4004 } else {
4005 goto out;
4006 }
4007 }
4008 /* save our key for returning back */
4009 btrfs_node_key_to_cpu(cur, &found_key, slot);
4010 path->slots[level] = slot;
4011 if (level == path->lowest_level) {
4012 ret = 0;
4013 unlock_up(path, level, 1);
4014 goto out;
4015 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004016 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004017 cur = read_node_slot(root, cur, slot);
4018
4019 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004020
Chris Mason3f157a22008-06-25 16:01:31 -04004021 path->locks[level - 1] = 1;
4022 path->nodes[level - 1] = cur;
4023 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004024 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004025 }
4026out:
4027 if (ret == 0)
4028 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004029 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004030 return ret;
4031}
4032
4033/*
4034 * this is similar to btrfs_next_leaf, but does not try to preserve
4035 * and fixup the path. It looks for and returns the next key in the
4036 * tree based on the current path and the cache_only and min_trans
4037 * parameters.
4038 *
4039 * 0 is returned if another key is found, < 0 if there are any errors
4040 * and 1 is returned if there are no higher keys in the tree
4041 *
4042 * path->keep_locks should be set to 1 on the search made before
4043 * calling this function.
4044 */
Chris Masone7a84562008-06-25 16:01:31 -04004045int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004046 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004047 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004048{
Chris Masone7a84562008-06-25 16:01:31 -04004049 int slot;
4050 struct extent_buffer *c;
4051
Chris Mason934d3752008-12-08 16:43:10 -05004052 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004053 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004054 if (!path->nodes[level])
4055 return 1;
4056
4057 slot = path->slots[level] + 1;
4058 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004059next:
Chris Masone7a84562008-06-25 16:01:31 -04004060 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004061 int ret;
4062 int orig_lowest;
4063 struct btrfs_key cur_key;
4064 if (level + 1 >= BTRFS_MAX_LEVEL ||
4065 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004066 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004067
4068 if (path->locks[level + 1]) {
4069 level++;
4070 continue;
4071 }
4072
4073 slot = btrfs_header_nritems(c) - 1;
4074 if (level == 0)
4075 btrfs_item_key_to_cpu(c, &cur_key, slot);
4076 else
4077 btrfs_node_key_to_cpu(c, &cur_key, slot);
4078
4079 orig_lowest = path->lowest_level;
4080 btrfs_release_path(root, path);
4081 path->lowest_level = level;
4082 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4083 0, 0);
4084 path->lowest_level = orig_lowest;
4085 if (ret < 0)
4086 return ret;
4087
4088 c = path->nodes[level];
4089 slot = path->slots[level];
4090 if (ret == 0)
4091 slot++;
4092 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004093 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004094
Chris Masone7a84562008-06-25 16:01:31 -04004095 if (level == 0)
4096 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004097 else {
4098 u64 blockptr = btrfs_node_blockptr(c, slot);
4099 u64 gen = btrfs_node_ptr_generation(c, slot);
4100
4101 if (cache_only) {
4102 struct extent_buffer *cur;
4103 cur = btrfs_find_tree_block(root, blockptr,
4104 btrfs_level_size(root, level - 1));
4105 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4106 slot++;
4107 if (cur)
4108 free_extent_buffer(cur);
4109 goto next;
4110 }
4111 free_extent_buffer(cur);
4112 }
4113 if (gen < min_trans) {
4114 slot++;
4115 goto next;
4116 }
Chris Masone7a84562008-06-25 16:01:31 -04004117 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004118 }
Chris Masone7a84562008-06-25 16:01:31 -04004119 return 0;
4120 }
4121 return 1;
4122}
4123
Chris Mason7bb86312007-12-11 09:25:06 -05004124/*
Chris Mason925baed2008-06-25 16:01:30 -04004125 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004126 * returns 0 if it found something or 1 if there are no greater leaves.
4127 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004128 */
Chris Mason234b63a2007-03-13 10:46:10 -04004129int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004130{
4131 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004132 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004133 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004134 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004135 struct btrfs_key key;
4136 u32 nritems;
4137 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004138 int old_spinning = path->leave_spinning;
4139 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004140
4141 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004142 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004143 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004144
Chris Mason8e73f272009-04-03 10:14:18 -04004145 /*
4146 * we take the blocks in an order that upsets lockdep. Using
4147 * blocking mode is the only way around it.
4148 */
4149#ifdef CONFIG_DEBUG_LOCK_ALLOC
4150 force_blocking = 1;
4151#endif
Chris Mason925baed2008-06-25 16:01:30 -04004152
Chris Mason8e73f272009-04-03 10:14:18 -04004153 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4154again:
4155 level = 1;
4156 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004157 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004158
Chris Masona2135012008-06-25 16:01:30 -04004159 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004160
4161 if (!force_blocking)
4162 path->leave_spinning = 1;
4163
Chris Mason925baed2008-06-25 16:01:30 -04004164 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4165 path->keep_locks = 0;
4166
4167 if (ret < 0)
4168 return ret;
4169
Chris Masona2135012008-06-25 16:01:30 -04004170 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004171 /*
4172 * by releasing the path above we dropped all our locks. A balance
4173 * could have added more items next to the key that used to be
4174 * at the very end of the block. So, check again here and
4175 * advance the path if there are now more items available.
4176 */
Chris Masona2135012008-06-25 16:01:30 -04004177 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004178 if (ret == 0)
4179 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004180 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004181 goto done;
4182 }
Chris Masond97e63b2007-02-20 16:40:44 -05004183
Chris Masond3977122009-01-05 21:25:51 -05004184 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004185 if (!path->nodes[level]) {
4186 ret = 1;
4187 goto done;
4188 }
Chris Mason5f39d392007-10-15 16:14:19 -04004189
Chris Masond97e63b2007-02-20 16:40:44 -05004190 slot = path->slots[level] + 1;
4191 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004192 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004193 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004194 if (level == BTRFS_MAX_LEVEL) {
4195 ret = 1;
4196 goto done;
4197 }
Chris Masond97e63b2007-02-20 16:40:44 -05004198 continue;
4199 }
Chris Mason5f39d392007-10-15 16:14:19 -04004200
Chris Mason925baed2008-06-25 16:01:30 -04004201 if (next) {
4202 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004203 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004204 }
Chris Mason5f39d392007-10-15 16:14:19 -04004205
Chris Mason8e73f272009-04-03 10:14:18 -04004206 next = c;
4207 ret = read_block_for_search(NULL, root, path, &next, level,
4208 slot, &key);
4209 if (ret == -EAGAIN)
4210 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004211
Chris Mason76a05b32009-05-14 13:24:30 -04004212 if (ret < 0) {
4213 btrfs_release_path(root, path);
4214 goto done;
4215 }
4216
Chris Mason5cd57b22008-06-25 16:01:30 -04004217 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004218 ret = btrfs_try_spin_lock(next);
4219 if (!ret) {
4220 btrfs_set_path_blocking(path);
4221 btrfs_tree_lock(next);
4222 if (!force_blocking)
4223 btrfs_clear_path_blocking(path, next);
4224 }
4225 if (force_blocking)
4226 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004227 }
Chris Masond97e63b2007-02-20 16:40:44 -05004228 break;
4229 }
4230 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004231 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004232 level--;
4233 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004234 if (path->locks[level])
4235 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004236
Chris Mason5f39d392007-10-15 16:14:19 -04004237 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004238 path->nodes[level] = next;
4239 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004240 if (!path->skip_locking)
4241 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004242
Chris Masond97e63b2007-02-20 16:40:44 -05004243 if (!level)
4244 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004245
Chris Mason8e73f272009-04-03 10:14:18 -04004246 ret = read_block_for_search(NULL, root, path, &next, level,
4247 0, &key);
4248 if (ret == -EAGAIN)
4249 goto again;
4250
Chris Mason76a05b32009-05-14 13:24:30 -04004251 if (ret < 0) {
4252 btrfs_release_path(root, path);
4253 goto done;
4254 }
4255
Chris Mason5cd57b22008-06-25 16:01:30 -04004256 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004257 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004258 ret = btrfs_try_spin_lock(next);
4259 if (!ret) {
4260 btrfs_set_path_blocking(path);
4261 btrfs_tree_lock(next);
4262 if (!force_blocking)
4263 btrfs_clear_path_blocking(path, next);
4264 }
4265 if (force_blocking)
4266 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004267 }
Chris Masond97e63b2007-02-20 16:40:44 -05004268 }
Chris Mason8e73f272009-04-03 10:14:18 -04004269 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004270done:
4271 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004272 path->leave_spinning = old_spinning;
4273 if (!old_spinning)
4274 btrfs_set_path_blocking(path);
4275
4276 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004277}
Chris Mason0b86a832008-03-24 15:01:56 -04004278
Chris Mason3f157a22008-06-25 16:01:31 -04004279/*
4280 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4281 * searching until it gets past min_objectid or finds an item of 'type'
4282 *
4283 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4284 */
Chris Mason0b86a832008-03-24 15:01:56 -04004285int btrfs_previous_item(struct btrfs_root *root,
4286 struct btrfs_path *path, u64 min_objectid,
4287 int type)
4288{
4289 struct btrfs_key found_key;
4290 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004291 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004292 int ret;
4293
Chris Masond3977122009-01-05 21:25:51 -05004294 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004295 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004296 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004297 ret = btrfs_prev_leaf(root, path);
4298 if (ret != 0)
4299 return ret;
4300 } else {
4301 path->slots[0]--;
4302 }
4303 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004304 nritems = btrfs_header_nritems(leaf);
4305 if (nritems == 0)
4306 return 1;
4307 if (path->slots[0] == nritems)
4308 path->slots[0]--;
4309
Chris Mason0b86a832008-03-24 15:01:56 -04004310 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4311 if (found_key.type == type)
4312 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004313 if (found_key.objectid < min_objectid)
4314 break;
4315 if (found_key.objectid == min_objectid &&
4316 found_key.type < type)
4317 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004318 }
4319 return 1;
4320}