blob: 6921231e0efb8e279da15a0d6ff73469e2b9f886 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050021#include "ctree.h"
22#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040023#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040024#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040025#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050026
Chris Masone089f052007-03-16 16:20:31 -040027static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040030 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040031 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040032static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040034 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040035static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040039static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40 struct btrfs_path *path, int level, int slot);
Yan, Zhengad48fd752009-11-12 09:33:58 +000041static int setup_items_for_insert(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root, struct btrfs_path *path,
43 struct btrfs_key *cpu_key, u32 *data_size,
44 u32 total_data, u32 total_size, int nr);
45
Chris Masond97e63b2007-02-20 16:40:44 -050046
Chris Mason2c90e5d2007-04-02 10:50:19 -040047struct btrfs_path *btrfs_alloc_path(void)
48{
Chris Masondf24a2b2007-04-04 09:36:31 -040049 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050050 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
51 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040053 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040054}
55
Chris Masonb4ce94d2009-02-04 09:25:08 -050056/*
57 * set all locked nodes in the path to blocking locks. This should
58 * be done before scheduling
59 */
60noinline void btrfs_set_path_blocking(struct btrfs_path *p)
61{
62 int i;
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
64 if (p->nodes[i] && p->locks[i])
65 btrfs_set_lock_blocking(p->nodes[i]);
66 }
67}
68
69/*
70 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050071 *
72 * held is used to keep lockdep happy, when lockdep is enabled
73 * we set held to a blocking lock before we go around and
74 * retake all the spinlocks in the path. You can safely use NULL
75 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050076 */
Chris Mason4008c042009-02-12 14:09:45 -050077noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
78 struct extent_buffer *held)
Chris Masonb4ce94d2009-02-04 09:25:08 -050079{
80 int i;
Chris Mason4008c042009-02-12 14:09:45 -050081
82#ifdef CONFIG_DEBUG_LOCK_ALLOC
83 /* lockdep really cares that we take all of these spinlocks
84 * in the right order. If any of the locks in the path are not
85 * currently blocking, it is going to complain. So, make really
86 * really sure by forcing the path to blocking before we clear
87 * the path blocking.
88 */
89 if (held)
90 btrfs_set_lock_blocking(held);
91 btrfs_set_path_blocking(p);
92#endif
93
94 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonb4ce94d2009-02-04 09:25:08 -050095 if (p->nodes[i] && p->locks[i])
96 btrfs_clear_lock_blocking(p->nodes[i]);
97 }
Chris Mason4008c042009-02-12 14:09:45 -050098
99#ifdef CONFIG_DEBUG_LOCK_ALLOC
100 if (held)
101 btrfs_clear_lock_blocking(held);
102#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500103}
104
Chris Masond352ac62008-09-29 15:18:18 -0400105/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400106void btrfs_free_path(struct btrfs_path *p)
107{
Chris Masondf24a2b2007-04-04 09:36:31 -0400108 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400109 kmem_cache_free(btrfs_path_cachep, p);
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
113 * path release drops references on the extent buffers in the path
114 * and it drops any locks held by this path
115 *
116 * It is safe to call this on paths that no locks or extent buffers held.
117 */
Chris Masond3977122009-01-05 21:25:51 -0500118noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500119{
120 int i;
Chris Masona2135012008-06-25 16:01:30 -0400121
Chris Mason234b63a2007-03-13 10:46:10 -0400122 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400123 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500124 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400125 continue;
126 if (p->locks[i]) {
127 btrfs_tree_unlock(p->nodes[i]);
128 p->locks[i] = 0;
129 }
Chris Mason5f39d392007-10-15 16:14:19 -0400130 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400131 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500132 }
133}
134
Chris Masond352ac62008-09-29 15:18:18 -0400135/*
136 * safely gets a reference on the root node of a tree. A lock
137 * is not taken, so a concurrent writer may put a different node
138 * at the root of the tree. See btrfs_lock_root_node for the
139 * looping required.
140 *
141 * The extent buffer returned by this has a reference taken, so
142 * it won't disappear. It may stop being the root of the tree
143 * at any time because there are no locks held.
144 */
Chris Mason925baed2008-06-25 16:01:30 -0400145struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
146{
147 struct extent_buffer *eb;
148 spin_lock(&root->node_lock);
149 eb = root->node;
150 extent_buffer_get(eb);
151 spin_unlock(&root->node_lock);
152 return eb;
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/* loop around taking references on and locking the root node of the
156 * tree until you end up with a lock on the root. A locked buffer
157 * is returned, with a reference held.
158 */
Chris Mason925baed2008-06-25 16:01:30 -0400159struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
160{
161 struct extent_buffer *eb;
162
Chris Masond3977122009-01-05 21:25:51 -0500163 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400164 eb = btrfs_root_node(root);
165 btrfs_tree_lock(eb);
166
167 spin_lock(&root->node_lock);
168 if (eb == root->node) {
169 spin_unlock(&root->node_lock);
170 break;
171 }
172 spin_unlock(&root->node_lock);
173
174 btrfs_tree_unlock(eb);
175 free_extent_buffer(eb);
176 }
177 return eb;
178}
179
Chris Masond352ac62008-09-29 15:18:18 -0400180/* cowonly root (everything not a reference counted cow subvolume), just get
181 * put onto a simple dirty list. transaction.c walks this to make sure they
182 * get properly updated on disk.
183 */
Chris Mason0b86a832008-03-24 15:01:56 -0400184static void add_root_to_dirty_list(struct btrfs_root *root)
185{
186 if (root->track_dirty && list_empty(&root->dirty_list)) {
187 list_add(&root->dirty_list,
188 &root->fs_info->dirty_cowonly_roots);
189 }
190}
191
Chris Masond352ac62008-09-29 15:18:18 -0400192/*
193 * used by snapshot creation to make a copy of a root for a tree with
194 * a given objectid. The buffer with the new root node is returned in
195 * cow_ret, and this func returns zero on success or a negative error code.
196 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500197int btrfs_copy_root(struct btrfs_trans_handle *trans,
198 struct btrfs_root *root,
199 struct extent_buffer *buf,
200 struct extent_buffer **cow_ret, u64 new_root_objectid)
201{
202 struct extent_buffer *cow;
203 u32 nritems;
204 int ret = 0;
205 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400206 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500207
208 WARN_ON(root->ref_cows && trans->transid !=
209 root->fs_info->running_transaction->transid);
210 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
211
212 level = btrfs_header_level(buf);
213 nritems = btrfs_header_nritems(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400214 if (level == 0)
215 btrfs_item_key(buf, &disk_key, 0);
216 else
217 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400218
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400219 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
220 new_root_objectid, &disk_key, level,
221 buf->start, 0);
222 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500223 return PTR_ERR(cow);
224
225 copy_extent_buffer(cow, buf, 0, 0, cow->len);
226 btrfs_set_header_bytenr(cow, cow->start);
227 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400228 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
229 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
230 BTRFS_HEADER_FLAG_RELOC);
231 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
232 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
233 else
234 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500235
Yan Zheng2b820322008-11-17 21:11:30 -0500236 write_extent_buffer(cow, root->fs_info->fsid,
237 (unsigned long)btrfs_header_fsid(cow),
238 BTRFS_FSID_SIZE);
239
Chris Masonbe20aa92007-12-17 20:14:01 -0500240 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400241 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
242 ret = btrfs_inc_ref(trans, root, cow, 1);
243 else
244 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500245
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 if (ret)
247 return ret;
248
249 btrfs_mark_buffer_dirty(cow);
250 *cow_ret = cow;
251 return 0;
252}
253
Chris Masond352ac62008-09-29 15:18:18 -0400254/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 * check if the tree block can be shared by multiple trees
256 */
257int btrfs_block_can_be_shared(struct btrfs_root *root,
258 struct extent_buffer *buf)
259{
260 /*
261 * Tree blocks not in refernece counted trees and tree roots
262 * are never shared. If a block was allocated after the last
263 * snapshot and the block was not allocated by tree relocation,
264 * we know the block is not shared.
265 */
266 if (root->ref_cows &&
267 buf != root->node && buf != root->commit_root &&
268 (btrfs_header_generation(buf) <=
269 btrfs_root_last_snapshot(&root->root_item) ||
270 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
271 return 1;
272#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
273 if (root->ref_cows &&
274 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
275 return 1;
276#endif
277 return 0;
278}
279
280static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root,
282 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400283 struct extent_buffer *cow,
284 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400285{
286 u64 refs;
287 u64 owner;
288 u64 flags;
289 u64 new_flags = 0;
290 int ret;
291
292 /*
293 * Backrefs update rules:
294 *
295 * Always use full backrefs for extent pointers in tree block
296 * allocated by tree relocation.
297 *
298 * If a shared tree block is no longer referenced by its owner
299 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
300 * use full backrefs for extent pointers in tree block.
301 *
302 * If a tree block is been relocating
303 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
304 * use full backrefs for extent pointers in tree block.
305 * The reason for this is some operations (such as drop tree)
306 * are only allowed for blocks use full backrefs.
307 */
308
309 if (btrfs_block_can_be_shared(root, buf)) {
310 ret = btrfs_lookup_extent_info(trans, root, buf->start,
311 buf->len, &refs, &flags);
312 BUG_ON(ret);
313 BUG_ON(refs == 0);
314 } else {
315 refs = 1;
316 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
317 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
318 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
319 else
320 flags = 0;
321 }
322
323 owner = btrfs_header_owner(buf);
324 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
325 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
326
327 if (refs > 1) {
328 if ((owner == root->root_key.objectid ||
329 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
330 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
331 ret = btrfs_inc_ref(trans, root, buf, 1);
332 BUG_ON(ret);
333
334 if (root->root_key.objectid ==
335 BTRFS_TREE_RELOC_OBJECTID) {
336 ret = btrfs_dec_ref(trans, root, buf, 0);
337 BUG_ON(ret);
338 ret = btrfs_inc_ref(trans, root, cow, 1);
339 BUG_ON(ret);
340 }
341 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
342 } else {
343
344 if (root->root_key.objectid ==
345 BTRFS_TREE_RELOC_OBJECTID)
346 ret = btrfs_inc_ref(trans, root, cow, 1);
347 else
348 ret = btrfs_inc_ref(trans, root, cow, 0);
349 BUG_ON(ret);
350 }
351 if (new_flags != 0) {
352 ret = btrfs_set_disk_extent_flags(trans, root,
353 buf->start,
354 buf->len,
355 new_flags, 0);
356 BUG_ON(ret);
357 }
358 } else {
359 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
360 if (root->root_key.objectid ==
361 BTRFS_TREE_RELOC_OBJECTID)
362 ret = btrfs_inc_ref(trans, root, cow, 1);
363 else
364 ret = btrfs_inc_ref(trans, root, cow, 0);
365 BUG_ON(ret);
366 ret = btrfs_dec_ref(trans, root, buf, 1);
367 BUG_ON(ret);
368 }
369 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400370 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400371 }
372 return 0;
373}
374
375/*
Chris Masond3977122009-01-05 21:25:51 -0500376 * does the dirty work in cow of a single block. The parent block (if
377 * supplied) is updated to point to the new cow copy. The new buffer is marked
378 * dirty and returned locked. If you modify the block it needs to be marked
379 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400380 *
381 * search_start -- an allocation hint for the new block
382 *
Chris Masond3977122009-01-05 21:25:51 -0500383 * empty_size -- a hint that you plan on doing more cow. This is the size in
384 * bytes the allocator should try to find free next to the block it returns.
385 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400386 */
Chris Masond3977122009-01-05 21:25:51 -0500387static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400388 struct btrfs_root *root,
389 struct extent_buffer *buf,
390 struct extent_buffer *parent, int parent_slot,
391 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400392 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400393{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400394 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400395 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500396 int level;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400397 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400398 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400399 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400400
Chris Mason925baed2008-06-25 16:01:30 -0400401 if (*cow_ret == buf)
402 unlock_orig = 1;
403
Chris Masonb9447ef82009-03-09 11:45:38 -0400404 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400405
Chris Mason7bb86312007-12-11 09:25:06 -0500406 WARN_ON(root->ref_cows && trans->transid !=
407 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400408 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400409
Chris Mason7bb86312007-12-11 09:25:06 -0500410 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400411
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400412 if (level == 0)
413 btrfs_item_key(buf, &disk_key, 0);
414 else
415 btrfs_node_key(buf, &disk_key, 0);
416
417 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
418 if (parent)
419 parent_start = parent->start;
420 else
421 parent_start = 0;
422 } else
423 parent_start = 0;
424
425 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
426 root->root_key.objectid, &disk_key,
427 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400428 if (IS_ERR(cow))
429 return PTR_ERR(cow);
430
Chris Masonb4ce94d2009-02-04 09:25:08 -0500431 /* cow is set to blocking by btrfs_init_new_buffer */
432
Chris Mason5f39d392007-10-15 16:14:19 -0400433 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400434 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400435 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400436 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
437 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
438 BTRFS_HEADER_FLAG_RELOC);
439 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
440 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
441 else
442 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400443
Yan Zheng2b820322008-11-17 21:11:30 -0500444 write_extent_buffer(cow, root->fs_info->fsid,
445 (unsigned long)btrfs_header_fsid(cow),
446 BTRFS_FSID_SIZE);
447
Yan, Zhengf0486c62010-05-16 10:46:25 -0400448 update_ref_for_cow(trans, root, buf, cow, &last_ref);
Zheng Yan1a40e232008-09-26 10:09:34 -0400449
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400450 if (root->ref_cows)
451 btrfs_reloc_cow_block(trans, root, buf, cow);
452
Chris Mason6702ed42007-08-07 16:15:09 -0400453 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400454 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400455 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
456 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
457 parent_start = buf->start;
458 else
459 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400460
461 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400462 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400463 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400464 spin_unlock(&root->node_lock);
465
Yan, Zhengf0486c62010-05-16 10:46:25 -0400466 btrfs_free_tree_block(trans, root, buf, parent_start,
467 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400468 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400469 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400470 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400471 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
472 parent_start = parent->start;
473 else
474 parent_start = 0;
475
476 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400477 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400478 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500479 btrfs_set_node_ptr_generation(parent, parent_slot,
480 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400481 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400482 btrfs_free_tree_block(trans, root, buf, parent_start,
483 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400484 }
Chris Mason925baed2008-06-25 16:01:30 -0400485 if (unlock_orig)
486 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400487 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400488 btrfs_mark_buffer_dirty(cow);
489 *cow_ret = cow;
490 return 0;
491}
492
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400493static inline int should_cow_block(struct btrfs_trans_handle *trans,
494 struct btrfs_root *root,
495 struct extent_buffer *buf)
496{
497 if (btrfs_header_generation(buf) == trans->transid &&
498 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
499 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
500 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
501 return 0;
502 return 1;
503}
504
Chris Masond352ac62008-09-29 15:18:18 -0400505/*
506 * cows a single block, see __btrfs_cow_block for the real work.
507 * This version of it has extra checks so that a block isn't cow'd more than
508 * once per transaction, as long as it hasn't been written yet
509 */
Chris Masond3977122009-01-05 21:25:51 -0500510noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400511 struct btrfs_root *root, struct extent_buffer *buf,
512 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400513 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500514{
Chris Mason6702ed42007-08-07 16:15:09 -0400515 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400516 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500517
Chris Masonccd467d2007-06-28 15:57:36 -0400518 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500519 printk(KERN_CRIT "trans %llu running %llu\n",
520 (unsigned long long)trans->transid,
521 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400522 root->fs_info->running_transaction->transid);
523 WARN_ON(1);
524 }
525 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500526 printk(KERN_CRIT "trans %llu running %llu\n",
527 (unsigned long long)trans->transid,
528 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400529 WARN_ON(1);
530 }
Chris Masondc17ff82008-01-08 15:46:30 -0500531
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400532 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500533 *cow_ret = buf;
534 return 0;
535 }
Chris Masonc4876852009-02-04 09:24:25 -0500536
Chris Mason0b86a832008-03-24 15:01:56 -0400537 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500538
539 if (parent)
540 btrfs_set_lock_blocking(parent);
541 btrfs_set_lock_blocking(buf);
542
Chris Masonf510cfe2007-10-15 16:14:48 -0400543 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400544 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400545 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400546}
547
Chris Masond352ac62008-09-29 15:18:18 -0400548/*
549 * helper function for defrag to decide if two blocks pointed to by a
550 * node are actually close by
551 */
Chris Mason6b800532007-10-15 16:17:34 -0400552static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400553{
Chris Mason6b800532007-10-15 16:17:34 -0400554 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400555 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400556 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400557 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500558 return 0;
559}
560
Chris Mason081e9572007-11-06 10:26:24 -0500561/*
562 * compare two keys in a memcmp fashion
563 */
564static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
565{
566 struct btrfs_key k1;
567
568 btrfs_disk_key_to_cpu(&k1, disk);
569
Diego Calleja20736ab2009-07-24 11:06:52 -0400570 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500571}
572
Josef Bacikf3465ca2008-11-12 14:19:50 -0500573/*
574 * same as comp_keys only with two btrfs_key's
575 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400576int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500577{
578 if (k1->objectid > k2->objectid)
579 return 1;
580 if (k1->objectid < k2->objectid)
581 return -1;
582 if (k1->type > k2->type)
583 return 1;
584 if (k1->type < k2->type)
585 return -1;
586 if (k1->offset > k2->offset)
587 return 1;
588 if (k1->offset < k2->offset)
589 return -1;
590 return 0;
591}
Chris Mason081e9572007-11-06 10:26:24 -0500592
Chris Masond352ac62008-09-29 15:18:18 -0400593/*
594 * this is used by the defrag code to go through all the
595 * leaves pointed to by a node and reallocate them so that
596 * disk order is close to key order
597 */
Chris Mason6702ed42007-08-07 16:15:09 -0400598int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400599 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400600 int start_slot, int cache_only, u64 *last_ret,
601 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400602{
Chris Mason6b800532007-10-15 16:17:34 -0400603 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400604 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400605 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400606 u64 search_start = *last_ret;
607 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400608 u64 other;
609 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400610 int end_slot;
611 int i;
612 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400613 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400614 int uptodate;
615 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500616 int progress_passed = 0;
617 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400618
Chris Mason5708b952007-10-25 15:43:18 -0400619 parent_level = btrfs_header_level(parent);
620 if (cache_only && parent_level != 1)
621 return 0;
622
Chris Masond3977122009-01-05 21:25:51 -0500623 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400624 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500625 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400626 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400627
Chris Mason6b800532007-10-15 16:17:34 -0400628 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400629 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400630 end_slot = parent_nritems;
631
632 if (parent_nritems == 1)
633 return 0;
634
Chris Masonb4ce94d2009-02-04 09:25:08 -0500635 btrfs_set_lock_blocking(parent);
636
Chris Mason6702ed42007-08-07 16:15:09 -0400637 for (i = start_slot; i < end_slot; i++) {
638 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400639
Chris Mason5708b952007-10-25 15:43:18 -0400640 if (!parent->map_token) {
641 map_extent_buffer(parent,
642 btrfs_node_key_ptr_offset(i),
643 sizeof(struct btrfs_key_ptr),
644 &parent->map_token, &parent->kaddr,
645 &parent->map_start, &parent->map_len,
646 KM_USER1);
647 }
Chris Mason081e9572007-11-06 10:26:24 -0500648 btrfs_node_key(parent, &disk_key, i);
649 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
650 continue;
651
652 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400653 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400654 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400655 if (last_block == 0)
656 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400657
Chris Mason6702ed42007-08-07 16:15:09 -0400658 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400659 other = btrfs_node_blockptr(parent, i - 1);
660 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400661 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400662 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400663 other = btrfs_node_blockptr(parent, i + 1);
664 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400665 }
Chris Masone9d0b132007-08-10 14:06:19 -0400666 if (close) {
667 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400668 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400669 }
Chris Mason5708b952007-10-25 15:43:18 -0400670 if (parent->map_token) {
671 unmap_extent_buffer(parent, parent->map_token,
672 KM_USER1);
673 parent->map_token = NULL;
674 }
Chris Mason6702ed42007-08-07 16:15:09 -0400675
Chris Mason6b800532007-10-15 16:17:34 -0400676 cur = btrfs_find_tree_block(root, blocknr, blocksize);
677 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400678 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400679 else
680 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400681 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400682 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400683 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400684 continue;
685 }
Chris Mason6b800532007-10-15 16:17:34 -0400686 if (!cur) {
687 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400688 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400689 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400690 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400691 }
Chris Mason6702ed42007-08-07 16:15:09 -0400692 }
Chris Masone9d0b132007-08-10 14:06:19 -0400693 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400694 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400695
Chris Masone7a84562008-06-25 16:01:31 -0400696 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500697 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400698 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400699 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400700 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400701 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400702 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400703 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400704 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400705 break;
Yan252c38f2007-08-29 09:11:44 -0400706 }
Chris Masone7a84562008-06-25 16:01:31 -0400707 search_start = cur->start;
708 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400709 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400710 btrfs_tree_unlock(cur);
711 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400712 }
Chris Mason5708b952007-10-25 15:43:18 -0400713 if (parent->map_token) {
714 unmap_extent_buffer(parent, parent->map_token,
715 KM_USER1);
716 parent->map_token = NULL;
717 }
Chris Mason6702ed42007-08-07 16:15:09 -0400718 return err;
719}
720
Chris Mason74123bd2007-02-02 11:05:29 -0500721/*
722 * The leaf data grows from end-to-front in the node.
723 * this returns the address of the start of the last item,
724 * which is the stop of the leaf data stack
725 */
Chris Mason123abc82007-03-14 14:14:43 -0400726static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400727 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500728{
Chris Mason5f39d392007-10-15 16:14:19 -0400729 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500730 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400731 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400732 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500733}
734
Chris Masond352ac62008-09-29 15:18:18 -0400735/*
736 * extra debugging checks to make sure all the items in a key are
737 * well formed and in the proper order
738 */
Chris Mason123abc82007-03-14 14:14:43 -0400739static int check_node(struct btrfs_root *root, struct btrfs_path *path,
740 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500741{
Chris Mason5f39d392007-10-15 16:14:19 -0400742 struct extent_buffer *parent = NULL;
743 struct extent_buffer *node = path->nodes[level];
744 struct btrfs_disk_key parent_key;
745 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500746 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400747 int slot;
748 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400749 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500750
751 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400752 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400753
Chris Mason8d7be552007-05-10 11:24:42 -0400754 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400755 BUG_ON(nritems == 0);
756 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400757 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400758 btrfs_node_key(parent, &parent_key, parent_slot);
759 btrfs_node_key(node, &node_key, 0);
760 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400761 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400762 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400763 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500764 }
Chris Mason123abc82007-03-14 14:14:43 -0400765 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400766 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400767 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
768 btrfs_node_key(node, &node_key, slot);
769 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400770 }
771 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400772 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
773 btrfs_node_key(node, &node_key, slot);
774 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500775 }
776 return 0;
777}
778
Chris Masond352ac62008-09-29 15:18:18 -0400779/*
780 * extra checking to make sure all the items in a leaf are
781 * well formed and in the proper order
782 */
Chris Mason123abc82007-03-14 14:14:43 -0400783static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
784 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500785{
Chris Mason5f39d392007-10-15 16:14:19 -0400786 struct extent_buffer *leaf = path->nodes[level];
787 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500788 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400789 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400790 struct btrfs_disk_key parent_key;
791 struct btrfs_disk_key leaf_key;
792 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400793
Chris Mason5f39d392007-10-15 16:14:19 -0400794 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500795
796 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400797 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400798
799 if (nritems == 0)
800 return 0;
801
802 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400803 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400804 btrfs_node_key(parent, &parent_key, parent_slot);
805 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400806
Chris Mason5f39d392007-10-15 16:14:19 -0400807 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400808 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400809 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400810 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500811 }
Chris Mason5f39d392007-10-15 16:14:19 -0400812 if (slot != 0 && slot < nritems - 1) {
813 btrfs_item_key(leaf, &leaf_key, slot);
814 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
815 if (comp_keys(&leaf_key, &cpukey) <= 0) {
816 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500817 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400818 BUG_ON(1);
819 }
820 if (btrfs_item_offset_nr(leaf, slot - 1) !=
821 btrfs_item_end_nr(leaf, slot)) {
822 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500823 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400824 BUG_ON(1);
825 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500826 }
Chris Mason8d7be552007-05-10 11:24:42 -0400827 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400828 btrfs_item_key(leaf, &leaf_key, slot);
829 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
830 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
831 if (btrfs_item_offset_nr(leaf, slot) !=
832 btrfs_item_end_nr(leaf, slot + 1)) {
833 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500834 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400835 BUG_ON(1);
836 }
Chris Mason8d7be552007-05-10 11:24:42 -0400837 }
Chris Mason5f39d392007-10-15 16:14:19 -0400838 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
839 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500840 return 0;
841}
842
Chris Masond3977122009-01-05 21:25:51 -0500843static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500844 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500845{
Chris Mason85d824c2008-04-10 10:23:19 -0400846 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500847 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400848 return check_leaf(root, path, level);
849 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500850}
851
Chris Mason74123bd2007-02-02 11:05:29 -0500852/*
Chris Mason5f39d392007-10-15 16:14:19 -0400853 * search for key in the extent_buffer. The items start at offset p,
854 * and they are item_size apart. There are 'max' items in p.
855 *
Chris Mason74123bd2007-02-02 11:05:29 -0500856 * the slot in the array is returned via slot, and it points to
857 * the place where you would insert key if it is not found in
858 * the array.
859 *
860 * slot may point to max if the key is bigger than all of the keys
861 */
Chris Masone02119d2008-09-05 16:13:11 -0400862static noinline int generic_bin_search(struct extent_buffer *eb,
863 unsigned long p,
864 int item_size, struct btrfs_key *key,
865 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500866{
867 int low = 0;
868 int high = max;
869 int mid;
870 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400871 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400872 struct btrfs_disk_key unaligned;
873 unsigned long offset;
874 char *map_token = NULL;
875 char *kaddr = NULL;
876 unsigned long map_start = 0;
877 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400878 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500879
Chris Masond3977122009-01-05 21:25:51 -0500880 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500881 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 offset = p + mid * item_size;
883
884 if (!map_token || offset < map_start ||
885 (offset + sizeof(struct btrfs_disk_key)) >
886 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400887 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400888 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400889 map_token = NULL;
890 }
Chris Mason934d3752008-12-08 16:43:10 -0500891
892 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400893 sizeof(struct btrfs_disk_key),
894 &map_token, &kaddr,
895 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400896
Chris Mason479965d2007-10-15 16:14:27 -0400897 if (!err) {
898 tmp = (struct btrfs_disk_key *)(kaddr + offset -
899 map_start);
900 } else {
901 read_extent_buffer(eb, &unaligned,
902 offset, sizeof(unaligned));
903 tmp = &unaligned;
904 }
905
Chris Mason5f39d392007-10-15 16:14:19 -0400906 } else {
907 tmp = (struct btrfs_disk_key *)(kaddr + offset -
908 map_start);
909 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500910 ret = comp_keys(tmp, key);
911
912 if (ret < 0)
913 low = mid + 1;
914 else if (ret > 0)
915 high = mid;
916 else {
917 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400918 if (map_token)
919 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500920 return 0;
921 }
922 }
923 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400924 if (map_token)
925 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500926 return 1;
927}
928
Chris Mason97571fd2007-02-24 13:39:08 -0500929/*
930 * simple bin_search frontend that does the right thing for
931 * leaves vs nodes
932 */
Chris Mason5f39d392007-10-15 16:14:19 -0400933static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
934 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500935{
Chris Mason5f39d392007-10-15 16:14:19 -0400936 if (level == 0) {
937 return generic_bin_search(eb,
938 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400939 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400940 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400941 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500942 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400943 return generic_bin_search(eb,
944 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400945 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400946 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400947 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500948 }
949 return -1;
950}
951
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
953 int level, int *slot)
954{
955 return bin_search(eb, key, level, slot);
956}
957
Yan, Zhengf0486c62010-05-16 10:46:25 -0400958static void root_add_used(struct btrfs_root *root, u32 size)
959{
960 spin_lock(&root->accounting_lock);
961 btrfs_set_root_used(&root->root_item,
962 btrfs_root_used(&root->root_item) + size);
963 spin_unlock(&root->accounting_lock);
964}
965
966static void root_sub_used(struct btrfs_root *root, u32 size)
967{
968 spin_lock(&root->accounting_lock);
969 btrfs_set_root_used(&root->root_item,
970 btrfs_root_used(&root->root_item) - size);
971 spin_unlock(&root->accounting_lock);
972}
973
Chris Masond352ac62008-09-29 15:18:18 -0400974/* given a node and slot number, this reads the blocks it points to. The
975 * extent buffer is returned with a reference taken (but unlocked).
976 * NULL is returned on error.
977 */
Chris Masone02119d2008-09-05 16:13:11 -0400978static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400979 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500980{
Chris Masonca7a79a2008-05-12 12:59:19 -0400981 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500982 if (slot < 0)
983 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400984 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500985 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400986
987 BUG_ON(level == 0);
988
Chris Masondb945352007-10-15 16:15:53 -0400989 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400990 btrfs_level_size(root, level - 1),
991 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500992}
993
Chris Masond352ac62008-09-29 15:18:18 -0400994/*
995 * node level balancing, used to make sure nodes are in proper order for
996 * item deletion. We balance from the top down, so we have to make sure
997 * that a deletion won't leave an node completely empty later on.
998 */
Chris Masone02119d2008-09-05 16:13:11 -0400999static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001000 struct btrfs_root *root,
1001 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001002{
Chris Mason5f39d392007-10-15 16:14:19 -04001003 struct extent_buffer *right = NULL;
1004 struct extent_buffer *mid;
1005 struct extent_buffer *left = NULL;
1006 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001007 int ret = 0;
1008 int wret;
1009 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001010 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -04001011 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001012 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001013
1014 if (level == 0)
1015 return 0;
1016
Chris Mason5f39d392007-10-15 16:14:19 -04001017 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001018
Chris Mason925baed2008-06-25 16:01:30 -04001019 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -05001020 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1021
Chris Mason1d4f8a02007-03-13 09:28:32 -04001022 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001023
Chris Mason234b63a2007-03-13 10:46:10 -04001024 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001025 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -05001026 pslot = path->slots[level + 1];
1027
Chris Mason40689472007-03-17 14:29:23 -04001028 /*
1029 * deal with the case where there is only one pointer in the root
1030 * by promoting the node below to a root
1031 */
Chris Mason5f39d392007-10-15 16:14:19 -04001032 if (!parent) {
1033 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001034
Chris Mason5f39d392007-10-15 16:14:19 -04001035 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001036 return 0;
1037
1038 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001039 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -05001040 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -04001041 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001042 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001043 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001044 if (ret) {
1045 btrfs_tree_unlock(child);
1046 free_extent_buffer(child);
1047 goto enospc;
1048 }
Yan2f375ab2008-02-01 14:58:07 -05001049
Chris Mason925baed2008-06-25 16:01:30 -04001050 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -05001051 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -04001052 spin_unlock(&root->node_lock);
1053
Chris Mason0b86a832008-03-24 15:01:56 -04001054 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001055 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001056
Chris Mason925baed2008-06-25 16:01:30 -04001057 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001058 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001059 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001060 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001061 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001062 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001063
1064 root_sub_used(root, mid->len);
1065 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001066 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -04001067 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001068 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001069 }
Chris Mason5f39d392007-10-15 16:14:19 -04001070 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001071 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001072 return 0;
1073
Chris Mason5f39d392007-10-15 16:14:19 -04001074 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001075 err_on_enospc = 1;
1076
Chris Mason5f39d392007-10-15 16:14:19 -04001077 left = read_node_slot(root, parent, pslot - 1);
1078 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001079 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001080 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001081 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001082 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001083 if (wret) {
1084 ret = wret;
1085 goto enospc;
1086 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001087 }
Chris Mason5f39d392007-10-15 16:14:19 -04001088 right = read_node_slot(root, parent, pslot + 1);
1089 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001090 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001091 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001092 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001093 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001094 if (wret) {
1095 ret = wret;
1096 goto enospc;
1097 }
1098 }
1099
1100 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001101 if (left) {
1102 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001103 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001104 if (wret < 0)
1105 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001106 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001107 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001108 }
Chris Mason79f95c82007-03-01 15:16:26 -05001109
1110 /*
1111 * then try to empty the right most buffer into the middle
1112 */
Chris Mason5f39d392007-10-15 16:14:19 -04001113 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001114 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001115 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001116 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001117 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001118 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001119 btrfs_tree_unlock(right);
Chris Masone089f052007-03-16 16:20:31 -04001120 wret = del_ptr(trans, root, path, level + 1, pslot +
1121 1);
Chris Masonbb803952007-03-01 12:04:21 -05001122 if (wret)
1123 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001124 root_sub_used(root, right->len);
1125 btrfs_free_tree_block(trans, root, right, 0, 1);
1126 free_extent_buffer(right);
1127 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001128 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001129 struct btrfs_disk_key right_key;
1130 btrfs_node_key(right, &right_key, 0);
1131 btrfs_set_node_key(parent, &right_key, pslot + 1);
1132 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001133 }
1134 }
Chris Mason5f39d392007-10-15 16:14:19 -04001135 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001136 /*
1137 * we're not allowed to leave a node with one item in the
1138 * tree during a delete. A deletion from lower in the tree
1139 * could try to delete the only pointer in this node.
1140 * So, pull some keys from the left.
1141 * There has to be a left pointer at this point because
1142 * otherwise we would have pulled some pointers from the
1143 * right
1144 */
Chris Mason5f39d392007-10-15 16:14:19 -04001145 BUG_ON(!left);
1146 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001147 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001148 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001149 goto enospc;
1150 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001151 if (wret == 1) {
1152 wret = push_node_left(trans, root, left, mid, 1);
1153 if (wret < 0)
1154 ret = wret;
1155 }
Chris Mason79f95c82007-03-01 15:16:26 -05001156 BUG_ON(wret == 1);
1157 }
Chris Mason5f39d392007-10-15 16:14:19 -04001158 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001159 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001160 btrfs_tree_unlock(mid);
Chris Masone089f052007-03-16 16:20:31 -04001161 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001162 if (wret)
1163 ret = wret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001164 root_sub_used(root, mid->len);
1165 btrfs_free_tree_block(trans, root, mid, 0, 1);
1166 free_extent_buffer(mid);
1167 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001168 } else {
1169 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001170 struct btrfs_disk_key mid_key;
1171 btrfs_node_key(mid, &mid_key, 0);
1172 btrfs_set_node_key(parent, &mid_key, pslot);
1173 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001174 }
Chris Masonbb803952007-03-01 12:04:21 -05001175
Chris Mason79f95c82007-03-01 15:16:26 -05001176 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001177 if (left) {
1178 if (btrfs_header_nritems(left) > orig_slot) {
1179 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001180 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001181 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001182 path->slots[level + 1] -= 1;
1183 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001184 if (mid) {
1185 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001186 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001187 }
Chris Masonbb803952007-03-01 12:04:21 -05001188 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001189 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001190 path->slots[level] = orig_slot;
1191 }
1192 }
Chris Mason79f95c82007-03-01 15:16:26 -05001193 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001194 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001195 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001196 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001197 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001198enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001199 if (right) {
1200 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001201 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001202 }
1203 if (left) {
1204 if (path->nodes[level] != left)
1205 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001206 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001207 }
Chris Masonbb803952007-03-01 12:04:21 -05001208 return ret;
1209}
1210
Chris Masond352ac62008-09-29 15:18:18 -04001211/* Node balancing for insertion. Here we only split or push nodes around
1212 * when they are completely full. This is also done top down, so we
1213 * have to be pessimistic.
1214 */
Chris Masond3977122009-01-05 21:25:51 -05001215static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001216 struct btrfs_root *root,
1217 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001218{
Chris Mason5f39d392007-10-15 16:14:19 -04001219 struct extent_buffer *right = NULL;
1220 struct extent_buffer *mid;
1221 struct extent_buffer *left = NULL;
1222 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001223 int ret = 0;
1224 int wret;
1225 int pslot;
1226 int orig_slot = path->slots[level];
1227 u64 orig_ptr;
1228
1229 if (level == 0)
1230 return 1;
1231
Chris Mason5f39d392007-10-15 16:14:19 -04001232 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001233 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001234 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1235
1236 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001237 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001238 pslot = path->slots[level + 1];
1239
Chris Mason5f39d392007-10-15 16:14:19 -04001240 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001241 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001242
Chris Mason5f39d392007-10-15 16:14:19 -04001243 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001244
1245 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001246 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001247 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001248
1249 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001250 btrfs_set_lock_blocking(left);
1251
Chris Mason5f39d392007-10-15 16:14:19 -04001252 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001253 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1254 wret = 1;
1255 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001256 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001257 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001258 if (ret)
1259 wret = 1;
1260 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001261 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001262 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001263 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001264 }
Chris Masone66f7092007-04-20 13:16:02 -04001265 if (wret < 0)
1266 ret = wret;
1267 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001268 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001269 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001270 btrfs_node_key(mid, &disk_key, 0);
1271 btrfs_set_node_key(parent, &disk_key, pslot);
1272 btrfs_mark_buffer_dirty(parent);
1273 if (btrfs_header_nritems(left) > orig_slot) {
1274 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001275 path->slots[level + 1] -= 1;
1276 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001277 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001278 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001279 } else {
1280 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001281 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001282 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001283 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001284 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001285 }
Chris Masone66f7092007-04-20 13:16:02 -04001286 return 0;
1287 }
Chris Mason925baed2008-06-25 16:01:30 -04001288 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001289 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001290 }
Chris Mason925baed2008-06-25 16:01:30 -04001291 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001292
1293 /*
1294 * then try to empty the right most buffer into the middle
1295 */
Chris Mason5f39d392007-10-15 16:14:19 -04001296 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001297 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001298
Chris Mason925baed2008-06-25 16:01:30 -04001299 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001300 btrfs_set_lock_blocking(right);
1301
Chris Mason5f39d392007-10-15 16:14:19 -04001302 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001303 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1304 wret = 1;
1305 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001306 ret = btrfs_cow_block(trans, root, right,
1307 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001308 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001309 if (ret)
1310 wret = 1;
1311 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001312 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001313 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001314 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001315 }
Chris Masone66f7092007-04-20 13:16:02 -04001316 if (wret < 0)
1317 ret = wret;
1318 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001319 struct btrfs_disk_key disk_key;
1320
1321 btrfs_node_key(right, &disk_key, 0);
1322 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1323 btrfs_mark_buffer_dirty(parent);
1324
1325 if (btrfs_header_nritems(mid) <= orig_slot) {
1326 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001327 path->slots[level + 1] += 1;
1328 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001329 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001330 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001331 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001332 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001333 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001334 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001335 }
Chris Masone66f7092007-04-20 13:16:02 -04001336 return 0;
1337 }
Chris Mason925baed2008-06-25 16:01:30 -04001338 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001339 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001340 }
Chris Masone66f7092007-04-20 13:16:02 -04001341 return 1;
1342}
1343
Chris Mason74123bd2007-02-02 11:05:29 -05001344/*
Chris Masond352ac62008-09-29 15:18:18 -04001345 * readahead one full node of leaves, finding things that are close
1346 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001347 */
Chris Masonc8c42862009-04-03 10:14:18 -04001348static void reada_for_search(struct btrfs_root *root,
1349 struct btrfs_path *path,
1350 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001351{
Chris Mason5f39d392007-10-15 16:14:19 -04001352 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001353 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001354 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001355 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001356 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001357 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001358 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001359 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001360 u32 nr;
1361 u32 blocksize;
1362 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001363
Chris Masona6b6e752007-10-15 16:22:39 -04001364 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001365 return;
1366
Chris Mason6702ed42007-08-07 16:15:09 -04001367 if (!path->nodes[level])
1368 return;
1369
Chris Mason5f39d392007-10-15 16:14:19 -04001370 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001371
Chris Mason3c69fae2007-08-07 15:52:22 -04001372 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001373 blocksize = btrfs_level_size(root, level - 1);
1374 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001375 if (eb) {
1376 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001377 return;
1378 }
1379
Chris Masona7175312009-01-22 09:23:10 -05001380 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001381
Chris Mason5f39d392007-10-15 16:14:19 -04001382 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001383 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001384 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001385 if (direction < 0) {
1386 if (nr == 0)
1387 break;
1388 nr--;
1389 } else if (direction > 0) {
1390 nr++;
1391 if (nr >= nritems)
1392 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001393 }
Chris Mason01f46652007-12-21 16:24:26 -05001394 if (path->reada < 0 && objectid) {
1395 btrfs_node_key(node, &disk_key, nr);
1396 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1397 break;
1398 }
Chris Mason6b800532007-10-15 16:17:34 -04001399 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001400 if ((search <= target && target - search <= 65536) ||
1401 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001402 readahead_tree_block(root, search, blocksize,
1403 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001404 nread += blocksize;
1405 }
1406 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001407 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001408 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001409 }
1410}
Chris Mason925baed2008-06-25 16:01:30 -04001411
Chris Masond352ac62008-09-29 15:18:18 -04001412/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001413 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1414 * cache
1415 */
1416static noinline int reada_for_balance(struct btrfs_root *root,
1417 struct btrfs_path *path, int level)
1418{
1419 int slot;
1420 int nritems;
1421 struct extent_buffer *parent;
1422 struct extent_buffer *eb;
1423 u64 gen;
1424 u64 block1 = 0;
1425 u64 block2 = 0;
1426 int ret = 0;
1427 int blocksize;
1428
Chris Mason8c594ea2009-04-20 15:50:10 -04001429 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001430 if (!parent)
1431 return 0;
1432
1433 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001434 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001435 blocksize = btrfs_level_size(root, level);
1436
1437 if (slot > 0) {
1438 block1 = btrfs_node_blockptr(parent, slot - 1);
1439 gen = btrfs_node_ptr_generation(parent, slot - 1);
1440 eb = btrfs_find_tree_block(root, block1, blocksize);
1441 if (eb && btrfs_buffer_uptodate(eb, gen))
1442 block1 = 0;
1443 free_extent_buffer(eb);
1444 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001445 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001446 block2 = btrfs_node_blockptr(parent, slot + 1);
1447 gen = btrfs_node_ptr_generation(parent, slot + 1);
1448 eb = btrfs_find_tree_block(root, block2, blocksize);
1449 if (eb && btrfs_buffer_uptodate(eb, gen))
1450 block2 = 0;
1451 free_extent_buffer(eb);
1452 }
1453 if (block1 || block2) {
1454 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001455
1456 /* release the whole path */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001457 btrfs_release_path(root, path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001458
1459 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001460 if (block1)
1461 readahead_tree_block(root, block1, blocksize, 0);
1462 if (block2)
1463 readahead_tree_block(root, block2, blocksize, 0);
1464
1465 if (block1) {
1466 eb = read_tree_block(root, block1, blocksize, 0);
1467 free_extent_buffer(eb);
1468 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001469 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001470 eb = read_tree_block(root, block2, blocksize, 0);
1471 free_extent_buffer(eb);
1472 }
1473 }
1474 return ret;
1475}
1476
1477
1478/*
Chris Masond3977122009-01-05 21:25:51 -05001479 * when we walk down the tree, it is usually safe to unlock the higher layers
1480 * in the tree. The exceptions are when our path goes through slot 0, because
1481 * operations on the tree might require changing key pointers higher up in the
1482 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001483 *
Chris Masond3977122009-01-05 21:25:51 -05001484 * callers might also have set path->keep_locks, which tells this code to keep
1485 * the lock if the path points to the last slot in the block. This is part of
1486 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001487 *
Chris Masond3977122009-01-05 21:25:51 -05001488 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1489 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001490 */
Chris Masone02119d2008-09-05 16:13:11 -04001491static noinline void unlock_up(struct btrfs_path *path, int level,
1492 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001493{
1494 int i;
1495 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001496 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001497 struct extent_buffer *t;
1498
1499 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1500 if (!path->nodes[i])
1501 break;
1502 if (!path->locks[i])
1503 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001504 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001505 skip_level = i + 1;
1506 continue;
1507 }
Chris Mason051e1b92008-06-25 16:01:30 -04001508 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001509 u32 nritems;
1510 t = path->nodes[i];
1511 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001512 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001513 skip_level = i + 1;
1514 continue;
1515 }
1516 }
Chris Mason051e1b92008-06-25 16:01:30 -04001517 if (skip_level < i && i >= lowest_unlock)
1518 no_skips = 1;
1519
Chris Mason925baed2008-06-25 16:01:30 -04001520 t = path->nodes[i];
1521 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1522 btrfs_tree_unlock(t);
1523 path->locks[i] = 0;
1524 }
1525 }
1526}
1527
Chris Mason3c69fae2007-08-07 15:52:22 -04001528/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001529 * This releases any locks held in the path starting at level and
1530 * going all the way up to the root.
1531 *
1532 * btrfs_search_slot will keep the lock held on higher nodes in a few
1533 * corner cases, such as COW of the block at slot zero in the node. This
1534 * ignores those rules, and it should only be called when there are no
1535 * more updates to be done higher up in the tree.
1536 */
1537noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1538{
1539 int i;
1540
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001541 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001542 return;
1543
1544 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1545 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001546 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001547 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001548 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001549 btrfs_tree_unlock(path->nodes[i]);
1550 path->locks[i] = 0;
1551 }
1552}
1553
1554/*
Chris Masonc8c42862009-04-03 10:14:18 -04001555 * helper function for btrfs_search_slot. The goal is to find a block
1556 * in cache without setting the path to blocking. If we find the block
1557 * we return zero and the path is unchanged.
1558 *
1559 * If we can't find the block, we set the path blocking and do some
1560 * reada. -EAGAIN is returned and the search must be repeated.
1561 */
1562static int
1563read_block_for_search(struct btrfs_trans_handle *trans,
1564 struct btrfs_root *root, struct btrfs_path *p,
1565 struct extent_buffer **eb_ret, int level, int slot,
1566 struct btrfs_key *key)
1567{
1568 u64 blocknr;
1569 u64 gen;
1570 u32 blocksize;
1571 struct extent_buffer *b = *eb_ret;
1572 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001573 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001574
1575 blocknr = btrfs_node_blockptr(b, slot);
1576 gen = btrfs_node_ptr_generation(b, slot);
1577 blocksize = btrfs_level_size(root, level - 1);
1578
1579 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001580 if (tmp) {
1581 if (btrfs_buffer_uptodate(tmp, 0)) {
1582 if (btrfs_buffer_uptodate(tmp, gen)) {
1583 /*
1584 * we found an up to date block without
1585 * sleeping, return
1586 * right away
1587 */
1588 *eb_ret = tmp;
1589 return 0;
1590 }
1591 /* the pages were up to date, but we failed
1592 * the generation number check. Do a full
1593 * read for the generation number that is correct.
1594 * We must do this without dropping locks so
1595 * we can trust our generation number
1596 */
1597 free_extent_buffer(tmp);
1598 tmp = read_tree_block(root, blocknr, blocksize, gen);
1599 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1600 *eb_ret = tmp;
1601 return 0;
1602 }
1603 free_extent_buffer(tmp);
1604 btrfs_release_path(NULL, p);
1605 return -EIO;
1606 }
Chris Masonc8c42862009-04-03 10:14:18 -04001607 }
1608
1609 /*
1610 * reduce lock contention at high levels
1611 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001612 * we read. Don't release the lock on the current
1613 * level because we need to walk this node to figure
1614 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001615 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001616 btrfs_unlock_up_safe(p, level + 1);
1617 btrfs_set_path_blocking(p);
1618
Chris Masoncb449212010-10-24 11:01:27 -04001619 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001620 if (p->reada)
1621 reada_for_search(root, p, level, slot, key->objectid);
1622
Chris Mason8c594ea2009-04-20 15:50:10 -04001623 btrfs_release_path(NULL, p);
Chris Mason76a05b32009-05-14 13:24:30 -04001624
1625 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001626 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001627 if (tmp) {
1628 /*
1629 * If the read above didn't mark this buffer up to date,
1630 * it will never end up being up to date. Set ret to EIO now
1631 * and give up so that our caller doesn't loop forever
1632 * on our EAGAINs.
1633 */
1634 if (!btrfs_buffer_uptodate(tmp, 0))
1635 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001636 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001637 }
1638 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001639}
1640
1641/*
1642 * helper function for btrfs_search_slot. This does all of the checks
1643 * for node-level blocks and does any balancing required based on
1644 * the ins_len.
1645 *
1646 * If no extra work was required, zero is returned. If we had to
1647 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1648 * start over
1649 */
1650static int
1651setup_nodes_for_search(struct btrfs_trans_handle *trans,
1652 struct btrfs_root *root, struct btrfs_path *p,
1653 struct extent_buffer *b, int level, int ins_len)
1654{
1655 int ret;
1656 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1657 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1658 int sret;
1659
1660 sret = reada_for_balance(root, p, level);
1661 if (sret)
1662 goto again;
1663
1664 btrfs_set_path_blocking(p);
1665 sret = split_node(trans, root, p, level);
1666 btrfs_clear_path_blocking(p, NULL);
1667
1668 BUG_ON(sret > 0);
1669 if (sret) {
1670 ret = sret;
1671 goto done;
1672 }
1673 b = p->nodes[level];
1674 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04001675 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001676 int sret;
1677
1678 sret = reada_for_balance(root, p, level);
1679 if (sret)
1680 goto again;
1681
1682 btrfs_set_path_blocking(p);
1683 sret = balance_level(trans, root, p, level);
1684 btrfs_clear_path_blocking(p, NULL);
1685
1686 if (sret) {
1687 ret = sret;
1688 goto done;
1689 }
1690 b = p->nodes[level];
1691 if (!b) {
1692 btrfs_release_path(NULL, p);
1693 goto again;
1694 }
1695 BUG_ON(btrfs_header_nritems(b) == 1);
1696 }
1697 return 0;
1698
1699again:
1700 ret = -EAGAIN;
1701done:
1702 return ret;
1703}
1704
1705/*
Chris Mason74123bd2007-02-02 11:05:29 -05001706 * look for key in the tree. path is filled in with nodes along the way
1707 * if key is found, we return zero and you can find the item in the leaf
1708 * level of the path (level 0)
1709 *
1710 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001711 * be inserted, and 1 is returned. If there are other errors during the
1712 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001713 *
1714 * if ins_len > 0, nodes and leaves will be split as we walk down the
1715 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1716 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001717 */
Chris Masone089f052007-03-16 16:20:31 -04001718int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1719 *root, struct btrfs_key *key, struct btrfs_path *p, int
1720 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001721{
Chris Mason5f39d392007-10-15 16:14:19 -04001722 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001723 int slot;
1724 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001725 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001726 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001727 int lowest_unlock = 1;
Chris Mason9f3a7422007-08-07 15:52:19 -04001728 u8 lowest_level = 0;
1729
Chris Mason6702ed42007-08-07 16:15:09 -04001730 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001731 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001732 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001733
Chris Mason925baed2008-06-25 16:01:30 -04001734 if (ins_len < 0)
1735 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001736
Chris Masonbb803952007-03-01 12:04:21 -05001737again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001738 if (p->search_commit_root) {
1739 b = root->commit_root;
1740 extent_buffer_get(b);
1741 if (!p->skip_locking)
1742 btrfs_tree_lock(b);
1743 } else {
1744 if (p->skip_locking)
1745 b = btrfs_root_node(root);
1746 else
1747 b = btrfs_lock_root_node(root);
1748 }
Chris Mason925baed2008-06-25 16:01:30 -04001749
Chris Masoneb60cea2007-02-02 09:18:22 -05001750 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001751 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001752
1753 /*
1754 * setup the path here so we can release it under lock
1755 * contention with the cow code
1756 */
1757 p->nodes[level] = b;
1758 if (!p->skip_locking)
1759 p->locks[level] = 1;
1760
Chris Mason02217ed2007-03-02 16:08:05 -05001761 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04001762 /*
1763 * if we don't really need to cow this block
1764 * then we don't want to set the path blocking,
1765 * so we test it here
1766 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001767 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04001768 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001769
Chris Masonb4ce94d2009-02-04 09:25:08 -05001770 btrfs_set_path_blocking(p);
1771
Yan Zheng33c66f42009-07-22 09:59:00 -04001772 err = btrfs_cow_block(trans, root, b,
1773 p->nodes[level + 1],
1774 p->slots[level + 1], &b);
1775 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001776 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001777 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001778 }
Chris Mason02217ed2007-03-02 16:08:05 -05001779 }
Chris Mason65b51a02008-08-01 15:11:20 -04001780cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001781 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001782 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001783 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001784 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001785
Chris Masoneb60cea2007-02-02 09:18:22 -05001786 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001787 if (!p->skip_locking)
1788 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001789
Chris Mason4008c042009-02-12 14:09:45 -05001790 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001791
1792 /*
1793 * we have a lock on b and as long as we aren't changing
1794 * the tree, there is no way to for the items in b to change.
1795 * It is safe to drop the lock on our parent before we
1796 * go through the expensive btree search on b.
1797 *
1798 * If cow is true, then we might be changing slot zero,
1799 * which may require changing the parent. So, we can't
1800 * drop the lock until after we know which slot we're
1801 * operating on.
1802 */
1803 if (!cow)
1804 btrfs_unlock_up_safe(p, level + 1);
1805
Chris Mason123abc82007-03-14 14:14:43 -04001806 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001807 if (ret) {
1808 ret = -1;
1809 goto done;
1810 }
Chris Mason925baed2008-06-25 16:01:30 -04001811
Chris Mason5f39d392007-10-15 16:14:19 -04001812 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001813
Chris Mason5f39d392007-10-15 16:14:19 -04001814 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001815 int dec = 0;
1816 if (ret && slot > 0) {
1817 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04001819 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001820 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04001821 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonc8c42862009-04-03 10:14:18 -04001822 ins_len);
Yan Zheng33c66f42009-07-22 09:59:00 -04001823 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001824 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001825 if (err) {
1826 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04001827 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001828 }
Chris Masonc8c42862009-04-03 10:14:18 -04001829 b = p->nodes[level];
1830 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001831
Chris Masonf9efa9c2008-06-25 16:14:04 -04001832 unlock_up(p, level, lowest_unlock);
1833
Chris Mason925baed2008-06-25 16:01:30 -04001834 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001835 if (dec)
1836 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001837 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001838 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001839
Yan Zheng33c66f42009-07-22 09:59:00 -04001840 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04001841 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04001842 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04001843 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04001844 if (err) {
1845 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04001846 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04001847 }
Chris Mason76a05b32009-05-14 13:24:30 -04001848
Chris Masonb4ce94d2009-02-04 09:25:08 -05001849 if (!p->skip_locking) {
Chris Mason4008c042009-02-12 14:09:45 -05001850 btrfs_clear_path_blocking(p, NULL);
Yan Zheng33c66f42009-07-22 09:59:00 -04001851 err = btrfs_try_spin_lock(b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001852
Yan Zheng33c66f42009-07-22 09:59:00 -04001853 if (!err) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001854 btrfs_set_path_blocking(p);
1855 btrfs_tree_lock(b);
Chris Mason4008c042009-02-12 14:09:45 -05001856 btrfs_clear_path_blocking(p, b);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001857 }
1858 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001859 } else {
1860 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001861 if (ins_len > 0 &&
1862 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001863 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04001864 err = split_leaf(trans, root, key,
1865 p, ins_len, ret == 0);
Chris Mason4008c042009-02-12 14:09:45 -05001866 btrfs_clear_path_blocking(p, NULL);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001867
Yan Zheng33c66f42009-07-22 09:59:00 -04001868 BUG_ON(err > 0);
1869 if (err) {
1870 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001871 goto done;
1872 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001873 }
Chris Mason459931e2008-12-10 09:10:46 -05001874 if (!p->search_for_split)
1875 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001876 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001877 }
1878 }
Chris Mason65b51a02008-08-01 15:11:20 -04001879 ret = 1;
1880done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001881 /*
1882 * we don't really know what they plan on doing with the path
1883 * from here on, so for now just mark it as blocking
1884 */
Chris Masonb9473432009-03-13 11:00:37 -04001885 if (!p->leave_spinning)
1886 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001887 if (ret < 0)
1888 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001889 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001890}
1891
Chris Mason74123bd2007-02-02 11:05:29 -05001892/*
1893 * adjust the pointers going up the tree, starting at level
1894 * making sure the right key of each node is points to 'key'.
1895 * This is used after shifting pointers to the left, so it stops
1896 * fixing up pointers when a given leaf/node is not in slot 0 of the
1897 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001898 *
1899 * If this fails to write a tree block, it returns -1, but continues
1900 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001901 */
Chris Mason5f39d392007-10-15 16:14:19 -04001902static int fixup_low_keys(struct btrfs_trans_handle *trans,
1903 struct btrfs_root *root, struct btrfs_path *path,
1904 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001905{
1906 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001907 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001908 struct extent_buffer *t;
1909
Chris Mason234b63a2007-03-13 10:46:10 -04001910 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001911 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001912 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001913 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001914 t = path->nodes[i];
1915 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001916 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001917 if (tslot != 0)
1918 break;
1919 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001920 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001921}
1922
Chris Mason74123bd2007-02-02 11:05:29 -05001923/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001924 * update item key.
1925 *
1926 * This function isn't completely safe. It's the caller's responsibility
1927 * that the new key won't break the order
1928 */
1929int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1930 struct btrfs_root *root, struct btrfs_path *path,
1931 struct btrfs_key *new_key)
1932{
1933 struct btrfs_disk_key disk_key;
1934 struct extent_buffer *eb;
1935 int slot;
1936
1937 eb = path->nodes[0];
1938 slot = path->slots[0];
1939 if (slot > 0) {
1940 btrfs_item_key(eb, &disk_key, slot - 1);
1941 if (comp_keys(&disk_key, new_key) >= 0)
1942 return -1;
1943 }
1944 if (slot < btrfs_header_nritems(eb) - 1) {
1945 btrfs_item_key(eb, &disk_key, slot + 1);
1946 if (comp_keys(&disk_key, new_key) <= 0)
1947 return -1;
1948 }
1949
1950 btrfs_cpu_key_to_disk(&disk_key, new_key);
1951 btrfs_set_item_key(eb, &disk_key, slot);
1952 btrfs_mark_buffer_dirty(eb);
1953 if (slot == 0)
1954 fixup_low_keys(trans, root, path, &disk_key, 1);
1955 return 0;
1956}
1957
1958/*
Chris Mason74123bd2007-02-02 11:05:29 -05001959 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001960 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001961 *
1962 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1963 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001964 */
Chris Mason98ed5172008-01-03 10:01:48 -05001965static int push_node_left(struct btrfs_trans_handle *trans,
1966 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001967 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001968{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001969 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001970 int src_nritems;
1971 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001972 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001973
Chris Mason5f39d392007-10-15 16:14:19 -04001974 src_nritems = btrfs_header_nritems(src);
1975 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001976 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001977 WARN_ON(btrfs_header_generation(src) != trans->transid);
1978 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001979
Chris Masonbce4eae2008-04-24 14:42:46 -04001980 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001981 return 1;
1982
Chris Masond3977122009-01-05 21:25:51 -05001983 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001984 return 1;
1985
Chris Masonbce4eae2008-04-24 14:42:46 -04001986 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001987 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001988 if (push_items < src_nritems) {
1989 /* leave at least 8 pointers in the node if
1990 * we aren't going to empty it
1991 */
1992 if (src_nritems - push_items < 8) {
1993 if (push_items <= 8)
1994 return 1;
1995 push_items -= 8;
1996 }
1997 }
1998 } else
1999 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002000
Chris Mason5f39d392007-10-15 16:14:19 -04002001 copy_extent_buffer(dst, src,
2002 btrfs_node_key_ptr_offset(dst_nritems),
2003 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002004 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002005
Chris Masonbb803952007-03-01 12:04:21 -05002006 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002007 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2008 btrfs_node_key_ptr_offset(push_items),
2009 (src_nritems - push_items) *
2010 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002011 }
Chris Mason5f39d392007-10-15 16:14:19 -04002012 btrfs_set_header_nritems(src, src_nritems - push_items);
2013 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2014 btrfs_mark_buffer_dirty(src);
2015 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002016
Chris Masonbb803952007-03-01 12:04:21 -05002017 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002018}
2019
Chris Mason97571fd2007-02-24 13:39:08 -05002020/*
Chris Mason79f95c82007-03-01 15:16:26 -05002021 * try to push data from one node into the next node right in the
2022 * tree.
2023 *
2024 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2025 * error, and > 0 if there was no room in the right hand block.
2026 *
2027 * this will only push up to 1/2 the contents of the left node over
2028 */
Chris Mason5f39d392007-10-15 16:14:19 -04002029static int balance_node_right(struct btrfs_trans_handle *trans,
2030 struct btrfs_root *root,
2031 struct extent_buffer *dst,
2032 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002033{
Chris Mason79f95c82007-03-01 15:16:26 -05002034 int push_items = 0;
2035 int max_push;
2036 int src_nritems;
2037 int dst_nritems;
2038 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002039
Chris Mason7bb86312007-12-11 09:25:06 -05002040 WARN_ON(btrfs_header_generation(src) != trans->transid);
2041 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 src_nritems = btrfs_header_nritems(src);
2044 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002045 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002046 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002047 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002048
Chris Masond3977122009-01-05 21:25:51 -05002049 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002050 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002051
2052 max_push = src_nritems / 2 + 1;
2053 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002054 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002055 return 1;
Yan252c38f2007-08-29 09:11:44 -04002056
Chris Mason79f95c82007-03-01 15:16:26 -05002057 if (max_push < push_items)
2058 push_items = max_push;
2059
Chris Mason5f39d392007-10-15 16:14:19 -04002060 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2061 btrfs_node_key_ptr_offset(0),
2062 (dst_nritems) *
2063 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002064
Chris Mason5f39d392007-10-15 16:14:19 -04002065 copy_extent_buffer(dst, src,
2066 btrfs_node_key_ptr_offset(0),
2067 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002068 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002069
Chris Mason5f39d392007-10-15 16:14:19 -04002070 btrfs_set_header_nritems(src, src_nritems - push_items);
2071 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002072
Chris Mason5f39d392007-10-15 16:14:19 -04002073 btrfs_mark_buffer_dirty(src);
2074 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002075
Chris Mason79f95c82007-03-01 15:16:26 -05002076 return ret;
2077}
2078
2079/*
Chris Mason97571fd2007-02-24 13:39:08 -05002080 * helper function to insert a new root level in the tree.
2081 * A new node is allocated, and a single item is inserted to
2082 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002083 *
2084 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002085 */
Chris Masond3977122009-01-05 21:25:51 -05002086static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002087 struct btrfs_root *root,
2088 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002089{
Chris Mason7bb86312007-12-11 09:25:06 -05002090 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002091 struct extent_buffer *lower;
2092 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002093 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002094 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002095
2096 BUG_ON(path->nodes[level]);
2097 BUG_ON(path->nodes[level-1] != root->node);
2098
Chris Mason7bb86312007-12-11 09:25:06 -05002099 lower = path->nodes[level-1];
2100 if (level == 1)
2101 btrfs_item_key(lower, &lower_key, 0);
2102 else
2103 btrfs_node_key(lower, &lower_key, 0);
2104
Zheng Yan31840ae2008-09-23 13:14:14 -04002105 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002106 root->root_key.objectid, &lower_key,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002107 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002108 if (IS_ERR(c))
2109 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002110
Yan, Zhengf0486c62010-05-16 10:46:25 -04002111 root_add_used(root, root->nodesize);
2112
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002114 btrfs_set_header_nritems(c, 1);
2115 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002116 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002117 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002118 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002119 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002120
Chris Mason5f39d392007-10-15 16:14:19 -04002121 write_extent_buffer(c, root->fs_info->fsid,
2122 (unsigned long)btrfs_header_fsid(c),
2123 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002124
2125 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2126 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2127 BTRFS_UUID_SIZE);
2128
Chris Mason5f39d392007-10-15 16:14:19 -04002129 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002130 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002131 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002132 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002133
2134 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002135
2136 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002137
Chris Mason925baed2008-06-25 16:01:30 -04002138 spin_lock(&root->node_lock);
2139 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002140 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002141 spin_unlock(&root->node_lock);
2142
2143 /* the super has an extra ref to root->node */
2144 free_extent_buffer(old);
2145
Chris Mason0b86a832008-03-24 15:01:56 -04002146 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002147 extent_buffer_get(c);
2148 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002149 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002150 path->slots[level] = 0;
2151 return 0;
2152}
2153
Chris Mason74123bd2007-02-02 11:05:29 -05002154/*
2155 * worker function to insert a single pointer in a node.
2156 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002157 *
Chris Mason74123bd2007-02-02 11:05:29 -05002158 * slot and level indicate where you want the key to go, and
2159 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002160 *
2161 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002162 */
Chris Masone089f052007-03-16 16:20:31 -04002163static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2164 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002165 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002166{
Chris Mason5f39d392007-10-15 16:14:19 -04002167 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002168 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002169
2170 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002171 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002172 lower = path->nodes[level];
2173 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002174 BUG_ON(slot > nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002175 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002176 BUG();
2177 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002178 memmove_extent_buffer(lower,
2179 btrfs_node_key_ptr_offset(slot + 1),
2180 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002181 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002182 }
Chris Mason5f39d392007-10-15 16:14:19 -04002183 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002184 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002185 WARN_ON(trans->transid == 0);
2186 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002187 btrfs_set_header_nritems(lower, nritems + 1);
2188 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002189 return 0;
2190}
2191
Chris Mason97571fd2007-02-24 13:39:08 -05002192/*
2193 * split the node at the specified level in path in two.
2194 * The path is corrected to point to the appropriate node after the split
2195 *
2196 * Before splitting this tries to make some room in the node by pushing
2197 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002198 *
2199 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002200 */
Chris Masone02119d2008-09-05 16:13:11 -04002201static noinline int split_node(struct btrfs_trans_handle *trans,
2202 struct btrfs_root *root,
2203 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002204{
Chris Mason5f39d392007-10-15 16:14:19 -04002205 struct extent_buffer *c;
2206 struct extent_buffer *split;
2207 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002208 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002209 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002210 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002211 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002212
Chris Mason5f39d392007-10-15 16:14:19 -04002213 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002214 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002215 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002216 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002217 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002218 if (ret)
2219 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002220 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002221 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002222 c = path->nodes[level];
2223 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002224 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002225 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002226 if (ret < 0)
2227 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002228 }
Chris Masone66f7092007-04-20 13:16:02 -04002229
Chris Mason5f39d392007-10-15 16:14:19 -04002230 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002231 mid = (c_nritems + 1) / 2;
2232 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002233
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002234 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002235 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002236 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002237 if (IS_ERR(split))
2238 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002239
Yan, Zhengf0486c62010-05-16 10:46:25 -04002240 root_add_used(root, root->nodesize);
2241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002243 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002244 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002245 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002247 btrfs_set_header_owner(split, root->root_key.objectid);
2248 write_extent_buffer(split, root->fs_info->fsid,
2249 (unsigned long)btrfs_header_fsid(split),
2250 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002251 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2252 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2253 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002254
Chris Mason5f39d392007-10-15 16:14:19 -04002255
2256 copy_extent_buffer(split, c,
2257 btrfs_node_key_ptr_offset(0),
2258 btrfs_node_key_ptr_offset(mid),
2259 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2260 btrfs_set_header_nritems(split, c_nritems - mid);
2261 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002262 ret = 0;
2263
Chris Mason5f39d392007-10-15 16:14:19 -04002264 btrfs_mark_buffer_dirty(c);
2265 btrfs_mark_buffer_dirty(split);
2266
Chris Masondb945352007-10-15 16:15:53 -04002267 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002268 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002269 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002270 if (wret)
2271 ret = wret;
2272
Chris Mason5de08d72007-02-24 06:24:44 -05002273 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002274 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002275 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002276 free_extent_buffer(c);
2277 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002278 path->slots[level + 1] += 1;
2279 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002280 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002281 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002282 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002283 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002284}
2285
Chris Mason74123bd2007-02-02 11:05:29 -05002286/*
2287 * how many bytes are required to store the items in a leaf. start
2288 * and nr indicate which items in the leaf to check. This totals up the
2289 * space used both by the item structs and the item data
2290 */
Chris Mason5f39d392007-10-15 16:14:19 -04002291static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002292{
2293 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002294 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002295 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002296
2297 if (!nr)
2298 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002299 data_len = btrfs_item_end_nr(l, start);
2300 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002301 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002302 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002303 return data_len;
2304}
2305
Chris Mason74123bd2007-02-02 11:05:29 -05002306/*
Chris Masond4dbff92007-04-04 14:08:15 -04002307 * The space between the end of the leaf items and
2308 * the start of the leaf data. IOW, how much room
2309 * the leaf has left for both items and data
2310 */
Chris Masond3977122009-01-05 21:25:51 -05002311noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002312 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002313{
Chris Mason5f39d392007-10-15 16:14:19 -04002314 int nritems = btrfs_header_nritems(leaf);
2315 int ret;
2316 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2317 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002318 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2319 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002320 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002321 leaf_space_used(leaf, 0, nritems), nritems);
2322 }
2323 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002324}
2325
Chris Mason99d8f832010-07-07 10:51:48 -04002326/*
2327 * min slot controls the lowest index we're willing to push to the
2328 * right. We'll push up to and including min_slot, but no lower
2329 */
Chris Mason44871b12009-03-13 10:04:31 -04002330static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2331 struct btrfs_root *root,
2332 struct btrfs_path *path,
2333 int data_size, int empty,
2334 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002335 int free_space, u32 left_nritems,
2336 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002337{
Chris Mason5f39d392007-10-15 16:14:19 -04002338 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002339 struct extent_buffer *upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002340 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002341 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002342 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002343 int push_space = 0;
2344 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002345 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002346 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002347 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002348 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002349 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002350
Chris Mason34a38212007-11-07 13:31:03 -05002351 if (empty)
2352 nr = 0;
2353 else
Chris Mason99d8f832010-07-07 10:51:48 -04002354 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002355
Zheng Yan31840ae2008-09-23 13:14:14 -04002356 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002357 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002358
Chris Mason44871b12009-03-13 10:04:31 -04002359 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002360 i = left_nritems - 1;
2361 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002362 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002363
Zheng Yan31840ae2008-09-23 13:14:14 -04002364 if (!empty && push_items > 0) {
2365 if (path->slots[0] > i)
2366 break;
2367 if (path->slots[0] == i) {
2368 int space = btrfs_leaf_free_space(root, left);
2369 if (space + push_space * 2 > free_space)
2370 break;
2371 }
2372 }
2373
Chris Mason00ec4c52007-02-24 12:47:20 -05002374 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002375 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002376
2377 if (!left->map_token) {
2378 map_extent_buffer(left, (unsigned long)item,
2379 sizeof(struct btrfs_item),
2380 &left->map_token, &left->kaddr,
2381 &left->map_start, &left->map_len,
2382 KM_USER1);
2383 }
2384
2385 this_item_size = btrfs_item_size(left, item);
2386 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002387 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002388
Chris Mason00ec4c52007-02-24 12:47:20 -05002389 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002390 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002391 if (i == 0)
2392 break;
2393 i--;
Chris Masondb945352007-10-15 16:15:53 -04002394 }
2395 if (left->map_token) {
2396 unmap_extent_buffer(left, left->map_token, KM_USER1);
2397 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002398 }
Chris Mason5f39d392007-10-15 16:14:19 -04002399
Chris Mason925baed2008-06-25 16:01:30 -04002400 if (push_items == 0)
2401 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002402
Chris Mason34a38212007-11-07 13:31:03 -05002403 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002404 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002405
Chris Mason00ec4c52007-02-24 12:47:20 -05002406 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002407 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002408
Chris Mason5f39d392007-10-15 16:14:19 -04002409 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002410 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002411
Chris Mason00ec4c52007-02-24 12:47:20 -05002412 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002413 data_end = leaf_data_end(root, right);
2414 memmove_extent_buffer(right,
2415 btrfs_leaf_data(right) + data_end - push_space,
2416 btrfs_leaf_data(right) + data_end,
2417 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2418
Chris Mason00ec4c52007-02-24 12:47:20 -05002419 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002420 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002421 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2422 btrfs_leaf_data(left) + leaf_data_end(root, left),
2423 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002424
2425 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2426 btrfs_item_nr_offset(0),
2427 right_nritems * sizeof(struct btrfs_item));
2428
Chris Mason00ec4c52007-02-24 12:47:20 -05002429 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002430 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2431 btrfs_item_nr_offset(left_nritems - push_items),
2432 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002433
2434 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002435 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002436 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002437 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002438 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002439 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002440 if (!right->map_token) {
2441 map_extent_buffer(right, (unsigned long)item,
2442 sizeof(struct btrfs_item),
2443 &right->map_token, &right->kaddr,
2444 &right->map_start, &right->map_len,
2445 KM_USER1);
2446 }
2447 push_space -= btrfs_item_size(right, item);
2448 btrfs_set_item_offset(right, item, push_space);
2449 }
2450
2451 if (right->map_token) {
2452 unmap_extent_buffer(right, right->map_token, KM_USER1);
2453 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002454 }
Chris Mason7518a232007-03-12 12:01:18 -04002455 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002456 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002457
Chris Mason34a38212007-11-07 13:31:03 -05002458 if (left_nritems)
2459 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002460 else
2461 clean_tree_block(trans, root, left);
2462
Chris Mason5f39d392007-10-15 16:14:19 -04002463 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002464
Chris Mason5f39d392007-10-15 16:14:19 -04002465 btrfs_item_key(right, &disk_key, 0);
2466 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002467 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002468
Chris Mason00ec4c52007-02-24 12:47:20 -05002469 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002470 if (path->slots[0] >= left_nritems) {
2471 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002472 if (btrfs_header_nritems(path->nodes[0]) == 0)
2473 clean_tree_block(trans, root, path->nodes[0]);
2474 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002475 free_extent_buffer(path->nodes[0]);
2476 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002477 path->slots[1] += 1;
2478 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002479 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002480 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002481 }
2482 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002483
2484out_unlock:
2485 btrfs_tree_unlock(right);
2486 free_extent_buffer(right);
2487 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002488}
Chris Mason925baed2008-06-25 16:01:30 -04002489
Chris Mason00ec4c52007-02-24 12:47:20 -05002490/*
Chris Mason44871b12009-03-13 10:04:31 -04002491 * push some data in the path leaf to the right, trying to free up at
2492 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2493 *
2494 * returns 1 if the push failed because the other node didn't have enough
2495 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002496 *
2497 * this will push starting from min_slot to the end of the leaf. It won't
2498 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002499 */
2500static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002501 *root, struct btrfs_path *path,
2502 int min_data_size, int data_size,
2503 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002504{
2505 struct extent_buffer *left = path->nodes[0];
2506 struct extent_buffer *right;
2507 struct extent_buffer *upper;
2508 int slot;
2509 int free_space;
2510 u32 left_nritems;
2511 int ret;
2512
2513 if (!path->nodes[1])
2514 return 1;
2515
2516 slot = path->slots[1];
2517 upper = path->nodes[1];
2518 if (slot >= btrfs_header_nritems(upper) - 1)
2519 return 1;
2520
2521 btrfs_assert_tree_locked(path->nodes[1]);
2522
2523 right = read_node_slot(root, upper, slot + 1);
2524 btrfs_tree_lock(right);
2525 btrfs_set_lock_blocking(right);
2526
2527 free_space = btrfs_leaf_free_space(root, right);
2528 if (free_space < data_size)
2529 goto out_unlock;
2530
2531 /* cow and double check */
2532 ret = btrfs_cow_block(trans, root, right, upper,
2533 slot + 1, &right);
2534 if (ret)
2535 goto out_unlock;
2536
2537 free_space = btrfs_leaf_free_space(root, right);
2538 if (free_space < data_size)
2539 goto out_unlock;
2540
2541 left_nritems = btrfs_header_nritems(left);
2542 if (left_nritems == 0)
2543 goto out_unlock;
2544
Chris Mason99d8f832010-07-07 10:51:48 -04002545 return __push_leaf_right(trans, root, path, min_data_size, empty,
2546 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002547out_unlock:
2548 btrfs_tree_unlock(right);
2549 free_extent_buffer(right);
2550 return 1;
2551}
2552
2553/*
Chris Mason74123bd2007-02-02 11:05:29 -05002554 * push some data in the path leaf to the left, trying to free up at
2555 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002556 *
2557 * max_slot can put a limit on how far into the leaf we'll push items. The
2558 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2559 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002560 */
Chris Mason44871b12009-03-13 10:04:31 -04002561static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2562 struct btrfs_root *root,
2563 struct btrfs_path *path, int data_size,
2564 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002565 int free_space, u32 right_nritems,
2566 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002567{
Chris Mason5f39d392007-10-15 16:14:19 -04002568 struct btrfs_disk_key disk_key;
2569 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570 int slot;
2571 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002572 int push_space = 0;
2573 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002574 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002575 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002576 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002577 int ret = 0;
2578 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002579 u32 this_item_size;
2580 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002581
2582 slot = path->slots[1];
Chris Mason02217ed2007-03-02 16:08:05 -05002583
Chris Mason34a38212007-11-07 13:31:03 -05002584 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002585 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002586 else
Chris Mason99d8f832010-07-07 10:51:48 -04002587 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002588
2589 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002590 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002591 if (!right->map_token) {
2592 map_extent_buffer(right, (unsigned long)item,
2593 sizeof(struct btrfs_item),
2594 &right->map_token, &right->kaddr,
2595 &right->map_start, &right->map_len,
2596 KM_USER1);
2597 }
2598
Zheng Yan31840ae2008-09-23 13:14:14 -04002599 if (!empty && push_items > 0) {
2600 if (path->slots[0] < i)
2601 break;
2602 if (path->slots[0] == i) {
2603 int space = btrfs_leaf_free_space(root, right);
2604 if (space + push_space * 2 > free_space)
2605 break;
2606 }
2607 }
2608
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002610 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002611
2612 this_item_size = btrfs_item_size(right, item);
2613 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002614 break;
Chris Masondb945352007-10-15 16:15:53 -04002615
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002617 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002618 }
Chris Masondb945352007-10-15 16:15:53 -04002619
2620 if (right->map_token) {
2621 unmap_extent_buffer(right, right->map_token, KM_USER1);
2622 right->map_token = NULL;
2623 }
2624
Chris Masonbe0e5c02007-01-26 15:51:26 -05002625 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002626 ret = 1;
2627 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002628 }
Chris Mason34a38212007-11-07 13:31:03 -05002629 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002630 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002631
Chris Masonbe0e5c02007-01-26 15:51:26 -05002632 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002633 copy_extent_buffer(left, right,
2634 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2635 btrfs_item_nr_offset(0),
2636 push_items * sizeof(struct btrfs_item));
2637
Chris Mason123abc82007-03-14 14:14:43 -04002638 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002639 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002640
2641 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002642 leaf_data_end(root, left) - push_space,
2643 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002644 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002645 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002646 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002647 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002648
Chris Masondb945352007-10-15 16:15:53 -04002649 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002650 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002651 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002652
Chris Mason5f39d392007-10-15 16:14:19 -04002653 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002654 if (!left->map_token) {
2655 map_extent_buffer(left, (unsigned long)item,
2656 sizeof(struct btrfs_item),
2657 &left->map_token, &left->kaddr,
2658 &left->map_start, &left->map_len,
2659 KM_USER1);
2660 }
2661
Chris Mason5f39d392007-10-15 16:14:19 -04002662 ioff = btrfs_item_offset(left, item);
2663 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002664 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002665 }
Chris Mason5f39d392007-10-15 16:14:19 -04002666 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002667 if (left->map_token) {
2668 unmap_extent_buffer(left, left->map_token, KM_USER1);
2669 left->map_token = NULL;
2670 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002671
2672 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002673 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002674 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2675 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002676 WARN_ON(1);
2677 }
Chris Mason5f39d392007-10-15 16:14:19 -04002678
Chris Mason34a38212007-11-07 13:31:03 -05002679 if (push_items < right_nritems) {
2680 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2681 leaf_data_end(root, right);
2682 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2683 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2684 btrfs_leaf_data(right) +
2685 leaf_data_end(root, right), push_space);
2686
2687 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002688 btrfs_item_nr_offset(push_items),
2689 (btrfs_header_nritems(right) - push_items) *
2690 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002691 }
Yaneef1c492007-11-26 10:58:13 -05002692 right_nritems -= push_items;
2693 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002694 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002695 for (i = 0; i < right_nritems; i++) {
2696 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002697
2698 if (!right->map_token) {
2699 map_extent_buffer(right, (unsigned long)item,
2700 sizeof(struct btrfs_item),
2701 &right->map_token, &right->kaddr,
2702 &right->map_start, &right->map_len,
2703 KM_USER1);
2704 }
2705
2706 push_space = push_space - btrfs_item_size(right, item);
2707 btrfs_set_item_offset(right, item, push_space);
2708 }
2709 if (right->map_token) {
2710 unmap_extent_buffer(right, right->map_token, KM_USER1);
2711 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002712 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002713
Chris Mason5f39d392007-10-15 16:14:19 -04002714 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002715 if (right_nritems)
2716 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002717 else
2718 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04002719
Chris Mason5f39d392007-10-15 16:14:19 -04002720 btrfs_item_key(right, &disk_key, 0);
2721 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002722 if (wret)
2723 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002724
2725 /* then fixup the leaf pointer in the path */
2726 if (path->slots[0] < push_items) {
2727 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002728 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002729 free_extent_buffer(path->nodes[0]);
2730 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002731 path->slots[1] -= 1;
2732 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002733 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002734 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002735 path->slots[0] -= push_items;
2736 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002737 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002738 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002739out:
2740 btrfs_tree_unlock(left);
2741 free_extent_buffer(left);
2742 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002743}
2744
Chris Mason74123bd2007-02-02 11:05:29 -05002745/*
Chris Mason44871b12009-03-13 10:04:31 -04002746 * push some data in the path leaf to the left, trying to free up at
2747 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002748 *
2749 * max_slot can put a limit on how far into the leaf we'll push items. The
2750 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2751 * items
Chris Mason44871b12009-03-13 10:04:31 -04002752 */
2753static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002754 *root, struct btrfs_path *path, int min_data_size,
2755 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002756{
2757 struct extent_buffer *right = path->nodes[0];
2758 struct extent_buffer *left;
2759 int slot;
2760 int free_space;
2761 u32 right_nritems;
2762 int ret = 0;
2763
2764 slot = path->slots[1];
2765 if (slot == 0)
2766 return 1;
2767 if (!path->nodes[1])
2768 return 1;
2769
2770 right_nritems = btrfs_header_nritems(right);
2771 if (right_nritems == 0)
2772 return 1;
2773
2774 btrfs_assert_tree_locked(path->nodes[1]);
2775
2776 left = read_node_slot(root, path->nodes[1], slot - 1);
2777 btrfs_tree_lock(left);
2778 btrfs_set_lock_blocking(left);
2779
2780 free_space = btrfs_leaf_free_space(root, left);
2781 if (free_space < data_size) {
2782 ret = 1;
2783 goto out;
2784 }
2785
2786 /* cow and double check */
2787 ret = btrfs_cow_block(trans, root, left,
2788 path->nodes[1], slot - 1, &left);
2789 if (ret) {
2790 /* we hit -ENOSPC, but it isn't fatal here */
2791 ret = 1;
2792 goto out;
2793 }
2794
2795 free_space = btrfs_leaf_free_space(root, left);
2796 if (free_space < data_size) {
2797 ret = 1;
2798 goto out;
2799 }
2800
Chris Mason99d8f832010-07-07 10:51:48 -04002801 return __push_leaf_left(trans, root, path, min_data_size,
2802 empty, left, free_space, right_nritems,
2803 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002804out:
2805 btrfs_tree_unlock(left);
2806 free_extent_buffer(left);
2807 return ret;
2808}
2809
2810/*
Chris Mason74123bd2007-02-02 11:05:29 -05002811 * split the path's leaf in two, making sure there is at least data_size
2812 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002813 *
2814 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002815 */
Chris Mason44871b12009-03-13 10:04:31 -04002816static noinline int copy_for_split(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04002817 struct btrfs_root *root,
Chris Mason44871b12009-03-13 10:04:31 -04002818 struct btrfs_path *path,
2819 struct extent_buffer *l,
2820 struct extent_buffer *right,
2821 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002822{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002823 int data_copy_size;
2824 int rt_data_off;
2825 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002826 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002827 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04002828 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002829
Chris Mason5f39d392007-10-15 16:14:19 -04002830 nritems = nritems - mid;
2831 btrfs_set_header_nritems(right, nritems);
2832 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2833
2834 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2835 btrfs_item_nr_offset(mid),
2836 nritems * sizeof(struct btrfs_item));
2837
2838 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002839 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2840 data_copy_size, btrfs_leaf_data(l) +
2841 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002842
Chris Mason5f39d392007-10-15 16:14:19 -04002843 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2844 btrfs_item_end_nr(l, mid);
2845
2846 for (i = 0; i < nritems; i++) {
2847 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002848 u32 ioff;
2849
2850 if (!right->map_token) {
2851 map_extent_buffer(right, (unsigned long)item,
2852 sizeof(struct btrfs_item),
2853 &right->map_token, &right->kaddr,
2854 &right->map_start, &right->map_len,
2855 KM_USER1);
2856 }
2857
2858 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002859 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002860 }
Chris Mason74123bd2007-02-02 11:05:29 -05002861
Chris Masondb945352007-10-15 16:15:53 -04002862 if (right->map_token) {
2863 unmap_extent_buffer(right, right->map_token, KM_USER1);
2864 right->map_token = NULL;
2865 }
2866
Chris Mason5f39d392007-10-15 16:14:19 -04002867 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002868 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002869 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002870 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2871 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002872 if (wret)
2873 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002874
2875 btrfs_mark_buffer_dirty(right);
2876 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002877 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002878
Chris Masonbe0e5c02007-01-26 15:51:26 -05002879 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002880 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002881 free_extent_buffer(path->nodes[0]);
2882 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002883 path->slots[0] -= mid;
2884 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002885 } else {
2886 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002887 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002888 }
Chris Mason5f39d392007-10-15 16:14:19 -04002889
Chris Masoneb60cea2007-02-02 09:18:22 -05002890 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002891
Chris Mason44871b12009-03-13 10:04:31 -04002892 return ret;
2893}
2894
2895/*
Chris Mason99d8f832010-07-07 10:51:48 -04002896 * double splits happen when we need to insert a big item in the middle
2897 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2898 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2899 * A B C
2900 *
2901 * We avoid this by trying to push the items on either side of our target
2902 * into the adjacent leaves. If all goes well we can avoid the double split
2903 * completely.
2904 */
2905static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2906 struct btrfs_root *root,
2907 struct btrfs_path *path,
2908 int data_size)
2909{
2910 int ret;
2911 int progress = 0;
2912 int slot;
2913 u32 nritems;
2914
2915 slot = path->slots[0];
2916
2917 /*
2918 * try to push all the items after our slot into the
2919 * right leaf
2920 */
2921 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2922 if (ret < 0)
2923 return ret;
2924
2925 if (ret == 0)
2926 progress++;
2927
2928 nritems = btrfs_header_nritems(path->nodes[0]);
2929 /*
2930 * our goal is to get our slot at the start or end of a leaf. If
2931 * we've done so we're done
2932 */
2933 if (path->slots[0] == 0 || path->slots[0] == nritems)
2934 return 0;
2935
2936 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2937 return 0;
2938
2939 /* try to push all the items before our slot into the next leaf */
2940 slot = path->slots[0];
2941 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2942 if (ret < 0)
2943 return ret;
2944
2945 if (ret == 0)
2946 progress++;
2947
2948 if (progress)
2949 return 0;
2950 return 1;
2951}
2952
2953/*
Chris Mason44871b12009-03-13 10:04:31 -04002954 * split the path's leaf in two, making sure there is at least data_size
2955 * available for the resulting leaf level of the path.
2956 *
2957 * returns 0 if all went well and < 0 on failure.
2958 */
2959static noinline int split_leaf(struct btrfs_trans_handle *trans,
2960 struct btrfs_root *root,
2961 struct btrfs_key *ins_key,
2962 struct btrfs_path *path, int data_size,
2963 int extend)
2964{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002965 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04002966 struct extent_buffer *l;
2967 u32 nritems;
2968 int mid;
2969 int slot;
2970 struct extent_buffer *right;
2971 int ret = 0;
2972 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002973 int split;
Chris Mason44871b12009-03-13 10:04:31 -04002974 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04002975 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04002976
Yan, Zhenga5719522009-09-24 09:17:31 -04002977 l = path->nodes[0];
2978 slot = path->slots[0];
2979 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2980 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2981 return -EOVERFLOW;
2982
Chris Mason44871b12009-03-13 10:04:31 -04002983 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04002984 if (data_size) {
2985 wret = push_leaf_right(trans, root, path, data_size,
2986 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04002987 if (wret < 0)
2988 return wret;
2989 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04002990 wret = push_leaf_left(trans, root, path, data_size,
2991 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04002992 if (wret < 0)
2993 return wret;
2994 }
2995 l = path->nodes[0];
2996
2997 /* did the pushes work? */
2998 if (btrfs_leaf_free_space(root, l) >= data_size)
2999 return 0;
3000 }
3001
3002 if (!path->nodes[1]) {
3003 ret = insert_new_root(trans, root, path, 1);
3004 if (ret)
3005 return ret;
3006 }
3007again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003008 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003009 l = path->nodes[0];
3010 slot = path->slots[0];
3011 nritems = btrfs_header_nritems(l);
3012 mid = (nritems + 1) / 2;
3013
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003014 if (mid <= slot) {
3015 if (nritems == 1 ||
3016 leaf_space_used(l, mid, nritems - mid) + data_size >
3017 BTRFS_LEAF_DATA_SIZE(root)) {
3018 if (slot >= nritems) {
3019 split = 0;
3020 } else {
3021 mid = slot;
3022 if (mid != nritems &&
3023 leaf_space_used(l, mid, nritems - mid) +
3024 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003025 if (data_size && !tried_avoid_double)
3026 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003027 split = 2;
3028 }
3029 }
3030 }
3031 } else {
3032 if (leaf_space_used(l, 0, mid) + data_size >
3033 BTRFS_LEAF_DATA_SIZE(root)) {
3034 if (!extend && data_size && slot == 0) {
3035 split = 0;
3036 } else if ((extend || !data_size) && slot == 0) {
3037 mid = 1;
3038 } else {
3039 mid = slot;
3040 if (mid != nritems &&
3041 leaf_space_used(l, mid, nritems - mid) +
3042 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003043 if (data_size && !tried_avoid_double)
3044 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003045 split = 2 ;
3046 }
3047 }
3048 }
3049 }
3050
3051 if (split == 0)
3052 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3053 else
3054 btrfs_item_key(l, &disk_key, mid);
3055
3056 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003057 root->root_key.objectid,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003058 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003059 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003060 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003061
3062 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003063
3064 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3065 btrfs_set_header_bytenr(right, right->start);
3066 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003067 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003068 btrfs_set_header_owner(right, root->root_key.objectid);
3069 btrfs_set_header_level(right, 0);
3070 write_extent_buffer(right, root->fs_info->fsid,
3071 (unsigned long)btrfs_header_fsid(right),
3072 BTRFS_FSID_SIZE);
3073
3074 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3075 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3076 BTRFS_UUID_SIZE);
3077
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003078 if (split == 0) {
3079 if (mid <= slot) {
3080 btrfs_set_header_nritems(right, 0);
3081 wret = insert_ptr(trans, root, path,
3082 &disk_key, right->start,
3083 path->slots[1] + 1, 1);
3084 if (wret)
3085 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003086
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003087 btrfs_tree_unlock(path->nodes[0]);
3088 free_extent_buffer(path->nodes[0]);
3089 path->nodes[0] = right;
3090 path->slots[0] = 0;
3091 path->slots[1] += 1;
3092 } else {
3093 btrfs_set_header_nritems(right, 0);
3094 wret = insert_ptr(trans, root, path,
3095 &disk_key,
3096 right->start,
3097 path->slots[1], 1);
3098 if (wret)
3099 ret = wret;
3100 btrfs_tree_unlock(path->nodes[0]);
3101 free_extent_buffer(path->nodes[0]);
3102 path->nodes[0] = right;
3103 path->slots[0] = 0;
3104 if (path->slots[1] == 0) {
3105 wret = fixup_low_keys(trans, root,
3106 path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003107 if (wret)
3108 ret = wret;
Chris Mason44871b12009-03-13 10:04:31 -04003109 }
3110 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003111 btrfs_mark_buffer_dirty(right);
3112 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003113 }
3114
3115 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3116 BUG_ON(ret);
3117
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003118 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003119 BUG_ON(num_doubles != 0);
3120 num_doubles++;
3121 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003122 }
Chris Mason44871b12009-03-13 10:04:31 -04003123
Chris Masonbe0e5c02007-01-26 15:51:26 -05003124 return ret;
Chris Mason99d8f832010-07-07 10:51:48 -04003125
3126push_for_double:
3127 push_for_double_split(trans, root, path, data_size);
3128 tried_avoid_double = 1;
3129 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3130 return 0;
3131 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003132}
3133
Yan, Zhengad48fd752009-11-12 09:33:58 +00003134static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3135 struct btrfs_root *root,
3136 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003137{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003138 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003139 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003140 struct btrfs_file_extent_item *fi;
3141 u64 extent_len = 0;
3142 u32 item_size;
3143 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003144
3145 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003146 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3147
3148 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3149 key.type != BTRFS_EXTENT_CSUM_KEY);
3150
3151 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3152 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003153
3154 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003155 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3156 fi = btrfs_item_ptr(leaf, path->slots[0],
3157 struct btrfs_file_extent_item);
3158 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3159 }
Chris Mason459931e2008-12-10 09:10:46 -05003160 btrfs_release_path(root, path);
3161
Chris Mason459931e2008-12-10 09:10:46 -05003162 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003163 path->search_for_split = 1;
3164 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003165 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003166 if (ret < 0)
3167 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003168
Yan, Zhengad48fd752009-11-12 09:33:58 +00003169 ret = -EAGAIN;
3170 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003171 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003172 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3173 goto err;
3174
Chris Mason109f6ae2010-04-02 09:20:18 -04003175 /* the leaf has changed, it now has room. return now */
3176 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3177 goto err;
3178
Yan, Zhengad48fd752009-11-12 09:33:58 +00003179 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3180 fi = btrfs_item_ptr(leaf, path->slots[0],
3181 struct btrfs_file_extent_item);
3182 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3183 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003184 }
3185
Chris Masonb9473432009-03-13 11:00:37 -04003186 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003187 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003188 if (ret)
3189 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003190
Yan, Zhengad48fd752009-11-12 09:33:58 +00003191 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003192 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003193 return 0;
3194err:
3195 path->keep_locks = 0;
3196 return ret;
3197}
3198
3199static noinline int split_item(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *root,
3201 struct btrfs_path *path,
3202 struct btrfs_key *new_key,
3203 unsigned long split_offset)
3204{
3205 struct extent_buffer *leaf;
3206 struct btrfs_item *item;
3207 struct btrfs_item *new_item;
3208 int slot;
3209 char *buf;
3210 u32 nritems;
3211 u32 item_size;
3212 u32 orig_offset;
3213 struct btrfs_disk_key disk_key;
3214
Chris Masonb9473432009-03-13 11:00:37 -04003215 leaf = path->nodes[0];
3216 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3217
Chris Masonb4ce94d2009-02-04 09:25:08 -05003218 btrfs_set_path_blocking(path);
3219
Chris Mason459931e2008-12-10 09:10:46 -05003220 item = btrfs_item_nr(leaf, path->slots[0]);
3221 orig_offset = btrfs_item_offset(leaf, item);
3222 item_size = btrfs_item_size(leaf, item);
3223
Chris Mason459931e2008-12-10 09:10:46 -05003224 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003225 if (!buf)
3226 return -ENOMEM;
3227
Chris Mason459931e2008-12-10 09:10:46 -05003228 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3229 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003230
Chris Mason459931e2008-12-10 09:10:46 -05003231 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003232 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003233 if (slot != nritems) {
3234 /* shift the items */
3235 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003236 btrfs_item_nr_offset(slot),
3237 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003238 }
3239
3240 btrfs_cpu_key_to_disk(&disk_key, new_key);
3241 btrfs_set_item_key(leaf, &disk_key, slot);
3242
3243 new_item = btrfs_item_nr(leaf, slot);
3244
3245 btrfs_set_item_offset(leaf, new_item, orig_offset);
3246 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3247
3248 btrfs_set_item_offset(leaf, item,
3249 orig_offset + item_size - split_offset);
3250 btrfs_set_item_size(leaf, item, split_offset);
3251
3252 btrfs_set_header_nritems(leaf, nritems + 1);
3253
3254 /* write the data for the start of the original item */
3255 write_extent_buffer(leaf, buf,
3256 btrfs_item_ptr_offset(leaf, path->slots[0]),
3257 split_offset);
3258
3259 /* write the data for the new item */
3260 write_extent_buffer(leaf, buf + split_offset,
3261 btrfs_item_ptr_offset(leaf, slot),
3262 item_size - split_offset);
3263 btrfs_mark_buffer_dirty(leaf);
3264
Yan, Zhengad48fd752009-11-12 09:33:58 +00003265 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003266 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003267 return 0;
3268}
3269
3270/*
3271 * This function splits a single item into two items,
3272 * giving 'new_key' to the new item and splitting the
3273 * old one at split_offset (from the start of the item).
3274 *
3275 * The path may be released by this operation. After
3276 * the split, the path is pointing to the old item. The
3277 * new item is going to be in the same node as the old one.
3278 *
3279 * Note, the item being split must be smaller enough to live alone on
3280 * a tree block with room for one extra struct btrfs_item
3281 *
3282 * This allows us to split the item in place, keeping a lock on the
3283 * leaf the entire time.
3284 */
3285int btrfs_split_item(struct btrfs_trans_handle *trans,
3286 struct btrfs_root *root,
3287 struct btrfs_path *path,
3288 struct btrfs_key *new_key,
3289 unsigned long split_offset)
3290{
3291 int ret;
3292 ret = setup_leaf_for_split(trans, root, path,
3293 sizeof(struct btrfs_item));
3294 if (ret)
3295 return ret;
3296
3297 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003298 return ret;
3299}
3300
3301/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003302 * This function duplicate a item, giving 'new_key' to the new item.
3303 * It guarantees both items live in the same tree leaf and the new item
3304 * is contiguous with the original item.
3305 *
3306 * This allows us to split file extent in place, keeping a lock on the
3307 * leaf the entire time.
3308 */
3309int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3310 struct btrfs_root *root,
3311 struct btrfs_path *path,
3312 struct btrfs_key *new_key)
3313{
3314 struct extent_buffer *leaf;
3315 int ret;
3316 u32 item_size;
3317
3318 leaf = path->nodes[0];
3319 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3320 ret = setup_leaf_for_split(trans, root, path,
3321 item_size + sizeof(struct btrfs_item));
3322 if (ret)
3323 return ret;
3324
3325 path->slots[0]++;
3326 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3327 item_size, item_size +
3328 sizeof(struct btrfs_item), 1);
3329 BUG_ON(ret);
3330
3331 leaf = path->nodes[0];
3332 memcpy_extent_buffer(leaf,
3333 btrfs_item_ptr_offset(leaf, path->slots[0]),
3334 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3335 item_size);
3336 return 0;
3337}
3338
3339/*
Chris Masond352ac62008-09-29 15:18:18 -04003340 * make the item pointed to by the path smaller. new_size indicates
3341 * how small to make it, and from_end tells us if we just chop bytes
3342 * off the end of the item or if we shift the item to chop bytes off
3343 * the front.
3344 */
Chris Masonb18c6682007-04-17 13:26:50 -04003345int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3346 struct btrfs_root *root,
3347 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003348 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003349{
3350 int ret = 0;
3351 int slot;
3352 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003353 struct extent_buffer *leaf;
3354 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003355 u32 nritems;
3356 unsigned int data_end;
3357 unsigned int old_data_start;
3358 unsigned int old_size;
3359 unsigned int size_diff;
3360 int i;
3361
3362 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003363 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003364 slot = path->slots[0];
3365
3366 old_size = btrfs_item_size_nr(leaf, slot);
3367 if (old_size == new_size)
3368 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003369
Chris Mason5f39d392007-10-15 16:14:19 -04003370 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003371 data_end = leaf_data_end(root, leaf);
3372
Chris Mason5f39d392007-10-15 16:14:19 -04003373 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003374
Chris Masonb18c6682007-04-17 13:26:50 -04003375 size_diff = old_size - new_size;
3376
3377 BUG_ON(slot < 0);
3378 BUG_ON(slot >= nritems);
3379
3380 /*
3381 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3382 */
3383 /* first correct the data pointers */
3384 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003385 u32 ioff;
3386 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003387
3388 if (!leaf->map_token) {
3389 map_extent_buffer(leaf, (unsigned long)item,
3390 sizeof(struct btrfs_item),
3391 &leaf->map_token, &leaf->kaddr,
3392 &leaf->map_start, &leaf->map_len,
3393 KM_USER1);
3394 }
3395
Chris Mason5f39d392007-10-15 16:14:19 -04003396 ioff = btrfs_item_offset(leaf, item);
3397 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003398 }
Chris Masondb945352007-10-15 16:15:53 -04003399
3400 if (leaf->map_token) {
3401 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3402 leaf->map_token = NULL;
3403 }
3404
Chris Masonb18c6682007-04-17 13:26:50 -04003405 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003406 if (from_end) {
3407 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3408 data_end + size_diff, btrfs_leaf_data(leaf) +
3409 data_end, old_data_start + new_size - data_end);
3410 } else {
3411 struct btrfs_disk_key disk_key;
3412 u64 offset;
3413
3414 btrfs_item_key(leaf, &disk_key, slot);
3415
3416 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3417 unsigned long ptr;
3418 struct btrfs_file_extent_item *fi;
3419
3420 fi = btrfs_item_ptr(leaf, slot,
3421 struct btrfs_file_extent_item);
3422 fi = (struct btrfs_file_extent_item *)(
3423 (unsigned long)fi - size_diff);
3424
3425 if (btrfs_file_extent_type(leaf, fi) ==
3426 BTRFS_FILE_EXTENT_INLINE) {
3427 ptr = btrfs_item_ptr_offset(leaf, slot);
3428 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003429 (unsigned long)fi,
3430 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003431 disk_bytenr));
3432 }
3433 }
3434
3435 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3436 data_end + size_diff, btrfs_leaf_data(leaf) +
3437 data_end, old_data_start - data_end);
3438
3439 offset = btrfs_disk_key_offset(&disk_key);
3440 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3441 btrfs_set_item_key(leaf, &disk_key, slot);
3442 if (slot == 0)
3443 fixup_low_keys(trans, root, path, &disk_key, 1);
3444 }
Chris Mason5f39d392007-10-15 16:14:19 -04003445
3446 item = btrfs_item_nr(leaf, slot);
3447 btrfs_set_item_size(leaf, item, new_size);
3448 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003449
3450 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003451 if (btrfs_leaf_free_space(root, leaf) < 0) {
3452 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003453 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003454 }
Chris Masonb18c6682007-04-17 13:26:50 -04003455 return ret;
3456}
3457
Chris Masond352ac62008-09-29 15:18:18 -04003458/*
3459 * make the item pointed to by the path bigger, data_size is the new size.
3460 */
Chris Mason5f39d392007-10-15 16:14:19 -04003461int btrfs_extend_item(struct btrfs_trans_handle *trans,
3462 struct btrfs_root *root, struct btrfs_path *path,
3463 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003464{
3465 int ret = 0;
3466 int slot;
3467 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003468 struct extent_buffer *leaf;
3469 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003470 u32 nritems;
3471 unsigned int data_end;
3472 unsigned int old_data;
3473 unsigned int old_size;
3474 int i;
3475
3476 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003477 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003478
Chris Mason5f39d392007-10-15 16:14:19 -04003479 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003480 data_end = leaf_data_end(root, leaf);
3481
Chris Mason5f39d392007-10-15 16:14:19 -04003482 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3483 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003484 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003485 }
Chris Mason6567e832007-04-16 09:22:45 -04003486 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003487 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003488
3489 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003490 if (slot >= nritems) {
3491 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003492 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3493 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003494 BUG_ON(1);
3495 }
Chris Mason6567e832007-04-16 09:22:45 -04003496
3497 /*
3498 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3499 */
3500 /* first correct the data pointers */
3501 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003502 u32 ioff;
3503 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003504
3505 if (!leaf->map_token) {
3506 map_extent_buffer(leaf, (unsigned long)item,
3507 sizeof(struct btrfs_item),
3508 &leaf->map_token, &leaf->kaddr,
3509 &leaf->map_start, &leaf->map_len,
3510 KM_USER1);
3511 }
Chris Mason5f39d392007-10-15 16:14:19 -04003512 ioff = btrfs_item_offset(leaf, item);
3513 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003514 }
Chris Mason5f39d392007-10-15 16:14:19 -04003515
Chris Masondb945352007-10-15 16:15:53 -04003516 if (leaf->map_token) {
3517 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3518 leaf->map_token = NULL;
3519 }
3520
Chris Mason6567e832007-04-16 09:22:45 -04003521 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003522 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003523 data_end - data_size, btrfs_leaf_data(leaf) +
3524 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003525
Chris Mason6567e832007-04-16 09:22:45 -04003526 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003527 old_size = btrfs_item_size_nr(leaf, slot);
3528 item = btrfs_item_nr(leaf, slot);
3529 btrfs_set_item_size(leaf, item, old_size + data_size);
3530 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003531
3532 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003533 if (btrfs_leaf_free_space(root, leaf) < 0) {
3534 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003535 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003536 }
Chris Mason6567e832007-04-16 09:22:45 -04003537 return ret;
3538}
3539
Chris Mason74123bd2007-02-02 11:05:29 -05003540/*
Chris Masond352ac62008-09-29 15:18:18 -04003541 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003542 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003543 * Returns the number of keys that were inserted.
3544 */
3545int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3546 struct btrfs_root *root,
3547 struct btrfs_path *path,
3548 struct btrfs_key *cpu_key, u32 *data_size,
3549 int nr)
3550{
3551 struct extent_buffer *leaf;
3552 struct btrfs_item *item;
3553 int ret = 0;
3554 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003555 int i;
3556 u32 nritems;
3557 u32 total_data = 0;
3558 u32 total_size = 0;
3559 unsigned int data_end;
3560 struct btrfs_disk_key disk_key;
3561 struct btrfs_key found_key;
3562
Yan Zheng87b29b22008-12-17 10:21:48 -05003563 for (i = 0; i < nr; i++) {
3564 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3565 BTRFS_LEAF_DATA_SIZE(root)) {
3566 break;
3567 nr = i;
3568 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003569 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003570 total_size += data_size[i] + sizeof(struct btrfs_item);
3571 }
3572 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003573
Josef Bacikf3465ca2008-11-12 14:19:50 -05003574 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3575 if (ret == 0)
3576 return -EEXIST;
3577 if (ret < 0)
3578 goto out;
3579
Josef Bacikf3465ca2008-11-12 14:19:50 -05003580 leaf = path->nodes[0];
3581
3582 nritems = btrfs_header_nritems(leaf);
3583 data_end = leaf_data_end(root, leaf);
3584
3585 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3586 for (i = nr; i >= 0; i--) {
3587 total_data -= data_size[i];
3588 total_size -= data_size[i] + sizeof(struct btrfs_item);
3589 if (total_size < btrfs_leaf_free_space(root, leaf))
3590 break;
3591 }
3592 nr = i;
3593 }
3594
3595 slot = path->slots[0];
3596 BUG_ON(slot < 0);
3597
3598 if (slot != nritems) {
3599 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3600
3601 item = btrfs_item_nr(leaf, slot);
3602 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3603
3604 /* figure out how many keys we can insert in here */
3605 total_data = data_size[0];
3606 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003607 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003608 break;
3609 total_data += data_size[i];
3610 }
3611 nr = i;
3612
3613 if (old_data < data_end) {
3614 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003615 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003616 slot, old_data, data_end);
3617 BUG_ON(1);
3618 }
3619 /*
3620 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3621 */
3622 /* first correct the data pointers */
3623 WARN_ON(leaf->map_token);
3624 for (i = slot; i < nritems; i++) {
3625 u32 ioff;
3626
3627 item = btrfs_item_nr(leaf, i);
3628 if (!leaf->map_token) {
3629 map_extent_buffer(leaf, (unsigned long)item,
3630 sizeof(struct btrfs_item),
3631 &leaf->map_token, &leaf->kaddr,
3632 &leaf->map_start, &leaf->map_len,
3633 KM_USER1);
3634 }
3635
3636 ioff = btrfs_item_offset(leaf, item);
3637 btrfs_set_item_offset(leaf, item, ioff - total_data);
3638 }
3639 if (leaf->map_token) {
3640 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3641 leaf->map_token = NULL;
3642 }
3643
3644 /* shift the items */
3645 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3646 btrfs_item_nr_offset(slot),
3647 (nritems - slot) * sizeof(struct btrfs_item));
3648
3649 /* shift the data */
3650 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3651 data_end - total_data, btrfs_leaf_data(leaf) +
3652 data_end, old_data - data_end);
3653 data_end = old_data;
3654 } else {
3655 /*
3656 * this sucks but it has to be done, if we are inserting at
3657 * the end of the leaf only insert 1 of the items, since we
3658 * have no way of knowing whats on the next leaf and we'd have
3659 * to drop our current locks to figure it out
3660 */
3661 nr = 1;
3662 }
3663
3664 /* setup the item for the new data */
3665 for (i = 0; i < nr; i++) {
3666 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3667 btrfs_set_item_key(leaf, &disk_key, slot + i);
3668 item = btrfs_item_nr(leaf, slot + i);
3669 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3670 data_end -= data_size[i];
3671 btrfs_set_item_size(leaf, item, data_size[i]);
3672 }
3673 btrfs_set_header_nritems(leaf, nritems + nr);
3674 btrfs_mark_buffer_dirty(leaf);
3675
3676 ret = 0;
3677 if (slot == 0) {
3678 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3679 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3680 }
3681
3682 if (btrfs_leaf_free_space(root, leaf) < 0) {
3683 btrfs_print_leaf(root, leaf);
3684 BUG();
3685 }
3686out:
3687 if (!ret)
3688 ret = nr;
3689 return ret;
3690}
3691
3692/*
Chris Mason44871b12009-03-13 10:04:31 -04003693 * this is a helper for btrfs_insert_empty_items, the main goal here is
3694 * to save stack depth by doing the bulk of the work in a function
3695 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05003696 */
Chris Mason44871b12009-03-13 10:04:31 -04003697static noinline_for_stack int
3698setup_items_for_insert(struct btrfs_trans_handle *trans,
3699 struct btrfs_root *root, struct btrfs_path *path,
3700 struct btrfs_key *cpu_key, u32 *data_size,
3701 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003702{
Chris Mason5f39d392007-10-15 16:14:19 -04003703 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003704 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003705 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003706 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003707 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003708 int ret;
3709 struct extent_buffer *leaf;
3710 int slot;
Chris Masone2fa7222007-03-12 16:22:34 -04003711
Chris Mason5f39d392007-10-15 16:14:19 -04003712 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003713 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003714
Chris Mason5f39d392007-10-15 16:14:19 -04003715 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003716 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003717
Chris Masonf25956c2008-09-12 15:32:53 -04003718 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003719 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003720 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003721 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003722 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003723 }
Chris Mason5f39d392007-10-15 16:14:19 -04003724
Chris Masonbe0e5c02007-01-26 15:51:26 -05003725 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003726 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003727
Chris Mason5f39d392007-10-15 16:14:19 -04003728 if (old_data < data_end) {
3729 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003730 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003731 slot, old_data, data_end);
3732 BUG_ON(1);
3733 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003734 /*
3735 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3736 */
3737 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003738 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003739 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003740 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003741
Chris Mason5f39d392007-10-15 16:14:19 -04003742 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003743 if (!leaf->map_token) {
3744 map_extent_buffer(leaf, (unsigned long)item,
3745 sizeof(struct btrfs_item),
3746 &leaf->map_token, &leaf->kaddr,
3747 &leaf->map_start, &leaf->map_len,
3748 KM_USER1);
3749 }
3750
Chris Mason5f39d392007-10-15 16:14:19 -04003751 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003752 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003753 }
Chris Masondb945352007-10-15 16:15:53 -04003754 if (leaf->map_token) {
3755 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3756 leaf->map_token = NULL;
3757 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003758
3759 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003760 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003761 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003762 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003763
3764 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003765 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003766 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003767 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003768 data_end = old_data;
3769 }
Chris Mason5f39d392007-10-15 16:14:19 -04003770
Chris Mason62e27492007-03-15 12:56:47 -04003771 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003772 for (i = 0; i < nr; i++) {
3773 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3774 btrfs_set_item_key(leaf, &disk_key, slot + i);
3775 item = btrfs_item_nr(leaf, slot + i);
3776 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3777 data_end -= data_size[i];
3778 btrfs_set_item_size(leaf, item, data_size[i]);
3779 }
Chris Mason44871b12009-03-13 10:04:31 -04003780
Chris Mason9c583092008-01-29 15:15:18 -05003781 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003782
3783 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003784 if (slot == 0) {
Chris Mason44871b12009-03-13 10:04:31 -04003785 struct btrfs_disk_key disk_key;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003786 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003787 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003788 }
Chris Masonb9473432009-03-13 11:00:37 -04003789 btrfs_unlock_up_safe(path, 1);
3790 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003791
Chris Mason5f39d392007-10-15 16:14:19 -04003792 if (btrfs_leaf_free_space(root, leaf) < 0) {
3793 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003794 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003795 }
Chris Mason44871b12009-03-13 10:04:31 -04003796 return ret;
3797}
3798
3799/*
3800 * Given a key and some data, insert items into the tree.
3801 * This does all the path init required, making room in the tree if needed.
3802 */
3803int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3804 struct btrfs_root *root,
3805 struct btrfs_path *path,
3806 struct btrfs_key *cpu_key, u32 *data_size,
3807 int nr)
3808{
3809 struct extent_buffer *leaf;
3810 int ret = 0;
3811 int slot;
3812 int i;
3813 u32 total_size = 0;
3814 u32 total_data = 0;
3815
3816 for (i = 0; i < nr; i++)
3817 total_data += data_size[i];
3818
3819 total_size = total_data + (nr * sizeof(struct btrfs_item));
3820 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3821 if (ret == 0)
3822 return -EEXIST;
3823 if (ret < 0)
3824 goto out;
3825
3826 leaf = path->nodes[0];
3827 slot = path->slots[0];
3828 BUG_ON(slot < 0);
3829
3830 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3831 total_data, total_size, nr);
3832
Chris Masoned2ff2c2007-03-01 18:59:40 -05003833out:
Chris Mason62e27492007-03-15 12:56:47 -04003834 return ret;
3835}
3836
3837/*
3838 * Given a key and some data, insert an item into the tree.
3839 * This does all the path init required, making room in the tree if needed.
3840 */
Chris Masone089f052007-03-16 16:20:31 -04003841int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3842 *root, struct btrfs_key *cpu_key, void *data, u32
3843 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003844{
3845 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003846 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003847 struct extent_buffer *leaf;
3848 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003849
Chris Mason2c90e5d2007-04-02 10:50:19 -04003850 path = btrfs_alloc_path();
3851 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003852 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003853 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003854 leaf = path->nodes[0];
3855 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3856 write_extent_buffer(leaf, data, ptr, data_size);
3857 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003858 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003859 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003860 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003861}
3862
Chris Mason74123bd2007-02-02 11:05:29 -05003863/*
Chris Mason5de08d72007-02-24 06:24:44 -05003864 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003865 *
Chris Masond352ac62008-09-29 15:18:18 -04003866 * the tree should have been previously balanced so the deletion does not
3867 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003868 */
Chris Masone089f052007-03-16 16:20:31 -04003869static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3870 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003871{
Chris Mason5f39d392007-10-15 16:14:19 -04003872 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003873 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003874 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003875 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003876
Chris Mason5f39d392007-10-15 16:14:19 -04003877 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003878 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003879 memmove_extent_buffer(parent,
3880 btrfs_node_key_ptr_offset(slot),
3881 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003882 sizeof(struct btrfs_key_ptr) *
3883 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003884 }
Chris Mason7518a232007-03-12 12:01:18 -04003885 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003886 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003887 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003888 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003889 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003890 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003891 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003892 struct btrfs_disk_key disk_key;
3893
3894 btrfs_node_key(parent, &disk_key, 0);
3895 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003896 if (wret)
3897 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003898 }
Chris Masond6025572007-03-30 14:27:56 -04003899 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003900 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003901}
3902
Chris Mason74123bd2007-02-02 11:05:29 -05003903/*
Chris Mason323ac952008-10-01 19:05:46 -04003904 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003905 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04003906 *
3907 * This deletes the pointer in path->nodes[1] and frees the leaf
3908 * block extent. zero is returned if it all worked out, < 0 otherwise.
3909 *
3910 * The path must have already been setup for deleting the leaf, including
3911 * all the proper balancing. path->nodes[1] must be locked.
3912 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003913static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3914 struct btrfs_root *root,
3915 struct btrfs_path *path,
3916 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04003917{
3918 int ret;
Chris Mason323ac952008-10-01 19:05:46 -04003919
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003920 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Chris Mason323ac952008-10-01 19:05:46 -04003921 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3922 if (ret)
3923 return ret;
3924
Chris Mason4d081c42009-02-04 09:31:28 -05003925 /*
3926 * btrfs_free_extent is expensive, we want to make sure we
3927 * aren't holding any locks when we call it
3928 */
3929 btrfs_unlock_up_safe(path, 0);
3930
Yan, Zhengf0486c62010-05-16 10:46:25 -04003931 root_sub_used(root, leaf->len);
3932
3933 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3934 return 0;
Chris Mason323ac952008-10-01 19:05:46 -04003935}
3936/*
Chris Mason74123bd2007-02-02 11:05:29 -05003937 * delete the item at the leaf level in path. If that empties
3938 * the leaf, remove it from the tree
3939 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003940int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3941 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003942{
Chris Mason5f39d392007-10-15 16:14:19 -04003943 struct extent_buffer *leaf;
3944 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003945 int last_off;
3946 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003947 int ret = 0;
3948 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003949 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003950 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003951
Chris Mason5f39d392007-10-15 16:14:19 -04003952 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003953 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3954
3955 for (i = 0; i < nr; i++)
3956 dsize += btrfs_item_size_nr(leaf, slot + i);
3957
Chris Mason5f39d392007-10-15 16:14:19 -04003958 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003959
Chris Mason85e21ba2008-01-29 15:11:36 -05003960 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003961 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003962
3963 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003964 data_end + dsize,
3965 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003966 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003967
Chris Mason85e21ba2008-01-29 15:11:36 -05003968 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003969 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003970
Chris Mason5f39d392007-10-15 16:14:19 -04003971 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003972 if (!leaf->map_token) {
3973 map_extent_buffer(leaf, (unsigned long)item,
3974 sizeof(struct btrfs_item),
3975 &leaf->map_token, &leaf->kaddr,
3976 &leaf->map_start, &leaf->map_len,
3977 KM_USER1);
3978 }
Chris Mason5f39d392007-10-15 16:14:19 -04003979 ioff = btrfs_item_offset(leaf, item);
3980 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003981 }
Chris Masondb945352007-10-15 16:15:53 -04003982
3983 if (leaf->map_token) {
3984 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3985 leaf->map_token = NULL;
3986 }
3987
Chris Mason5f39d392007-10-15 16:14:19 -04003988 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003989 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003990 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003991 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003992 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003993 btrfs_set_header_nritems(leaf, nritems - nr);
3994 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003995
Chris Mason74123bd2007-02-02 11:05:29 -05003996 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003997 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003998 if (leaf == root->node) {
3999 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004000 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004001 btrfs_set_path_blocking(path);
4002 clean_tree_block(trans, root, leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004003 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004004 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05004005 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004006 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004007 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004008 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004009 struct btrfs_disk_key disk_key;
4010
4011 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04004012 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04004013 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004014 if (wret)
4015 ret = wret;
4016 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004017
Chris Mason74123bd2007-02-02 11:05:29 -05004018 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004019 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004020 /* push_leaf_left fixes the path.
4021 * make sure the path still points to our leaf
4022 * for possible call to del_ptr below
4023 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004024 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004025 extent_buffer_get(leaf);
4026
Chris Masonb9473432009-03-13 11:00:37 -04004027 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004028 wret = push_leaf_left(trans, root, path, 1, 1,
4029 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004030 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004031 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004032
4033 if (path->nodes[0] == leaf &&
4034 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004035 wret = push_leaf_right(trans, root, path, 1,
4036 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004037 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004038 ret = wret;
4039 }
Chris Mason5f39d392007-10-15 16:14:19 -04004040
4041 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004042 path->slots[1] = slot;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004043 ret = btrfs_del_leaf(trans, root, path, leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004044 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004045 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05004046 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004047 /* if we're still in the path, make sure
4048 * we're dirty. Otherwise, one of the
4049 * push_leaf functions must have already
4050 * dirtied this buffer
4051 */
4052 if (path->nodes[0] == leaf)
4053 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004054 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004055 }
Chris Masond5719762007-03-23 10:01:08 -04004056 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004057 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004058 }
4059 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004060 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004061}
4062
Chris Mason97571fd2007-02-24 13:39:08 -05004063/*
Chris Mason925baed2008-06-25 16:01:30 -04004064 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004065 * returns 0 if it found something or 1 if there are no lesser leaves.
4066 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004067 *
4068 * This may release the path, and so you may lose any locks held at the
4069 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004070 */
4071int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4072{
Chris Mason925baed2008-06-25 16:01:30 -04004073 struct btrfs_key key;
4074 struct btrfs_disk_key found_key;
4075 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004076
Chris Mason925baed2008-06-25 16:01:30 -04004077 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004078
Chris Mason925baed2008-06-25 16:01:30 -04004079 if (key.offset > 0)
4080 key.offset--;
4081 else if (key.type > 0)
4082 key.type--;
4083 else if (key.objectid > 0)
4084 key.objectid--;
4085 else
4086 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004087
Chris Mason925baed2008-06-25 16:01:30 -04004088 btrfs_release_path(root, path);
4089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4090 if (ret < 0)
4091 return ret;
4092 btrfs_item_key(path->nodes[0], &found_key, 0);
4093 ret = comp_keys(&found_key, &key);
4094 if (ret < 0)
4095 return 0;
4096 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004097}
4098
Chris Mason3f157a22008-06-25 16:01:31 -04004099/*
4100 * A helper function to walk down the tree starting at min_key, and looking
4101 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004102 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004103 *
4104 * This does not cow, but it does stuff the starting key it finds back
4105 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4106 * key and get a writable path.
4107 *
4108 * This does lock as it descends, and path->keep_locks should be set
4109 * to 1 by the caller.
4110 *
4111 * This honors path->lowest_level to prevent descent past a given level
4112 * of the tree.
4113 *
Chris Masond352ac62008-09-29 15:18:18 -04004114 * min_trans indicates the oldest transaction that you are interested
4115 * in walking through. Any nodes or leaves older than min_trans are
4116 * skipped over (without reading them).
4117 *
Chris Mason3f157a22008-06-25 16:01:31 -04004118 * returns zero if something useful was found, < 0 on error and 1 if there
4119 * was nothing in the tree that matched the search criteria.
4120 */
4121int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004122 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004123 struct btrfs_path *path, int cache_only,
4124 u64 min_trans)
4125{
4126 struct extent_buffer *cur;
4127 struct btrfs_key found_key;
4128 int slot;
Yan96524802008-07-24 12:19:49 -04004129 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004130 u32 nritems;
4131 int level;
4132 int ret = 1;
4133
Chris Mason934d3752008-12-08 16:43:10 -05004134 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004135again:
4136 cur = btrfs_lock_root_node(root);
4137 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004138 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004139 path->nodes[level] = cur;
4140 path->locks[level] = 1;
4141
4142 if (btrfs_header_generation(cur) < min_trans) {
4143 ret = 1;
4144 goto out;
4145 }
Chris Masond3977122009-01-05 21:25:51 -05004146 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004147 nritems = btrfs_header_nritems(cur);
4148 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004149 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004150
Chris Mason323ac952008-10-01 19:05:46 -04004151 /* at the lowest level, we're done, setup the path and exit */
4152 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004153 if (slot >= nritems)
4154 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004155 ret = 0;
4156 path->slots[level] = slot;
4157 btrfs_item_key_to_cpu(cur, &found_key, slot);
4158 goto out;
4159 }
Yan96524802008-07-24 12:19:49 -04004160 if (sret && slot > 0)
4161 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004162 /*
4163 * check this node pointer against the cache_only and
4164 * min_trans parameters. If it isn't in cache or is too
4165 * old, skip to the next one.
4166 */
Chris Masond3977122009-01-05 21:25:51 -05004167 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004168 u64 blockptr;
4169 u64 gen;
4170 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004171 struct btrfs_disk_key disk_key;
4172
Chris Mason3f157a22008-06-25 16:01:31 -04004173 blockptr = btrfs_node_blockptr(cur, slot);
4174 gen = btrfs_node_ptr_generation(cur, slot);
4175 if (gen < min_trans) {
4176 slot++;
4177 continue;
4178 }
4179 if (!cache_only)
4180 break;
4181
Chris Masone02119d2008-09-05 16:13:11 -04004182 if (max_key) {
4183 btrfs_node_key(cur, &disk_key, slot);
4184 if (comp_keys(&disk_key, max_key) >= 0) {
4185 ret = 1;
4186 goto out;
4187 }
4188 }
4189
Chris Mason3f157a22008-06-25 16:01:31 -04004190 tmp = btrfs_find_tree_block(root, blockptr,
4191 btrfs_level_size(root, level - 1));
4192
4193 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4194 free_extent_buffer(tmp);
4195 break;
4196 }
4197 if (tmp)
4198 free_extent_buffer(tmp);
4199 slot++;
4200 }
Chris Masone02119d2008-09-05 16:13:11 -04004201find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004202 /*
4203 * we didn't find a candidate key in this node, walk forward
4204 * and find another one
4205 */
4206 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004207 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004208 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004209 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004210 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004211 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004212 btrfs_release_path(root, path);
4213 goto again;
4214 } else {
4215 goto out;
4216 }
4217 }
4218 /* save our key for returning back */
4219 btrfs_node_key_to_cpu(cur, &found_key, slot);
4220 path->slots[level] = slot;
4221 if (level == path->lowest_level) {
4222 ret = 0;
4223 unlock_up(path, level, 1);
4224 goto out;
4225 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004226 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004227 cur = read_node_slot(root, cur, slot);
4228
4229 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004230
Chris Mason3f157a22008-06-25 16:01:31 -04004231 path->locks[level - 1] = 1;
4232 path->nodes[level - 1] = cur;
4233 unlock_up(path, level, 1);
Chris Mason4008c042009-02-12 14:09:45 -05004234 btrfs_clear_path_blocking(path, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004235 }
4236out:
4237 if (ret == 0)
4238 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004239 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004240 return ret;
4241}
4242
4243/*
4244 * this is similar to btrfs_next_leaf, but does not try to preserve
4245 * and fixup the path. It looks for and returns the next key in the
4246 * tree based on the current path and the cache_only and min_trans
4247 * parameters.
4248 *
4249 * 0 is returned if another key is found, < 0 if there are any errors
4250 * and 1 is returned if there are no higher keys in the tree
4251 *
4252 * path->keep_locks should be set to 1 on the search made before
4253 * calling this function.
4254 */
Chris Masone7a84562008-06-25 16:01:31 -04004255int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004256 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004257 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004258{
Chris Masone7a84562008-06-25 16:01:31 -04004259 int slot;
4260 struct extent_buffer *c;
4261
Chris Mason934d3752008-12-08 16:43:10 -05004262 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004263 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004264 if (!path->nodes[level])
4265 return 1;
4266
4267 slot = path->slots[level] + 1;
4268 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004269next:
Chris Masone7a84562008-06-25 16:01:31 -04004270 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004271 int ret;
4272 int orig_lowest;
4273 struct btrfs_key cur_key;
4274 if (level + 1 >= BTRFS_MAX_LEVEL ||
4275 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004276 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004277
4278 if (path->locks[level + 1]) {
4279 level++;
4280 continue;
4281 }
4282
4283 slot = btrfs_header_nritems(c) - 1;
4284 if (level == 0)
4285 btrfs_item_key_to_cpu(c, &cur_key, slot);
4286 else
4287 btrfs_node_key_to_cpu(c, &cur_key, slot);
4288
4289 orig_lowest = path->lowest_level;
4290 btrfs_release_path(root, path);
4291 path->lowest_level = level;
4292 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4293 0, 0);
4294 path->lowest_level = orig_lowest;
4295 if (ret < 0)
4296 return ret;
4297
4298 c = path->nodes[level];
4299 slot = path->slots[level];
4300 if (ret == 0)
4301 slot++;
4302 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004303 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004304
Chris Masone7a84562008-06-25 16:01:31 -04004305 if (level == 0)
4306 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004307 else {
4308 u64 blockptr = btrfs_node_blockptr(c, slot);
4309 u64 gen = btrfs_node_ptr_generation(c, slot);
4310
4311 if (cache_only) {
4312 struct extent_buffer *cur;
4313 cur = btrfs_find_tree_block(root, blockptr,
4314 btrfs_level_size(root, level - 1));
4315 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4316 slot++;
4317 if (cur)
4318 free_extent_buffer(cur);
4319 goto next;
4320 }
4321 free_extent_buffer(cur);
4322 }
4323 if (gen < min_trans) {
4324 slot++;
4325 goto next;
4326 }
Chris Masone7a84562008-06-25 16:01:31 -04004327 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004328 }
Chris Masone7a84562008-06-25 16:01:31 -04004329 return 0;
4330 }
4331 return 1;
4332}
4333
Chris Mason7bb86312007-12-11 09:25:06 -05004334/*
Chris Mason925baed2008-06-25 16:01:30 -04004335 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004336 * returns 0 if it found something or 1 if there are no greater leaves.
4337 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004338 */
Chris Mason234b63a2007-03-13 10:46:10 -04004339int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004340{
4341 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004342 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004343 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004344 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004345 struct btrfs_key key;
4346 u32 nritems;
4347 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004348 int old_spinning = path->leave_spinning;
4349 int force_blocking = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004350
4351 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004352 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004353 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004354
Chris Mason8e73f272009-04-03 10:14:18 -04004355 /*
4356 * we take the blocks in an order that upsets lockdep. Using
4357 * blocking mode is the only way around it.
4358 */
4359#ifdef CONFIG_DEBUG_LOCK_ALLOC
4360 force_blocking = 1;
4361#endif
Chris Mason925baed2008-06-25 16:01:30 -04004362
Chris Mason8e73f272009-04-03 10:14:18 -04004363 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4364again:
4365 level = 1;
4366 next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004367 btrfs_release_path(root, path);
Chris Mason8e73f272009-04-03 10:14:18 -04004368
Chris Masona2135012008-06-25 16:01:30 -04004369 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004370
4371 if (!force_blocking)
4372 path->leave_spinning = 1;
4373
Chris Mason925baed2008-06-25 16:01:30 -04004374 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4375 path->keep_locks = 0;
4376
4377 if (ret < 0)
4378 return ret;
4379
Chris Masona2135012008-06-25 16:01:30 -04004380 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004381 /*
4382 * by releasing the path above we dropped all our locks. A balance
4383 * could have added more items next to the key that used to be
4384 * at the very end of the block. So, check again here and
4385 * advance the path if there are now more items available.
4386 */
Chris Masona2135012008-06-25 16:01:30 -04004387 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004388 if (ret == 0)
4389 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004390 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004391 goto done;
4392 }
Chris Masond97e63b2007-02-20 16:40:44 -05004393
Chris Masond3977122009-01-05 21:25:51 -05004394 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004395 if (!path->nodes[level]) {
4396 ret = 1;
4397 goto done;
4398 }
Chris Mason5f39d392007-10-15 16:14:19 -04004399
Chris Masond97e63b2007-02-20 16:40:44 -05004400 slot = path->slots[level] + 1;
4401 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004402 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004403 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004404 if (level == BTRFS_MAX_LEVEL) {
4405 ret = 1;
4406 goto done;
4407 }
Chris Masond97e63b2007-02-20 16:40:44 -05004408 continue;
4409 }
Chris Mason5f39d392007-10-15 16:14:19 -04004410
Chris Mason925baed2008-06-25 16:01:30 -04004411 if (next) {
4412 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004413 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004414 }
Chris Mason5f39d392007-10-15 16:14:19 -04004415
Chris Mason8e73f272009-04-03 10:14:18 -04004416 next = c;
4417 ret = read_block_for_search(NULL, root, path, &next, level,
4418 slot, &key);
4419 if (ret == -EAGAIN)
4420 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004421
Chris Mason76a05b32009-05-14 13:24:30 -04004422 if (ret < 0) {
4423 btrfs_release_path(root, path);
4424 goto done;
4425 }
4426
Chris Mason5cd57b22008-06-25 16:01:30 -04004427 if (!path->skip_locking) {
Chris Mason8e73f272009-04-03 10:14:18 -04004428 ret = btrfs_try_spin_lock(next);
4429 if (!ret) {
4430 btrfs_set_path_blocking(path);
4431 btrfs_tree_lock(next);
4432 if (!force_blocking)
4433 btrfs_clear_path_blocking(path, next);
4434 }
4435 if (force_blocking)
4436 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004437 }
Chris Masond97e63b2007-02-20 16:40:44 -05004438 break;
4439 }
4440 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004441 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004442 level--;
4443 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004444 if (path->locks[level])
4445 btrfs_tree_unlock(c);
Chris Mason8e73f272009-04-03 10:14:18 -04004446
Chris Mason5f39d392007-10-15 16:14:19 -04004447 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004448 path->nodes[level] = next;
4449 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004450 if (!path->skip_locking)
4451 path->locks[level] = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004452
Chris Masond97e63b2007-02-20 16:40:44 -05004453 if (!level)
4454 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004455
Chris Mason8e73f272009-04-03 10:14:18 -04004456 ret = read_block_for_search(NULL, root, path, &next, level,
4457 0, &key);
4458 if (ret == -EAGAIN)
4459 goto again;
4460
Chris Mason76a05b32009-05-14 13:24:30 -04004461 if (ret < 0) {
4462 btrfs_release_path(root, path);
4463 goto done;
4464 }
4465
Chris Mason5cd57b22008-06-25 16:01:30 -04004466 if (!path->skip_locking) {
Chris Masonb9447ef82009-03-09 11:45:38 -04004467 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004468 ret = btrfs_try_spin_lock(next);
4469 if (!ret) {
4470 btrfs_set_path_blocking(path);
4471 btrfs_tree_lock(next);
4472 if (!force_blocking)
4473 btrfs_clear_path_blocking(path, next);
4474 }
4475 if (force_blocking)
4476 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004477 }
Chris Masond97e63b2007-02-20 16:40:44 -05004478 }
Chris Mason8e73f272009-04-03 10:14:18 -04004479 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004480done:
4481 unlock_up(path, 0, 1);
Chris Mason8e73f272009-04-03 10:14:18 -04004482 path->leave_spinning = old_spinning;
4483 if (!old_spinning)
4484 btrfs_set_path_blocking(path);
4485
4486 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004487}
Chris Mason0b86a832008-03-24 15:01:56 -04004488
Chris Mason3f157a22008-06-25 16:01:31 -04004489/*
4490 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4491 * searching until it gets past min_objectid or finds an item of 'type'
4492 *
4493 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4494 */
Chris Mason0b86a832008-03-24 15:01:56 -04004495int btrfs_previous_item(struct btrfs_root *root,
4496 struct btrfs_path *path, u64 min_objectid,
4497 int type)
4498{
4499 struct btrfs_key found_key;
4500 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004501 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004502 int ret;
4503
Chris Masond3977122009-01-05 21:25:51 -05004504 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004505 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004506 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004507 ret = btrfs_prev_leaf(root, path);
4508 if (ret != 0)
4509 return ret;
4510 } else {
4511 path->slots[0]--;
4512 }
4513 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004514 nritems = btrfs_header_nritems(leaf);
4515 if (nritems == 0)
4516 return 1;
4517 if (path->slots[0] == nritems)
4518 path->slots[0]--;
4519
Chris Mason0b86a832008-03-24 15:01:56 -04004520 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004521 if (found_key.objectid < min_objectid)
4522 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004523 if (found_key.type == type)
4524 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004525 if (found_key.objectid == min_objectid &&
4526 found_key.type < type)
4527 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004528 }
4529 return 1;
4530}