blob: ff3261ff2e19143df4663090342a67818f3ac489 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050040
Chris Masondf24a2b2007-04-04 09:36:31 -040041inline void btrfs_init_path(struct btrfs_path *p)
42{
43 memset(p, 0, sizeof(*p));
44}
45
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040050 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040051 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
53 }
Chris Masondf24a2b2007-04-04 09:36:31 -040054 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040055}
56
Chris Masond352ac62008-09-29 15:18:18 -040057/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040058void btrfs_free_path(struct btrfs_path *p)
59{
Chris Masondf24a2b2007-04-04 09:36:31 -040060 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040061 kmem_cache_free(btrfs_path_cachep, p);
62}
63
Chris Masond352ac62008-09-29 15:18:18 -040064/*
65 * path release drops references on the extent buffers in the path
66 * and it drops any locks held by this path
67 *
68 * It is safe to call this on paths that no locks or extent buffers held.
69 */
Chris Masone02119d2008-09-05 16:13:11 -040070void noinline btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050071{
72 int i;
Chris Masona2135012008-06-25 16:01:30 -040073
Chris Mason234b63a2007-03-13 10:46:10 -040074 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -040075 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050076 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040077 continue;
78 if (p->locks[i]) {
79 btrfs_tree_unlock(p->nodes[i]);
80 p->locks[i] = 0;
81 }
Chris Mason5f39d392007-10-15 16:14:19 -040082 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -040083 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -050084 }
85}
86
Chris Masond352ac62008-09-29 15:18:18 -040087/*
88 * safely gets a reference on the root node of a tree. A lock
89 * is not taken, so a concurrent writer may put a different node
90 * at the root of the tree. See btrfs_lock_root_node for the
91 * looping required.
92 *
93 * The extent buffer returned by this has a reference taken, so
94 * it won't disappear. It may stop being the root of the tree
95 * at any time because there are no locks held.
96 */
Chris Mason925baed2008-06-25 16:01:30 -040097struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
98{
99 struct extent_buffer *eb;
100 spin_lock(&root->node_lock);
101 eb = root->node;
102 extent_buffer_get(eb);
103 spin_unlock(&root->node_lock);
104 return eb;
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/* loop around taking references on and locking the root node of the
108 * tree until you end up with a lock on the root. A locked buffer
109 * is returned, with a reference held.
110 */
Chris Mason925baed2008-06-25 16:01:30 -0400111struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
112{
113 struct extent_buffer *eb;
114
115 while(1) {
116 eb = btrfs_root_node(root);
117 btrfs_tree_lock(eb);
118
119 spin_lock(&root->node_lock);
120 if (eb == root->node) {
121 spin_unlock(&root->node_lock);
122 break;
123 }
124 spin_unlock(&root->node_lock);
125
126 btrfs_tree_unlock(eb);
127 free_extent_buffer(eb);
128 }
129 return eb;
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/* cowonly root (everything not a reference counted cow subvolume), just get
133 * put onto a simple dirty list. transaction.c walks this to make sure they
134 * get properly updated on disk.
135 */
Chris Mason0b86a832008-03-24 15:01:56 -0400136static void add_root_to_dirty_list(struct btrfs_root *root)
137{
138 if (root->track_dirty && list_empty(&root->dirty_list)) {
139 list_add(&root->dirty_list,
140 &root->fs_info->dirty_cowonly_roots);
141 }
142}
143
Chris Masond352ac62008-09-29 15:18:18 -0400144/*
145 * used by snapshot creation to make a copy of a root for a tree with
146 * a given objectid. The buffer with the new root node is returned in
147 * cow_ret, and this func returns zero on success or a negative error code.
148 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500149int btrfs_copy_root(struct btrfs_trans_handle *trans,
150 struct btrfs_root *root,
151 struct extent_buffer *buf,
152 struct extent_buffer **cow_ret, u64 new_root_objectid)
153{
154 struct extent_buffer *cow;
155 u32 nritems;
156 int ret = 0;
157 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500158 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500159
Chris Mason4aec2b52007-12-18 16:25:45 -0500160 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
161 if (!new_root)
162 return -ENOMEM;
163
164 memcpy(new_root, root, sizeof(*new_root));
165 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500166
167 WARN_ON(root->ref_cows && trans->transid !=
168 root->fs_info->running_transaction->transid);
169 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
170
171 level = btrfs_header_level(buf);
172 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400173
174 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
175 new_root_objectid, trans->transid,
176 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500177 if (IS_ERR(cow)) {
178 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500179 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500180 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500181
182 copy_extent_buffer(cow, buf, 0, 0, cow->len);
183 btrfs_set_header_bytenr(cow, cow->start);
184 btrfs_set_header_generation(cow, trans->transid);
185 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500187
188 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400189 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500190 kfree(new_root);
191
Chris Masonbe20aa92007-12-17 20:14:01 -0500192 if (ret)
193 return ret;
194
195 btrfs_mark_buffer_dirty(cow);
196 *cow_ret = cow;
197 return 0;
198}
199
Chris Masond352ac62008-09-29 15:18:18 -0400200/*
201 * does the dirty work in cow of a single block. The parent block
202 * (if supplied) is updated to point to the new cow copy. The new
203 * buffer is marked dirty and returned locked. If you modify the block
204 * it needs to be marked dirty again.
205 *
206 * search_start -- an allocation hint for the new block
207 *
208 * empty_size -- a hint that you plan on doing more cow. This is the size in bytes
209 * the allocator should try to find free next to the block it returns. This is
210 * just a hint and may be ignored by the allocator.
211 *
212 * prealloc_dest -- if you have already reserved a destination for the cow,
213 * this uses that block instead of allocating a new one. btrfs_alloc_reserved_extent
214 * is used to finish the allocation.
215 */
Chris Masone02119d2008-09-05 16:13:11 -0400216int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400217 struct btrfs_root *root,
218 struct extent_buffer *buf,
219 struct extent_buffer *parent, int parent_slot,
220 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400221 u64 search_start, u64 empty_size,
222 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400223{
Zheng Yan31840ae2008-09-23 13:14:14 -0400224 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400225 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500226 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400227 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500228 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400229 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400230
Chris Mason925baed2008-06-25 16:01:30 -0400231 if (*cow_ret == buf)
232 unlock_orig = 1;
233
234 WARN_ON(!btrfs_tree_locked(buf));
235
Zheng Yan31840ae2008-09-23 13:14:14 -0400236 if (parent)
237 parent_start = parent->start;
238 else
239 parent_start = 0;
240
Chris Mason7bb86312007-12-11 09:25:06 -0500241 WARN_ON(root->ref_cows && trans->transid !=
242 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400243 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400244
Chris Mason7bb86312007-12-11 09:25:06 -0500245 level = btrfs_header_level(buf);
246 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400247
Chris Mason65b51a02008-08-01 15:11:20 -0400248 if (prealloc_dest) {
249 struct btrfs_key ins;
250
251 ins.objectid = prealloc_dest;
252 ins.offset = buf->len;
253 ins.type = BTRFS_EXTENT_ITEM_KEY;
254
Zheng Yan31840ae2008-09-23 13:14:14 -0400255 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400256 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400257 trans->transid, level, 0,
Chris Mason65b51a02008-08-01 15:11:20 -0400258 &ins);
259 BUG_ON(ret);
260 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
261 buf->len);
262 } else {
263 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400264 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400265 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400266 trans->transid, level,
267 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400268 }
Chris Mason6702ed42007-08-07 16:15:09 -0400269 if (IS_ERR(cow))
270 return PTR_ERR(cow);
271
Chris Mason5f39d392007-10-15 16:14:19 -0400272 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400273 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400274 btrfs_set_header_generation(cow, trans->transid);
275 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400276 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400277
Chris Mason5f39d392007-10-15 16:14:19 -0400278 WARN_ON(btrfs_header_generation(buf) > trans->transid);
279 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400280 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400281 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400282 if (ret)
283 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400284
285 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
286 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400287 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
288 /*
289 * There are only two places that can drop reference to
290 * tree blocks owned by living reloc trees, one is here,
291 * the other place is btrfs_merge_path. In both places,
292 * we check reference count while tree block is locked.
293 * Furthermore, if reference count is one, it won't get
294 * increased by someone else.
295 */
296 u32 refs;
297 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
298 buf->len, &refs);
299 BUG_ON(ret);
300 if (refs == 1) {
301 ret = btrfs_update_ref(trans, root, buf, cow,
302 0, nritems);
303 clean_tree_block(trans, root, buf);
304 } else {
305 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
306 }
307 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400308 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400309 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
310 if (ret)
311 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400312 clean_tree_block(trans, root, buf);
313 }
314
Zheng Yan1a40e232008-09-26 10:09:34 -0400315 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
316 ret = btrfs_add_reloc_mapping(root, buf->start,
317 buf->len, cow->start);
318 BUG_ON(ret);
319 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
320 WARN_ON(ret);
321 }
322
Chris Mason6702ed42007-08-07 16:15:09 -0400323 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400324 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400325
326 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400327 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400328 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400329 spin_unlock(&root->node_lock);
330
Chris Mason6702ed42007-08-07 16:15:09 -0400331 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400332 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400333 buf->len, buf->start,
334 root->root_key.objectid,
335 btrfs_header_generation(buf),
336 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400337 }
Chris Mason5f39d392007-10-15 16:14:19 -0400338 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400339 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400340 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400341 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400342 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500343 WARN_ON(trans->transid == 0);
344 btrfs_set_node_ptr_generation(parent, parent_slot,
345 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400346 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400347 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500348 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400349 parent_start, btrfs_header_owner(parent),
350 btrfs_header_generation(parent), 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400351 }
Chris Mason925baed2008-06-25 16:01:30 -0400352 if (unlock_orig)
353 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400354 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400355 btrfs_mark_buffer_dirty(cow);
356 *cow_ret = cow;
357 return 0;
358}
359
Chris Masond352ac62008-09-29 15:18:18 -0400360/*
361 * cows a single block, see __btrfs_cow_block for the real work.
362 * This version of it has extra checks so that a block isn't cow'd more than
363 * once per transaction, as long as it hasn't been written yet
364 */
Chris Masone02119d2008-09-05 16:13:11 -0400365int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400366 struct btrfs_root *root, struct extent_buffer *buf,
367 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400368 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500369{
Chris Mason6702ed42007-08-07 16:15:09 -0400370 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400371 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500372
Chris Masonccd467d2007-06-28 15:57:36 -0400373 if (trans->transaction != root->fs_info->running_transaction) {
374 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
375 root->fs_info->running_transaction->transid);
376 WARN_ON(1);
377 }
378 if (trans->transid != root->fs_info->generation) {
379 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
380 root->fs_info->generation);
381 WARN_ON(1);
382 }
Chris Masondc17ff82008-01-08 15:46:30 -0500383
Chris Mason63b10fc2008-04-01 11:21:32 -0400384 spin_lock(&root->fs_info->hash_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400385 if (btrfs_header_generation(buf) == trans->transid &&
386 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400387 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500388 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400389 spin_unlock(&root->fs_info->hash_lock);
Chris Mason65b51a02008-08-01 15:11:20 -0400390 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500391 return 0;
392 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400393 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400394 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400395 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400396 parent_slot, cow_ret, search_start, 0,
397 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400398 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400399}
400
Chris Masond352ac62008-09-29 15:18:18 -0400401/*
402 * helper function for defrag to decide if two blocks pointed to by a
403 * node are actually close by
404 */
Chris Mason6b800532007-10-15 16:17:34 -0400405static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400406{
Chris Mason6b800532007-10-15 16:17:34 -0400407 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400408 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400409 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400410 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500411 return 0;
412}
413
Chris Mason081e9572007-11-06 10:26:24 -0500414/*
415 * compare two keys in a memcmp fashion
416 */
417static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
418{
419 struct btrfs_key k1;
420
421 btrfs_disk_key_to_cpu(&k1, disk);
422
423 if (k1.objectid > k2->objectid)
424 return 1;
425 if (k1.objectid < k2->objectid)
426 return -1;
427 if (k1.type > k2->type)
428 return 1;
429 if (k1.type < k2->type)
430 return -1;
431 if (k1.offset > k2->offset)
432 return 1;
433 if (k1.offset < k2->offset)
434 return -1;
435 return 0;
436}
437
438
Chris Masond352ac62008-09-29 15:18:18 -0400439/*
440 * this is used by the defrag code to go through all the
441 * leaves pointed to by a node and reallocate them so that
442 * disk order is close to key order
443 */
Chris Mason6702ed42007-08-07 16:15:09 -0400444int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400445 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400446 int start_slot, int cache_only, u64 *last_ret,
447 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400448{
Chris Mason6b800532007-10-15 16:17:34 -0400449 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400450 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400451 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400452 u64 search_start = *last_ret;
453 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400454 u64 other;
455 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400456 int end_slot;
457 int i;
458 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400459 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400460 int uptodate;
461 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500462 int progress_passed = 0;
463 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400464
Chris Mason5708b952007-10-25 15:43:18 -0400465 parent_level = btrfs_header_level(parent);
466 if (cache_only && parent_level != 1)
467 return 0;
468
Chris Mason6702ed42007-08-07 16:15:09 -0400469 if (trans->transaction != root->fs_info->running_transaction) {
470 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
471 root->fs_info->running_transaction->transid);
472 WARN_ON(1);
473 }
474 if (trans->transid != root->fs_info->generation) {
475 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
476 root->fs_info->generation);
477 WARN_ON(1);
478 }
Chris Mason86479a02007-09-10 19:58:16 -0400479
Chris Mason6b800532007-10-15 16:17:34 -0400480 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400481 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400482 end_slot = parent_nritems;
483
484 if (parent_nritems == 1)
485 return 0;
486
487 for (i = start_slot; i < end_slot; i++) {
488 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400489
Chris Mason5708b952007-10-25 15:43:18 -0400490 if (!parent->map_token) {
491 map_extent_buffer(parent,
492 btrfs_node_key_ptr_offset(i),
493 sizeof(struct btrfs_key_ptr),
494 &parent->map_token, &parent->kaddr,
495 &parent->map_start, &parent->map_len,
496 KM_USER1);
497 }
Chris Mason081e9572007-11-06 10:26:24 -0500498 btrfs_node_key(parent, &disk_key, i);
499 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
500 continue;
501
502 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400503 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400504 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400505 if (last_block == 0)
506 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400507
Chris Mason6702ed42007-08-07 16:15:09 -0400508 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400509 other = btrfs_node_blockptr(parent, i - 1);
510 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400511 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400512 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400513 other = btrfs_node_blockptr(parent, i + 1);
514 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400515 }
Chris Masone9d0b132007-08-10 14:06:19 -0400516 if (close) {
517 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400518 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400519 }
Chris Mason5708b952007-10-25 15:43:18 -0400520 if (parent->map_token) {
521 unmap_extent_buffer(parent, parent->map_token,
522 KM_USER1);
523 parent->map_token = NULL;
524 }
Chris Mason6702ed42007-08-07 16:15:09 -0400525
Chris Mason6b800532007-10-15 16:17:34 -0400526 cur = btrfs_find_tree_block(root, blocknr, blocksize);
527 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400528 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400529 else
530 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400531 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400532 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400533 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400534 continue;
535 }
Chris Mason6b800532007-10-15 16:17:34 -0400536 if (!cur) {
537 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400538 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400539 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400540 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400541 }
Chris Mason6702ed42007-08-07 16:15:09 -0400542 }
Chris Masone9d0b132007-08-10 14:06:19 -0400543 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400544 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400545
Chris Masone7a84562008-06-25 16:01:31 -0400546 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400547 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400548 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400549 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400550 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400551 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400552 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400553 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400554 break;
Yan252c38f2007-08-29 09:11:44 -0400555 }
Chris Masone7a84562008-06-25 16:01:31 -0400556 search_start = cur->start;
557 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400558 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400559 btrfs_tree_unlock(cur);
560 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400561 }
Chris Mason5708b952007-10-25 15:43:18 -0400562 if (parent->map_token) {
563 unmap_extent_buffer(parent, parent->map_token,
564 KM_USER1);
565 parent->map_token = NULL;
566 }
Chris Mason6702ed42007-08-07 16:15:09 -0400567 return err;
568}
569
Chris Mason74123bd2007-02-02 11:05:29 -0500570/*
571 * The leaf data grows from end-to-front in the node.
572 * this returns the address of the start of the last item,
573 * which is the stop of the leaf data stack
574 */
Chris Mason123abc82007-03-14 14:14:43 -0400575static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400576 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500577{
Chris Mason5f39d392007-10-15 16:14:19 -0400578 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500579 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400580 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400581 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500582}
583
Chris Masond352ac62008-09-29 15:18:18 -0400584/*
585 * extra debugging checks to make sure all the items in a key are
586 * well formed and in the proper order
587 */
Chris Mason123abc82007-03-14 14:14:43 -0400588static int check_node(struct btrfs_root *root, struct btrfs_path *path,
589 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500590{
Chris Mason5f39d392007-10-15 16:14:19 -0400591 struct extent_buffer *parent = NULL;
592 struct extent_buffer *node = path->nodes[level];
593 struct btrfs_disk_key parent_key;
594 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500595 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400596 int slot;
597 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400598 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500599
600 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400601 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400602
Chris Mason8d7be552007-05-10 11:24:42 -0400603 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400604 BUG_ON(nritems == 0);
605 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400606 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400607 btrfs_node_key(parent, &parent_key, parent_slot);
608 btrfs_node_key(node, &node_key, 0);
609 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400610 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400611 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400612 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500613 }
Chris Mason123abc82007-03-14 14:14:43 -0400614 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400615 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400616 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
617 btrfs_node_key(node, &node_key, slot);
618 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400619 }
620 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400621 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
622 btrfs_node_key(node, &node_key, slot);
623 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500624 }
625 return 0;
626}
627
Chris Masond352ac62008-09-29 15:18:18 -0400628/*
629 * extra checking to make sure all the items in a leaf are
630 * well formed and in the proper order
631 */
Chris Mason123abc82007-03-14 14:14:43 -0400632static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
633 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500634{
Chris Mason5f39d392007-10-15 16:14:19 -0400635 struct extent_buffer *leaf = path->nodes[level];
636 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500637 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400638 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400639 struct btrfs_disk_key parent_key;
640 struct btrfs_disk_key leaf_key;
641 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400642
Chris Mason5f39d392007-10-15 16:14:19 -0400643 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500644
645 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400646 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400647
648 if (nritems == 0)
649 return 0;
650
651 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400652 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400653 btrfs_node_key(parent, &parent_key, parent_slot);
654 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400655
Chris Mason5f39d392007-10-15 16:14:19 -0400656 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400657 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400658 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400659 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500660 }
Chris Mason5f39d392007-10-15 16:14:19 -0400661#if 0
662 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
663 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
664 btrfs_item_key(leaf, &leaf_key, i);
665 if (comp_keys(&leaf_key, &cpukey) >= 0) {
666 btrfs_print_leaf(root, leaf);
667 printk("slot %d offset bad key\n", i);
668 BUG_ON(1);
669 }
670 if (btrfs_item_offset_nr(leaf, i) !=
671 btrfs_item_end_nr(leaf, i + 1)) {
672 btrfs_print_leaf(root, leaf);
673 printk("slot %d offset bad\n", i);
674 BUG_ON(1);
675 }
676 if (i == 0) {
677 if (btrfs_item_offset_nr(leaf, i) +
678 btrfs_item_size_nr(leaf, i) !=
679 BTRFS_LEAF_DATA_SIZE(root)) {
680 btrfs_print_leaf(root, leaf);
681 printk("slot %d first offset bad\n", i);
682 BUG_ON(1);
683 }
684 }
685 }
686 if (nritems > 0) {
687 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
688 btrfs_print_leaf(root, leaf);
689 printk("slot %d bad size \n", nritems - 1);
690 BUG_ON(1);
691 }
692 }
693#endif
694 if (slot != 0 && slot < nritems - 1) {
695 btrfs_item_key(leaf, &leaf_key, slot);
696 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
697 if (comp_keys(&leaf_key, &cpukey) <= 0) {
698 btrfs_print_leaf(root, leaf);
699 printk("slot %d offset bad key\n", slot);
700 BUG_ON(1);
701 }
702 if (btrfs_item_offset_nr(leaf, slot - 1) !=
703 btrfs_item_end_nr(leaf, slot)) {
704 btrfs_print_leaf(root, leaf);
705 printk("slot %d offset bad\n", slot);
706 BUG_ON(1);
707 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500708 }
Chris Mason8d7be552007-05-10 11:24:42 -0400709 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400710 btrfs_item_key(leaf, &leaf_key, slot);
711 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
712 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
713 if (btrfs_item_offset_nr(leaf, slot) !=
714 btrfs_item_end_nr(leaf, slot + 1)) {
715 btrfs_print_leaf(root, leaf);
716 printk("slot %d offset bad\n", slot);
717 BUG_ON(1);
718 }
Chris Mason8d7be552007-05-10 11:24:42 -0400719 }
Chris Mason5f39d392007-10-15 16:14:19 -0400720 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
721 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500722 return 0;
723}
724
Chris Mason98ed5172008-01-03 10:01:48 -0500725static int noinline check_block(struct btrfs_root *root,
726 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500727{
Chris Masonf1885912008-04-09 16:28:12 -0400728 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400729 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400730 if (btrfs_header_level(path->nodes[level]) != level)
731 printk("warning: bad level %Lu wanted %d found %d\n",
732 path->nodes[level]->start, level,
733 btrfs_header_level(path->nodes[level]));
734 found_start = btrfs_header_bytenr(path->nodes[level]);
735 if (found_start != path->nodes[level]->start) {
736 printk("warning: bad bytentr %Lu found %Lu\n",
737 path->nodes[level]->start, found_start);
738 }
Chris Masondb945352007-10-15 16:15:53 -0400739#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400740 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400741
Chris Mason479965d2007-10-15 16:14:27 -0400742 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
743 (unsigned long)btrfs_header_fsid(buf),
744 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400745 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400746 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400747 }
Chris Masondb945352007-10-15 16:15:53 -0400748#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500749 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400750 return check_leaf(root, path, level);
751 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500752}
753
Chris Mason74123bd2007-02-02 11:05:29 -0500754/*
Chris Mason5f39d392007-10-15 16:14:19 -0400755 * search for key in the extent_buffer. The items start at offset p,
756 * and they are item_size apart. There are 'max' items in p.
757 *
Chris Mason74123bd2007-02-02 11:05:29 -0500758 * the slot in the array is returned via slot, and it points to
759 * the place where you would insert key if it is not found in
760 * the array.
761 *
762 * slot may point to max if the key is bigger than all of the keys
763 */
Chris Masone02119d2008-09-05 16:13:11 -0400764static noinline int generic_bin_search(struct extent_buffer *eb,
765 unsigned long p,
766 int item_size, struct btrfs_key *key,
767 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500768{
769 int low = 0;
770 int high = max;
771 int mid;
772 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400773 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400774 struct btrfs_disk_key unaligned;
775 unsigned long offset;
776 char *map_token = NULL;
777 char *kaddr = NULL;
778 unsigned long map_start = 0;
779 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400780 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500781
782 while(low < high) {
783 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400784 offset = p + mid * item_size;
785
786 if (!map_token || offset < map_start ||
787 (offset + sizeof(struct btrfs_disk_key)) >
788 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400789 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400790 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400791 map_token = NULL;
792 }
793 err = map_extent_buffer(eb, offset,
794 sizeof(struct btrfs_disk_key),
795 &map_token, &kaddr,
796 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400797
Chris Mason479965d2007-10-15 16:14:27 -0400798 if (!err) {
799 tmp = (struct btrfs_disk_key *)(kaddr + offset -
800 map_start);
801 } else {
802 read_extent_buffer(eb, &unaligned,
803 offset, sizeof(unaligned));
804 tmp = &unaligned;
805 }
806
Chris Mason5f39d392007-10-15 16:14:19 -0400807 } else {
808 tmp = (struct btrfs_disk_key *)(kaddr + offset -
809 map_start);
810 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500811 ret = comp_keys(tmp, key);
812
813 if (ret < 0)
814 low = mid + 1;
815 else if (ret > 0)
816 high = mid;
817 else {
818 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400819 if (map_token)
820 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500821 return 0;
822 }
823 }
824 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400825 if (map_token)
826 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500827 return 1;
828}
829
Chris Mason97571fd2007-02-24 13:39:08 -0500830/*
831 * simple bin_search frontend that does the right thing for
832 * leaves vs nodes
833 */
Chris Mason5f39d392007-10-15 16:14:19 -0400834static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
835 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500836{
Chris Mason5f39d392007-10-15 16:14:19 -0400837 if (level == 0) {
838 return generic_bin_search(eb,
839 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400840 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400841 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400842 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500843 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400844 return generic_bin_search(eb,
845 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400846 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400847 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400848 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500849 }
850 return -1;
851}
852
Chris Masond352ac62008-09-29 15:18:18 -0400853/* given a node and slot number, this reads the blocks it points to. The
854 * extent buffer is returned with a reference taken (but unlocked).
855 * NULL is returned on error.
856 */
Chris Masone02119d2008-09-05 16:13:11 -0400857static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400858 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500859{
Chris Masonca7a79a2008-05-12 12:59:19 -0400860 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500861 if (slot < 0)
862 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400863 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500864 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400865
866 BUG_ON(level == 0);
867
Chris Masondb945352007-10-15 16:15:53 -0400868 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400869 btrfs_level_size(root, level - 1),
870 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500871}
872
Chris Masond352ac62008-09-29 15:18:18 -0400873/*
874 * node level balancing, used to make sure nodes are in proper order for
875 * item deletion. We balance from the top down, so we have to make sure
876 * that a deletion won't leave an node completely empty later on.
877 */
Chris Masone02119d2008-09-05 16:13:11 -0400878static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500879 struct btrfs_root *root,
880 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500881{
Chris Mason5f39d392007-10-15 16:14:19 -0400882 struct extent_buffer *right = NULL;
883 struct extent_buffer *mid;
884 struct extent_buffer *left = NULL;
885 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500886 int ret = 0;
887 int wret;
888 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500889 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400890 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500891 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500892
893 if (level == 0)
894 return 0;
895
Chris Mason5f39d392007-10-15 16:14:19 -0400896 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400897 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500898 WARN_ON(btrfs_header_generation(mid) != trans->transid);
899
Chris Mason1d4f8a02007-03-13 09:28:32 -0400900 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500901
Chris Mason234b63a2007-03-13 10:46:10 -0400902 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400903 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500904 pslot = path->slots[level + 1];
905
Chris Mason40689472007-03-17 14:29:23 -0400906 /*
907 * deal with the case where there is only one pointer in the root
908 * by promoting the node below to a root
909 */
Chris Mason5f39d392007-10-15 16:14:19 -0400910 if (!parent) {
911 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500912
Chris Mason5f39d392007-10-15 16:14:19 -0400913 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500914 return 0;
915
916 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400917 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400918 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500919 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400920 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500921 BUG_ON(ret);
922
Chris Mason925baed2008-06-25 16:01:30 -0400923 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500924 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400925 spin_unlock(&root->node_lock);
926
Zheng Yan31840ae2008-09-23 13:14:14 -0400927 ret = btrfs_update_extent_ref(trans, root, child->start,
928 mid->start, child->start,
929 root->root_key.objectid,
930 trans->transid, level - 1, 0);
931 BUG_ON(ret);
932
Chris Mason0b86a832008-03-24 15:01:56 -0400933 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400934 btrfs_tree_unlock(child);
935 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500936 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400937 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400938 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500939 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400940 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500941 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400942 mid->start, root->root_key.objectid,
Chris Mason7bb86312007-12-11 09:25:06 -0500943 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500944 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400945 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400946 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500947 }
Chris Mason5f39d392007-10-15 16:14:19 -0400948 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400949 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500950 return 0;
951
Chris Mason5f39d392007-10-15 16:14:19 -0400952 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400953 err_on_enospc = 1;
954
Chris Mason5f39d392007-10-15 16:14:19 -0400955 left = read_node_slot(root, parent, pslot - 1);
956 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400957 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400958 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400959 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400960 if (wret) {
961 ret = wret;
962 goto enospc;
963 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400964 }
Chris Mason5f39d392007-10-15 16:14:19 -0400965 right = read_node_slot(root, parent, pslot + 1);
966 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400967 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400968 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400969 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400970 if (wret) {
971 ret = wret;
972 goto enospc;
973 }
974 }
975
976 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400977 if (left) {
978 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400979 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500980 if (wret < 0)
981 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400982 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400983 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500984 }
Chris Mason79f95c82007-03-01 15:16:26 -0500985
986 /*
987 * then try to empty the right most buffer into the middle
988 */
Chris Mason5f39d392007-10-15 16:14:19 -0400989 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400990 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400991 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500992 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400993 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400994 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500995 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400996 u32 blocksize = right->len;
997
Chris Mason5f39d392007-10-15 16:14:19 -0400998 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400999 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001000 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001001 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001002 wret = del_ptr(trans, root, path, level + 1, pslot +
1003 1);
Chris Masonbb803952007-03-01 12:04:21 -05001004 if (wret)
1005 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001006 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001007 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001008 btrfs_header_owner(parent),
1009 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001010 if (wret)
1011 ret = wret;
1012 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001013 struct btrfs_disk_key right_key;
1014 btrfs_node_key(right, &right_key, 0);
1015 btrfs_set_node_key(parent, &right_key, pslot + 1);
1016 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001017 }
1018 }
Chris Mason5f39d392007-10-15 16:14:19 -04001019 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001020 /*
1021 * we're not allowed to leave a node with one item in the
1022 * tree during a delete. A deletion from lower in the tree
1023 * could try to delete the only pointer in this node.
1024 * So, pull some keys from the left.
1025 * There has to be a left pointer at this point because
1026 * otherwise we would have pulled some pointers from the
1027 * right
1028 */
Chris Mason5f39d392007-10-15 16:14:19 -04001029 BUG_ON(!left);
1030 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001031 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001032 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001033 goto enospc;
1034 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001035 if (wret == 1) {
1036 wret = push_node_left(trans, root, left, mid, 1);
1037 if (wret < 0)
1038 ret = wret;
1039 }
Chris Mason79f95c82007-03-01 15:16:26 -05001040 BUG_ON(wret == 1);
1041 }
Chris Mason5f39d392007-10-15 16:14:19 -04001042 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001043 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001044 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001045 u64 bytenr = mid->start;
1046 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001047
Chris Mason5f39d392007-10-15 16:14:19 -04001048 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001049 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001050 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001051 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001052 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001053 if (wret)
1054 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001055 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001056 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001057 btrfs_header_owner(parent),
1058 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001059 if (wret)
1060 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001061 } else {
1062 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001063 struct btrfs_disk_key mid_key;
1064 btrfs_node_key(mid, &mid_key, 0);
1065 btrfs_set_node_key(parent, &mid_key, pslot);
1066 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001067 }
Chris Masonbb803952007-03-01 12:04:21 -05001068
Chris Mason79f95c82007-03-01 15:16:26 -05001069 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001070 if (left) {
1071 if (btrfs_header_nritems(left) > orig_slot) {
1072 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001073 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001074 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001075 path->slots[level + 1] -= 1;
1076 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001077 if (mid) {
1078 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001079 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001080 }
Chris Masonbb803952007-03-01 12:04:21 -05001081 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001082 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001083 path->slots[level] = orig_slot;
1084 }
1085 }
Chris Mason79f95c82007-03-01 15:16:26 -05001086 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001087 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001088 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001089 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001090 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001091enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001092 if (right) {
1093 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001094 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001095 }
1096 if (left) {
1097 if (path->nodes[level] != left)
1098 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001099 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001100 }
Chris Masonbb803952007-03-01 12:04:21 -05001101 return ret;
1102}
1103
Chris Masond352ac62008-09-29 15:18:18 -04001104/* Node balancing for insertion. Here we only split or push nodes around
1105 * when they are completely full. This is also done top down, so we
1106 * have to be pessimistic.
1107 */
Chris Mason98ed5172008-01-03 10:01:48 -05001108static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1109 struct btrfs_root *root,
1110 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001111{
Chris Mason5f39d392007-10-15 16:14:19 -04001112 struct extent_buffer *right = NULL;
1113 struct extent_buffer *mid;
1114 struct extent_buffer *left = NULL;
1115 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001116 int ret = 0;
1117 int wret;
1118 int pslot;
1119 int orig_slot = path->slots[level];
1120 u64 orig_ptr;
1121
1122 if (level == 0)
1123 return 1;
1124
Chris Mason5f39d392007-10-15 16:14:19 -04001125 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001126 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001127 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1128
1129 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001130 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001131 pslot = path->slots[level + 1];
1132
Chris Mason5f39d392007-10-15 16:14:19 -04001133 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001134 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001135
Chris Mason5f39d392007-10-15 16:14:19 -04001136 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001137
1138 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001139 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001140 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001141
1142 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001143 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001144 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1145 wret = 1;
1146 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001147 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001148 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001149 if (ret)
1150 wret = 1;
1151 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001152 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001153 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001154 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001155 }
Chris Masone66f7092007-04-20 13:16:02 -04001156 if (wret < 0)
1157 ret = wret;
1158 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001159 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001160 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001161 btrfs_node_key(mid, &disk_key, 0);
1162 btrfs_set_node_key(parent, &disk_key, pslot);
1163 btrfs_mark_buffer_dirty(parent);
1164 if (btrfs_header_nritems(left) > orig_slot) {
1165 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001166 path->slots[level + 1] -= 1;
1167 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001168 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001169 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001170 } else {
1171 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001172 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001173 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001174 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001175 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001176 }
Chris Masone66f7092007-04-20 13:16:02 -04001177 return 0;
1178 }
Chris Mason925baed2008-06-25 16:01:30 -04001179 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001180 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001181 }
Chris Mason925baed2008-06-25 16:01:30 -04001182 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001183
1184 /*
1185 * then try to empty the right most buffer into the middle
1186 */
Chris Mason5f39d392007-10-15 16:14:19 -04001187 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001188 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001189 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001190 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001191 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1192 wret = 1;
1193 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001194 ret = btrfs_cow_block(trans, root, right,
1195 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001196 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001197 if (ret)
1198 wret = 1;
1199 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001200 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001201 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001202 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001203 }
Chris Masone66f7092007-04-20 13:16:02 -04001204 if (wret < 0)
1205 ret = wret;
1206 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001207 struct btrfs_disk_key disk_key;
1208
1209 btrfs_node_key(right, &disk_key, 0);
1210 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1211 btrfs_mark_buffer_dirty(parent);
1212
1213 if (btrfs_header_nritems(mid) <= orig_slot) {
1214 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001215 path->slots[level + 1] += 1;
1216 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001217 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001218 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001219 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001220 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001221 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001222 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001223 }
Chris Masone66f7092007-04-20 13:16:02 -04001224 return 0;
1225 }
Chris Mason925baed2008-06-25 16:01:30 -04001226 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001227 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001228 }
Chris Masone66f7092007-04-20 13:16:02 -04001229 return 1;
1230}
1231
Chris Mason74123bd2007-02-02 11:05:29 -05001232/*
Chris Masond352ac62008-09-29 15:18:18 -04001233 * readahead one full node of leaves, finding things that are close
1234 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001235 */
Chris Masone02119d2008-09-05 16:13:11 -04001236static noinline void reada_for_search(struct btrfs_root *root,
1237 struct btrfs_path *path,
1238 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001239{
Chris Mason5f39d392007-10-15 16:14:19 -04001240 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001241 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001242 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001243 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001244 u64 lowest_read;
1245 u64 highest_read;
1246 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001247 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001248 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001249 u32 nr;
1250 u32 blocksize;
1251 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001252
Chris Masona6b6e752007-10-15 16:22:39 -04001253 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001254 return;
1255
Chris Mason6702ed42007-08-07 16:15:09 -04001256 if (!path->nodes[level])
1257 return;
1258
Chris Mason5f39d392007-10-15 16:14:19 -04001259 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001260
Chris Mason3c69fae2007-08-07 15:52:22 -04001261 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001262 blocksize = btrfs_level_size(root, level - 1);
1263 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001264 if (eb) {
1265 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001266 return;
1267 }
1268
Chris Mason6b800532007-10-15 16:17:34 -04001269 highest_read = search;
1270 lowest_read = search;
1271
Chris Mason5f39d392007-10-15 16:14:19 -04001272 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001273 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001274 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001275 if (direction < 0) {
1276 if (nr == 0)
1277 break;
1278 nr--;
1279 } else if (direction > 0) {
1280 nr++;
1281 if (nr >= nritems)
1282 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001283 }
Chris Mason01f46652007-12-21 16:24:26 -05001284 if (path->reada < 0 && objectid) {
1285 btrfs_node_key(node, &disk_key, nr);
1286 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1287 break;
1288 }
Chris Mason6b800532007-10-15 16:17:34 -04001289 search = btrfs_node_blockptr(node, nr);
1290 if ((search >= lowest_read && search <= highest_read) ||
1291 (search < lowest_read && lowest_read - search <= 32768) ||
1292 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001293 readahead_tree_block(root, search, blocksize,
1294 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001295 nread += blocksize;
1296 }
1297 nscan++;
1298 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1299 break;
1300 if(nread > (1024 * 1024) || nscan > 128)
1301 break;
1302
1303 if (search < lowest_read)
1304 lowest_read = search;
1305 if (search > highest_read)
1306 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001307 }
1308}
Chris Mason925baed2008-06-25 16:01:30 -04001309
Chris Masond352ac62008-09-29 15:18:18 -04001310/*
1311 * when we walk down the tree, it is usually safe to unlock the higher layers in
1312 * the tree. The exceptions are when our path goes through slot 0, because operations
1313 * on the tree might require changing key pointers higher up in the tree.
1314 *
1315 * callers might also have set path->keep_locks, which tells this code to
1316 * keep the lock if the path points to the last slot in the block. This is
1317 * part of walking through the tree, and selecting the next slot in the higher
1318 * block.
1319 *
1320 * lowest_unlock sets the lowest level in the tree we're allowed to unlock.
1321 * so if lowest_unlock is 1, level 0 won't be unlocked
1322 */
Chris Masone02119d2008-09-05 16:13:11 -04001323static noinline void unlock_up(struct btrfs_path *path, int level,
1324 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001325{
1326 int i;
1327 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001328 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001329 struct extent_buffer *t;
1330
1331 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1332 if (!path->nodes[i])
1333 break;
1334 if (!path->locks[i])
1335 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001336 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001337 skip_level = i + 1;
1338 continue;
1339 }
Chris Mason051e1b92008-06-25 16:01:30 -04001340 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001341 u32 nritems;
1342 t = path->nodes[i];
1343 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001344 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001345 skip_level = i + 1;
1346 continue;
1347 }
1348 }
Chris Mason051e1b92008-06-25 16:01:30 -04001349 if (skip_level < i && i >= lowest_unlock)
1350 no_skips = 1;
1351
Chris Mason925baed2008-06-25 16:01:30 -04001352 t = path->nodes[i];
1353 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1354 btrfs_tree_unlock(t);
1355 path->locks[i] = 0;
1356 }
1357 }
1358}
1359
Chris Mason3c69fae2007-08-07 15:52:22 -04001360/*
Chris Mason74123bd2007-02-02 11:05:29 -05001361 * look for key in the tree. path is filled in with nodes along the way
1362 * if key is found, we return zero and you can find the item in the leaf
1363 * level of the path (level 0)
1364 *
1365 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001366 * be inserted, and 1 is returned. If there are other errors during the
1367 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001368 *
1369 * if ins_len > 0, nodes and leaves will be split as we walk down the
1370 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1371 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001372 */
Chris Masone089f052007-03-16 16:20:31 -04001373int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1374 *root, struct btrfs_key *key, struct btrfs_path *p, int
1375 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001376{
Chris Mason5f39d392007-10-15 16:14:19 -04001377 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001378 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001379 int slot;
1380 int ret;
1381 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001382 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001383 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001384 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001385 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001386 u64 blocknr;
1387 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001388 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001389
Chris Mason6702ed42007-08-07 16:15:09 -04001390 lowest_level = p->lowest_level;
1391 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001392 WARN_ON(p->nodes[0] != NULL);
Chris Mason333db942008-06-25 16:01:30 -04001393 WARN_ON(cow && root == root->fs_info->extent_root &&
Chris Mason925baed2008-06-25 16:01:30 -04001394 !mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason925baed2008-06-25 16:01:30 -04001395 if (ins_len < 0)
1396 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001397
1398 prealloc_block.objectid = 0;
1399
Chris Masonbb803952007-03-01 12:04:21 -05001400again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001401 if (p->skip_locking)
1402 b = btrfs_root_node(root);
1403 else
1404 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001405
Chris Masoneb60cea2007-02-02 09:18:22 -05001406 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001407 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001408
1409 /*
1410 * setup the path here so we can release it under lock
1411 * contention with the cow code
1412 */
1413 p->nodes[level] = b;
1414 if (!p->skip_locking)
1415 p->locks[level] = 1;
1416
Chris Mason02217ed2007-03-02 16:08:05 -05001417 if (cow) {
1418 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001419
1420 /* is a cow on this block not required */
1421 spin_lock(&root->fs_info->hash_lock);
1422 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001423 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001424 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1425 spin_unlock(&root->fs_info->hash_lock);
1426 goto cow_done;
1427 }
1428 spin_unlock(&root->fs_info->hash_lock);
1429
1430 /* ok, we have to cow, is our old prealloc the right
1431 * size?
1432 */
1433 if (prealloc_block.objectid &&
1434 prealloc_block.offset != b->len) {
1435 btrfs_free_reserved_extent(root,
1436 prealloc_block.objectid,
1437 prealloc_block.offset);
1438 prealloc_block.objectid = 0;
1439 }
1440
1441 /*
1442 * for higher level blocks, try not to allocate blocks
1443 * with the block and the parent locks held.
1444 */
1445 if (level > 1 && !prealloc_block.objectid &&
1446 btrfs_path_lock_waiting(p, level)) {
1447 u32 size = b->len;
1448 u64 hint = b->start;
1449
1450 btrfs_release_path(root, p);
1451 ret = btrfs_reserve_extent(trans, root,
1452 size, size, 0,
1453 hint, (u64)-1,
1454 &prealloc_block, 0);
1455 BUG_ON(ret);
1456 goto again;
1457 }
1458
Chris Masone20d96d2007-03-22 12:13:20 -04001459 wret = btrfs_cow_block(trans, root, b,
1460 p->nodes[level + 1],
1461 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001462 &b, prealloc_block.objectid);
1463 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001464 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001465 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001466 ret = wret;
1467 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001468 }
Chris Mason02217ed2007-03-02 16:08:05 -05001469 }
Chris Mason65b51a02008-08-01 15:11:20 -04001470cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001471 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001472 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001473 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001474 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001475
Chris Masoneb60cea2007-02-02 09:18:22 -05001476 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001477 if (!p->skip_locking)
1478 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001479
Chris Mason123abc82007-03-14 14:14:43 -04001480 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001481 if (ret) {
1482 ret = -1;
1483 goto done;
1484 }
Chris Mason925baed2008-06-25 16:01:30 -04001485
Chris Mason5f39d392007-10-15 16:14:19 -04001486 ret = bin_search(b, key, level, &slot);
1487 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001488 if (ret && slot > 0)
1489 slot -= 1;
1490 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001491 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001492 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001493 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001494 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001495 if (sret) {
1496 ret = sret;
1497 goto done;
1498 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001499 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001500 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001501 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001502 int sret = balance_level(trans, root, p,
1503 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001504 if (sret) {
1505 ret = sret;
1506 goto done;
1507 }
Chris Masonbb803952007-03-01 12:04:21 -05001508 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001509 if (!b) {
1510 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001511 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001512 }
Chris Masonbb803952007-03-01 12:04:21 -05001513 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001514 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001515 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001516 unlock_up(p, level, lowest_unlock);
1517
Chris Mason9f3a7422007-08-07 15:52:19 -04001518 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001519 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001520 ret = 0;
1521 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001522 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001523
Chris Mason594a24e2008-06-25 16:01:30 -04001524 blocknr = btrfs_node_blockptr(b, slot);
1525 gen = btrfs_node_ptr_generation(b, slot);
1526 blocksize = btrfs_level_size(root, level - 1);
1527
1528 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1529 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001530 b = tmp;
1531 } else {
1532 /*
1533 * reduce lock contention at high levels
1534 * of the btree by dropping locks before
1535 * we read.
1536 */
1537 if (level > 1) {
1538 btrfs_release_path(NULL, p);
1539 if (tmp)
1540 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001541 if (should_reada)
1542 reada_for_search(root, p,
1543 level, slot,
1544 key->objectid);
1545
Chris Mason594a24e2008-06-25 16:01:30 -04001546 tmp = read_tree_block(root, blocknr,
1547 blocksize, gen);
1548 if (tmp)
1549 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001550 goto again;
1551 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001552 if (tmp)
1553 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001554 if (should_reada)
1555 reada_for_search(root, p,
1556 level, slot,
1557 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001558 b = read_node_slot(root, b, slot);
1559 }
1560 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001561 if (!p->skip_locking)
1562 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001563 } else {
1564 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001565 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001566 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001567 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001568 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001569 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001570 if (sret) {
1571 ret = sret;
1572 goto done;
1573 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001574 }
Chris Mason925baed2008-06-25 16:01:30 -04001575 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001576 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001577 }
1578 }
Chris Mason65b51a02008-08-01 15:11:20 -04001579 ret = 1;
1580done:
1581 if (prealloc_block.objectid) {
1582 btrfs_free_reserved_extent(root,
1583 prealloc_block.objectid,
1584 prealloc_block.offset);
1585 }
1586
1587 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001588}
1589
Zheng Yan1a40e232008-09-26 10:09:34 -04001590int btrfs_merge_path(struct btrfs_trans_handle *trans,
1591 struct btrfs_root *root,
1592 struct btrfs_key *node_keys,
1593 u64 *nodes, int lowest_level)
1594{
1595 struct extent_buffer *eb;
1596 struct extent_buffer *parent;
1597 struct btrfs_key key;
1598 u64 bytenr;
1599 u64 generation;
1600 u32 blocksize;
1601 int level;
1602 int slot;
1603 int key_match;
1604 int ret;
1605
1606 eb = btrfs_lock_root_node(root);
1607 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1608 BUG_ON(ret);
1609
1610 parent = eb;
1611 while (1) {
1612 level = btrfs_header_level(parent);
1613 if (level == 0 || level <= lowest_level)
1614 break;
1615
1616 ret = bin_search(parent, &node_keys[lowest_level], level,
1617 &slot);
1618 if (ret && slot > 0)
1619 slot--;
1620
1621 bytenr = btrfs_node_blockptr(parent, slot);
1622 if (nodes[level - 1] == bytenr)
1623 break;
1624
1625 blocksize = btrfs_level_size(root, level - 1);
1626 generation = btrfs_node_ptr_generation(parent, slot);
1627 btrfs_node_key_to_cpu(eb, &key, slot);
1628 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1629
1630 /*
1631 * if node keys match and node pointer hasn't been modified
1632 * in the running transaction, we can merge the path. for
1633 * reloc trees, the node pointer check is skipped, this is
1634 * because the reloc trees are fully controlled by the space
1635 * balance code, no one else can modify them.
1636 */
1637 if (!nodes[level - 1] || !key_match ||
1638 (generation == trans->transid &&
1639 root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)) {
1640next_level:
1641 if (level == 1 || level == lowest_level + 1)
1642 break;
1643
1644 eb = read_tree_block(root, bytenr, blocksize,
1645 generation);
1646 btrfs_tree_lock(eb);
1647
1648 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1649 &eb, 0);
1650 BUG_ON(ret);
1651
1652 btrfs_tree_unlock(parent);
1653 free_extent_buffer(parent);
1654 parent = eb;
1655 continue;
1656 }
1657
1658 if (generation == trans->transid) {
1659 u32 refs;
1660 BUG_ON(btrfs_header_owner(eb) !=
1661 BTRFS_TREE_RELOC_OBJECTID);
1662 /*
1663 * lock the block to keep __btrfs_cow_block from
1664 * changing the reference count.
1665 */
1666 eb = read_tree_block(root, bytenr, blocksize,
1667 generation);
1668 btrfs_tree_lock(eb);
1669
1670 ret = btrfs_lookup_extent_ref(trans, root, bytenr,
1671 blocksize, &refs);
1672 BUG_ON(ret);
1673 /*
1674 * if replace block whose reference count is one,
1675 * we have to "drop the subtree". so skip it for
1676 * simplicity
1677 */
1678 if (refs == 1) {
1679 btrfs_tree_unlock(eb);
1680 free_extent_buffer(eb);
1681 goto next_level;
1682 }
1683 }
1684
1685 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1686 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1687 btrfs_mark_buffer_dirty(parent);
1688
1689 ret = btrfs_inc_extent_ref(trans, root,
1690 nodes[level - 1],
1691 blocksize, parent->start,
1692 btrfs_header_owner(parent),
1693 btrfs_header_generation(parent),
1694 level - 1, 0);
1695 BUG_ON(ret);
1696 ret = btrfs_free_extent(trans, root, bytenr,
1697 blocksize, parent->start,
1698 btrfs_header_owner(parent),
1699 btrfs_header_generation(parent),
1700 level - 1, 0, 1);
1701 BUG_ON(ret);
1702
1703 if (generation == trans->transid) {
1704 btrfs_tree_unlock(eb);
1705 free_extent_buffer(eb);
1706 }
1707 break;
1708 }
1709 btrfs_tree_unlock(parent);
1710 free_extent_buffer(parent);
1711 return 0;
1712}
1713
Chris Mason74123bd2007-02-02 11:05:29 -05001714/*
1715 * adjust the pointers going up the tree, starting at level
1716 * making sure the right key of each node is points to 'key'.
1717 * This is used after shifting pointers to the left, so it stops
1718 * fixing up pointers when a given leaf/node is not in slot 0 of the
1719 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001720 *
1721 * If this fails to write a tree block, it returns -1, but continues
1722 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001723 */
Chris Mason5f39d392007-10-15 16:14:19 -04001724static int fixup_low_keys(struct btrfs_trans_handle *trans,
1725 struct btrfs_root *root, struct btrfs_path *path,
1726 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001727{
1728 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001729 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001730 struct extent_buffer *t;
1731
Chris Mason234b63a2007-03-13 10:46:10 -04001732 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001733 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001734 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001735 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001736 t = path->nodes[i];
1737 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001738 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001739 if (tslot != 0)
1740 break;
1741 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001742 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001743}
1744
Chris Mason74123bd2007-02-02 11:05:29 -05001745/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001746 * update item key.
1747 *
1748 * This function isn't completely safe. It's the caller's responsibility
1749 * that the new key won't break the order
1750 */
1751int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1752 struct btrfs_root *root, struct btrfs_path *path,
1753 struct btrfs_key *new_key)
1754{
1755 struct btrfs_disk_key disk_key;
1756 struct extent_buffer *eb;
1757 int slot;
1758
1759 eb = path->nodes[0];
1760 slot = path->slots[0];
1761 if (slot > 0) {
1762 btrfs_item_key(eb, &disk_key, slot - 1);
1763 if (comp_keys(&disk_key, new_key) >= 0)
1764 return -1;
1765 }
1766 if (slot < btrfs_header_nritems(eb) - 1) {
1767 btrfs_item_key(eb, &disk_key, slot + 1);
1768 if (comp_keys(&disk_key, new_key) <= 0)
1769 return -1;
1770 }
1771
1772 btrfs_cpu_key_to_disk(&disk_key, new_key);
1773 btrfs_set_item_key(eb, &disk_key, slot);
1774 btrfs_mark_buffer_dirty(eb);
1775 if (slot == 0)
1776 fixup_low_keys(trans, root, path, &disk_key, 1);
1777 return 0;
1778}
1779
1780/*
Chris Mason74123bd2007-02-02 11:05:29 -05001781 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001782 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001783 *
1784 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1785 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001786 */
Chris Mason98ed5172008-01-03 10:01:48 -05001787static int push_node_left(struct btrfs_trans_handle *trans,
1788 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001789 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001790{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001791 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001792 int src_nritems;
1793 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001794 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001795
Chris Mason5f39d392007-10-15 16:14:19 -04001796 src_nritems = btrfs_header_nritems(src);
1797 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001798 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001799 WARN_ON(btrfs_header_generation(src) != trans->transid);
1800 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001801
Chris Masonbce4eae2008-04-24 14:42:46 -04001802 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001803 return 1;
1804
Chris Masoneb60cea2007-02-02 09:18:22 -05001805 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001806 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001807 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001808
Chris Masonbce4eae2008-04-24 14:42:46 -04001809 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001810 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001811 if (push_items < src_nritems) {
1812 /* leave at least 8 pointers in the node if
1813 * we aren't going to empty it
1814 */
1815 if (src_nritems - push_items < 8) {
1816 if (push_items <= 8)
1817 return 1;
1818 push_items -= 8;
1819 }
1820 }
1821 } else
1822 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001823
Chris Mason5f39d392007-10-15 16:14:19 -04001824 copy_extent_buffer(dst, src,
1825 btrfs_node_key_ptr_offset(dst_nritems),
1826 btrfs_node_key_ptr_offset(0),
1827 push_items * sizeof(struct btrfs_key_ptr));
1828
Chris Masonbb803952007-03-01 12:04:21 -05001829 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001830 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1831 btrfs_node_key_ptr_offset(push_items),
1832 (src_nritems - push_items) *
1833 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001834 }
Chris Mason5f39d392007-10-15 16:14:19 -04001835 btrfs_set_header_nritems(src, src_nritems - push_items);
1836 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1837 btrfs_mark_buffer_dirty(src);
1838 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001839
1840 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1841 BUG_ON(ret);
1842
Chris Masonbb803952007-03-01 12:04:21 -05001843 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001844}
1845
Chris Mason97571fd2007-02-24 13:39:08 -05001846/*
Chris Mason79f95c82007-03-01 15:16:26 -05001847 * try to push data from one node into the next node right in the
1848 * tree.
1849 *
1850 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1851 * error, and > 0 if there was no room in the right hand block.
1852 *
1853 * this will only push up to 1/2 the contents of the left node over
1854 */
Chris Mason5f39d392007-10-15 16:14:19 -04001855static int balance_node_right(struct btrfs_trans_handle *trans,
1856 struct btrfs_root *root,
1857 struct extent_buffer *dst,
1858 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001859{
Chris Mason79f95c82007-03-01 15:16:26 -05001860 int push_items = 0;
1861 int max_push;
1862 int src_nritems;
1863 int dst_nritems;
1864 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001865
Chris Mason7bb86312007-12-11 09:25:06 -05001866 WARN_ON(btrfs_header_generation(src) != trans->transid);
1867 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1868
Chris Mason5f39d392007-10-15 16:14:19 -04001869 src_nritems = btrfs_header_nritems(src);
1870 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001871 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001872 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001873 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001874 }
1875
1876 if (src_nritems < 4) {
1877 return 1;
1878 }
Chris Mason79f95c82007-03-01 15:16:26 -05001879
1880 max_push = src_nritems / 2 + 1;
1881 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001882 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001883 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001884 }
Yan252c38f2007-08-29 09:11:44 -04001885
Chris Mason79f95c82007-03-01 15:16:26 -05001886 if (max_push < push_items)
1887 push_items = max_push;
1888
Chris Mason5f39d392007-10-15 16:14:19 -04001889 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1890 btrfs_node_key_ptr_offset(0),
1891 (dst_nritems) *
1892 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001893
Chris Mason5f39d392007-10-15 16:14:19 -04001894 copy_extent_buffer(dst, src,
1895 btrfs_node_key_ptr_offset(0),
1896 btrfs_node_key_ptr_offset(src_nritems - push_items),
1897 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001898
Chris Mason5f39d392007-10-15 16:14:19 -04001899 btrfs_set_header_nritems(src, src_nritems - push_items);
1900 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001901
Chris Mason5f39d392007-10-15 16:14:19 -04001902 btrfs_mark_buffer_dirty(src);
1903 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001904
1905 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1906 BUG_ON(ret);
1907
Chris Mason79f95c82007-03-01 15:16:26 -05001908 return ret;
1909}
1910
1911/*
Chris Mason97571fd2007-02-24 13:39:08 -05001912 * helper function to insert a new root level in the tree.
1913 * A new node is allocated, and a single item is inserted to
1914 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001915 *
1916 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001917 */
Chris Mason98ed5172008-01-03 10:01:48 -05001918static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001919 struct btrfs_root *root,
1920 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001921{
Chris Mason7bb86312007-12-11 09:25:06 -05001922 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001923 struct extent_buffer *lower;
1924 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001925 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001926 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001927 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001928
1929 BUG_ON(path->nodes[level]);
1930 BUG_ON(path->nodes[level-1] != root->node);
1931
Chris Mason7bb86312007-12-11 09:25:06 -05001932 lower = path->nodes[level-1];
1933 if (level == 1)
1934 btrfs_item_key(lower, &lower_key, 0);
1935 else
1936 btrfs_node_key(lower, &lower_key, 0);
1937
Zheng Yan31840ae2008-09-23 13:14:14 -04001938 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1939 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001940 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001941 if (IS_ERR(c))
1942 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001943
Chris Mason5f39d392007-10-15 16:14:19 -04001944 memset_extent_buffer(c, 0, 0, root->nodesize);
1945 btrfs_set_header_nritems(c, 1);
1946 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001947 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001948 btrfs_set_header_generation(c, trans->transid);
1949 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001950
Chris Mason5f39d392007-10-15 16:14:19 -04001951 write_extent_buffer(c, root->fs_info->fsid,
1952 (unsigned long)btrfs_header_fsid(c),
1953 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001954
1955 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1956 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1957 BTRFS_UUID_SIZE);
1958
Chris Mason5f39d392007-10-15 16:14:19 -04001959 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001960 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001961 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001962 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001963
1964 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001965
1966 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001967
Chris Mason925baed2008-06-25 16:01:30 -04001968 spin_lock(&root->node_lock);
1969 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001970 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001971 spin_unlock(&root->node_lock);
1972
Zheng Yan31840ae2008-09-23 13:14:14 -04001973 ret = btrfs_update_extent_ref(trans, root, lower->start,
1974 lower->start, c->start,
1975 root->root_key.objectid,
1976 trans->transid, level - 1, 0);
1977 BUG_ON(ret);
1978
Chris Mason925baed2008-06-25 16:01:30 -04001979 /* the super has an extra ref to root->node */
1980 free_extent_buffer(old);
1981
Chris Mason0b86a832008-03-24 15:01:56 -04001982 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001983 extent_buffer_get(c);
1984 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001985 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001986 path->slots[level] = 0;
1987 return 0;
1988}
1989
Chris Mason74123bd2007-02-02 11:05:29 -05001990/*
1991 * worker function to insert a single pointer in a node.
1992 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001993 *
Chris Mason74123bd2007-02-02 11:05:29 -05001994 * slot and level indicate where you want the key to go, and
1995 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001996 *
1997 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001998 */
Chris Masone089f052007-03-16 16:20:31 -04001999static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2000 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002001 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002002{
Chris Mason5f39d392007-10-15 16:14:19 -04002003 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002004 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002005
2006 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002007 lower = path->nodes[level];
2008 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002009 if (slot > nritems)
2010 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002011 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002012 BUG();
2013 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002014 memmove_extent_buffer(lower,
2015 btrfs_node_key_ptr_offset(slot + 1),
2016 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002017 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002018 }
Chris Mason5f39d392007-10-15 16:14:19 -04002019 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002020 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002021 WARN_ON(trans->transid == 0);
2022 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002023 btrfs_set_header_nritems(lower, nritems + 1);
2024 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002025 return 0;
2026}
2027
Chris Mason97571fd2007-02-24 13:39:08 -05002028/*
2029 * split the node at the specified level in path in two.
2030 * The path is corrected to point to the appropriate node after the split
2031 *
2032 * Before splitting this tries to make some room in the node by pushing
2033 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002034 *
2035 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002036 */
Chris Masone02119d2008-09-05 16:13:11 -04002037static noinline int split_node(struct btrfs_trans_handle *trans,
2038 struct btrfs_root *root,
2039 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002040{
Chris Mason5f39d392007-10-15 16:14:19 -04002041 struct extent_buffer *c;
2042 struct extent_buffer *split;
2043 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002044 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002045 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002046 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002047 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002048
Chris Mason5f39d392007-10-15 16:14:19 -04002049 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002050 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002052 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002053 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002054 if (ret)
2055 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002056 } else {
2057 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002058 c = path->nodes[level];
2059 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002060 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002061 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002062 if (ret < 0)
2063 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002064 }
Chris Masone66f7092007-04-20 13:16:02 -04002065
Chris Mason5f39d392007-10-15 16:14:19 -04002066 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002067
Chris Mason925baed2008-06-25 16:01:30 -04002068 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002069 path->nodes[level + 1]->start,
2070 root->root_key.objectid,
2071 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002072 if (IS_ERR(split))
2073 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002074
Chris Mason5f39d392007-10-15 16:14:19 -04002075 btrfs_set_header_flags(split, btrfs_header_flags(c));
2076 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002077 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002078 btrfs_set_header_generation(split, trans->transid);
2079 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002080 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002081 write_extent_buffer(split, root->fs_info->fsid,
2082 (unsigned long)btrfs_header_fsid(split),
2083 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002084 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2085 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2086 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002087
Chris Mason7518a232007-03-12 12:01:18 -04002088 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002089
2090 copy_extent_buffer(split, c,
2091 btrfs_node_key_ptr_offset(0),
2092 btrfs_node_key_ptr_offset(mid),
2093 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2094 btrfs_set_header_nritems(split, c_nritems - mid);
2095 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002096 ret = 0;
2097
Chris Mason5f39d392007-10-15 16:14:19 -04002098 btrfs_mark_buffer_dirty(c);
2099 btrfs_mark_buffer_dirty(split);
2100
2101 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002102 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002103 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002104 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002105 if (wret)
2106 ret = wret;
2107
Zheng Yan31840ae2008-09-23 13:14:14 -04002108 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2109 BUG_ON(ret);
2110
Chris Mason5de08d72007-02-24 06:24:44 -05002111 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002112 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002113 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002114 free_extent_buffer(c);
2115 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002116 path->slots[level + 1] += 1;
2117 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002118 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002119 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002120 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002121 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002122}
2123
Chris Mason74123bd2007-02-02 11:05:29 -05002124/*
2125 * how many bytes are required to store the items in a leaf. start
2126 * and nr indicate which items in the leaf to check. This totals up the
2127 * space used both by the item structs and the item data
2128 */
Chris Mason5f39d392007-10-15 16:14:19 -04002129static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002130{
2131 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002132 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002133 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002134
2135 if (!nr)
2136 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002137 data_len = btrfs_item_end_nr(l, start);
2138 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002139 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002140 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002141 return data_len;
2142}
2143
Chris Mason74123bd2007-02-02 11:05:29 -05002144/*
Chris Masond4dbff92007-04-04 14:08:15 -04002145 * The space between the end of the leaf items and
2146 * the start of the leaf data. IOW, how much room
2147 * the leaf has left for both items and data
2148 */
Chris Masone02119d2008-09-05 16:13:11 -04002149int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2150 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002151{
Chris Mason5f39d392007-10-15 16:14:19 -04002152 int nritems = btrfs_header_nritems(leaf);
2153 int ret;
2154 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2155 if (ret < 0) {
2156 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002157 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002158 leaf_space_used(leaf, 0, nritems), nritems);
2159 }
2160 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002161}
2162
2163/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002164 * push some data in the path leaf to the right, trying to free up at
2165 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002166 *
2167 * returns 1 if the push failed because the other node didn't have enough
2168 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002169 */
Chris Masone089f052007-03-16 16:20:31 -04002170static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002171 *root, struct btrfs_path *path, int data_size,
2172 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002173{
Chris Mason5f39d392007-10-15 16:14:19 -04002174 struct extent_buffer *left = path->nodes[0];
2175 struct extent_buffer *right;
2176 struct extent_buffer *upper;
2177 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002178 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002179 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002180 int free_space;
2181 int push_space = 0;
2182 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002183 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002184 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002185 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002186 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002187 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002188 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002189 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002190
2191 slot = path->slots[1];
2192 if (!path->nodes[1]) {
2193 return 1;
2194 }
2195 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002196 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002197 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002198
Chris Masona2135012008-06-25 16:01:30 -04002199 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2200
Chris Masonca7a79a2008-05-12 12:59:19 -04002201 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002202 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002203 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002204 if (free_space < data_size + sizeof(struct btrfs_item))
2205 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002206
Chris Mason5f39d392007-10-15 16:14:19 -04002207 /* cow and double check */
2208 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002209 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002210 if (ret)
2211 goto out_unlock;
2212
Chris Mason5f39d392007-10-15 16:14:19 -04002213 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002214 if (free_space < data_size + sizeof(struct btrfs_item))
2215 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002216
2217 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002218 if (left_nritems == 0)
2219 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002220
Chris Mason34a38212007-11-07 13:31:03 -05002221 if (empty)
2222 nr = 0;
2223 else
2224 nr = 1;
2225
Zheng Yan31840ae2008-09-23 13:14:14 -04002226 if (path->slots[0] >= left_nritems)
2227 push_space += data_size + sizeof(*item);
2228
Chris Mason34a38212007-11-07 13:31:03 -05002229 i = left_nritems - 1;
2230 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002231 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002232
Zheng Yan31840ae2008-09-23 13:14:14 -04002233 if (!empty && push_items > 0) {
2234 if (path->slots[0] > i)
2235 break;
2236 if (path->slots[0] == i) {
2237 int space = btrfs_leaf_free_space(root, left);
2238 if (space + push_space * 2 > free_space)
2239 break;
2240 }
2241 }
2242
Chris Mason00ec4c52007-02-24 12:47:20 -05002243 if (path->slots[0] == i)
2244 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002245
2246 if (!left->map_token) {
2247 map_extent_buffer(left, (unsigned long)item,
2248 sizeof(struct btrfs_item),
2249 &left->map_token, &left->kaddr,
2250 &left->map_start, &left->map_len,
2251 KM_USER1);
2252 }
2253
2254 this_item_size = btrfs_item_size(left, item);
2255 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002256 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002257
Chris Mason00ec4c52007-02-24 12:47:20 -05002258 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002259 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002260 if (i == 0)
2261 break;
2262 i--;
Chris Masondb945352007-10-15 16:15:53 -04002263 }
2264 if (left->map_token) {
2265 unmap_extent_buffer(left, left->map_token, KM_USER1);
2266 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002267 }
Chris Mason5f39d392007-10-15 16:14:19 -04002268
Chris Mason925baed2008-06-25 16:01:30 -04002269 if (push_items == 0)
2270 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002271
Chris Mason34a38212007-11-07 13:31:03 -05002272 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002273 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002274
Chris Mason00ec4c52007-02-24 12:47:20 -05002275 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002276 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002277
Chris Mason5f39d392007-10-15 16:14:19 -04002278 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002279 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002280
Chris Mason00ec4c52007-02-24 12:47:20 -05002281 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002282 data_end = leaf_data_end(root, right);
2283 memmove_extent_buffer(right,
2284 btrfs_leaf_data(right) + data_end - push_space,
2285 btrfs_leaf_data(right) + data_end,
2286 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2287
Chris Mason00ec4c52007-02-24 12:47:20 -05002288 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002289 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002290 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2291 btrfs_leaf_data(left) + leaf_data_end(root, left),
2292 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002293
2294 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2295 btrfs_item_nr_offset(0),
2296 right_nritems * sizeof(struct btrfs_item));
2297
Chris Mason00ec4c52007-02-24 12:47:20 -05002298 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002299 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2300 btrfs_item_nr_offset(left_nritems - push_items),
2301 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002302
2303 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002304 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002305 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002306 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002307 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002308 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002309 if (!right->map_token) {
2310 map_extent_buffer(right, (unsigned long)item,
2311 sizeof(struct btrfs_item),
2312 &right->map_token, &right->kaddr,
2313 &right->map_start, &right->map_len,
2314 KM_USER1);
2315 }
2316 push_space -= btrfs_item_size(right, item);
2317 btrfs_set_item_offset(right, item, push_space);
2318 }
2319
2320 if (right->map_token) {
2321 unmap_extent_buffer(right, right->map_token, KM_USER1);
2322 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002323 }
Chris Mason7518a232007-03-12 12:01:18 -04002324 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002325 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002326
Chris Mason34a38212007-11-07 13:31:03 -05002327 if (left_nritems)
2328 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002329 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002330
Zheng Yan31840ae2008-09-23 13:14:14 -04002331 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2332 BUG_ON(ret);
2333
Chris Mason5f39d392007-10-15 16:14:19 -04002334 btrfs_item_key(right, &disk_key, 0);
2335 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002336 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002337
Chris Mason00ec4c52007-02-24 12:47:20 -05002338 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002339 if (path->slots[0] >= left_nritems) {
2340 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002341 if (btrfs_header_nritems(path->nodes[0]) == 0)
2342 clean_tree_block(trans, root, path->nodes[0]);
2343 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002344 free_extent_buffer(path->nodes[0]);
2345 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002346 path->slots[1] += 1;
2347 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002348 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002349 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002350 }
2351 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002352
2353out_unlock:
2354 btrfs_tree_unlock(right);
2355 free_extent_buffer(right);
2356 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002357}
Chris Mason925baed2008-06-25 16:01:30 -04002358
Chris Mason00ec4c52007-02-24 12:47:20 -05002359/*
Chris Mason74123bd2007-02-02 11:05:29 -05002360 * push some data in the path leaf to the left, trying to free up at
2361 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2362 */
Chris Masone089f052007-03-16 16:20:31 -04002363static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002364 *root, struct btrfs_path *path, int data_size,
2365 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002366{
Chris Mason5f39d392007-10-15 16:14:19 -04002367 struct btrfs_disk_key disk_key;
2368 struct extent_buffer *right = path->nodes[0];
2369 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002370 int slot;
2371 int i;
2372 int free_space;
2373 int push_space = 0;
2374 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002375 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002376 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002377 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002378 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002379 int ret = 0;
2380 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002381 u32 this_item_size;
2382 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002383
2384 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002385 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002386 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002387 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002388 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002389
Chris Mason3685f792007-10-19 09:23:27 -04002390 right_nritems = btrfs_header_nritems(right);
2391 if (right_nritems == 0) {
2392 return 1;
2393 }
2394
Chris Masona2135012008-06-25 16:01:30 -04002395 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2396
Chris Masonca7a79a2008-05-12 12:59:19 -04002397 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002398 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002399 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002400 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002401 ret = 1;
2402 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002403 }
Chris Mason02217ed2007-03-02 16:08:05 -05002404
2405 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002406 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002407 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002408 if (ret) {
2409 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002410 ret = 1;
2411 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002412 }
Chris Mason3685f792007-10-19 09:23:27 -04002413
Chris Mason123abc82007-03-14 14:14:43 -04002414 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002415 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002416 ret = 1;
2417 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002418 }
2419
Chris Mason34a38212007-11-07 13:31:03 -05002420 if (empty)
2421 nr = right_nritems;
2422 else
2423 nr = right_nritems - 1;
2424
2425 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002426 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002427 if (!right->map_token) {
2428 map_extent_buffer(right, (unsigned long)item,
2429 sizeof(struct btrfs_item),
2430 &right->map_token, &right->kaddr,
2431 &right->map_start, &right->map_len,
2432 KM_USER1);
2433 }
2434
Zheng Yan31840ae2008-09-23 13:14:14 -04002435 if (!empty && push_items > 0) {
2436 if (path->slots[0] < i)
2437 break;
2438 if (path->slots[0] == i) {
2439 int space = btrfs_leaf_free_space(root, right);
2440 if (space + push_space * 2 > free_space)
2441 break;
2442 }
2443 }
2444
Chris Masonbe0e5c02007-01-26 15:51:26 -05002445 if (path->slots[0] == i)
2446 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002447
2448 this_item_size = btrfs_item_size(right, item);
2449 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002450 break;
Chris Masondb945352007-10-15 16:15:53 -04002451
Chris Masonbe0e5c02007-01-26 15:51:26 -05002452 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002453 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002454 }
Chris Masondb945352007-10-15 16:15:53 -04002455
2456 if (right->map_token) {
2457 unmap_extent_buffer(right, right->map_token, KM_USER1);
2458 right->map_token = NULL;
2459 }
2460
Chris Masonbe0e5c02007-01-26 15:51:26 -05002461 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002462 ret = 1;
2463 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002464 }
Chris Mason34a38212007-11-07 13:31:03 -05002465 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002466 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002467
Chris Masonbe0e5c02007-01-26 15:51:26 -05002468 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002469 copy_extent_buffer(left, right,
2470 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2471 btrfs_item_nr_offset(0),
2472 push_items * sizeof(struct btrfs_item));
2473
Chris Mason123abc82007-03-14 14:14:43 -04002474 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002475 btrfs_item_offset_nr(right, push_items -1);
2476
2477 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002478 leaf_data_end(root, left) - push_space,
2479 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002480 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002481 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002482 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002483 BUG_ON(old_left_nritems < 0);
2484
Chris Masondb945352007-10-15 16:15:53 -04002485 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002486 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002487 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002488
Chris Mason5f39d392007-10-15 16:14:19 -04002489 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002490 if (!left->map_token) {
2491 map_extent_buffer(left, (unsigned long)item,
2492 sizeof(struct btrfs_item),
2493 &left->map_token, &left->kaddr,
2494 &left->map_start, &left->map_len,
2495 KM_USER1);
2496 }
2497
Chris Mason5f39d392007-10-15 16:14:19 -04002498 ioff = btrfs_item_offset(left, item);
2499 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002500 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002501 }
Chris Mason5f39d392007-10-15 16:14:19 -04002502 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002503 if (left->map_token) {
2504 unmap_extent_buffer(left, left->map_token, KM_USER1);
2505 left->map_token = NULL;
2506 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002507
2508 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002509 if (push_items > right_nritems) {
2510 printk("push items %d nr %u\n", push_items, right_nritems);
2511 WARN_ON(1);
2512 }
Chris Mason5f39d392007-10-15 16:14:19 -04002513
Chris Mason34a38212007-11-07 13:31:03 -05002514 if (push_items < right_nritems) {
2515 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2516 leaf_data_end(root, right);
2517 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2518 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2519 btrfs_leaf_data(right) +
2520 leaf_data_end(root, right), push_space);
2521
2522 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002523 btrfs_item_nr_offset(push_items),
2524 (btrfs_header_nritems(right) - push_items) *
2525 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002526 }
Yaneef1c492007-11-26 10:58:13 -05002527 right_nritems -= push_items;
2528 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002529 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002530 for (i = 0; i < right_nritems; i++) {
2531 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002532
2533 if (!right->map_token) {
2534 map_extent_buffer(right, (unsigned long)item,
2535 sizeof(struct btrfs_item),
2536 &right->map_token, &right->kaddr,
2537 &right->map_start, &right->map_len,
2538 KM_USER1);
2539 }
2540
2541 push_space = push_space - btrfs_item_size(right, item);
2542 btrfs_set_item_offset(right, item, push_space);
2543 }
2544 if (right->map_token) {
2545 unmap_extent_buffer(right, right->map_token, KM_USER1);
2546 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002547 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002548
Chris Mason5f39d392007-10-15 16:14:19 -04002549 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002550 if (right_nritems)
2551 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002552
Zheng Yan31840ae2008-09-23 13:14:14 -04002553 ret = btrfs_update_ref(trans, root, right, left,
2554 old_left_nritems, push_items);
2555 BUG_ON(ret);
2556
Chris Mason5f39d392007-10-15 16:14:19 -04002557 btrfs_item_key(right, &disk_key, 0);
2558 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002559 if (wret)
2560 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561
2562 /* then fixup the leaf pointer in the path */
2563 if (path->slots[0] < push_items) {
2564 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002565 if (btrfs_header_nritems(path->nodes[0]) == 0)
2566 clean_tree_block(trans, root, path->nodes[0]);
2567 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002568 free_extent_buffer(path->nodes[0]);
2569 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570 path->slots[1] -= 1;
2571 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002572 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002573 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002574 path->slots[0] -= push_items;
2575 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002576 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002577 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002578out:
2579 btrfs_tree_unlock(left);
2580 free_extent_buffer(left);
2581 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002582}
2583
Chris Mason74123bd2007-02-02 11:05:29 -05002584/*
2585 * split the path's leaf in two, making sure there is at least data_size
2586 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002587 *
2588 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002589 */
Chris Masone02119d2008-09-05 16:13:11 -04002590static noinline int split_leaf(struct btrfs_trans_handle *trans,
2591 struct btrfs_root *root,
2592 struct btrfs_key *ins_key,
2593 struct btrfs_path *path, int data_size,
2594 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002595{
Chris Mason5f39d392007-10-15 16:14:19 -04002596 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002597 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002598 int mid;
2599 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002600 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002601 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602 int data_copy_size;
2603 int rt_data_off;
2604 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002605 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002606 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002607 int double_split;
2608 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002609 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610
Chris Masoncc0c5532007-10-25 15:42:57 -04002611 if (extend)
2612 space_needed = data_size;
2613
Chris Mason40689472007-03-17 14:29:23 -04002614 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002615 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002616 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002617 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002618 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002619 }
2620 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002621 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002622 if (wret < 0)
2623 return wret;
2624 }
2625 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002626
Chris Mason3685f792007-10-19 09:23:27 -04002627 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002628 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002629 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002630 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002631
Chris Mason5c680ed2007-02-22 11:39:13 -05002632 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002633 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002634 if (ret)
2635 return ret;
2636 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002637again:
2638 double_split = 0;
2639 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002640 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002641 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002642 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002643
Chris Mason925baed2008-06-25 16:01:30 -04002644 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002645 path->nodes[1]->start,
2646 root->root_key.objectid,
2647 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002648 if (IS_ERR(right)) {
2649 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002650 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002651 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002652
Chris Mason5f39d392007-10-15 16:14:19 -04002653 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002654 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002655 btrfs_set_header_generation(right, trans->transid);
2656 btrfs_set_header_owner(right, root->root_key.objectid);
2657 btrfs_set_header_level(right, 0);
2658 write_extent_buffer(right, root->fs_info->fsid,
2659 (unsigned long)btrfs_header_fsid(right),
2660 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002661
2662 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2663 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2664 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002665 if (mid <= slot) {
2666 if (nritems == 1 ||
2667 leaf_space_used(l, mid, nritems - mid) + space_needed >
2668 BTRFS_LEAF_DATA_SIZE(root)) {
2669 if (slot >= nritems) {
2670 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002672 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002673 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002674 path->slots[1] + 1, 1);
2675 if (wret)
2676 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002677
2678 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002679 free_extent_buffer(path->nodes[0]);
2680 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002681 path->slots[0] = 0;
2682 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002683 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002684 return ret;
2685 }
2686 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002687 if (mid != nritems &&
2688 leaf_space_used(l, mid, nritems - mid) +
2689 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2690 double_split = 1;
2691 }
Chris Masond4dbff92007-04-04 14:08:15 -04002692 }
2693 } else {
2694 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2695 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002696 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002697 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002698 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002699 wret = insert_ptr(trans, root, path,
2700 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002701 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002702 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002703 if (wret)
2704 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002705 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002706 free_extent_buffer(path->nodes[0]);
2707 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002708 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002709 if (path->slots[1] == 0) {
2710 wret = fixup_low_keys(trans, root,
2711 path, &disk_key, 1);
2712 if (wret)
2713 ret = wret;
2714 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002715 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002716 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002717 } else if (extend && slot == 0) {
2718 mid = 1;
2719 } else {
2720 mid = slot;
2721 if (mid != nritems &&
2722 leaf_space_used(l, mid, nritems - mid) +
2723 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2724 double_split = 1;
2725 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002726 }
Chris Masond4dbff92007-04-04 14:08:15 -04002727 }
2728 }
Chris Mason5f39d392007-10-15 16:14:19 -04002729 nritems = nritems - mid;
2730 btrfs_set_header_nritems(right, nritems);
2731 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2732
2733 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2734 btrfs_item_nr_offset(mid),
2735 nritems * sizeof(struct btrfs_item));
2736
2737 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002738 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2739 data_copy_size, btrfs_leaf_data(l) +
2740 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002741
Chris Mason5f39d392007-10-15 16:14:19 -04002742 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2743 btrfs_item_end_nr(l, mid);
2744
2745 for (i = 0; i < nritems; i++) {
2746 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002747 u32 ioff;
2748
2749 if (!right->map_token) {
2750 map_extent_buffer(right, (unsigned long)item,
2751 sizeof(struct btrfs_item),
2752 &right->map_token, &right->kaddr,
2753 &right->map_start, &right->map_len,
2754 KM_USER1);
2755 }
2756
2757 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002758 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002759 }
Chris Mason74123bd2007-02-02 11:05:29 -05002760
Chris Masondb945352007-10-15 16:15:53 -04002761 if (right->map_token) {
2762 unmap_extent_buffer(right, right->map_token, KM_USER1);
2763 right->map_token = NULL;
2764 }
2765
Chris Mason5f39d392007-10-15 16:14:19 -04002766 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002767 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002768 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002769 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2770 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002771 if (wret)
2772 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002773
2774 btrfs_mark_buffer_dirty(right);
2775 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002776 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002777
Zheng Yan31840ae2008-09-23 13:14:14 -04002778 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2779 BUG_ON(ret);
2780
Chris Masonbe0e5c02007-01-26 15:51:26 -05002781 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002782 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002783 free_extent_buffer(path->nodes[0]);
2784 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002785 path->slots[0] -= mid;
2786 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002787 } else {
2788 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002789 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002790 }
Chris Mason5f39d392007-10-15 16:14:19 -04002791
Chris Masoneb60cea2007-02-02 09:18:22 -05002792 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002793
Chris Masoncc0c5532007-10-25 15:42:57 -04002794 if (double_split) {
2795 BUG_ON(num_doubles != 0);
2796 num_doubles++;
2797 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002798 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002799 return ret;
2800}
2801
Chris Masond352ac62008-09-29 15:18:18 -04002802/*
2803 * make the item pointed to by the path smaller. new_size indicates
2804 * how small to make it, and from_end tells us if we just chop bytes
2805 * off the end of the item or if we shift the item to chop bytes off
2806 * the front.
2807 */
Chris Masonb18c6682007-04-17 13:26:50 -04002808int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2809 struct btrfs_root *root,
2810 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002811 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002812{
2813 int ret = 0;
2814 int slot;
2815 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002816 struct extent_buffer *leaf;
2817 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002818 u32 nritems;
2819 unsigned int data_end;
2820 unsigned int old_data_start;
2821 unsigned int old_size;
2822 unsigned int size_diff;
2823 int i;
2824
2825 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002826 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002827 slot = path->slots[0];
2828
2829 old_size = btrfs_item_size_nr(leaf, slot);
2830 if (old_size == new_size)
2831 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002832
Chris Mason5f39d392007-10-15 16:14:19 -04002833 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002834 data_end = leaf_data_end(root, leaf);
2835
Chris Mason5f39d392007-10-15 16:14:19 -04002836 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002837
Chris Masonb18c6682007-04-17 13:26:50 -04002838 size_diff = old_size - new_size;
2839
2840 BUG_ON(slot < 0);
2841 BUG_ON(slot >= nritems);
2842
2843 /*
2844 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2845 */
2846 /* first correct the data pointers */
2847 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002848 u32 ioff;
2849 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002850
2851 if (!leaf->map_token) {
2852 map_extent_buffer(leaf, (unsigned long)item,
2853 sizeof(struct btrfs_item),
2854 &leaf->map_token, &leaf->kaddr,
2855 &leaf->map_start, &leaf->map_len,
2856 KM_USER1);
2857 }
2858
Chris Mason5f39d392007-10-15 16:14:19 -04002859 ioff = btrfs_item_offset(leaf, item);
2860 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002861 }
Chris Masondb945352007-10-15 16:15:53 -04002862
2863 if (leaf->map_token) {
2864 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2865 leaf->map_token = NULL;
2866 }
2867
Chris Masonb18c6682007-04-17 13:26:50 -04002868 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002869 if (from_end) {
2870 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2871 data_end + size_diff, btrfs_leaf_data(leaf) +
2872 data_end, old_data_start + new_size - data_end);
2873 } else {
2874 struct btrfs_disk_key disk_key;
2875 u64 offset;
2876
2877 btrfs_item_key(leaf, &disk_key, slot);
2878
2879 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2880 unsigned long ptr;
2881 struct btrfs_file_extent_item *fi;
2882
2883 fi = btrfs_item_ptr(leaf, slot,
2884 struct btrfs_file_extent_item);
2885 fi = (struct btrfs_file_extent_item *)(
2886 (unsigned long)fi - size_diff);
2887
2888 if (btrfs_file_extent_type(leaf, fi) ==
2889 BTRFS_FILE_EXTENT_INLINE) {
2890 ptr = btrfs_item_ptr_offset(leaf, slot);
2891 memmove_extent_buffer(leaf, ptr,
2892 (unsigned long)fi,
2893 offsetof(struct btrfs_file_extent_item,
2894 disk_bytenr));
2895 }
2896 }
2897
2898 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2899 data_end + size_diff, btrfs_leaf_data(leaf) +
2900 data_end, old_data_start - data_end);
2901
2902 offset = btrfs_disk_key_offset(&disk_key);
2903 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2904 btrfs_set_item_key(leaf, &disk_key, slot);
2905 if (slot == 0)
2906 fixup_low_keys(trans, root, path, &disk_key, 1);
2907 }
Chris Mason5f39d392007-10-15 16:14:19 -04002908
2909 item = btrfs_item_nr(leaf, slot);
2910 btrfs_set_item_size(leaf, item, new_size);
2911 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002912
2913 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002914 if (btrfs_leaf_free_space(root, leaf) < 0) {
2915 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002916 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002917 }
Chris Masonb18c6682007-04-17 13:26:50 -04002918 return ret;
2919}
2920
Chris Masond352ac62008-09-29 15:18:18 -04002921/*
2922 * make the item pointed to by the path bigger, data_size is the new size.
2923 */
Chris Mason5f39d392007-10-15 16:14:19 -04002924int btrfs_extend_item(struct btrfs_trans_handle *trans,
2925 struct btrfs_root *root, struct btrfs_path *path,
2926 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002927{
2928 int ret = 0;
2929 int slot;
2930 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002931 struct extent_buffer *leaf;
2932 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002933 u32 nritems;
2934 unsigned int data_end;
2935 unsigned int old_data;
2936 unsigned int old_size;
2937 int i;
2938
2939 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002940 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002941
Chris Mason5f39d392007-10-15 16:14:19 -04002942 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002943 data_end = leaf_data_end(root, leaf);
2944
Chris Mason5f39d392007-10-15 16:14:19 -04002945 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2946 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002947 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002948 }
Chris Mason6567e832007-04-16 09:22:45 -04002949 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002950 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002951
2952 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002953 if (slot >= nritems) {
2954 btrfs_print_leaf(root, leaf);
2955 printk("slot %d too large, nritems %d\n", slot, nritems);
2956 BUG_ON(1);
2957 }
Chris Mason6567e832007-04-16 09:22:45 -04002958
2959 /*
2960 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2961 */
2962 /* first correct the data pointers */
2963 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002964 u32 ioff;
2965 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002966
2967 if (!leaf->map_token) {
2968 map_extent_buffer(leaf, (unsigned long)item,
2969 sizeof(struct btrfs_item),
2970 &leaf->map_token, &leaf->kaddr,
2971 &leaf->map_start, &leaf->map_len,
2972 KM_USER1);
2973 }
Chris Mason5f39d392007-10-15 16:14:19 -04002974 ioff = btrfs_item_offset(leaf, item);
2975 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002976 }
Chris Mason5f39d392007-10-15 16:14:19 -04002977
Chris Masondb945352007-10-15 16:15:53 -04002978 if (leaf->map_token) {
2979 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2980 leaf->map_token = NULL;
2981 }
2982
Chris Mason6567e832007-04-16 09:22:45 -04002983 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002984 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002985 data_end - data_size, btrfs_leaf_data(leaf) +
2986 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002987
Chris Mason6567e832007-04-16 09:22:45 -04002988 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002989 old_size = btrfs_item_size_nr(leaf, slot);
2990 item = btrfs_item_nr(leaf, slot);
2991 btrfs_set_item_size(leaf, item, old_size + data_size);
2992 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002993
2994 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002995 if (btrfs_leaf_free_space(root, leaf) < 0) {
2996 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002997 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002998 }
Chris Mason6567e832007-04-16 09:22:45 -04002999 return ret;
3000}
3001
Chris Mason74123bd2007-02-02 11:05:29 -05003002/*
Chris Masond352ac62008-09-29 15:18:18 -04003003 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003004 * This does all the path init required, making room in the tree if needed.
3005 */
Chris Mason9c583092008-01-29 15:15:18 -05003006int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003007 struct btrfs_root *root,
3008 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003009 struct btrfs_key *cpu_key, u32 *data_size,
3010 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003011{
Chris Mason5f39d392007-10-15 16:14:19 -04003012 struct extent_buffer *leaf;
3013 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003014 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003015 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003016 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003017 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003018 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003019 u32 total_size = 0;
3020 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003021 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003022 struct btrfs_disk_key disk_key;
3023
Chris Mason9c583092008-01-29 15:15:18 -05003024 for (i = 0; i < nr; i++) {
3025 total_data += data_size[i];
3026 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003027
Josef Bacik7b128762008-07-24 12:17:14 -04003028 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003029 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003030 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003031 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003032 if (ret < 0)
3033 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003034
Chris Mason62e27492007-03-15 12:56:47 -04003035 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003036 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003037
Chris Mason5f39d392007-10-15 16:14:19 -04003038 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003039 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003040
Chris Masonf25956c2008-09-12 15:32:53 -04003041 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003042 btrfs_print_leaf(root, leaf);
3043 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003044 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003045 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003046 }
Chris Mason5f39d392007-10-15 16:14:19 -04003047
Chris Mason62e27492007-03-15 12:56:47 -04003048 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003049 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003050
Chris Masonbe0e5c02007-01-26 15:51:26 -05003051 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003052 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003053
Chris Mason5f39d392007-10-15 16:14:19 -04003054 if (old_data < data_end) {
3055 btrfs_print_leaf(root, leaf);
3056 printk("slot %d old_data %d data_end %d\n",
3057 slot, old_data, data_end);
3058 BUG_ON(1);
3059 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003060 /*
3061 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3062 */
3063 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003064 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003065 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003066 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003067
Chris Mason5f39d392007-10-15 16:14:19 -04003068 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003069 if (!leaf->map_token) {
3070 map_extent_buffer(leaf, (unsigned long)item,
3071 sizeof(struct btrfs_item),
3072 &leaf->map_token, &leaf->kaddr,
3073 &leaf->map_start, &leaf->map_len,
3074 KM_USER1);
3075 }
3076
Chris Mason5f39d392007-10-15 16:14:19 -04003077 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003078 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003079 }
Chris Masondb945352007-10-15 16:15:53 -04003080 if (leaf->map_token) {
3081 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3082 leaf->map_token = NULL;
3083 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003084
3085 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003086 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003087 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003088 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003089
3090 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003091 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003092 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003093 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003094 data_end = old_data;
3095 }
Chris Mason5f39d392007-10-15 16:14:19 -04003096
Chris Mason62e27492007-03-15 12:56:47 -04003097 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003098 for (i = 0; i < nr; i++) {
3099 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3100 btrfs_set_item_key(leaf, &disk_key, slot + i);
3101 item = btrfs_item_nr(leaf, slot + i);
3102 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3103 data_end -= data_size[i];
3104 btrfs_set_item_size(leaf, item, data_size[i]);
3105 }
3106 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003107 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003108
3109 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003110 if (slot == 0) {
3111 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003112 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003113 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003114
Chris Mason5f39d392007-10-15 16:14:19 -04003115 if (btrfs_leaf_free_space(root, leaf) < 0) {
3116 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003117 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003118 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003119out:
Chris Mason62e27492007-03-15 12:56:47 -04003120 return ret;
3121}
3122
3123/*
3124 * Given a key and some data, insert an item into the tree.
3125 * This does all the path init required, making room in the tree if needed.
3126 */
Chris Masone089f052007-03-16 16:20:31 -04003127int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3128 *root, struct btrfs_key *cpu_key, void *data, u32
3129 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003130{
3131 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003132 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003133 struct extent_buffer *leaf;
3134 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003135
Chris Mason2c90e5d2007-04-02 10:50:19 -04003136 path = btrfs_alloc_path();
3137 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003138 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003139 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003140 leaf = path->nodes[0];
3141 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3142 write_extent_buffer(leaf, data, ptr, data_size);
3143 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003144 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003145 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003146 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003147}
3148
Chris Mason74123bd2007-02-02 11:05:29 -05003149/*
Chris Mason5de08d72007-02-24 06:24:44 -05003150 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003151 *
Chris Masond352ac62008-09-29 15:18:18 -04003152 * the tree should have been previously balanced so the deletion does not
3153 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003154 */
Chris Masone089f052007-03-16 16:20:31 -04003155static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3156 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003157{
Chris Mason5f39d392007-10-15 16:14:19 -04003158 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003159 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003160 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003161 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003162
Chris Mason5f39d392007-10-15 16:14:19 -04003163 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05003164 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003165 memmove_extent_buffer(parent,
3166 btrfs_node_key_ptr_offset(slot),
3167 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003168 sizeof(struct btrfs_key_ptr) *
3169 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003170 }
Chris Mason7518a232007-03-12 12:01:18 -04003171 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003172 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003173 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003174 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003175 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003176 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003177 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003178 struct btrfs_disk_key disk_key;
3179
3180 btrfs_node_key(parent, &disk_key, 0);
3181 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003182 if (wret)
3183 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003184 }
Chris Masond6025572007-03-30 14:27:56 -04003185 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003186 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003187}
3188
Chris Mason74123bd2007-02-02 11:05:29 -05003189/*
3190 * delete the item at the leaf level in path. If that empties
3191 * the leaf, remove it from the tree
3192 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003193int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3194 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003195{
Chris Mason5f39d392007-10-15 16:14:19 -04003196 struct extent_buffer *leaf;
3197 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003198 int last_off;
3199 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003200 int ret = 0;
3201 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003202 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003203 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003204
Chris Mason5f39d392007-10-15 16:14:19 -04003205 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003206 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3207
3208 for (i = 0; i < nr; i++)
3209 dsize += btrfs_item_size_nr(leaf, slot + i);
3210
Chris Mason5f39d392007-10-15 16:14:19 -04003211 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003212
Chris Mason85e21ba2008-01-29 15:11:36 -05003213 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003214 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003215
3216 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003217 data_end + dsize,
3218 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003219 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003220
Chris Mason85e21ba2008-01-29 15:11:36 -05003221 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003222 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003223
Chris Mason5f39d392007-10-15 16:14:19 -04003224 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003225 if (!leaf->map_token) {
3226 map_extent_buffer(leaf, (unsigned long)item,
3227 sizeof(struct btrfs_item),
3228 &leaf->map_token, &leaf->kaddr,
3229 &leaf->map_start, &leaf->map_len,
3230 KM_USER1);
3231 }
Chris Mason5f39d392007-10-15 16:14:19 -04003232 ioff = btrfs_item_offset(leaf, item);
3233 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003234 }
Chris Masondb945352007-10-15 16:15:53 -04003235
3236 if (leaf->map_token) {
3237 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3238 leaf->map_token = NULL;
3239 }
3240
Chris Mason5f39d392007-10-15 16:14:19 -04003241 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003242 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003243 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003244 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003245 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003246 btrfs_set_header_nritems(leaf, nritems - nr);
3247 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003248
Chris Mason74123bd2007-02-02 11:05:29 -05003249 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003250 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003251 if (leaf == root->node) {
3252 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003253 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05003254 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04003255 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003256 if (wret)
3257 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04003258 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05003259 leaf->start, leaf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04003260 path->nodes[1]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003261 btrfs_header_owner(path->nodes[1]),
3262 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003263 if (wret)
3264 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05003265 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003266 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003267 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003268 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003269 struct btrfs_disk_key disk_key;
3270
3271 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003272 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003273 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003274 if (wret)
3275 ret = wret;
3276 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003277
Chris Mason74123bd2007-02-02 11:05:29 -05003278 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003279 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003280 /* push_leaf_left fixes the path.
3281 * make sure the path still points to our leaf
3282 * for possible call to del_ptr below
3283 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003284 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003285 extent_buffer_get(leaf);
3286
Chris Mason85e21ba2008-01-29 15:11:36 -05003287 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003288 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003289 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003290
3291 if (path->nodes[0] == leaf &&
3292 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003293 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003294 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003295 ret = wret;
3296 }
Chris Mason5f39d392007-10-15 16:14:19 -04003297
3298 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05003299 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04003300 u64 bytenr = leaf->start;
3301 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04003302
Chris Mason7bb86312007-12-11 09:25:06 -05003303 root_gen = btrfs_header_generation(
3304 path->nodes[1]);
3305
Chris Masone089f052007-03-16 16:20:31 -04003306 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003307 if (wret)
3308 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003309
3310 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04003311 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003312 blocksize, path->nodes[1]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003313 btrfs_header_owner(path->nodes[1]),
3314 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003315 if (wret)
3316 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05003317 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003318 /* if we're still in the path, make sure
3319 * we're dirty. Otherwise, one of the
3320 * push_leaf functions must have already
3321 * dirtied this buffer
3322 */
3323 if (path->nodes[0] == leaf)
3324 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003325 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003326 }
Chris Masond5719762007-03-23 10:01:08 -04003327 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003328 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003329 }
3330 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003331 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003332}
3333
Chris Mason97571fd2007-02-24 13:39:08 -05003334/*
Chris Mason925baed2008-06-25 16:01:30 -04003335 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003336 * returns 0 if it found something or 1 if there are no lesser leaves.
3337 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003338 *
3339 * This may release the path, and so you may lose any locks held at the
3340 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003341 */
3342int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3343{
Chris Mason925baed2008-06-25 16:01:30 -04003344 struct btrfs_key key;
3345 struct btrfs_disk_key found_key;
3346 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003347
Chris Mason925baed2008-06-25 16:01:30 -04003348 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003349
Chris Mason925baed2008-06-25 16:01:30 -04003350 if (key.offset > 0)
3351 key.offset--;
3352 else if (key.type > 0)
3353 key.type--;
3354 else if (key.objectid > 0)
3355 key.objectid--;
3356 else
3357 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003358
Chris Mason925baed2008-06-25 16:01:30 -04003359 btrfs_release_path(root, path);
3360 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3361 if (ret < 0)
3362 return ret;
3363 btrfs_item_key(path->nodes[0], &found_key, 0);
3364 ret = comp_keys(&found_key, &key);
3365 if (ret < 0)
3366 return 0;
3367 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003368}
3369
Chris Mason3f157a22008-06-25 16:01:31 -04003370/*
3371 * A helper function to walk down the tree starting at min_key, and looking
3372 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003373 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003374 *
3375 * This does not cow, but it does stuff the starting key it finds back
3376 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3377 * key and get a writable path.
3378 *
3379 * This does lock as it descends, and path->keep_locks should be set
3380 * to 1 by the caller.
3381 *
3382 * This honors path->lowest_level to prevent descent past a given level
3383 * of the tree.
3384 *
Chris Masond352ac62008-09-29 15:18:18 -04003385 * min_trans indicates the oldest transaction that you are interested
3386 * in walking through. Any nodes or leaves older than min_trans are
3387 * skipped over (without reading them).
3388 *
Chris Mason3f157a22008-06-25 16:01:31 -04003389 * returns zero if something useful was found, < 0 on error and 1 if there
3390 * was nothing in the tree that matched the search criteria.
3391 */
3392int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003393 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003394 struct btrfs_path *path, int cache_only,
3395 u64 min_trans)
3396{
3397 struct extent_buffer *cur;
3398 struct btrfs_key found_key;
3399 int slot;
Yan96524802008-07-24 12:19:49 -04003400 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003401 u32 nritems;
3402 int level;
3403 int ret = 1;
3404
3405again:
3406 cur = btrfs_lock_root_node(root);
3407 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003408 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003409 path->nodes[level] = cur;
3410 path->locks[level] = 1;
3411
3412 if (btrfs_header_generation(cur) < min_trans) {
3413 ret = 1;
3414 goto out;
3415 }
3416 while(1) {
3417 nritems = btrfs_header_nritems(cur);
3418 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003419 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003420
3421 /* at level = 0, we're done, setup the path and exit */
3422 if (level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003423 if (slot >= nritems)
3424 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003425 ret = 0;
3426 path->slots[level] = slot;
3427 btrfs_item_key_to_cpu(cur, &found_key, slot);
3428 goto out;
3429 }
Yan96524802008-07-24 12:19:49 -04003430 if (sret && slot > 0)
3431 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003432 /*
3433 * check this node pointer against the cache_only and
3434 * min_trans parameters. If it isn't in cache or is too
3435 * old, skip to the next one.
3436 */
3437 while(slot < nritems) {
3438 u64 blockptr;
3439 u64 gen;
3440 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003441 struct btrfs_disk_key disk_key;
3442
Chris Mason3f157a22008-06-25 16:01:31 -04003443 blockptr = btrfs_node_blockptr(cur, slot);
3444 gen = btrfs_node_ptr_generation(cur, slot);
3445 if (gen < min_trans) {
3446 slot++;
3447 continue;
3448 }
3449 if (!cache_only)
3450 break;
3451
Chris Masone02119d2008-09-05 16:13:11 -04003452 if (max_key) {
3453 btrfs_node_key(cur, &disk_key, slot);
3454 if (comp_keys(&disk_key, max_key) >= 0) {
3455 ret = 1;
3456 goto out;
3457 }
3458 }
3459
Chris Mason3f157a22008-06-25 16:01:31 -04003460 tmp = btrfs_find_tree_block(root, blockptr,
3461 btrfs_level_size(root, level - 1));
3462
3463 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3464 free_extent_buffer(tmp);
3465 break;
3466 }
3467 if (tmp)
3468 free_extent_buffer(tmp);
3469 slot++;
3470 }
Chris Masone02119d2008-09-05 16:13:11 -04003471find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003472 /*
3473 * we didn't find a candidate key in this node, walk forward
3474 * and find another one
3475 */
3476 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003477 path->slots[level] = slot;
3478 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003479 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003480 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003481 btrfs_release_path(root, path);
3482 goto again;
3483 } else {
3484 goto out;
3485 }
3486 }
3487 /* save our key for returning back */
3488 btrfs_node_key_to_cpu(cur, &found_key, slot);
3489 path->slots[level] = slot;
3490 if (level == path->lowest_level) {
3491 ret = 0;
3492 unlock_up(path, level, 1);
3493 goto out;
3494 }
3495 cur = read_node_slot(root, cur, slot);
3496
3497 btrfs_tree_lock(cur);
3498 path->locks[level - 1] = 1;
3499 path->nodes[level - 1] = cur;
3500 unlock_up(path, level, 1);
3501 }
3502out:
3503 if (ret == 0)
3504 memcpy(min_key, &found_key, sizeof(found_key));
3505 return ret;
3506}
3507
3508/*
3509 * this is similar to btrfs_next_leaf, but does not try to preserve
3510 * and fixup the path. It looks for and returns the next key in the
3511 * tree based on the current path and the cache_only and min_trans
3512 * parameters.
3513 *
3514 * 0 is returned if another key is found, < 0 if there are any errors
3515 * and 1 is returned if there are no higher keys in the tree
3516 *
3517 * path->keep_locks should be set to 1 on the search made before
3518 * calling this function.
3519 */
Chris Masone7a84562008-06-25 16:01:31 -04003520int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003521 struct btrfs_key *key, int lowest_level,
3522 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003523{
3524 int level = lowest_level;
3525 int slot;
3526 struct extent_buffer *c;
3527
3528 while(level < BTRFS_MAX_LEVEL) {
3529 if (!path->nodes[level])
3530 return 1;
3531
3532 slot = path->slots[level] + 1;
3533 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003534next:
Chris Masone7a84562008-06-25 16:01:31 -04003535 if (slot >= btrfs_header_nritems(c)) {
3536 level++;
3537 if (level == BTRFS_MAX_LEVEL) {
3538 return 1;
3539 }
3540 continue;
3541 }
3542 if (level == 0)
3543 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003544 else {
3545 u64 blockptr = btrfs_node_blockptr(c, slot);
3546 u64 gen = btrfs_node_ptr_generation(c, slot);
3547
3548 if (cache_only) {
3549 struct extent_buffer *cur;
3550 cur = btrfs_find_tree_block(root, blockptr,
3551 btrfs_level_size(root, level - 1));
3552 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3553 slot++;
3554 if (cur)
3555 free_extent_buffer(cur);
3556 goto next;
3557 }
3558 free_extent_buffer(cur);
3559 }
3560 if (gen < min_trans) {
3561 slot++;
3562 goto next;
3563 }
Chris Masone7a84562008-06-25 16:01:31 -04003564 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003565 }
Chris Masone7a84562008-06-25 16:01:31 -04003566 return 0;
3567 }
3568 return 1;
3569}
3570
Chris Mason7bb86312007-12-11 09:25:06 -05003571/*
Chris Mason925baed2008-06-25 16:01:30 -04003572 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003573 * returns 0 if it found something or 1 if there are no greater leaves.
3574 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003575 */
Chris Mason234b63a2007-03-13 10:46:10 -04003576int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003577{
3578 int slot;
3579 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003580 struct extent_buffer *c;
3581 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003582 struct btrfs_key key;
3583 u32 nritems;
3584 int ret;
3585
3586 nritems = btrfs_header_nritems(path->nodes[0]);
3587 if (nritems == 0) {
3588 return 1;
3589 }
3590
3591 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3592
Chris Mason925baed2008-06-25 16:01:30 -04003593 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003594 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003595 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3596 path->keep_locks = 0;
3597
3598 if (ret < 0)
3599 return ret;
3600
Chris Masona2135012008-06-25 16:01:30 -04003601 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003602 /*
3603 * by releasing the path above we dropped all our locks. A balance
3604 * could have added more items next to the key that used to be
3605 * at the very end of the block. So, check again here and
3606 * advance the path if there are now more items available.
3607 */
Chris Masona2135012008-06-25 16:01:30 -04003608 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003609 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003610 goto done;
3611 }
Chris Masond97e63b2007-02-20 16:40:44 -05003612
Chris Mason234b63a2007-03-13 10:46:10 -04003613 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003614 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003615 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003616
Chris Masond97e63b2007-02-20 16:40:44 -05003617 slot = path->slots[level] + 1;
3618 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003619 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003620 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003621 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003622 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003623 }
Chris Masond97e63b2007-02-20 16:40:44 -05003624 continue;
3625 }
Chris Mason5f39d392007-10-15 16:14:19 -04003626
Chris Mason925baed2008-06-25 16:01:30 -04003627 if (next) {
3628 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003629 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003630 }
Chris Mason5f39d392007-10-15 16:14:19 -04003631
Chris Mason0bd40a72008-07-17 12:54:43 -04003632 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3633 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003634 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003635
Chris Masonca7a79a2008-05-12 12:59:19 -04003636 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003637 if (!path->skip_locking) {
3638 WARN_ON(!btrfs_tree_locked(c));
3639 btrfs_tree_lock(next);
3640 }
Chris Masond97e63b2007-02-20 16:40:44 -05003641 break;
3642 }
3643 path->slots[level] = slot;
3644 while(1) {
3645 level--;
3646 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003647 if (path->locks[level])
3648 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003649 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003650 path->nodes[level] = next;
3651 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003652 if (!path->skip_locking)
3653 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003654 if (!level)
3655 break;
Chris Mason925baed2008-06-25 16:01:30 -04003656 if (level == 1 && path->locks[1] && path->reada)
3657 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003658 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003659 if (!path->skip_locking) {
3660 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3661 btrfs_tree_lock(next);
3662 }
Chris Masond97e63b2007-02-20 16:40:44 -05003663 }
Chris Mason925baed2008-06-25 16:01:30 -04003664done:
3665 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003666 return 0;
3667}
Chris Mason0b86a832008-03-24 15:01:56 -04003668
Chris Mason3f157a22008-06-25 16:01:31 -04003669/*
3670 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3671 * searching until it gets past min_objectid or finds an item of 'type'
3672 *
3673 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3674 */
Chris Mason0b86a832008-03-24 15:01:56 -04003675int btrfs_previous_item(struct btrfs_root *root,
3676 struct btrfs_path *path, u64 min_objectid,
3677 int type)
3678{
3679 struct btrfs_key found_key;
3680 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003681 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003682 int ret;
3683
3684 while(1) {
3685 if (path->slots[0] == 0) {
3686 ret = btrfs_prev_leaf(root, path);
3687 if (ret != 0)
3688 return ret;
3689 } else {
3690 path->slots[0]--;
3691 }
3692 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003693 nritems = btrfs_header_nritems(leaf);
3694 if (nritems == 0)
3695 return 1;
3696 if (path->slots[0] == nritems)
3697 path->slots[0]--;
3698
Chris Mason0b86a832008-03-24 15:01:56 -04003699 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3700 if (found_key.type == type)
3701 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003702 if (found_key.objectid < min_objectid)
3703 break;
3704 if (found_key.objectid == min_objectid &&
3705 found_key.type < type)
3706 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003707 }
3708 return 1;
3709}