blob: 9caeb377de6327c7d5d466af994d2f9e9dea6301 [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,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400257 trans->transid, level, &ins);
Chris Mason65b51a02008-08-01 15:11:20 -0400258 BUG_ON(ret);
259 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
260 buf->len);
261 } else {
262 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400263 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400264 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400265 trans->transid, level,
266 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400267 }
Chris Mason6702ed42007-08-07 16:15:09 -0400268 if (IS_ERR(cow))
269 return PTR_ERR(cow);
270
Chris Mason5f39d392007-10-15 16:14:19 -0400271 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400272 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400273 btrfs_set_header_generation(cow, trans->transid);
274 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400275 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400276
Chris Mason5f39d392007-10-15 16:14:19 -0400277 WARN_ON(btrfs_header_generation(buf) > trans->transid);
278 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400279 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400280 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400281 if (ret)
282 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400283
284 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
285 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400286 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
287 /*
288 * There are only two places that can drop reference to
289 * tree blocks owned by living reloc trees, one is here,
290 * the other place is btrfs_merge_path. In both places,
291 * we check reference count while tree block is locked.
292 * Furthermore, if reference count is one, it won't get
293 * increased by someone else.
294 */
295 u32 refs;
296 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
297 buf->len, &refs);
298 BUG_ON(ret);
299 if (refs == 1) {
300 ret = btrfs_update_ref(trans, root, buf, cow,
301 0, nritems);
302 clean_tree_block(trans, root, buf);
303 } else {
304 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
305 }
306 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400307 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400308 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
309 if (ret)
310 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400311 clean_tree_block(trans, root, buf);
312 }
313
Zheng Yan1a40e232008-09-26 10:09:34 -0400314 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
315 ret = btrfs_add_reloc_mapping(root, buf->start,
316 buf->len, cow->start);
317 BUG_ON(ret);
318 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
319 WARN_ON(ret);
320 }
321
Chris Mason6702ed42007-08-07 16:15:09 -0400322 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400323 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400324
325 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400326 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400327 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400328 spin_unlock(&root->node_lock);
329
Chris Mason6702ed42007-08-07 16:15:09 -0400330 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400331 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400332 buf->len, buf->start,
333 root->root_key.objectid,
334 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400335 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400336 }
Chris Mason5f39d392007-10-15 16:14:19 -0400337 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400338 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400339 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400340 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400341 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500342 WARN_ON(trans->transid == 0);
343 btrfs_set_node_ptr_generation(parent, parent_slot,
344 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400345 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400346 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500347 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400348 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400349 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400350 }
Chris Mason925baed2008-06-25 16:01:30 -0400351 if (unlock_orig)
352 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400353 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400354 btrfs_mark_buffer_dirty(cow);
355 *cow_ret = cow;
356 return 0;
357}
358
Chris Masond352ac62008-09-29 15:18:18 -0400359/*
360 * cows a single block, see __btrfs_cow_block for the real work.
361 * This version of it has extra checks so that a block isn't cow'd more than
362 * once per transaction, as long as it hasn't been written yet
363 */
Chris Masone02119d2008-09-05 16:13:11 -0400364int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400365 struct btrfs_root *root, struct extent_buffer *buf,
366 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400367 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500368{
Chris Mason6702ed42007-08-07 16:15:09 -0400369 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400370 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500371
Chris Masonccd467d2007-06-28 15:57:36 -0400372 if (trans->transaction != root->fs_info->running_transaction) {
373 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
374 root->fs_info->running_transaction->transid);
375 WARN_ON(1);
376 }
377 if (trans->transid != root->fs_info->generation) {
378 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
379 root->fs_info->generation);
380 WARN_ON(1);
381 }
Chris Masondc17ff82008-01-08 15:46:30 -0500382
Chris Mason63b10fc2008-04-01 11:21:32 -0400383 spin_lock(&root->fs_info->hash_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400384 if (btrfs_header_generation(buf) == trans->transid &&
385 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400386 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500387 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400388 spin_unlock(&root->fs_info->hash_lock);
Chris Mason65b51a02008-08-01 15:11:20 -0400389 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500390 return 0;
391 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400392 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400393 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400394 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400395 parent_slot, cow_ret, search_start, 0,
396 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400397 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400398}
399
Chris Masond352ac62008-09-29 15:18:18 -0400400/*
401 * helper function for defrag to decide if two blocks pointed to by a
402 * node are actually close by
403 */
Chris Mason6b800532007-10-15 16:17:34 -0400404static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400405{
Chris Mason6b800532007-10-15 16:17:34 -0400406 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400407 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400408 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400409 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500410 return 0;
411}
412
Chris Mason081e9572007-11-06 10:26:24 -0500413/*
414 * compare two keys in a memcmp fashion
415 */
416static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
417{
418 struct btrfs_key k1;
419
420 btrfs_disk_key_to_cpu(&k1, disk);
421
422 if (k1.objectid > k2->objectid)
423 return 1;
424 if (k1.objectid < k2->objectid)
425 return -1;
426 if (k1.type > k2->type)
427 return 1;
428 if (k1.type < k2->type)
429 return -1;
430 if (k1.offset > k2->offset)
431 return 1;
432 if (k1.offset < k2->offset)
433 return -1;
434 return 0;
435}
436
437
Chris Masond352ac62008-09-29 15:18:18 -0400438/*
439 * this is used by the defrag code to go through all the
440 * leaves pointed to by a node and reallocate them so that
441 * disk order is close to key order
442 */
Chris Mason6702ed42007-08-07 16:15:09 -0400443int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400444 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400445 int start_slot, int cache_only, u64 *last_ret,
446 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400447{
Chris Mason6b800532007-10-15 16:17:34 -0400448 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400449 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400450 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400451 u64 search_start = *last_ret;
452 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400453 u64 other;
454 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400455 int end_slot;
456 int i;
457 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400458 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400459 int uptodate;
460 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500461 int progress_passed = 0;
462 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400463
Chris Mason5708b952007-10-25 15:43:18 -0400464 parent_level = btrfs_header_level(parent);
465 if (cache_only && parent_level != 1)
466 return 0;
467
Chris Mason6702ed42007-08-07 16:15:09 -0400468 if (trans->transaction != root->fs_info->running_transaction) {
469 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
470 root->fs_info->running_transaction->transid);
471 WARN_ON(1);
472 }
473 if (trans->transid != root->fs_info->generation) {
474 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
475 root->fs_info->generation);
476 WARN_ON(1);
477 }
Chris Mason86479a02007-09-10 19:58:16 -0400478
Chris Mason6b800532007-10-15 16:17:34 -0400479 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400480 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400481 end_slot = parent_nritems;
482
483 if (parent_nritems == 1)
484 return 0;
485
486 for (i = start_slot; i < end_slot; i++) {
487 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400488
Chris Mason5708b952007-10-25 15:43:18 -0400489 if (!parent->map_token) {
490 map_extent_buffer(parent,
491 btrfs_node_key_ptr_offset(i),
492 sizeof(struct btrfs_key_ptr),
493 &parent->map_token, &parent->kaddr,
494 &parent->map_start, &parent->map_len,
495 KM_USER1);
496 }
Chris Mason081e9572007-11-06 10:26:24 -0500497 btrfs_node_key(parent, &disk_key, i);
498 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
499 continue;
500
501 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400502 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400503 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400504 if (last_block == 0)
505 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400506
Chris Mason6702ed42007-08-07 16:15:09 -0400507 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400508 other = btrfs_node_blockptr(parent, i - 1);
509 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400510 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400511 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400512 other = btrfs_node_blockptr(parent, i + 1);
513 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400514 }
Chris Masone9d0b132007-08-10 14:06:19 -0400515 if (close) {
516 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400517 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400518 }
Chris Mason5708b952007-10-25 15:43:18 -0400519 if (parent->map_token) {
520 unmap_extent_buffer(parent, parent->map_token,
521 KM_USER1);
522 parent->map_token = NULL;
523 }
Chris Mason6702ed42007-08-07 16:15:09 -0400524
Chris Mason6b800532007-10-15 16:17:34 -0400525 cur = btrfs_find_tree_block(root, blocknr, blocksize);
526 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400527 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400528 else
529 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400530 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400531 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400532 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400533 continue;
534 }
Chris Mason6b800532007-10-15 16:17:34 -0400535 if (!cur) {
536 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400537 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400538 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400539 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400540 }
Chris Mason6702ed42007-08-07 16:15:09 -0400541 }
Chris Masone9d0b132007-08-10 14:06:19 -0400542 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400543 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400544
Chris Masone7a84562008-06-25 16:01:31 -0400545 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400546 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400547 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400548 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400549 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400550 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400551 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400552 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400553 break;
Yan252c38f2007-08-29 09:11:44 -0400554 }
Chris Masone7a84562008-06-25 16:01:31 -0400555 search_start = cur->start;
556 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400557 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400558 btrfs_tree_unlock(cur);
559 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400560 }
Chris Mason5708b952007-10-25 15:43:18 -0400561 if (parent->map_token) {
562 unmap_extent_buffer(parent, parent->map_token,
563 KM_USER1);
564 parent->map_token = NULL;
565 }
Chris Mason6702ed42007-08-07 16:15:09 -0400566 return err;
567}
568
Chris Mason74123bd2007-02-02 11:05:29 -0500569/*
570 * The leaf data grows from end-to-front in the node.
571 * this returns the address of the start of the last item,
572 * which is the stop of the leaf data stack
573 */
Chris Mason123abc82007-03-14 14:14:43 -0400574static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400575 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500576{
Chris Mason5f39d392007-10-15 16:14:19 -0400577 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500578 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400579 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400580 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500581}
582
Chris Masond352ac62008-09-29 15:18:18 -0400583/*
584 * extra debugging checks to make sure all the items in a key are
585 * well formed and in the proper order
586 */
Chris Mason123abc82007-03-14 14:14:43 -0400587static int check_node(struct btrfs_root *root, struct btrfs_path *path,
588 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500589{
Chris Mason5f39d392007-10-15 16:14:19 -0400590 struct extent_buffer *parent = NULL;
591 struct extent_buffer *node = path->nodes[level];
592 struct btrfs_disk_key parent_key;
593 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500594 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400595 int slot;
596 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400597 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500598
599 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400600 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400601
Chris Mason8d7be552007-05-10 11:24:42 -0400602 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400603 BUG_ON(nritems == 0);
604 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400605 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400606 btrfs_node_key(parent, &parent_key, parent_slot);
607 btrfs_node_key(node, &node_key, 0);
608 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400609 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400610 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400611 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500612 }
Chris Mason123abc82007-03-14 14:14:43 -0400613 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400614 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400615 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
616 btrfs_node_key(node, &node_key, slot);
617 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400618 }
619 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400620 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
621 btrfs_node_key(node, &node_key, slot);
622 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500623 }
624 return 0;
625}
626
Chris Masond352ac62008-09-29 15:18:18 -0400627/*
628 * extra checking to make sure all the items in a leaf are
629 * well formed and in the proper order
630 */
Chris Mason123abc82007-03-14 14:14:43 -0400631static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
632 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500633{
Chris Mason5f39d392007-10-15 16:14:19 -0400634 struct extent_buffer *leaf = path->nodes[level];
635 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500636 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400637 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400638 struct btrfs_disk_key parent_key;
639 struct btrfs_disk_key leaf_key;
640 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400641
Chris Mason5f39d392007-10-15 16:14:19 -0400642 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500643
644 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400645 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400646
647 if (nritems == 0)
648 return 0;
649
650 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400651 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400652 btrfs_node_key(parent, &parent_key, parent_slot);
653 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400654
Chris Mason5f39d392007-10-15 16:14:19 -0400655 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400656 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400657 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400658 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500659 }
Chris Mason5f39d392007-10-15 16:14:19 -0400660#if 0
661 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
662 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
663 btrfs_item_key(leaf, &leaf_key, i);
664 if (comp_keys(&leaf_key, &cpukey) >= 0) {
665 btrfs_print_leaf(root, leaf);
666 printk("slot %d offset bad key\n", i);
667 BUG_ON(1);
668 }
669 if (btrfs_item_offset_nr(leaf, i) !=
670 btrfs_item_end_nr(leaf, i + 1)) {
671 btrfs_print_leaf(root, leaf);
672 printk("slot %d offset bad\n", i);
673 BUG_ON(1);
674 }
675 if (i == 0) {
676 if (btrfs_item_offset_nr(leaf, i) +
677 btrfs_item_size_nr(leaf, i) !=
678 BTRFS_LEAF_DATA_SIZE(root)) {
679 btrfs_print_leaf(root, leaf);
680 printk("slot %d first offset bad\n", i);
681 BUG_ON(1);
682 }
683 }
684 }
685 if (nritems > 0) {
686 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
687 btrfs_print_leaf(root, leaf);
688 printk("slot %d bad size \n", nritems - 1);
689 BUG_ON(1);
690 }
691 }
692#endif
693 if (slot != 0 && slot < nritems - 1) {
694 btrfs_item_key(leaf, &leaf_key, slot);
695 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
696 if (comp_keys(&leaf_key, &cpukey) <= 0) {
697 btrfs_print_leaf(root, leaf);
698 printk("slot %d offset bad key\n", slot);
699 BUG_ON(1);
700 }
701 if (btrfs_item_offset_nr(leaf, slot - 1) !=
702 btrfs_item_end_nr(leaf, slot)) {
703 btrfs_print_leaf(root, leaf);
704 printk("slot %d offset bad\n", slot);
705 BUG_ON(1);
706 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500707 }
Chris Mason8d7be552007-05-10 11:24:42 -0400708 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400709 btrfs_item_key(leaf, &leaf_key, slot);
710 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
711 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
712 if (btrfs_item_offset_nr(leaf, slot) !=
713 btrfs_item_end_nr(leaf, slot + 1)) {
714 btrfs_print_leaf(root, leaf);
715 printk("slot %d offset bad\n", slot);
716 BUG_ON(1);
717 }
Chris Mason8d7be552007-05-10 11:24:42 -0400718 }
Chris Mason5f39d392007-10-15 16:14:19 -0400719 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
720 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500721 return 0;
722}
723
Chris Mason98ed5172008-01-03 10:01:48 -0500724static int noinline check_block(struct btrfs_root *root,
725 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500726{
Chris Masonf1885912008-04-09 16:28:12 -0400727 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400728 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400729 if (btrfs_header_level(path->nodes[level]) != level)
730 printk("warning: bad level %Lu wanted %d found %d\n",
731 path->nodes[level]->start, level,
732 btrfs_header_level(path->nodes[level]));
733 found_start = btrfs_header_bytenr(path->nodes[level]);
734 if (found_start != path->nodes[level]->start) {
735 printk("warning: bad bytentr %Lu found %Lu\n",
736 path->nodes[level]->start, found_start);
737 }
Chris Masondb945352007-10-15 16:15:53 -0400738#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400739 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400740
Chris Mason479965d2007-10-15 16:14:27 -0400741 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
742 (unsigned long)btrfs_header_fsid(buf),
743 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400744 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400745 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400746 }
Chris Masondb945352007-10-15 16:15:53 -0400747#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500748 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400749 return check_leaf(root, path, level);
750 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500751}
752
Chris Mason74123bd2007-02-02 11:05:29 -0500753/*
Chris Mason5f39d392007-10-15 16:14:19 -0400754 * search for key in the extent_buffer. The items start at offset p,
755 * and they are item_size apart. There are 'max' items in p.
756 *
Chris Mason74123bd2007-02-02 11:05:29 -0500757 * the slot in the array is returned via slot, and it points to
758 * the place where you would insert key if it is not found in
759 * the array.
760 *
761 * slot may point to max if the key is bigger than all of the keys
762 */
Chris Masone02119d2008-09-05 16:13:11 -0400763static noinline int generic_bin_search(struct extent_buffer *eb,
764 unsigned long p,
765 int item_size, struct btrfs_key *key,
766 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500767{
768 int low = 0;
769 int high = max;
770 int mid;
771 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400772 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400773 struct btrfs_disk_key unaligned;
774 unsigned long offset;
775 char *map_token = NULL;
776 char *kaddr = NULL;
777 unsigned long map_start = 0;
778 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400779 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500780
781 while(low < high) {
782 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400783 offset = p + mid * item_size;
784
785 if (!map_token || offset < map_start ||
786 (offset + sizeof(struct btrfs_disk_key)) >
787 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400788 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400789 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400790 map_token = NULL;
791 }
792 err = map_extent_buffer(eb, offset,
793 sizeof(struct btrfs_disk_key),
794 &map_token, &kaddr,
795 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400796
Chris Mason479965d2007-10-15 16:14:27 -0400797 if (!err) {
798 tmp = (struct btrfs_disk_key *)(kaddr + offset -
799 map_start);
800 } else {
801 read_extent_buffer(eb, &unaligned,
802 offset, sizeof(unaligned));
803 tmp = &unaligned;
804 }
805
Chris Mason5f39d392007-10-15 16:14:19 -0400806 } else {
807 tmp = (struct btrfs_disk_key *)(kaddr + offset -
808 map_start);
809 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500810 ret = comp_keys(tmp, key);
811
812 if (ret < 0)
813 low = mid + 1;
814 else if (ret > 0)
815 high = mid;
816 else {
817 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400818 if (map_token)
819 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500820 return 0;
821 }
822 }
823 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400824 if (map_token)
825 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500826 return 1;
827}
828
Chris Mason97571fd2007-02-24 13:39:08 -0500829/*
830 * simple bin_search frontend that does the right thing for
831 * leaves vs nodes
832 */
Chris Mason5f39d392007-10-15 16:14:19 -0400833static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
834 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500835{
Chris Mason5f39d392007-10-15 16:14:19 -0400836 if (level == 0) {
837 return generic_bin_search(eb,
838 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400839 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400840 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400841 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500842 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400843 return generic_bin_search(eb,
844 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400845 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400846 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400847 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500848 }
849 return -1;
850}
851
Chris Masond352ac62008-09-29 15:18:18 -0400852/* given a node and slot number, this reads the blocks it points to. The
853 * extent buffer is returned with a reference taken (but unlocked).
854 * NULL is returned on error.
855 */
Chris Masone02119d2008-09-05 16:13:11 -0400856static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400857 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500858{
Chris Masonca7a79a2008-05-12 12:59:19 -0400859 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500860 if (slot < 0)
861 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400862 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500863 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400864
865 BUG_ON(level == 0);
866
Chris Masondb945352007-10-15 16:15:53 -0400867 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400868 btrfs_level_size(root, level - 1),
869 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500870}
871
Chris Masond352ac62008-09-29 15:18:18 -0400872/*
873 * node level balancing, used to make sure nodes are in proper order for
874 * item deletion. We balance from the top down, so we have to make sure
875 * that a deletion won't leave an node completely empty later on.
876 */
Chris Masone02119d2008-09-05 16:13:11 -0400877static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500878 struct btrfs_root *root,
879 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500880{
Chris Mason5f39d392007-10-15 16:14:19 -0400881 struct extent_buffer *right = NULL;
882 struct extent_buffer *mid;
883 struct extent_buffer *left = NULL;
884 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500885 int ret = 0;
886 int wret;
887 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500888 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400889 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500890 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500891
892 if (level == 0)
893 return 0;
894
Chris Mason5f39d392007-10-15 16:14:19 -0400895 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400896 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500897 WARN_ON(btrfs_header_generation(mid) != trans->transid);
898
Chris Mason1d4f8a02007-03-13 09:28:32 -0400899 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500900
Chris Mason234b63a2007-03-13 10:46:10 -0400901 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400902 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500903 pslot = path->slots[level + 1];
904
Chris Mason40689472007-03-17 14:29:23 -0400905 /*
906 * deal with the case where there is only one pointer in the root
907 * by promoting the node below to a root
908 */
Chris Mason5f39d392007-10-15 16:14:19 -0400909 if (!parent) {
910 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500911
Chris Mason5f39d392007-10-15 16:14:19 -0400912 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500913 return 0;
914
915 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400916 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400917 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500918 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400919 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500920 BUG_ON(ret);
921
Chris Mason925baed2008-06-25 16:01:30 -0400922 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500923 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400924 spin_unlock(&root->node_lock);
925
Zheng Yan31840ae2008-09-23 13:14:14 -0400926 ret = btrfs_update_extent_ref(trans, root, child->start,
927 mid->start, child->start,
928 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400929 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400930 BUG_ON(ret);
931
Chris Mason0b86a832008-03-24 15:01:56 -0400932 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400933 btrfs_tree_unlock(child);
934 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500935 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400936 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400937 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500938 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400939 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500940 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400941 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400942 btrfs_header_generation(mid),
943 level, 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),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001009 generation, level, 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),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001058 root_gen, level, 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;
Chris Mason323ac952008-10-01 19:05:46 -04001391 WARN_ON(lowest_level && ins_len > 0);
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),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001694 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001695 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),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001700 level - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001701 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,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001976 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001977 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/*
Chris Mason323ac952008-10-01 19:05:46 -04003190 * a helper function to delete the leaf pointed to by path->slots[1] and
3191 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3192 * already know it, it is faster to have them pass it down than to
3193 * read it out of the node again.
3194 *
3195 * This deletes the pointer in path->nodes[1] and frees the leaf
3196 * block extent. zero is returned if it all worked out, < 0 otherwise.
3197 *
3198 * The path must have already been setup for deleting the leaf, including
3199 * all the proper balancing. path->nodes[1] must be locked.
3200 */
3201noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3202 struct btrfs_root *root,
3203 struct btrfs_path *path, u64 bytenr)
3204{
3205 int ret;
3206 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3207
3208 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3209 if (ret)
3210 return ret;
3211
3212 ret = btrfs_free_extent(trans, root, bytenr,
3213 btrfs_level_size(root, 0),
3214 path->nodes[1]->start,
3215 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003216 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003217 return ret;
3218}
3219/*
Chris Mason74123bd2007-02-02 11:05:29 -05003220 * delete the item at the leaf level in path. If that empties
3221 * the leaf, remove it from the tree
3222 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003223int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3224 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003225{
Chris Mason5f39d392007-10-15 16:14:19 -04003226 struct extent_buffer *leaf;
3227 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003228 int last_off;
3229 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003230 int ret = 0;
3231 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003232 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003233 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003234
Chris Mason5f39d392007-10-15 16:14:19 -04003235 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003236 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3237
3238 for (i = 0; i < nr; i++)
3239 dsize += btrfs_item_size_nr(leaf, slot + i);
3240
Chris Mason5f39d392007-10-15 16:14:19 -04003241 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003242
Chris Mason85e21ba2008-01-29 15:11:36 -05003243 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003244 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003245
3246 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003247 data_end + dsize,
3248 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003249 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003250
Chris Mason85e21ba2008-01-29 15:11:36 -05003251 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003252 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003253
Chris Mason5f39d392007-10-15 16:14:19 -04003254 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003255 if (!leaf->map_token) {
3256 map_extent_buffer(leaf, (unsigned long)item,
3257 sizeof(struct btrfs_item),
3258 &leaf->map_token, &leaf->kaddr,
3259 &leaf->map_start, &leaf->map_len,
3260 KM_USER1);
3261 }
Chris Mason5f39d392007-10-15 16:14:19 -04003262 ioff = btrfs_item_offset(leaf, item);
3263 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003264 }
Chris Masondb945352007-10-15 16:15:53 -04003265
3266 if (leaf->map_token) {
3267 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3268 leaf->map_token = NULL;
3269 }
3270
Chris Mason5f39d392007-10-15 16:14:19 -04003271 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003272 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003273 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003274 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003275 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003276 btrfs_set_header_nritems(leaf, nritems - nr);
3277 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003278
Chris Mason74123bd2007-02-02 11:05:29 -05003279 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003280 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003281 if (leaf == root->node) {
3282 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003283 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003284 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3285 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003286 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003287 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003288 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003289 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003290 struct btrfs_disk_key disk_key;
3291
3292 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003293 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003294 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003295 if (wret)
3296 ret = wret;
3297 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003298
Chris Mason74123bd2007-02-02 11:05:29 -05003299 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003300 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003301 /* push_leaf_left fixes the path.
3302 * make sure the path still points to our leaf
3303 * for possible call to del_ptr below
3304 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003305 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003306 extent_buffer_get(leaf);
3307
Chris Mason85e21ba2008-01-29 15:11:36 -05003308 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003309 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003310 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003311
3312 if (path->nodes[0] == leaf &&
3313 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003314 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003315 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003316 ret = wret;
3317 }
Chris Mason5f39d392007-10-15 16:14:19 -04003318
3319 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003320 path->slots[1] = slot;
3321 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3322 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003323 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003324 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003325 /* if we're still in the path, make sure
3326 * we're dirty. Otherwise, one of the
3327 * push_leaf functions must have already
3328 * dirtied this buffer
3329 */
3330 if (path->nodes[0] == leaf)
3331 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003332 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003333 }
Chris Masond5719762007-03-23 10:01:08 -04003334 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003335 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003336 }
3337 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003338 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003339}
3340
Chris Mason97571fd2007-02-24 13:39:08 -05003341/*
Chris Mason925baed2008-06-25 16:01:30 -04003342 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003343 * returns 0 if it found something or 1 if there are no lesser leaves.
3344 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003345 *
3346 * This may release the path, and so you may lose any locks held at the
3347 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003348 */
3349int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3350{
Chris Mason925baed2008-06-25 16:01:30 -04003351 struct btrfs_key key;
3352 struct btrfs_disk_key found_key;
3353 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003354
Chris Mason925baed2008-06-25 16:01:30 -04003355 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003356
Chris Mason925baed2008-06-25 16:01:30 -04003357 if (key.offset > 0)
3358 key.offset--;
3359 else if (key.type > 0)
3360 key.type--;
3361 else if (key.objectid > 0)
3362 key.objectid--;
3363 else
3364 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003365
Chris Mason925baed2008-06-25 16:01:30 -04003366 btrfs_release_path(root, path);
3367 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3368 if (ret < 0)
3369 return ret;
3370 btrfs_item_key(path->nodes[0], &found_key, 0);
3371 ret = comp_keys(&found_key, &key);
3372 if (ret < 0)
3373 return 0;
3374 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003375}
3376
Chris Mason3f157a22008-06-25 16:01:31 -04003377/*
3378 * A helper function to walk down the tree starting at min_key, and looking
3379 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003380 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003381 *
3382 * This does not cow, but it does stuff the starting key it finds back
3383 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3384 * key and get a writable path.
3385 *
3386 * This does lock as it descends, and path->keep_locks should be set
3387 * to 1 by the caller.
3388 *
3389 * This honors path->lowest_level to prevent descent past a given level
3390 * of the tree.
3391 *
Chris Masond352ac62008-09-29 15:18:18 -04003392 * min_trans indicates the oldest transaction that you are interested
3393 * in walking through. Any nodes or leaves older than min_trans are
3394 * skipped over (without reading them).
3395 *
Chris Mason3f157a22008-06-25 16:01:31 -04003396 * returns zero if something useful was found, < 0 on error and 1 if there
3397 * was nothing in the tree that matched the search criteria.
3398 */
3399int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003400 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003401 struct btrfs_path *path, int cache_only,
3402 u64 min_trans)
3403{
3404 struct extent_buffer *cur;
3405 struct btrfs_key found_key;
3406 int slot;
Yan96524802008-07-24 12:19:49 -04003407 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003408 u32 nritems;
3409 int level;
3410 int ret = 1;
3411
3412again:
3413 cur = btrfs_lock_root_node(root);
3414 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003415 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003416 path->nodes[level] = cur;
3417 path->locks[level] = 1;
3418
3419 if (btrfs_header_generation(cur) < min_trans) {
3420 ret = 1;
3421 goto out;
3422 }
3423 while(1) {
3424 nritems = btrfs_header_nritems(cur);
3425 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003426 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003427
Chris Mason323ac952008-10-01 19:05:46 -04003428 /* at the lowest level, we're done, setup the path and exit */
3429 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003430 if (slot >= nritems)
3431 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003432 ret = 0;
3433 path->slots[level] = slot;
3434 btrfs_item_key_to_cpu(cur, &found_key, slot);
3435 goto out;
3436 }
Yan96524802008-07-24 12:19:49 -04003437 if (sret && slot > 0)
3438 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003439 /*
3440 * check this node pointer against the cache_only and
3441 * min_trans parameters. If it isn't in cache or is too
3442 * old, skip to the next one.
3443 */
3444 while(slot < nritems) {
3445 u64 blockptr;
3446 u64 gen;
3447 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003448 struct btrfs_disk_key disk_key;
3449
Chris Mason3f157a22008-06-25 16:01:31 -04003450 blockptr = btrfs_node_blockptr(cur, slot);
3451 gen = btrfs_node_ptr_generation(cur, slot);
3452 if (gen < min_trans) {
3453 slot++;
3454 continue;
3455 }
3456 if (!cache_only)
3457 break;
3458
Chris Masone02119d2008-09-05 16:13:11 -04003459 if (max_key) {
3460 btrfs_node_key(cur, &disk_key, slot);
3461 if (comp_keys(&disk_key, max_key) >= 0) {
3462 ret = 1;
3463 goto out;
3464 }
3465 }
3466
Chris Mason3f157a22008-06-25 16:01:31 -04003467 tmp = btrfs_find_tree_block(root, blockptr,
3468 btrfs_level_size(root, level - 1));
3469
3470 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3471 free_extent_buffer(tmp);
3472 break;
3473 }
3474 if (tmp)
3475 free_extent_buffer(tmp);
3476 slot++;
3477 }
Chris Masone02119d2008-09-05 16:13:11 -04003478find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003479 /*
3480 * we didn't find a candidate key in this node, walk forward
3481 * and find another one
3482 */
3483 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003484 path->slots[level] = slot;
3485 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003486 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003487 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003488 btrfs_release_path(root, path);
3489 goto again;
3490 } else {
3491 goto out;
3492 }
3493 }
3494 /* save our key for returning back */
3495 btrfs_node_key_to_cpu(cur, &found_key, slot);
3496 path->slots[level] = slot;
3497 if (level == path->lowest_level) {
3498 ret = 0;
3499 unlock_up(path, level, 1);
3500 goto out;
3501 }
3502 cur = read_node_slot(root, cur, slot);
3503
3504 btrfs_tree_lock(cur);
3505 path->locks[level - 1] = 1;
3506 path->nodes[level - 1] = cur;
3507 unlock_up(path, level, 1);
3508 }
3509out:
3510 if (ret == 0)
3511 memcpy(min_key, &found_key, sizeof(found_key));
3512 return ret;
3513}
3514
3515/*
3516 * this is similar to btrfs_next_leaf, but does not try to preserve
3517 * and fixup the path. It looks for and returns the next key in the
3518 * tree based on the current path and the cache_only and min_trans
3519 * parameters.
3520 *
3521 * 0 is returned if another key is found, < 0 if there are any errors
3522 * and 1 is returned if there are no higher keys in the tree
3523 *
3524 * path->keep_locks should be set to 1 on the search made before
3525 * calling this function.
3526 */
Chris Masone7a84562008-06-25 16:01:31 -04003527int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003528 struct btrfs_key *key, int lowest_level,
3529 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003530{
3531 int level = lowest_level;
3532 int slot;
3533 struct extent_buffer *c;
3534
3535 while(level < BTRFS_MAX_LEVEL) {
3536 if (!path->nodes[level])
3537 return 1;
3538
3539 slot = path->slots[level] + 1;
3540 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003541next:
Chris Masone7a84562008-06-25 16:01:31 -04003542 if (slot >= btrfs_header_nritems(c)) {
3543 level++;
3544 if (level == BTRFS_MAX_LEVEL) {
3545 return 1;
3546 }
3547 continue;
3548 }
3549 if (level == 0)
3550 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003551 else {
3552 u64 blockptr = btrfs_node_blockptr(c, slot);
3553 u64 gen = btrfs_node_ptr_generation(c, slot);
3554
3555 if (cache_only) {
3556 struct extent_buffer *cur;
3557 cur = btrfs_find_tree_block(root, blockptr,
3558 btrfs_level_size(root, level - 1));
3559 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3560 slot++;
3561 if (cur)
3562 free_extent_buffer(cur);
3563 goto next;
3564 }
3565 free_extent_buffer(cur);
3566 }
3567 if (gen < min_trans) {
3568 slot++;
3569 goto next;
3570 }
Chris Masone7a84562008-06-25 16:01:31 -04003571 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003572 }
Chris Masone7a84562008-06-25 16:01:31 -04003573 return 0;
3574 }
3575 return 1;
3576}
3577
Chris Mason7bb86312007-12-11 09:25:06 -05003578/*
Chris Mason925baed2008-06-25 16:01:30 -04003579 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003580 * returns 0 if it found something or 1 if there are no greater leaves.
3581 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003582 */
Chris Mason234b63a2007-03-13 10:46:10 -04003583int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003584{
3585 int slot;
3586 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003587 struct extent_buffer *c;
3588 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003589 struct btrfs_key key;
3590 u32 nritems;
3591 int ret;
3592
3593 nritems = btrfs_header_nritems(path->nodes[0]);
3594 if (nritems == 0) {
3595 return 1;
3596 }
3597
3598 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3599
Chris Mason925baed2008-06-25 16:01:30 -04003600 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003601 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003602 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3603 path->keep_locks = 0;
3604
3605 if (ret < 0)
3606 return ret;
3607
Chris Masona2135012008-06-25 16:01:30 -04003608 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003609 /*
3610 * by releasing the path above we dropped all our locks. A balance
3611 * could have added more items next to the key that used to be
3612 * at the very end of the block. So, check again here and
3613 * advance the path if there are now more items available.
3614 */
Chris Masona2135012008-06-25 16:01:30 -04003615 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003616 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003617 goto done;
3618 }
Chris Masond97e63b2007-02-20 16:40:44 -05003619
Chris Mason234b63a2007-03-13 10:46:10 -04003620 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003621 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003622 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003623
Chris Masond97e63b2007-02-20 16:40:44 -05003624 slot = path->slots[level] + 1;
3625 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003626 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003627 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003628 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003629 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003630 }
Chris Masond97e63b2007-02-20 16:40:44 -05003631 continue;
3632 }
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Chris Mason925baed2008-06-25 16:01:30 -04003634 if (next) {
3635 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003636 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003637 }
Chris Mason5f39d392007-10-15 16:14:19 -04003638
Chris Mason0bd40a72008-07-17 12:54:43 -04003639 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3640 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003641 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003642
Chris Masonca7a79a2008-05-12 12:59:19 -04003643 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003644 if (!path->skip_locking) {
3645 WARN_ON(!btrfs_tree_locked(c));
3646 btrfs_tree_lock(next);
3647 }
Chris Masond97e63b2007-02-20 16:40:44 -05003648 break;
3649 }
3650 path->slots[level] = slot;
3651 while(1) {
3652 level--;
3653 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003654 if (path->locks[level])
3655 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003656 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003657 path->nodes[level] = next;
3658 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003659 if (!path->skip_locking)
3660 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003661 if (!level)
3662 break;
Chris Mason925baed2008-06-25 16:01:30 -04003663 if (level == 1 && path->locks[1] && path->reada)
3664 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003665 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003666 if (!path->skip_locking) {
3667 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3668 btrfs_tree_lock(next);
3669 }
Chris Masond97e63b2007-02-20 16:40:44 -05003670 }
Chris Mason925baed2008-06-25 16:01:30 -04003671done:
3672 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003673 return 0;
3674}
Chris Mason0b86a832008-03-24 15:01:56 -04003675
Chris Mason3f157a22008-06-25 16:01:31 -04003676/*
3677 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3678 * searching until it gets past min_objectid or finds an item of 'type'
3679 *
3680 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3681 */
Chris Mason0b86a832008-03-24 15:01:56 -04003682int btrfs_previous_item(struct btrfs_root *root,
3683 struct btrfs_path *path, u64 min_objectid,
3684 int type)
3685{
3686 struct btrfs_key found_key;
3687 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003688 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003689 int ret;
3690
3691 while(1) {
3692 if (path->slots[0] == 0) {
3693 ret = btrfs_prev_leaf(root, path);
3694 if (ret != 0)
3695 return ret;
3696 } else {
3697 path->slots[0]--;
3698 }
3699 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003700 nritems = btrfs_header_nritems(leaf);
3701 if (nritems == 0)
3702 return 1;
3703 if (path->slots[0] == nritems)
3704 path->slots[0]--;
3705
Chris Mason0b86a832008-03-24 15:01:56 -04003706 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3707 if (found_key.type == type)
3708 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003709 if (found_key.objectid < min_objectid)
3710 break;
3711 if (found_key.objectid == min_objectid &&
3712 found_key.type < type)
3713 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003714 }
3715 return 1;
3716}