blob: 19c0dd33b1e8e5f7639c8cc98f70b7c2c07e7bec [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
Yan Zheng2b820322008-11-17 21:11:30 -0500188 write_extent_buffer(cow, root->fs_info->fsid,
189 (unsigned long)btrfs_header_fsid(cow),
190 BTRFS_FSID_SIZE);
191
Chris Masonbe20aa92007-12-17 20:14:01 -0500192 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400193 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500194 kfree(new_root);
195
Chris Masonbe20aa92007-12-17 20:14:01 -0500196 if (ret)
197 return ret;
198
199 btrfs_mark_buffer_dirty(cow);
200 *cow_ret = cow;
201 return 0;
202}
203
Chris Masond352ac62008-09-29 15:18:18 -0400204/*
205 * does the dirty work in cow of a single block. The parent block
206 * (if supplied) is updated to point to the new cow copy. The new
207 * buffer is marked dirty and returned locked. If you modify the block
208 * it needs to be marked dirty again.
209 *
210 * search_start -- an allocation hint for the new block
211 *
212 * empty_size -- a hint that you plan on doing more cow. This is the size in bytes
213 * the allocator should try to find free next to the block it returns. This is
214 * just a hint and may be ignored by the allocator.
215 *
216 * prealloc_dest -- if you have already reserved a destination for the cow,
217 * this uses that block instead of allocating a new one. btrfs_alloc_reserved_extent
218 * is used to finish the allocation.
219 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500220static int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400221 struct btrfs_root *root,
222 struct extent_buffer *buf,
223 struct extent_buffer *parent, int parent_slot,
224 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400225 u64 search_start, u64 empty_size,
226 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400227{
Zheng Yan31840ae2008-09-23 13:14:14 -0400228 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400229 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500230 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400231 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500232 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400233 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400234
Chris Mason925baed2008-06-25 16:01:30 -0400235 if (*cow_ret == buf)
236 unlock_orig = 1;
237
238 WARN_ON(!btrfs_tree_locked(buf));
239
Zheng Yan31840ae2008-09-23 13:14:14 -0400240 if (parent)
241 parent_start = parent->start;
242 else
243 parent_start = 0;
244
Chris Mason7bb86312007-12-11 09:25:06 -0500245 WARN_ON(root->ref_cows && trans->transid !=
246 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400247 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400248
Chris Mason7bb86312007-12-11 09:25:06 -0500249 level = btrfs_header_level(buf);
250 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400251
Chris Mason65b51a02008-08-01 15:11:20 -0400252 if (prealloc_dest) {
253 struct btrfs_key ins;
254
255 ins.objectid = prealloc_dest;
256 ins.offset = buf->len;
257 ins.type = BTRFS_EXTENT_ITEM_KEY;
258
Zheng Yan31840ae2008-09-23 13:14:14 -0400259 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400260 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400261 trans->transid, level, &ins);
Chris Mason65b51a02008-08-01 15:11:20 -0400262 BUG_ON(ret);
263 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
264 buf->len);
265 } else {
266 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400267 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400268 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400269 trans->transid, level,
270 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400271 }
Chris Mason6702ed42007-08-07 16:15:09 -0400272 if (IS_ERR(cow))
273 return PTR_ERR(cow);
274
Chris Mason5f39d392007-10-15 16:14:19 -0400275 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400276 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400277 btrfs_set_header_generation(cow, trans->transid);
278 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400279 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400280
Yan Zheng2b820322008-11-17 21:11:30 -0500281 write_extent_buffer(cow, root->fs_info->fsid,
282 (unsigned long)btrfs_header_fsid(cow),
283 BTRFS_FSID_SIZE);
284
Chris Mason5f39d392007-10-15 16:14:19 -0400285 WARN_ON(btrfs_header_generation(buf) > trans->transid);
286 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400287 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400288 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400289 if (ret)
290 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400291
292 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
293 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400294 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
295 /*
296 * There are only two places that can drop reference to
297 * tree blocks owned by living reloc trees, one is here,
Yan Zhengf82d02d2008-10-29 14:49:05 -0400298 * the other place is btrfs_drop_subtree. In both places,
Zheng Yan1a40e232008-09-26 10:09:34 -0400299 * we check reference count while tree block is locked.
300 * Furthermore, if reference count is one, it won't get
301 * increased by someone else.
302 */
303 u32 refs;
304 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
305 buf->len, &refs);
306 BUG_ON(ret);
307 if (refs == 1) {
308 ret = btrfs_update_ref(trans, root, buf, cow,
309 0, nritems);
310 clean_tree_block(trans, root, buf);
311 } else {
312 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
313 }
314 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400315 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400316 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
317 if (ret)
318 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400319 clean_tree_block(trans, root, buf);
320 }
321
Zheng Yan1a40e232008-09-26 10:09:34 -0400322 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Zheng Yan1a40e232008-09-26 10:09:34 -0400323 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
324 WARN_ON(ret);
325 }
326
Chris Mason6702ed42007-08-07 16:15:09 -0400327 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400328 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400329
330 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400331 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400332 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400333 spin_unlock(&root->node_lock);
334
Chris Mason6702ed42007-08-07 16:15:09 -0400335 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400336 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400337 buf->len, buf->start,
338 root->root_key.objectid,
339 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400340 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400341 }
Chris Mason5f39d392007-10-15 16:14:19 -0400342 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400343 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400344 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400345 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400346 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500347 WARN_ON(trans->transid == 0);
348 btrfs_set_node_ptr_generation(parent, parent_slot,
349 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400350 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400351 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500352 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400353 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400354 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400355 }
Chris Mason925baed2008-06-25 16:01:30 -0400356 if (unlock_orig)
357 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400358 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400359 btrfs_mark_buffer_dirty(cow);
360 *cow_ret = cow;
361 return 0;
362}
363
Chris Masond352ac62008-09-29 15:18:18 -0400364/*
365 * cows a single block, see __btrfs_cow_block for the real work.
366 * This version of it has extra checks so that a block isn't cow'd more than
367 * once per transaction, as long as it hasn't been written yet
368 */
Chris Masone02119d2008-09-05 16:13:11 -0400369int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400370 struct btrfs_root *root, struct extent_buffer *buf,
371 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400372 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500373{
Chris Mason6702ed42007-08-07 16:15:09 -0400374 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400375 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500376
Chris Masonccd467d2007-06-28 15:57:36 -0400377 if (trans->transaction != root->fs_info->running_transaction) {
378 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
379 root->fs_info->running_transaction->transid);
380 WARN_ON(1);
381 }
382 if (trans->transid != root->fs_info->generation) {
383 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
384 root->fs_info->generation);
385 WARN_ON(1);
386 }
Chris Masondc17ff82008-01-08 15:46:30 -0500387
Chris Mason63b10fc2008-04-01 11:21:32 -0400388 spin_lock(&root->fs_info->hash_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400389 if (btrfs_header_generation(buf) == trans->transid &&
390 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400391 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500392 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400393 spin_unlock(&root->fs_info->hash_lock);
Chris Mason65b51a02008-08-01 15:11:20 -0400394 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500395 return 0;
396 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400397 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400398 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400399 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400400 parent_slot, cow_ret, search_start, 0,
401 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400402 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400403}
404
Chris Masond352ac62008-09-29 15:18:18 -0400405/*
406 * helper function for defrag to decide if two blocks pointed to by a
407 * node are actually close by
408 */
Chris Mason6b800532007-10-15 16:17:34 -0400409static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400410{
Chris Mason6b800532007-10-15 16:17:34 -0400411 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400412 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400413 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400414 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500415 return 0;
416}
417
Chris Mason081e9572007-11-06 10:26:24 -0500418/*
419 * compare two keys in a memcmp fashion
420 */
421static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
422{
423 struct btrfs_key k1;
424
425 btrfs_disk_key_to_cpu(&k1, disk);
426
427 if (k1.objectid > k2->objectid)
428 return 1;
429 if (k1.objectid < k2->objectid)
430 return -1;
431 if (k1.type > k2->type)
432 return 1;
433 if (k1.type < k2->type)
434 return -1;
435 if (k1.offset > k2->offset)
436 return 1;
437 if (k1.offset < k2->offset)
438 return -1;
439 return 0;
440}
441
Josef Bacikf3465ca2008-11-12 14:19:50 -0500442/*
443 * same as comp_keys only with two btrfs_key's
444 */
445static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
446{
447 if (k1->objectid > k2->objectid)
448 return 1;
449 if (k1->objectid < k2->objectid)
450 return -1;
451 if (k1->type > k2->type)
452 return 1;
453 if (k1->type < k2->type)
454 return -1;
455 if (k1->offset > k2->offset)
456 return 1;
457 if (k1->offset < k2->offset)
458 return -1;
459 return 0;
460}
Chris Mason081e9572007-11-06 10:26:24 -0500461
Chris Masond352ac62008-09-29 15:18:18 -0400462/*
463 * this is used by the defrag code to go through all the
464 * leaves pointed to by a node and reallocate them so that
465 * disk order is close to key order
466 */
Chris Mason6702ed42007-08-07 16:15:09 -0400467int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400468 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400469 int start_slot, int cache_only, u64 *last_ret,
470 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400471{
Chris Mason6b800532007-10-15 16:17:34 -0400472 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400473 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400474 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400475 u64 search_start = *last_ret;
476 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400477 u64 other;
478 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400479 int end_slot;
480 int i;
481 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400482 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400483 int uptodate;
484 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500485 int progress_passed = 0;
486 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400487
Chris Mason5708b952007-10-25 15:43:18 -0400488 parent_level = btrfs_header_level(parent);
489 if (cache_only && parent_level != 1)
490 return 0;
491
Chris Mason6702ed42007-08-07 16:15:09 -0400492 if (trans->transaction != root->fs_info->running_transaction) {
493 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
494 root->fs_info->running_transaction->transid);
495 WARN_ON(1);
496 }
497 if (trans->transid != root->fs_info->generation) {
498 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
499 root->fs_info->generation);
500 WARN_ON(1);
501 }
Chris Mason86479a02007-09-10 19:58:16 -0400502
Chris Mason6b800532007-10-15 16:17:34 -0400503 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400504 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400505 end_slot = parent_nritems;
506
507 if (parent_nritems == 1)
508 return 0;
509
510 for (i = start_slot; i < end_slot; i++) {
511 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400512
Chris Mason5708b952007-10-25 15:43:18 -0400513 if (!parent->map_token) {
514 map_extent_buffer(parent,
515 btrfs_node_key_ptr_offset(i),
516 sizeof(struct btrfs_key_ptr),
517 &parent->map_token, &parent->kaddr,
518 &parent->map_start, &parent->map_len,
519 KM_USER1);
520 }
Chris Mason081e9572007-11-06 10:26:24 -0500521 btrfs_node_key(parent, &disk_key, i);
522 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
523 continue;
524
525 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400526 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400527 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400528 if (last_block == 0)
529 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400530
Chris Mason6702ed42007-08-07 16:15:09 -0400531 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400532 other = btrfs_node_blockptr(parent, i - 1);
533 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400534 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400535 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400536 other = btrfs_node_blockptr(parent, i + 1);
537 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400538 }
Chris Masone9d0b132007-08-10 14:06:19 -0400539 if (close) {
540 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400541 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400542 }
Chris Mason5708b952007-10-25 15:43:18 -0400543 if (parent->map_token) {
544 unmap_extent_buffer(parent, parent->map_token,
545 KM_USER1);
546 parent->map_token = NULL;
547 }
Chris Mason6702ed42007-08-07 16:15:09 -0400548
Chris Mason6b800532007-10-15 16:17:34 -0400549 cur = btrfs_find_tree_block(root, blocknr, blocksize);
550 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400551 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400552 else
553 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400554 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400555 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400556 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400557 continue;
558 }
Chris Mason6b800532007-10-15 16:17:34 -0400559 if (!cur) {
560 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400561 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400562 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400563 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400564 }
Chris Mason6702ed42007-08-07 16:15:09 -0400565 }
Chris Masone9d0b132007-08-10 14:06:19 -0400566 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400567 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400568
Chris Masone7a84562008-06-25 16:01:31 -0400569 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400570 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400571 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400572 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400573 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400574 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400575 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400576 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400577 break;
Yan252c38f2007-08-29 09:11:44 -0400578 }
Chris Masone7a84562008-06-25 16:01:31 -0400579 search_start = cur->start;
580 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400581 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400582 btrfs_tree_unlock(cur);
583 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400584 }
Chris Mason5708b952007-10-25 15:43:18 -0400585 if (parent->map_token) {
586 unmap_extent_buffer(parent, parent->map_token,
587 KM_USER1);
588 parent->map_token = NULL;
589 }
Chris Mason6702ed42007-08-07 16:15:09 -0400590 return err;
591}
592
Chris Mason74123bd2007-02-02 11:05:29 -0500593/*
594 * The leaf data grows from end-to-front in the node.
595 * this returns the address of the start of the last item,
596 * which is the stop of the leaf data stack
597 */
Chris Mason123abc82007-03-14 14:14:43 -0400598static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400599 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500600{
Chris Mason5f39d392007-10-15 16:14:19 -0400601 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500602 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400603 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400604 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500605}
606
Chris Masond352ac62008-09-29 15:18:18 -0400607/*
608 * extra debugging checks to make sure all the items in a key are
609 * well formed and in the proper order
610 */
Chris Mason123abc82007-03-14 14:14:43 -0400611static int check_node(struct btrfs_root *root, struct btrfs_path *path,
612 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500613{
Chris Mason5f39d392007-10-15 16:14:19 -0400614 struct extent_buffer *parent = NULL;
615 struct extent_buffer *node = path->nodes[level];
616 struct btrfs_disk_key parent_key;
617 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500618 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400619 int slot;
620 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400621 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500622
623 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400624 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400625
Chris Mason8d7be552007-05-10 11:24:42 -0400626 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400627 BUG_ON(nritems == 0);
628 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400629 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400630 btrfs_node_key(parent, &parent_key, parent_slot);
631 btrfs_node_key(node, &node_key, 0);
632 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400633 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400634 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400635 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500636 }
Chris Mason123abc82007-03-14 14:14:43 -0400637 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400638 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400639 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
640 btrfs_node_key(node, &node_key, slot);
641 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400642 }
643 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400644 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
645 btrfs_node_key(node, &node_key, slot);
646 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500647 }
648 return 0;
649}
650
Chris Masond352ac62008-09-29 15:18:18 -0400651/*
652 * extra checking to make sure all the items in a leaf are
653 * well formed and in the proper order
654 */
Chris Mason123abc82007-03-14 14:14:43 -0400655static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
656 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500657{
Chris Mason5f39d392007-10-15 16:14:19 -0400658 struct extent_buffer *leaf = path->nodes[level];
659 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500660 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400661 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400662 struct btrfs_disk_key parent_key;
663 struct btrfs_disk_key leaf_key;
664 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400665
Chris Mason5f39d392007-10-15 16:14:19 -0400666 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500667
668 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400669 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400670
671 if (nritems == 0)
672 return 0;
673
674 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400675 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400676 btrfs_node_key(parent, &parent_key, parent_slot);
677 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400678
Chris Mason5f39d392007-10-15 16:14:19 -0400679 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400680 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400681 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400682 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500683 }
Chris Mason5f39d392007-10-15 16:14:19 -0400684#if 0
685 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
686 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
687 btrfs_item_key(leaf, &leaf_key, i);
688 if (comp_keys(&leaf_key, &cpukey) >= 0) {
689 btrfs_print_leaf(root, leaf);
690 printk("slot %d offset bad key\n", i);
691 BUG_ON(1);
692 }
693 if (btrfs_item_offset_nr(leaf, i) !=
694 btrfs_item_end_nr(leaf, i + 1)) {
695 btrfs_print_leaf(root, leaf);
696 printk("slot %d offset bad\n", i);
697 BUG_ON(1);
698 }
699 if (i == 0) {
700 if (btrfs_item_offset_nr(leaf, i) +
701 btrfs_item_size_nr(leaf, i) !=
702 BTRFS_LEAF_DATA_SIZE(root)) {
703 btrfs_print_leaf(root, leaf);
704 printk("slot %d first offset bad\n", i);
705 BUG_ON(1);
706 }
707 }
708 }
709 if (nritems > 0) {
710 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
711 btrfs_print_leaf(root, leaf);
712 printk("slot %d bad size \n", nritems - 1);
713 BUG_ON(1);
714 }
715 }
716#endif
717 if (slot != 0 && slot < nritems - 1) {
718 btrfs_item_key(leaf, &leaf_key, slot);
719 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
720 if (comp_keys(&leaf_key, &cpukey) <= 0) {
721 btrfs_print_leaf(root, leaf);
722 printk("slot %d offset bad key\n", slot);
723 BUG_ON(1);
724 }
725 if (btrfs_item_offset_nr(leaf, slot - 1) !=
726 btrfs_item_end_nr(leaf, slot)) {
727 btrfs_print_leaf(root, leaf);
728 printk("slot %d offset bad\n", slot);
729 BUG_ON(1);
730 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500731 }
Chris Mason8d7be552007-05-10 11:24:42 -0400732 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400733 btrfs_item_key(leaf, &leaf_key, slot);
734 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
735 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
736 if (btrfs_item_offset_nr(leaf, slot) !=
737 btrfs_item_end_nr(leaf, slot + 1)) {
738 btrfs_print_leaf(root, leaf);
739 printk("slot %d offset bad\n", slot);
740 BUG_ON(1);
741 }
Chris Mason8d7be552007-05-10 11:24:42 -0400742 }
Chris Mason5f39d392007-10-15 16:14:19 -0400743 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
744 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500745 return 0;
746}
747
Chris Mason98ed5172008-01-03 10:01:48 -0500748static int noinline check_block(struct btrfs_root *root,
749 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500750{
Chris Masonf1885912008-04-09 16:28:12 -0400751 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400752 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400753 if (btrfs_header_level(path->nodes[level]) != level)
754 printk("warning: bad level %Lu wanted %d found %d\n",
755 path->nodes[level]->start, level,
756 btrfs_header_level(path->nodes[level]));
757 found_start = btrfs_header_bytenr(path->nodes[level]);
758 if (found_start != path->nodes[level]->start) {
759 printk("warning: bad bytentr %Lu found %Lu\n",
760 path->nodes[level]->start, found_start);
761 }
Chris Masondb945352007-10-15 16:15:53 -0400762#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400763 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400764
Chris Mason479965d2007-10-15 16:14:27 -0400765 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
766 (unsigned long)btrfs_header_fsid(buf),
767 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400768 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400769 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400770 }
Chris Masondb945352007-10-15 16:15:53 -0400771#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500772 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400773 return check_leaf(root, path, level);
774 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500775}
776
Chris Mason74123bd2007-02-02 11:05:29 -0500777/*
Chris Mason5f39d392007-10-15 16:14:19 -0400778 * search for key in the extent_buffer. The items start at offset p,
779 * and they are item_size apart. There are 'max' items in p.
780 *
Chris Mason74123bd2007-02-02 11:05:29 -0500781 * the slot in the array is returned via slot, and it points to
782 * the place where you would insert key if it is not found in
783 * the array.
784 *
785 * slot may point to max if the key is bigger than all of the keys
786 */
Chris Masone02119d2008-09-05 16:13:11 -0400787static noinline int generic_bin_search(struct extent_buffer *eb,
788 unsigned long p,
789 int item_size, struct btrfs_key *key,
790 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500791{
792 int low = 0;
793 int high = max;
794 int mid;
795 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400796 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400797 struct btrfs_disk_key unaligned;
798 unsigned long offset;
799 char *map_token = NULL;
800 char *kaddr = NULL;
801 unsigned long map_start = 0;
802 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400803 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500804
805 while(low < high) {
806 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400807 offset = p + mid * item_size;
808
809 if (!map_token || offset < map_start ||
810 (offset + sizeof(struct btrfs_disk_key)) >
811 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400812 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400813 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400814 map_token = NULL;
815 }
Chris Mason934d3752008-12-08 16:43:10 -0500816
817 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400818 sizeof(struct btrfs_disk_key),
819 &map_token, &kaddr,
820 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400821
Chris Mason479965d2007-10-15 16:14:27 -0400822 if (!err) {
823 tmp = (struct btrfs_disk_key *)(kaddr + offset -
824 map_start);
825 } else {
826 read_extent_buffer(eb, &unaligned,
827 offset, sizeof(unaligned));
828 tmp = &unaligned;
829 }
830
Chris Mason5f39d392007-10-15 16:14:19 -0400831 } else {
832 tmp = (struct btrfs_disk_key *)(kaddr + offset -
833 map_start);
834 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500835 ret = comp_keys(tmp, key);
836
837 if (ret < 0)
838 low = mid + 1;
839 else if (ret > 0)
840 high = mid;
841 else {
842 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400843 if (map_token)
844 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500845 return 0;
846 }
847 }
848 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400849 if (map_token)
850 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500851 return 1;
852}
853
Chris Mason97571fd2007-02-24 13:39:08 -0500854/*
855 * simple bin_search frontend that does the right thing for
856 * leaves vs nodes
857 */
Chris Mason5f39d392007-10-15 16:14:19 -0400858static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
859 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500860{
Chris Mason5f39d392007-10-15 16:14:19 -0400861 if (level == 0) {
862 return generic_bin_search(eb,
863 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400864 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400865 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400866 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500867 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400868 return generic_bin_search(eb,
869 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400870 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400871 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400872 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500873 }
874 return -1;
875}
876
Chris Masond352ac62008-09-29 15:18:18 -0400877/* given a node and slot number, this reads the blocks it points to. The
878 * extent buffer is returned with a reference taken (but unlocked).
879 * NULL is returned on error.
880 */
Chris Masone02119d2008-09-05 16:13:11 -0400881static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400882 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500883{
Chris Masonca7a79a2008-05-12 12:59:19 -0400884 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500885 if (slot < 0)
886 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400887 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500888 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400889
890 BUG_ON(level == 0);
891
Chris Masondb945352007-10-15 16:15:53 -0400892 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400893 btrfs_level_size(root, level - 1),
894 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500895}
896
Chris Masond352ac62008-09-29 15:18:18 -0400897/*
898 * node level balancing, used to make sure nodes are in proper order for
899 * item deletion. We balance from the top down, so we have to make sure
900 * that a deletion won't leave an node completely empty later on.
901 */
Chris Masone02119d2008-09-05 16:13:11 -0400902static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500903 struct btrfs_root *root,
904 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500905{
Chris Mason5f39d392007-10-15 16:14:19 -0400906 struct extent_buffer *right = NULL;
907 struct extent_buffer *mid;
908 struct extent_buffer *left = NULL;
909 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500910 int ret = 0;
911 int wret;
912 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500913 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400914 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500915 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500916
917 if (level == 0)
918 return 0;
919
Chris Mason5f39d392007-10-15 16:14:19 -0400920 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400921 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500922 WARN_ON(btrfs_header_generation(mid) != trans->transid);
923
Chris Mason1d4f8a02007-03-13 09:28:32 -0400924 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500925
Chris Mason234b63a2007-03-13 10:46:10 -0400926 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400927 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500928 pslot = path->slots[level + 1];
929
Chris Mason40689472007-03-17 14:29:23 -0400930 /*
931 * deal with the case where there is only one pointer in the root
932 * by promoting the node below to a root
933 */
Chris Mason5f39d392007-10-15 16:14:19 -0400934 if (!parent) {
935 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500936
Chris Mason5f39d392007-10-15 16:14:19 -0400937 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500938 return 0;
939
940 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400941 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400942 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500943 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400944 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500945 BUG_ON(ret);
946
Chris Mason925baed2008-06-25 16:01:30 -0400947 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500948 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400949 spin_unlock(&root->node_lock);
950
Zheng Yan31840ae2008-09-23 13:14:14 -0400951 ret = btrfs_update_extent_ref(trans, root, child->start,
952 mid->start, child->start,
953 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400954 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400955 BUG_ON(ret);
956
Chris Mason0b86a832008-03-24 15:01:56 -0400957 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400958 btrfs_tree_unlock(child);
959 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500960 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400961 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400962 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500963 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400964 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500965 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400966 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400967 btrfs_header_generation(mid),
968 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500969 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400970 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400971 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500972 }
Chris Mason5f39d392007-10-15 16:14:19 -0400973 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400974 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500975 return 0;
976
Chris Mason5f39d392007-10-15 16:14:19 -0400977 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400978 err_on_enospc = 1;
979
Chris Mason5f39d392007-10-15 16:14:19 -0400980 left = read_node_slot(root, parent, pslot - 1);
981 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400982 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400983 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400984 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400985 if (wret) {
986 ret = wret;
987 goto enospc;
988 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400989 }
Chris Mason5f39d392007-10-15 16:14:19 -0400990 right = read_node_slot(root, parent, pslot + 1);
991 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400992 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400993 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400994 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400995 if (wret) {
996 ret = wret;
997 goto enospc;
998 }
999 }
1000
1001 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001002 if (left) {
1003 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001004 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001005 if (wret < 0)
1006 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001007 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001008 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001009 }
Chris Mason79f95c82007-03-01 15:16:26 -05001010
1011 /*
1012 * then try to empty the right most buffer into the middle
1013 */
Chris Mason5f39d392007-10-15 16:14:19 -04001014 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001015 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001016 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001017 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001018 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001019 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -05001020 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001021 u32 blocksize = right->len;
1022
Chris Mason5f39d392007-10-15 16:14:19 -04001023 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001024 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001025 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001026 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001027 wret = del_ptr(trans, root, path, level + 1, pslot +
1028 1);
Chris Masonbb803952007-03-01 12:04:21 -05001029 if (wret)
1030 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001031 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001032 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001033 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001034 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001035 if (wret)
1036 ret = wret;
1037 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001038 struct btrfs_disk_key right_key;
1039 btrfs_node_key(right, &right_key, 0);
1040 btrfs_set_node_key(parent, &right_key, pslot + 1);
1041 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001042 }
1043 }
Chris Mason5f39d392007-10-15 16:14:19 -04001044 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001045 /*
1046 * we're not allowed to leave a node with one item in the
1047 * tree during a delete. A deletion from lower in the tree
1048 * could try to delete the only pointer in this node.
1049 * So, pull some keys from the left.
1050 * There has to be a left pointer at this point because
1051 * otherwise we would have pulled some pointers from the
1052 * right
1053 */
Chris Mason5f39d392007-10-15 16:14:19 -04001054 BUG_ON(!left);
1055 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001056 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001057 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001058 goto enospc;
1059 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001060 if (wret == 1) {
1061 wret = push_node_left(trans, root, left, mid, 1);
1062 if (wret < 0)
1063 ret = wret;
1064 }
Chris Mason79f95c82007-03-01 15:16:26 -05001065 BUG_ON(wret == 1);
1066 }
Chris Mason5f39d392007-10-15 16:14:19 -04001067 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001068 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001069 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001070 u64 bytenr = mid->start;
1071 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001072
Chris Mason5f39d392007-10-15 16:14:19 -04001073 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001074 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001075 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001076 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001077 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001078 if (wret)
1079 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001080 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001081 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001082 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001083 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001084 if (wret)
1085 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001086 } else {
1087 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001088 struct btrfs_disk_key mid_key;
1089 btrfs_node_key(mid, &mid_key, 0);
1090 btrfs_set_node_key(parent, &mid_key, pslot);
1091 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001092 }
Chris Masonbb803952007-03-01 12:04:21 -05001093
Chris Mason79f95c82007-03-01 15:16:26 -05001094 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001095 if (left) {
1096 if (btrfs_header_nritems(left) > orig_slot) {
1097 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001098 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001099 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001100 path->slots[level + 1] -= 1;
1101 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001102 if (mid) {
1103 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001104 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001105 }
Chris Masonbb803952007-03-01 12:04:21 -05001106 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001107 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001108 path->slots[level] = orig_slot;
1109 }
1110 }
Chris Mason79f95c82007-03-01 15:16:26 -05001111 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001112 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001113 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001114 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001115 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001116enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001117 if (right) {
1118 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001119 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001120 }
1121 if (left) {
1122 if (path->nodes[level] != left)
1123 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001124 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001125 }
Chris Masonbb803952007-03-01 12:04:21 -05001126 return ret;
1127}
1128
Chris Masond352ac62008-09-29 15:18:18 -04001129/* Node balancing for insertion. Here we only split or push nodes around
1130 * when they are completely full. This is also done top down, so we
1131 * have to be pessimistic.
1132 */
Chris Mason98ed5172008-01-03 10:01:48 -05001133static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1134 struct btrfs_root *root,
1135 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001136{
Chris Mason5f39d392007-10-15 16:14:19 -04001137 struct extent_buffer *right = NULL;
1138 struct extent_buffer *mid;
1139 struct extent_buffer *left = NULL;
1140 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001141 int ret = 0;
1142 int wret;
1143 int pslot;
1144 int orig_slot = path->slots[level];
1145 u64 orig_ptr;
1146
1147 if (level == 0)
1148 return 1;
1149
Chris Mason5f39d392007-10-15 16:14:19 -04001150 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001151 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001152 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1153
1154 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001155 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001156 pslot = path->slots[level + 1];
1157
Chris Mason5f39d392007-10-15 16:14:19 -04001158 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001159 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001160
Chris Mason5f39d392007-10-15 16:14:19 -04001161 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001162
1163 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001164 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001165 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001166
1167 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001168 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001169 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1170 wret = 1;
1171 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001172 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001173 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001174 if (ret)
1175 wret = 1;
1176 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001177 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001178 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001179 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001180 }
Chris Masone66f7092007-04-20 13:16:02 -04001181 if (wret < 0)
1182 ret = wret;
1183 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001184 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001185 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001186 btrfs_node_key(mid, &disk_key, 0);
1187 btrfs_set_node_key(parent, &disk_key, pslot);
1188 btrfs_mark_buffer_dirty(parent);
1189 if (btrfs_header_nritems(left) > orig_slot) {
1190 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001191 path->slots[level + 1] -= 1;
1192 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001193 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001194 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001195 } else {
1196 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001197 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001198 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001199 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001200 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001201 }
Chris Masone66f7092007-04-20 13:16:02 -04001202 return 0;
1203 }
Chris Mason925baed2008-06-25 16:01:30 -04001204 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001205 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001206 }
Chris Mason925baed2008-06-25 16:01:30 -04001207 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001208
1209 /*
1210 * then try to empty the right most buffer into the middle
1211 */
Chris Mason5f39d392007-10-15 16:14:19 -04001212 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001213 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001214 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001215 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001216 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1217 wret = 1;
1218 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001219 ret = btrfs_cow_block(trans, root, right,
1220 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001221 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001222 if (ret)
1223 wret = 1;
1224 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001225 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001226 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001227 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001228 }
Chris Masone66f7092007-04-20 13:16:02 -04001229 if (wret < 0)
1230 ret = wret;
1231 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001232 struct btrfs_disk_key disk_key;
1233
1234 btrfs_node_key(right, &disk_key, 0);
1235 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1236 btrfs_mark_buffer_dirty(parent);
1237
1238 if (btrfs_header_nritems(mid) <= orig_slot) {
1239 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001240 path->slots[level + 1] += 1;
1241 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001242 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001243 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001244 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001245 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001246 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001247 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001248 }
Chris Masone66f7092007-04-20 13:16:02 -04001249 return 0;
1250 }
Chris Mason925baed2008-06-25 16:01:30 -04001251 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001252 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001253 }
Chris Masone66f7092007-04-20 13:16:02 -04001254 return 1;
1255}
1256
Chris Mason74123bd2007-02-02 11:05:29 -05001257/*
Chris Masond352ac62008-09-29 15:18:18 -04001258 * readahead one full node of leaves, finding things that are close
1259 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001260 */
Chris Masone02119d2008-09-05 16:13:11 -04001261static noinline void reada_for_search(struct btrfs_root *root,
1262 struct btrfs_path *path,
1263 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001264{
Chris Mason5f39d392007-10-15 16:14:19 -04001265 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001266 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001267 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001268 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001269 u64 lowest_read;
1270 u64 highest_read;
1271 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001272 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001273 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001274 u32 nr;
1275 u32 blocksize;
1276 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001277
Chris Masona6b6e752007-10-15 16:22:39 -04001278 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001279 return;
1280
Chris Mason6702ed42007-08-07 16:15:09 -04001281 if (!path->nodes[level])
1282 return;
1283
Chris Mason5f39d392007-10-15 16:14:19 -04001284 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001285
Chris Mason3c69fae2007-08-07 15:52:22 -04001286 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001287 blocksize = btrfs_level_size(root, level - 1);
1288 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001289 if (eb) {
1290 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001291 return;
1292 }
1293
Chris Mason6b800532007-10-15 16:17:34 -04001294 highest_read = search;
1295 lowest_read = search;
1296
Chris Mason5f39d392007-10-15 16:14:19 -04001297 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001298 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001299 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001300 if (direction < 0) {
1301 if (nr == 0)
1302 break;
1303 nr--;
1304 } else if (direction > 0) {
1305 nr++;
1306 if (nr >= nritems)
1307 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001308 }
Chris Mason01f46652007-12-21 16:24:26 -05001309 if (path->reada < 0 && objectid) {
1310 btrfs_node_key(node, &disk_key, nr);
1311 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1312 break;
1313 }
Chris Mason6b800532007-10-15 16:17:34 -04001314 search = btrfs_node_blockptr(node, nr);
1315 if ((search >= lowest_read && search <= highest_read) ||
Chris Mason6f3577b2008-11-13 09:59:36 -05001316 (search < lowest_read && lowest_read - search <= 16384) ||
1317 (search > highest_read && search - highest_read <= 16384)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001318 readahead_tree_block(root, search, blocksize,
1319 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001320 nread += blocksize;
1321 }
1322 nscan++;
Chris Mason6f3577b2008-11-13 09:59:36 -05001323 if (path->reada < 2 && (nread > (64 * 1024) || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001324 break;
Chris Mason6f3577b2008-11-13 09:59:36 -05001325 if(nread > (256 * 1024) || nscan > 128)
Chris Mason6b800532007-10-15 16:17:34 -04001326 break;
1327
1328 if (search < lowest_read)
1329 lowest_read = search;
1330 if (search > highest_read)
1331 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001332 }
1333}
Chris Mason925baed2008-06-25 16:01:30 -04001334
Chris Masond352ac62008-09-29 15:18:18 -04001335/*
1336 * when we walk down the tree, it is usually safe to unlock the higher layers in
1337 * the tree. The exceptions are when our path goes through slot 0, because operations
1338 * on the tree might require changing key pointers higher up in the tree.
1339 *
1340 * callers might also have set path->keep_locks, which tells this code to
1341 * keep the lock if the path points to the last slot in the block. This is
1342 * part of walking through the tree, and selecting the next slot in the higher
1343 * block.
1344 *
1345 * lowest_unlock sets the lowest level in the tree we're allowed to unlock.
1346 * so if lowest_unlock is 1, level 0 won't be unlocked
1347 */
Chris Masone02119d2008-09-05 16:13:11 -04001348static noinline void unlock_up(struct btrfs_path *path, int level,
1349 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001350{
1351 int i;
1352 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001353 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001354 struct extent_buffer *t;
1355
1356 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1357 if (!path->nodes[i])
1358 break;
1359 if (!path->locks[i])
1360 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001361 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001362 skip_level = i + 1;
1363 continue;
1364 }
Chris Mason051e1b92008-06-25 16:01:30 -04001365 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001366 u32 nritems;
1367 t = path->nodes[i];
1368 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001369 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001370 skip_level = i + 1;
1371 continue;
1372 }
1373 }
Chris Mason051e1b92008-06-25 16:01:30 -04001374 if (skip_level < i && i >= lowest_unlock)
1375 no_skips = 1;
1376
Chris Mason925baed2008-06-25 16:01:30 -04001377 t = path->nodes[i];
1378 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1379 btrfs_tree_unlock(t);
1380 path->locks[i] = 0;
1381 }
1382 }
1383}
1384
Chris Mason3c69fae2007-08-07 15:52:22 -04001385/*
Chris Mason74123bd2007-02-02 11:05:29 -05001386 * look for key in the tree. path is filled in with nodes along the way
1387 * if key is found, we return zero and you can find the item in the leaf
1388 * level of the path (level 0)
1389 *
1390 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001391 * be inserted, and 1 is returned. If there are other errors during the
1392 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001393 *
1394 * if ins_len > 0, nodes and leaves will be split as we walk down the
1395 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1396 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001397 */
Chris Masone089f052007-03-16 16:20:31 -04001398int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1399 *root, struct btrfs_key *key, struct btrfs_path *p, int
1400 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001401{
Chris Mason5f39d392007-10-15 16:14:19 -04001402 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001403 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001404 int slot;
1405 int ret;
1406 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001407 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001408 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001409 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001410 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001411 u64 blocknr;
1412 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001413 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001414
Chris Mason6702ed42007-08-07 16:15:09 -04001415 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001416 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001417 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001418
Chris Mason925baed2008-06-25 16:01:30 -04001419 if (ins_len < 0)
1420 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001421
1422 prealloc_block.objectid = 0;
1423
Chris Masonbb803952007-03-01 12:04:21 -05001424again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001425 if (p->skip_locking)
1426 b = btrfs_root_node(root);
1427 else
1428 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001429
Chris Masoneb60cea2007-02-02 09:18:22 -05001430 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001431 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001432
1433 /*
1434 * setup the path here so we can release it under lock
1435 * contention with the cow code
1436 */
1437 p->nodes[level] = b;
1438 if (!p->skip_locking)
1439 p->locks[level] = 1;
1440
Chris Mason02217ed2007-03-02 16:08:05 -05001441 if (cow) {
1442 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001443
1444 /* is a cow on this block not required */
1445 spin_lock(&root->fs_info->hash_lock);
1446 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001447 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001448 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1449 spin_unlock(&root->fs_info->hash_lock);
1450 goto cow_done;
1451 }
1452 spin_unlock(&root->fs_info->hash_lock);
1453
1454 /* ok, we have to cow, is our old prealloc the right
1455 * size?
1456 */
1457 if (prealloc_block.objectid &&
1458 prealloc_block.offset != b->len) {
1459 btrfs_free_reserved_extent(root,
1460 prealloc_block.objectid,
1461 prealloc_block.offset);
1462 prealloc_block.objectid = 0;
1463 }
1464
1465 /*
1466 * for higher level blocks, try not to allocate blocks
1467 * with the block and the parent locks held.
1468 */
1469 if (level > 1 && !prealloc_block.objectid &&
1470 btrfs_path_lock_waiting(p, level)) {
1471 u32 size = b->len;
1472 u64 hint = b->start;
1473
1474 btrfs_release_path(root, p);
1475 ret = btrfs_reserve_extent(trans, root,
1476 size, size, 0,
1477 hint, (u64)-1,
1478 &prealloc_block, 0);
1479 BUG_ON(ret);
1480 goto again;
1481 }
1482
Chris Masone20d96d2007-03-22 12:13:20 -04001483 wret = btrfs_cow_block(trans, root, b,
1484 p->nodes[level + 1],
1485 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001486 &b, prealloc_block.objectid);
1487 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001488 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001489 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001490 ret = wret;
1491 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001492 }
Chris Mason02217ed2007-03-02 16:08:05 -05001493 }
Chris Mason65b51a02008-08-01 15:11:20 -04001494cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001495 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001496 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001497 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001498 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001499
Chris Masoneb60cea2007-02-02 09:18:22 -05001500 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001501 if (!p->skip_locking)
1502 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001503
Chris Mason123abc82007-03-14 14:14:43 -04001504 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001505 if (ret) {
1506 ret = -1;
1507 goto done;
1508 }
Chris Mason925baed2008-06-25 16:01:30 -04001509
Chris Mason5f39d392007-10-15 16:14:19 -04001510 ret = bin_search(b, key, level, &slot);
1511 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001512 if (ret && slot > 0)
1513 slot -= 1;
1514 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001515 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001516 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001517 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001518 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001519 if (sret) {
1520 ret = sret;
1521 goto done;
1522 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001523 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001524 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001525 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001526 int sret = balance_level(trans, root, p,
1527 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001528 if (sret) {
1529 ret = sret;
1530 goto done;
1531 }
Chris Masonbb803952007-03-01 12:04:21 -05001532 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001533 if (!b) {
1534 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001535 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001536 }
Chris Masonbb803952007-03-01 12:04:21 -05001537 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001538 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001539 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001540 unlock_up(p, level, lowest_unlock);
1541
Chris Mason9f3a7422007-08-07 15:52:19 -04001542 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001543 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001544 ret = 0;
1545 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001546 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001547
Chris Mason594a24e2008-06-25 16:01:30 -04001548 blocknr = btrfs_node_blockptr(b, slot);
1549 gen = btrfs_node_ptr_generation(b, slot);
1550 blocksize = btrfs_level_size(root, level - 1);
1551
1552 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1553 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001554 b = tmp;
1555 } else {
1556 /*
1557 * reduce lock contention at high levels
1558 * of the btree by dropping locks before
1559 * we read.
1560 */
1561 if (level > 1) {
1562 btrfs_release_path(NULL, p);
1563 if (tmp)
1564 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001565 if (should_reada)
1566 reada_for_search(root, p,
1567 level, slot,
1568 key->objectid);
1569
Chris Mason594a24e2008-06-25 16:01:30 -04001570 tmp = read_tree_block(root, blocknr,
1571 blocksize, gen);
1572 if (tmp)
1573 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001574 goto again;
1575 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001576 if (tmp)
1577 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001578 if (should_reada)
1579 reada_for_search(root, p,
1580 level, slot,
1581 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001582 b = read_node_slot(root, b, slot);
1583 }
1584 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001585 if (!p->skip_locking)
1586 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001587 } else {
1588 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001589 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001590 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001591 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001592 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001593 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001594 if (sret) {
1595 ret = sret;
1596 goto done;
1597 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001598 }
Chris Mason925baed2008-06-25 16:01:30 -04001599 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001600 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001601 }
1602 }
Chris Mason65b51a02008-08-01 15:11:20 -04001603 ret = 1;
1604done:
1605 if (prealloc_block.objectid) {
1606 btrfs_free_reserved_extent(root,
1607 prealloc_block.objectid,
1608 prealloc_block.offset);
1609 }
1610
1611 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001612}
1613
Zheng Yan1a40e232008-09-26 10:09:34 -04001614int btrfs_merge_path(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 struct btrfs_key *node_keys,
1617 u64 *nodes, int lowest_level)
1618{
1619 struct extent_buffer *eb;
1620 struct extent_buffer *parent;
1621 struct btrfs_key key;
1622 u64 bytenr;
1623 u64 generation;
1624 u32 blocksize;
1625 int level;
1626 int slot;
1627 int key_match;
1628 int ret;
1629
1630 eb = btrfs_lock_root_node(root);
1631 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1632 BUG_ON(ret);
1633
1634 parent = eb;
1635 while (1) {
1636 level = btrfs_header_level(parent);
1637 if (level == 0 || level <= lowest_level)
1638 break;
1639
1640 ret = bin_search(parent, &node_keys[lowest_level], level,
1641 &slot);
1642 if (ret && slot > 0)
1643 slot--;
1644
1645 bytenr = btrfs_node_blockptr(parent, slot);
1646 if (nodes[level - 1] == bytenr)
1647 break;
1648
1649 blocksize = btrfs_level_size(root, level - 1);
1650 generation = btrfs_node_ptr_generation(parent, slot);
1651 btrfs_node_key_to_cpu(eb, &key, slot);
1652 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1653
Yan Zhengf82d02d2008-10-29 14:49:05 -04001654 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001655 eb = read_tree_block(root, bytenr, blocksize,
1656 generation);
1657 btrfs_tree_lock(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001658 }
1659
1660 /*
1661 * if node keys match and node pointer hasn't been modified
1662 * in the running transaction, we can merge the path. for
1663 * blocks owened by reloc trees, the node pointer check is
1664 * skipped, this is because these blocks are fully controlled
1665 * by the space balance code, no one else can modify them.
1666 */
1667 if (!nodes[level - 1] || !key_match ||
1668 (generation == trans->transid &&
1669 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1670 if (level == 1 || level == lowest_level + 1) {
1671 if (generation == trans->transid) {
1672 btrfs_tree_unlock(eb);
1673 free_extent_buffer(eb);
1674 }
1675 break;
1676 }
1677
1678 if (generation != trans->transid) {
1679 eb = read_tree_block(root, bytenr, blocksize,
1680 generation);
1681 btrfs_tree_lock(eb);
1682 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001683
1684 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1685 &eb, 0);
1686 BUG_ON(ret);
1687
Yan Zhengf82d02d2008-10-29 14:49:05 -04001688 if (root->root_key.objectid ==
1689 BTRFS_TREE_RELOC_OBJECTID) {
1690 if (!nodes[level - 1]) {
1691 nodes[level - 1] = eb->start;
1692 memcpy(&node_keys[level - 1], &key,
1693 sizeof(node_keys[0]));
1694 } else {
1695 WARN_ON(1);
1696 }
1697 }
1698
Zheng Yan1a40e232008-09-26 10:09:34 -04001699 btrfs_tree_unlock(parent);
1700 free_extent_buffer(parent);
1701 parent = eb;
1702 continue;
1703 }
1704
Zheng Yan1a40e232008-09-26 10:09:34 -04001705 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1706 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1707 btrfs_mark_buffer_dirty(parent);
1708
1709 ret = btrfs_inc_extent_ref(trans, root,
1710 nodes[level - 1],
1711 blocksize, parent->start,
1712 btrfs_header_owner(parent),
1713 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001714 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001715 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001716
1717 /*
1718 * If the block was created in the running transaction,
1719 * it's possible this is the last reference to it, so we
1720 * should drop the subtree.
1721 */
1722 if (generation == trans->transid) {
1723 ret = btrfs_drop_subtree(trans, root, eb, parent);
1724 BUG_ON(ret);
1725 btrfs_tree_unlock(eb);
1726 free_extent_buffer(eb);
1727 } else {
1728 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001729 blocksize, parent->start,
1730 btrfs_header_owner(parent),
1731 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001732 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001733 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001734 }
1735 break;
1736 }
1737 btrfs_tree_unlock(parent);
1738 free_extent_buffer(parent);
1739 return 0;
1740}
1741
Chris Mason74123bd2007-02-02 11:05:29 -05001742/*
1743 * adjust the pointers going up the tree, starting at level
1744 * making sure the right key of each node is points to 'key'.
1745 * This is used after shifting pointers to the left, so it stops
1746 * fixing up pointers when a given leaf/node is not in slot 0 of the
1747 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001748 *
1749 * If this fails to write a tree block, it returns -1, but continues
1750 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001751 */
Chris Mason5f39d392007-10-15 16:14:19 -04001752static int fixup_low_keys(struct btrfs_trans_handle *trans,
1753 struct btrfs_root *root, struct btrfs_path *path,
1754 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001755{
1756 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001757 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001758 struct extent_buffer *t;
1759
Chris Mason234b63a2007-03-13 10:46:10 -04001760 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001761 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001762 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001763 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001764 t = path->nodes[i];
1765 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001766 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001767 if (tslot != 0)
1768 break;
1769 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001770 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001771}
1772
Chris Mason74123bd2007-02-02 11:05:29 -05001773/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001774 * update item key.
1775 *
1776 * This function isn't completely safe. It's the caller's responsibility
1777 * that the new key won't break the order
1778 */
1779int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1780 struct btrfs_root *root, struct btrfs_path *path,
1781 struct btrfs_key *new_key)
1782{
1783 struct btrfs_disk_key disk_key;
1784 struct extent_buffer *eb;
1785 int slot;
1786
1787 eb = path->nodes[0];
1788 slot = path->slots[0];
1789 if (slot > 0) {
1790 btrfs_item_key(eb, &disk_key, slot - 1);
1791 if (comp_keys(&disk_key, new_key) >= 0)
1792 return -1;
1793 }
1794 if (slot < btrfs_header_nritems(eb) - 1) {
1795 btrfs_item_key(eb, &disk_key, slot + 1);
1796 if (comp_keys(&disk_key, new_key) <= 0)
1797 return -1;
1798 }
1799
1800 btrfs_cpu_key_to_disk(&disk_key, new_key);
1801 btrfs_set_item_key(eb, &disk_key, slot);
1802 btrfs_mark_buffer_dirty(eb);
1803 if (slot == 0)
1804 fixup_low_keys(trans, root, path, &disk_key, 1);
1805 return 0;
1806}
1807
1808/*
Chris Mason74123bd2007-02-02 11:05:29 -05001809 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001810 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001811 *
1812 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1813 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001814 */
Chris Mason98ed5172008-01-03 10:01:48 -05001815static int push_node_left(struct btrfs_trans_handle *trans,
1816 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001817 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001819 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001820 int src_nritems;
1821 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001822 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001823
Chris Mason5f39d392007-10-15 16:14:19 -04001824 src_nritems = btrfs_header_nritems(src);
1825 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001826 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001827 WARN_ON(btrfs_header_generation(src) != trans->transid);
1828 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001829
Chris Masonbce4eae2008-04-24 14:42:46 -04001830 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001831 return 1;
1832
Chris Masoneb60cea2007-02-02 09:18:22 -05001833 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001834 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001835 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001836
Chris Masonbce4eae2008-04-24 14:42:46 -04001837 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001838 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001839 if (push_items < src_nritems) {
1840 /* leave at least 8 pointers in the node if
1841 * we aren't going to empty it
1842 */
1843 if (src_nritems - push_items < 8) {
1844 if (push_items <= 8)
1845 return 1;
1846 push_items -= 8;
1847 }
1848 }
1849 } else
1850 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001851
Chris Mason5f39d392007-10-15 16:14:19 -04001852 copy_extent_buffer(dst, src,
1853 btrfs_node_key_ptr_offset(dst_nritems),
1854 btrfs_node_key_ptr_offset(0),
1855 push_items * sizeof(struct btrfs_key_ptr));
1856
Chris Masonbb803952007-03-01 12:04:21 -05001857 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001858 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1859 btrfs_node_key_ptr_offset(push_items),
1860 (src_nritems - push_items) *
1861 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001862 }
Chris Mason5f39d392007-10-15 16:14:19 -04001863 btrfs_set_header_nritems(src, src_nritems - push_items);
1864 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1865 btrfs_mark_buffer_dirty(src);
1866 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001867
1868 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1869 BUG_ON(ret);
1870
Chris Masonbb803952007-03-01 12:04:21 -05001871 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001872}
1873
Chris Mason97571fd2007-02-24 13:39:08 -05001874/*
Chris Mason79f95c82007-03-01 15:16:26 -05001875 * try to push data from one node into the next node right in the
1876 * tree.
1877 *
1878 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1879 * error, and > 0 if there was no room in the right hand block.
1880 *
1881 * this will only push up to 1/2 the contents of the left node over
1882 */
Chris Mason5f39d392007-10-15 16:14:19 -04001883static int balance_node_right(struct btrfs_trans_handle *trans,
1884 struct btrfs_root *root,
1885 struct extent_buffer *dst,
1886 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001887{
Chris Mason79f95c82007-03-01 15:16:26 -05001888 int push_items = 0;
1889 int max_push;
1890 int src_nritems;
1891 int dst_nritems;
1892 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001893
Chris Mason7bb86312007-12-11 09:25:06 -05001894 WARN_ON(btrfs_header_generation(src) != trans->transid);
1895 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1896
Chris Mason5f39d392007-10-15 16:14:19 -04001897 src_nritems = btrfs_header_nritems(src);
1898 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001899 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001900 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001901 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001902 }
1903
1904 if (src_nritems < 4) {
1905 return 1;
1906 }
Chris Mason79f95c82007-03-01 15:16:26 -05001907
1908 max_push = src_nritems / 2 + 1;
1909 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001910 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001911 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001912 }
Yan252c38f2007-08-29 09:11:44 -04001913
Chris Mason79f95c82007-03-01 15:16:26 -05001914 if (max_push < push_items)
1915 push_items = max_push;
1916
Chris Mason5f39d392007-10-15 16:14:19 -04001917 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1918 btrfs_node_key_ptr_offset(0),
1919 (dst_nritems) *
1920 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001921
Chris Mason5f39d392007-10-15 16:14:19 -04001922 copy_extent_buffer(dst, src,
1923 btrfs_node_key_ptr_offset(0),
1924 btrfs_node_key_ptr_offset(src_nritems - push_items),
1925 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001926
Chris Mason5f39d392007-10-15 16:14:19 -04001927 btrfs_set_header_nritems(src, src_nritems - push_items);
1928 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001929
Chris Mason5f39d392007-10-15 16:14:19 -04001930 btrfs_mark_buffer_dirty(src);
1931 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001932
1933 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1934 BUG_ON(ret);
1935
Chris Mason79f95c82007-03-01 15:16:26 -05001936 return ret;
1937}
1938
1939/*
Chris Mason97571fd2007-02-24 13:39:08 -05001940 * helper function to insert a new root level in the tree.
1941 * A new node is allocated, and a single item is inserted to
1942 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001943 *
1944 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001945 */
Chris Mason98ed5172008-01-03 10:01:48 -05001946static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001947 struct btrfs_root *root,
1948 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001949{
Chris Mason7bb86312007-12-11 09:25:06 -05001950 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001951 struct extent_buffer *lower;
1952 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001953 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001954 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001955 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001956
1957 BUG_ON(path->nodes[level]);
1958 BUG_ON(path->nodes[level-1] != root->node);
1959
Chris Mason7bb86312007-12-11 09:25:06 -05001960 lower = path->nodes[level-1];
1961 if (level == 1)
1962 btrfs_item_key(lower, &lower_key, 0);
1963 else
1964 btrfs_node_key(lower, &lower_key, 0);
1965
Zheng Yan31840ae2008-09-23 13:14:14 -04001966 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1967 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001968 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001969 if (IS_ERR(c))
1970 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001971
Chris Mason5f39d392007-10-15 16:14:19 -04001972 memset_extent_buffer(c, 0, 0, root->nodesize);
1973 btrfs_set_header_nritems(c, 1);
1974 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001975 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001976 btrfs_set_header_generation(c, trans->transid);
1977 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001978
Chris Mason5f39d392007-10-15 16:14:19 -04001979 write_extent_buffer(c, root->fs_info->fsid,
1980 (unsigned long)btrfs_header_fsid(c),
1981 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001982
1983 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1984 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1985 BTRFS_UUID_SIZE);
1986
Chris Mason5f39d392007-10-15 16:14:19 -04001987 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001988 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001989 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001990 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001991
1992 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001993
1994 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001995
Chris Mason925baed2008-06-25 16:01:30 -04001996 spin_lock(&root->node_lock);
1997 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001998 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001999 spin_unlock(&root->node_lock);
2000
Zheng Yan31840ae2008-09-23 13:14:14 -04002001 ret = btrfs_update_extent_ref(trans, root, lower->start,
2002 lower->start, c->start,
2003 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002004 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002005 BUG_ON(ret);
2006
Chris Mason925baed2008-06-25 16:01:30 -04002007 /* the super has an extra ref to root->node */
2008 free_extent_buffer(old);
2009
Chris Mason0b86a832008-03-24 15:01:56 -04002010 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002011 extent_buffer_get(c);
2012 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002013 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002014 path->slots[level] = 0;
2015 return 0;
2016}
2017
Chris Mason74123bd2007-02-02 11:05:29 -05002018/*
2019 * worker function to insert a single pointer in a node.
2020 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002021 *
Chris Mason74123bd2007-02-02 11:05:29 -05002022 * slot and level indicate where you want the key to go, and
2023 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002024 *
2025 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002026 */
Chris Masone089f052007-03-16 16:20:31 -04002027static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2028 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002029 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002030{
Chris Mason5f39d392007-10-15 16:14:19 -04002031 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002032 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002033
2034 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002035 lower = path->nodes[level];
2036 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002037 if (slot > nritems)
2038 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002039 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002040 BUG();
2041 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002042 memmove_extent_buffer(lower,
2043 btrfs_node_key_ptr_offset(slot + 1),
2044 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002045 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002046 }
Chris Mason5f39d392007-10-15 16:14:19 -04002047 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002048 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002049 WARN_ON(trans->transid == 0);
2050 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 btrfs_set_header_nritems(lower, nritems + 1);
2052 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002053 return 0;
2054}
2055
Chris Mason97571fd2007-02-24 13:39:08 -05002056/*
2057 * split the node at the specified level in path in two.
2058 * The path is corrected to point to the appropriate node after the split
2059 *
2060 * Before splitting this tries to make some room in the node by pushing
2061 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002062 *
2063 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002064 */
Chris Masone02119d2008-09-05 16:13:11 -04002065static noinline int split_node(struct btrfs_trans_handle *trans,
2066 struct btrfs_root *root,
2067 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002068{
Chris Mason5f39d392007-10-15 16:14:19 -04002069 struct extent_buffer *c;
2070 struct extent_buffer *split;
2071 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002072 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002073 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002074 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002075 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002076
Chris Mason5f39d392007-10-15 16:14:19 -04002077 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002078 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002079 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002080 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002081 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002082 if (ret)
2083 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002084 } else {
2085 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002086 c = path->nodes[level];
2087 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002088 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002089 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002090 if (ret < 0)
2091 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002092 }
Chris Masone66f7092007-04-20 13:16:02 -04002093
Chris Mason5f39d392007-10-15 16:14:19 -04002094 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002095
Chris Mason925baed2008-06-25 16:01:30 -04002096 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002097 path->nodes[level + 1]->start,
2098 root->root_key.objectid,
2099 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 if (IS_ERR(split))
2101 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002102
Chris Mason5f39d392007-10-15 16:14:19 -04002103 btrfs_set_header_flags(split, btrfs_header_flags(c));
2104 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002105 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 btrfs_set_header_generation(split, trans->transid);
2107 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002108 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002109 write_extent_buffer(split, root->fs_info->fsid,
2110 (unsigned long)btrfs_header_fsid(split),
2111 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002112 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2113 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2114 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002115
Chris Mason7518a232007-03-12 12:01:18 -04002116 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002117
2118 copy_extent_buffer(split, c,
2119 btrfs_node_key_ptr_offset(0),
2120 btrfs_node_key_ptr_offset(mid),
2121 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2122 btrfs_set_header_nritems(split, c_nritems - mid);
2123 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002124 ret = 0;
2125
Chris Mason5f39d392007-10-15 16:14:19 -04002126 btrfs_mark_buffer_dirty(c);
2127 btrfs_mark_buffer_dirty(split);
2128
2129 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002130 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002131 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002132 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002133 if (wret)
2134 ret = wret;
2135
Zheng Yan31840ae2008-09-23 13:14:14 -04002136 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2137 BUG_ON(ret);
2138
Chris Mason5de08d72007-02-24 06:24:44 -05002139 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002140 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002141 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 free_extent_buffer(c);
2143 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002144 path->slots[level + 1] += 1;
2145 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002146 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002147 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002148 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002149 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002150}
2151
Chris Mason74123bd2007-02-02 11:05:29 -05002152/*
2153 * how many bytes are required to store the items in a leaf. start
2154 * and nr indicate which items in the leaf to check. This totals up the
2155 * space used both by the item structs and the item data
2156 */
Chris Mason5f39d392007-10-15 16:14:19 -04002157static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002158{
2159 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002160 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002161 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002162
2163 if (!nr)
2164 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002165 data_len = btrfs_item_end_nr(l, start);
2166 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002167 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002168 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169 return data_len;
2170}
2171
Chris Mason74123bd2007-02-02 11:05:29 -05002172/*
Chris Masond4dbff92007-04-04 14:08:15 -04002173 * The space between the end of the leaf items and
2174 * the start of the leaf data. IOW, how much room
2175 * the leaf has left for both items and data
2176 */
Chris Masone02119d2008-09-05 16:13:11 -04002177int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2178 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002179{
Chris Mason5f39d392007-10-15 16:14:19 -04002180 int nritems = btrfs_header_nritems(leaf);
2181 int ret;
2182 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2183 if (ret < 0) {
2184 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002185 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002186 leaf_space_used(leaf, 0, nritems), nritems);
2187 }
2188 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002189}
2190
2191/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002192 * push some data in the path leaf to the right, trying to free up at
2193 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002194 *
2195 * returns 1 if the push failed because the other node didn't have enough
2196 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002197 */
Chris Masone089f052007-03-16 16:20:31 -04002198static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002199 *root, struct btrfs_path *path, int data_size,
2200 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002201{
Chris Mason5f39d392007-10-15 16:14:19 -04002202 struct extent_buffer *left = path->nodes[0];
2203 struct extent_buffer *right;
2204 struct extent_buffer *upper;
2205 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002206 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002207 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002208 int free_space;
2209 int push_space = 0;
2210 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002211 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002212 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002213 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002214 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002215 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002216 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002217 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002218
2219 slot = path->slots[1];
2220 if (!path->nodes[1]) {
2221 return 1;
2222 }
2223 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002224 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002225 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002226
Chris Masona2135012008-06-25 16:01:30 -04002227 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2228
Chris Masonca7a79a2008-05-12 12:59:19 -04002229 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002230 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002231 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002232 if (free_space < data_size + sizeof(struct btrfs_item))
2233 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002234
Chris Mason5f39d392007-10-15 16:14:19 -04002235 /* cow and double check */
2236 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002237 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002238 if (ret)
2239 goto out_unlock;
2240
Chris Mason5f39d392007-10-15 16:14:19 -04002241 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002242 if (free_space < data_size + sizeof(struct btrfs_item))
2243 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002244
2245 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002246 if (left_nritems == 0)
2247 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002248
Chris Mason34a38212007-11-07 13:31:03 -05002249 if (empty)
2250 nr = 0;
2251 else
2252 nr = 1;
2253
Zheng Yan31840ae2008-09-23 13:14:14 -04002254 if (path->slots[0] >= left_nritems)
2255 push_space += data_size + sizeof(*item);
2256
Chris Mason34a38212007-11-07 13:31:03 -05002257 i = left_nritems - 1;
2258 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002259 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002260
Zheng Yan31840ae2008-09-23 13:14:14 -04002261 if (!empty && push_items > 0) {
2262 if (path->slots[0] > i)
2263 break;
2264 if (path->slots[0] == i) {
2265 int space = btrfs_leaf_free_space(root, left);
2266 if (space + push_space * 2 > free_space)
2267 break;
2268 }
2269 }
2270
Chris Mason00ec4c52007-02-24 12:47:20 -05002271 if (path->slots[0] == i)
2272 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002273
2274 if (!left->map_token) {
2275 map_extent_buffer(left, (unsigned long)item,
2276 sizeof(struct btrfs_item),
2277 &left->map_token, &left->kaddr,
2278 &left->map_start, &left->map_len,
2279 KM_USER1);
2280 }
2281
2282 this_item_size = btrfs_item_size(left, item);
2283 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002284 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002285
Chris Mason00ec4c52007-02-24 12:47:20 -05002286 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002287 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002288 if (i == 0)
2289 break;
2290 i--;
Chris Masondb945352007-10-15 16:15:53 -04002291 }
2292 if (left->map_token) {
2293 unmap_extent_buffer(left, left->map_token, KM_USER1);
2294 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002295 }
Chris Mason5f39d392007-10-15 16:14:19 -04002296
Chris Mason925baed2008-06-25 16:01:30 -04002297 if (push_items == 0)
2298 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002299
Chris Mason34a38212007-11-07 13:31:03 -05002300 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002301 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002302
Chris Mason00ec4c52007-02-24 12:47:20 -05002303 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002304 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002305
Chris Mason5f39d392007-10-15 16:14:19 -04002306 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002307 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002308
Chris Mason00ec4c52007-02-24 12:47:20 -05002309 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002310 data_end = leaf_data_end(root, right);
2311 memmove_extent_buffer(right,
2312 btrfs_leaf_data(right) + data_end - push_space,
2313 btrfs_leaf_data(right) + data_end,
2314 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2315
Chris Mason00ec4c52007-02-24 12:47:20 -05002316 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002317 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002318 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2319 btrfs_leaf_data(left) + leaf_data_end(root, left),
2320 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002321
2322 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2323 btrfs_item_nr_offset(0),
2324 right_nritems * sizeof(struct btrfs_item));
2325
Chris Mason00ec4c52007-02-24 12:47:20 -05002326 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002327 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2328 btrfs_item_nr_offset(left_nritems - push_items),
2329 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002330
2331 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002332 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002333 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002334 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002335 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002336 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002337 if (!right->map_token) {
2338 map_extent_buffer(right, (unsigned long)item,
2339 sizeof(struct btrfs_item),
2340 &right->map_token, &right->kaddr,
2341 &right->map_start, &right->map_len,
2342 KM_USER1);
2343 }
2344 push_space -= btrfs_item_size(right, item);
2345 btrfs_set_item_offset(right, item, push_space);
2346 }
2347
2348 if (right->map_token) {
2349 unmap_extent_buffer(right, right->map_token, KM_USER1);
2350 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002351 }
Chris Mason7518a232007-03-12 12:01:18 -04002352 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002353 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002354
Chris Mason34a38212007-11-07 13:31:03 -05002355 if (left_nritems)
2356 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002357 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002358
Zheng Yan31840ae2008-09-23 13:14:14 -04002359 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2360 BUG_ON(ret);
2361
Chris Mason5f39d392007-10-15 16:14:19 -04002362 btrfs_item_key(right, &disk_key, 0);
2363 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002364 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002365
Chris Mason00ec4c52007-02-24 12:47:20 -05002366 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002367 if (path->slots[0] >= left_nritems) {
2368 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002369 if (btrfs_header_nritems(path->nodes[0]) == 0)
2370 clean_tree_block(trans, root, path->nodes[0]);
2371 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002372 free_extent_buffer(path->nodes[0]);
2373 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002374 path->slots[1] += 1;
2375 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002376 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002377 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002378 }
2379 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002380
2381out_unlock:
2382 btrfs_tree_unlock(right);
2383 free_extent_buffer(right);
2384 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002385}
Chris Mason925baed2008-06-25 16:01:30 -04002386
Chris Mason00ec4c52007-02-24 12:47:20 -05002387/*
Chris Mason74123bd2007-02-02 11:05:29 -05002388 * push some data in the path leaf to the left, trying to free up at
2389 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2390 */
Chris Masone089f052007-03-16 16:20:31 -04002391static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002392 *root, struct btrfs_path *path, int data_size,
2393 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002394{
Chris Mason5f39d392007-10-15 16:14:19 -04002395 struct btrfs_disk_key disk_key;
2396 struct extent_buffer *right = path->nodes[0];
2397 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002398 int slot;
2399 int i;
2400 int free_space;
2401 int push_space = 0;
2402 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002403 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002404 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002405 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002406 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002407 int ret = 0;
2408 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002409 u32 this_item_size;
2410 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002411
2412 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002413 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002414 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002415 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002416 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002417
Chris Mason3685f792007-10-19 09:23:27 -04002418 right_nritems = btrfs_header_nritems(right);
2419 if (right_nritems == 0) {
2420 return 1;
2421 }
2422
Chris Masona2135012008-06-25 16:01:30 -04002423 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2424
Chris Masonca7a79a2008-05-12 12:59:19 -04002425 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002426 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002427 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002428 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002429 ret = 1;
2430 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002431 }
Chris Mason02217ed2007-03-02 16:08:05 -05002432
2433 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002434 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002435 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002436 if (ret) {
2437 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002438 ret = 1;
2439 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002440 }
Chris Mason3685f792007-10-19 09:23:27 -04002441
Chris Mason123abc82007-03-14 14:14:43 -04002442 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002443 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002444 ret = 1;
2445 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002446 }
2447
Chris Mason34a38212007-11-07 13:31:03 -05002448 if (empty)
2449 nr = right_nritems;
2450 else
2451 nr = right_nritems - 1;
2452
2453 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002454 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002455 if (!right->map_token) {
2456 map_extent_buffer(right, (unsigned long)item,
2457 sizeof(struct btrfs_item),
2458 &right->map_token, &right->kaddr,
2459 &right->map_start, &right->map_len,
2460 KM_USER1);
2461 }
2462
Zheng Yan31840ae2008-09-23 13:14:14 -04002463 if (!empty && push_items > 0) {
2464 if (path->slots[0] < i)
2465 break;
2466 if (path->slots[0] == i) {
2467 int space = btrfs_leaf_free_space(root, right);
2468 if (space + push_space * 2 > free_space)
2469 break;
2470 }
2471 }
2472
Chris Masonbe0e5c02007-01-26 15:51:26 -05002473 if (path->slots[0] == i)
2474 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002475
2476 this_item_size = btrfs_item_size(right, item);
2477 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002478 break;
Chris Masondb945352007-10-15 16:15:53 -04002479
Chris Masonbe0e5c02007-01-26 15:51:26 -05002480 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002481 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002482 }
Chris Masondb945352007-10-15 16:15:53 -04002483
2484 if (right->map_token) {
2485 unmap_extent_buffer(right, right->map_token, KM_USER1);
2486 right->map_token = NULL;
2487 }
2488
Chris Masonbe0e5c02007-01-26 15:51:26 -05002489 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002490 ret = 1;
2491 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002492 }
Chris Mason34a38212007-11-07 13:31:03 -05002493 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002494 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002495
Chris Masonbe0e5c02007-01-26 15:51:26 -05002496 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002497 copy_extent_buffer(left, right,
2498 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2499 btrfs_item_nr_offset(0),
2500 push_items * sizeof(struct btrfs_item));
2501
Chris Mason123abc82007-03-14 14:14:43 -04002502 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002503 btrfs_item_offset_nr(right, push_items -1);
2504
2505 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002506 leaf_data_end(root, left) - push_space,
2507 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002508 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002509 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002510 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002511 BUG_ON(old_left_nritems < 0);
2512
Chris Masondb945352007-10-15 16:15:53 -04002513 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002514 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002515 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002516
Chris Mason5f39d392007-10-15 16:14:19 -04002517 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002518 if (!left->map_token) {
2519 map_extent_buffer(left, (unsigned long)item,
2520 sizeof(struct btrfs_item),
2521 &left->map_token, &left->kaddr,
2522 &left->map_start, &left->map_len,
2523 KM_USER1);
2524 }
2525
Chris Mason5f39d392007-10-15 16:14:19 -04002526 ioff = btrfs_item_offset(left, item);
2527 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002528 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002529 }
Chris Mason5f39d392007-10-15 16:14:19 -04002530 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002531 if (left->map_token) {
2532 unmap_extent_buffer(left, left->map_token, KM_USER1);
2533 left->map_token = NULL;
2534 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002535
2536 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002537 if (push_items > right_nritems) {
2538 printk("push items %d nr %u\n", push_items, right_nritems);
2539 WARN_ON(1);
2540 }
Chris Mason5f39d392007-10-15 16:14:19 -04002541
Chris Mason34a38212007-11-07 13:31:03 -05002542 if (push_items < right_nritems) {
2543 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2544 leaf_data_end(root, right);
2545 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2546 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2547 btrfs_leaf_data(right) +
2548 leaf_data_end(root, right), push_space);
2549
2550 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002551 btrfs_item_nr_offset(push_items),
2552 (btrfs_header_nritems(right) - push_items) *
2553 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002554 }
Yaneef1c492007-11-26 10:58:13 -05002555 right_nritems -= push_items;
2556 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002557 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002558 for (i = 0; i < right_nritems; i++) {
2559 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002560
2561 if (!right->map_token) {
2562 map_extent_buffer(right, (unsigned long)item,
2563 sizeof(struct btrfs_item),
2564 &right->map_token, &right->kaddr,
2565 &right->map_start, &right->map_len,
2566 KM_USER1);
2567 }
2568
2569 push_space = push_space - btrfs_item_size(right, item);
2570 btrfs_set_item_offset(right, item, push_space);
2571 }
2572 if (right->map_token) {
2573 unmap_extent_buffer(right, right->map_token, KM_USER1);
2574 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002575 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002576
Chris Mason5f39d392007-10-15 16:14:19 -04002577 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002578 if (right_nritems)
2579 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002580
Zheng Yan31840ae2008-09-23 13:14:14 -04002581 ret = btrfs_update_ref(trans, root, right, left,
2582 old_left_nritems, push_items);
2583 BUG_ON(ret);
2584
Chris Mason5f39d392007-10-15 16:14:19 -04002585 btrfs_item_key(right, &disk_key, 0);
2586 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002587 if (wret)
2588 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002589
2590 /* then fixup the leaf pointer in the path */
2591 if (path->slots[0] < push_items) {
2592 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002593 if (btrfs_header_nritems(path->nodes[0]) == 0)
2594 clean_tree_block(trans, root, path->nodes[0]);
2595 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002596 free_extent_buffer(path->nodes[0]);
2597 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598 path->slots[1] -= 1;
2599 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002600 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002601 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602 path->slots[0] -= push_items;
2603 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002604 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002605 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002606out:
2607 btrfs_tree_unlock(left);
2608 free_extent_buffer(left);
2609 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610}
2611
Chris Mason74123bd2007-02-02 11:05:29 -05002612/*
2613 * split the path's leaf in two, making sure there is at least data_size
2614 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002615 *
2616 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002617 */
Chris Masone02119d2008-09-05 16:13:11 -04002618static noinline int split_leaf(struct btrfs_trans_handle *trans,
2619 struct btrfs_root *root,
2620 struct btrfs_key *ins_key,
2621 struct btrfs_path *path, int data_size,
2622 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002623{
Chris Mason5f39d392007-10-15 16:14:19 -04002624 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002625 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002626 int mid;
2627 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002628 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002629 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002630 int data_copy_size;
2631 int rt_data_off;
2632 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002633 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002634 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002635 int double_split;
2636 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002637 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002638
Chris Masoncc0c5532007-10-25 15:42:57 -04002639 if (extend)
2640 space_needed = data_size;
2641
Chris Mason40689472007-03-17 14:29:23 -04002642 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002643 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002644 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002645 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002646 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002647 }
2648 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002649 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002650 if (wret < 0)
2651 return wret;
2652 }
2653 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002654
Chris Mason3685f792007-10-19 09:23:27 -04002655 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002656 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002657 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002658 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002659
Chris Mason5c680ed2007-02-22 11:39:13 -05002660 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002661 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002662 if (ret)
2663 return ret;
2664 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002665again:
2666 double_split = 0;
2667 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002668 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002669 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002670 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002671
Chris Mason925baed2008-06-25 16:01:30 -04002672 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002673 path->nodes[1]->start,
2674 root->root_key.objectid,
2675 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002676 if (IS_ERR(right)) {
2677 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002678 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002679 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002680
Chris Mason5f39d392007-10-15 16:14:19 -04002681 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002682 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002683 btrfs_set_header_generation(right, trans->transid);
2684 btrfs_set_header_owner(right, root->root_key.objectid);
2685 btrfs_set_header_level(right, 0);
2686 write_extent_buffer(right, root->fs_info->fsid,
2687 (unsigned long)btrfs_header_fsid(right),
2688 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002689
2690 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2691 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2692 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002693 if (mid <= slot) {
2694 if (nritems == 1 ||
2695 leaf_space_used(l, mid, nritems - mid) + space_needed >
2696 BTRFS_LEAF_DATA_SIZE(root)) {
2697 if (slot >= nritems) {
2698 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002699 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002700 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002701 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002702 path->slots[1] + 1, 1);
2703 if (wret)
2704 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002705
2706 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002707 free_extent_buffer(path->nodes[0]);
2708 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002709 path->slots[0] = 0;
2710 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002711 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002712 return ret;
2713 }
2714 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002715 if (mid != nritems &&
2716 leaf_space_used(l, mid, nritems - mid) +
2717 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2718 double_split = 1;
2719 }
Chris Masond4dbff92007-04-04 14:08:15 -04002720 }
2721 } else {
2722 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2723 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002724 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002725 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002726 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002727 wret = insert_ptr(trans, root, path,
2728 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002729 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002730 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002731 if (wret)
2732 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002733 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002734 free_extent_buffer(path->nodes[0]);
2735 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002736 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002737 if (path->slots[1] == 0) {
2738 wret = fixup_low_keys(trans, root,
2739 path, &disk_key, 1);
2740 if (wret)
2741 ret = wret;
2742 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002743 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002744 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002745 } else if (extend && slot == 0) {
2746 mid = 1;
2747 } else {
2748 mid = slot;
2749 if (mid != nritems &&
2750 leaf_space_used(l, mid, nritems - mid) +
2751 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2752 double_split = 1;
2753 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002754 }
Chris Masond4dbff92007-04-04 14:08:15 -04002755 }
2756 }
Chris Mason5f39d392007-10-15 16:14:19 -04002757 nritems = nritems - mid;
2758 btrfs_set_header_nritems(right, nritems);
2759 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2760
2761 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2762 btrfs_item_nr_offset(mid),
2763 nritems * sizeof(struct btrfs_item));
2764
2765 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002766 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2767 data_copy_size, btrfs_leaf_data(l) +
2768 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002769
Chris Mason5f39d392007-10-15 16:14:19 -04002770 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2771 btrfs_item_end_nr(l, mid);
2772
2773 for (i = 0; i < nritems; i++) {
2774 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002775 u32 ioff;
2776
2777 if (!right->map_token) {
2778 map_extent_buffer(right, (unsigned long)item,
2779 sizeof(struct btrfs_item),
2780 &right->map_token, &right->kaddr,
2781 &right->map_start, &right->map_len,
2782 KM_USER1);
2783 }
2784
2785 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002786 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002787 }
Chris Mason74123bd2007-02-02 11:05:29 -05002788
Chris Masondb945352007-10-15 16:15:53 -04002789 if (right->map_token) {
2790 unmap_extent_buffer(right, right->map_token, KM_USER1);
2791 right->map_token = NULL;
2792 }
2793
Chris Mason5f39d392007-10-15 16:14:19 -04002794 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002795 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002796 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002797 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2798 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002799 if (wret)
2800 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002801
2802 btrfs_mark_buffer_dirty(right);
2803 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002804 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002805
Zheng Yan31840ae2008-09-23 13:14:14 -04002806 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2807 BUG_ON(ret);
2808
Chris Masonbe0e5c02007-01-26 15:51:26 -05002809 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002810 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002811 free_extent_buffer(path->nodes[0]);
2812 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002813 path->slots[0] -= mid;
2814 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002815 } else {
2816 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002817 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002818 }
Chris Mason5f39d392007-10-15 16:14:19 -04002819
Chris Masoneb60cea2007-02-02 09:18:22 -05002820 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002821
Chris Masoncc0c5532007-10-25 15:42:57 -04002822 if (double_split) {
2823 BUG_ON(num_doubles != 0);
2824 num_doubles++;
2825 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002826 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002827 return ret;
2828}
2829
Chris Masond352ac62008-09-29 15:18:18 -04002830/*
2831 * make the item pointed to by the path smaller. new_size indicates
2832 * how small to make it, and from_end tells us if we just chop bytes
2833 * off the end of the item or if we shift the item to chop bytes off
2834 * the front.
2835 */
Chris Masonb18c6682007-04-17 13:26:50 -04002836int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2837 struct btrfs_root *root,
2838 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002839 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002840{
2841 int ret = 0;
2842 int slot;
2843 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002844 struct extent_buffer *leaf;
2845 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002846 u32 nritems;
2847 unsigned int data_end;
2848 unsigned int old_data_start;
2849 unsigned int old_size;
2850 unsigned int size_diff;
2851 int i;
2852
2853 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002854 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002855 slot = path->slots[0];
2856
2857 old_size = btrfs_item_size_nr(leaf, slot);
2858 if (old_size == new_size)
2859 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002860
Chris Mason5f39d392007-10-15 16:14:19 -04002861 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002862 data_end = leaf_data_end(root, leaf);
2863
Chris Mason5f39d392007-10-15 16:14:19 -04002864 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002865
Chris Masonb18c6682007-04-17 13:26:50 -04002866 size_diff = old_size - new_size;
2867
2868 BUG_ON(slot < 0);
2869 BUG_ON(slot >= nritems);
2870
2871 /*
2872 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2873 */
2874 /* first correct the data pointers */
2875 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002876 u32 ioff;
2877 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002878
2879 if (!leaf->map_token) {
2880 map_extent_buffer(leaf, (unsigned long)item,
2881 sizeof(struct btrfs_item),
2882 &leaf->map_token, &leaf->kaddr,
2883 &leaf->map_start, &leaf->map_len,
2884 KM_USER1);
2885 }
2886
Chris Mason5f39d392007-10-15 16:14:19 -04002887 ioff = btrfs_item_offset(leaf, item);
2888 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002889 }
Chris Masondb945352007-10-15 16:15:53 -04002890
2891 if (leaf->map_token) {
2892 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2893 leaf->map_token = NULL;
2894 }
2895
Chris Masonb18c6682007-04-17 13:26:50 -04002896 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002897 if (from_end) {
2898 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2899 data_end + size_diff, btrfs_leaf_data(leaf) +
2900 data_end, old_data_start + new_size - data_end);
2901 } else {
2902 struct btrfs_disk_key disk_key;
2903 u64 offset;
2904
2905 btrfs_item_key(leaf, &disk_key, slot);
2906
2907 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2908 unsigned long ptr;
2909 struct btrfs_file_extent_item *fi;
2910
2911 fi = btrfs_item_ptr(leaf, slot,
2912 struct btrfs_file_extent_item);
2913 fi = (struct btrfs_file_extent_item *)(
2914 (unsigned long)fi - size_diff);
2915
2916 if (btrfs_file_extent_type(leaf, fi) ==
2917 BTRFS_FILE_EXTENT_INLINE) {
2918 ptr = btrfs_item_ptr_offset(leaf, slot);
2919 memmove_extent_buffer(leaf, ptr,
2920 (unsigned long)fi,
2921 offsetof(struct btrfs_file_extent_item,
2922 disk_bytenr));
2923 }
2924 }
2925
2926 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2927 data_end + size_diff, btrfs_leaf_data(leaf) +
2928 data_end, old_data_start - data_end);
2929
2930 offset = btrfs_disk_key_offset(&disk_key);
2931 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2932 btrfs_set_item_key(leaf, &disk_key, slot);
2933 if (slot == 0)
2934 fixup_low_keys(trans, root, path, &disk_key, 1);
2935 }
Chris Mason5f39d392007-10-15 16:14:19 -04002936
2937 item = btrfs_item_nr(leaf, slot);
2938 btrfs_set_item_size(leaf, item, new_size);
2939 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002940
2941 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002942 if (btrfs_leaf_free_space(root, leaf) < 0) {
2943 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002944 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002945 }
Chris Masonb18c6682007-04-17 13:26:50 -04002946 return ret;
2947}
2948
Chris Masond352ac62008-09-29 15:18:18 -04002949/*
2950 * make the item pointed to by the path bigger, data_size is the new size.
2951 */
Chris Mason5f39d392007-10-15 16:14:19 -04002952int btrfs_extend_item(struct btrfs_trans_handle *trans,
2953 struct btrfs_root *root, struct btrfs_path *path,
2954 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002955{
2956 int ret = 0;
2957 int slot;
2958 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002959 struct extent_buffer *leaf;
2960 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002961 u32 nritems;
2962 unsigned int data_end;
2963 unsigned int old_data;
2964 unsigned int old_size;
2965 int i;
2966
2967 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002968 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002969
Chris Mason5f39d392007-10-15 16:14:19 -04002970 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002971 data_end = leaf_data_end(root, leaf);
2972
Chris Mason5f39d392007-10-15 16:14:19 -04002973 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2974 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002975 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002976 }
Chris Mason6567e832007-04-16 09:22:45 -04002977 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002978 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002979
2980 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002981 if (slot >= nritems) {
2982 btrfs_print_leaf(root, leaf);
2983 printk("slot %d too large, nritems %d\n", slot, nritems);
2984 BUG_ON(1);
2985 }
Chris Mason6567e832007-04-16 09:22:45 -04002986
2987 /*
2988 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2989 */
2990 /* first correct the data pointers */
2991 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002992 u32 ioff;
2993 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002994
2995 if (!leaf->map_token) {
2996 map_extent_buffer(leaf, (unsigned long)item,
2997 sizeof(struct btrfs_item),
2998 &leaf->map_token, &leaf->kaddr,
2999 &leaf->map_start, &leaf->map_len,
3000 KM_USER1);
3001 }
Chris Mason5f39d392007-10-15 16:14:19 -04003002 ioff = btrfs_item_offset(leaf, item);
3003 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003004 }
Chris Mason5f39d392007-10-15 16:14:19 -04003005
Chris Masondb945352007-10-15 16:15:53 -04003006 if (leaf->map_token) {
3007 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3008 leaf->map_token = NULL;
3009 }
3010
Chris Mason6567e832007-04-16 09:22:45 -04003011 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003012 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003013 data_end - data_size, btrfs_leaf_data(leaf) +
3014 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003015
Chris Mason6567e832007-04-16 09:22:45 -04003016 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003017 old_size = btrfs_item_size_nr(leaf, slot);
3018 item = btrfs_item_nr(leaf, slot);
3019 btrfs_set_item_size(leaf, item, old_size + data_size);
3020 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003021
3022 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003023 if (btrfs_leaf_free_space(root, leaf) < 0) {
3024 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003025 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003026 }
Chris Mason6567e832007-04-16 09:22:45 -04003027 return ret;
3028}
3029
Chris Mason74123bd2007-02-02 11:05:29 -05003030/*
Chris Masond352ac62008-09-29 15:18:18 -04003031 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003032 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003033 * Returns the number of keys that were inserted.
3034 */
3035int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3036 struct btrfs_root *root,
3037 struct btrfs_path *path,
3038 struct btrfs_key *cpu_key, u32 *data_size,
3039 int nr)
3040{
3041 struct extent_buffer *leaf;
3042 struct btrfs_item *item;
3043 int ret = 0;
3044 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003045 int i;
3046 u32 nritems;
3047 u32 total_data = 0;
3048 u32 total_size = 0;
3049 unsigned int data_end;
3050 struct btrfs_disk_key disk_key;
3051 struct btrfs_key found_key;
3052
3053 found_key.objectid = 0;
3054 nr = min_t(int, nr, BTRFS_NODEPTRS_PER_BLOCK(root));
3055
3056 for (i = 0; i < nr; i++)
3057 total_data += data_size[i];
3058
3059 total_data = min_t(u32, total_data, BTRFS_LEAF_DATA_SIZE(root));
3060 total_size = total_data + (nr * sizeof(struct btrfs_item));
3061 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3062 if (ret == 0)
3063 return -EEXIST;
3064 if (ret < 0)
3065 goto out;
3066
Josef Bacikf3465ca2008-11-12 14:19:50 -05003067 leaf = path->nodes[0];
3068
3069 nritems = btrfs_header_nritems(leaf);
3070 data_end = leaf_data_end(root, leaf);
3071
3072 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3073 for (i = nr; i >= 0; i--) {
3074 total_data -= data_size[i];
3075 total_size -= data_size[i] + sizeof(struct btrfs_item);
3076 if (total_size < btrfs_leaf_free_space(root, leaf))
3077 break;
3078 }
3079 nr = i;
3080 }
3081
3082 slot = path->slots[0];
3083 BUG_ON(slot < 0);
3084
3085 if (slot != nritems) {
3086 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3087
3088 item = btrfs_item_nr(leaf, slot);
3089 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3090
3091 /* figure out how many keys we can insert in here */
3092 total_data = data_size[0];
3093 for (i = 1; i < nr; i++) {
3094 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3095 break;
3096 total_data += data_size[i];
3097 }
3098 nr = i;
3099
3100 if (old_data < data_end) {
3101 btrfs_print_leaf(root, leaf);
3102 printk("slot %d old_data %d data_end %d\n",
3103 slot, old_data, data_end);
3104 BUG_ON(1);
3105 }
3106 /*
3107 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3108 */
3109 /* first correct the data pointers */
3110 WARN_ON(leaf->map_token);
3111 for (i = slot; i < nritems; i++) {
3112 u32 ioff;
3113
3114 item = btrfs_item_nr(leaf, i);
3115 if (!leaf->map_token) {
3116 map_extent_buffer(leaf, (unsigned long)item,
3117 sizeof(struct btrfs_item),
3118 &leaf->map_token, &leaf->kaddr,
3119 &leaf->map_start, &leaf->map_len,
3120 KM_USER1);
3121 }
3122
3123 ioff = btrfs_item_offset(leaf, item);
3124 btrfs_set_item_offset(leaf, item, ioff - total_data);
3125 }
3126 if (leaf->map_token) {
3127 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3128 leaf->map_token = NULL;
3129 }
3130
3131 /* shift the items */
3132 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3133 btrfs_item_nr_offset(slot),
3134 (nritems - slot) * sizeof(struct btrfs_item));
3135
3136 /* shift the data */
3137 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3138 data_end - total_data, btrfs_leaf_data(leaf) +
3139 data_end, old_data - data_end);
3140 data_end = old_data;
3141 } else {
3142 /*
3143 * this sucks but it has to be done, if we are inserting at
3144 * the end of the leaf only insert 1 of the items, since we
3145 * have no way of knowing whats on the next leaf and we'd have
3146 * to drop our current locks to figure it out
3147 */
3148 nr = 1;
3149 }
3150
3151 /* setup the item for the new data */
3152 for (i = 0; i < nr; i++) {
3153 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3154 btrfs_set_item_key(leaf, &disk_key, slot + i);
3155 item = btrfs_item_nr(leaf, slot + i);
3156 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3157 data_end -= data_size[i];
3158 btrfs_set_item_size(leaf, item, data_size[i]);
3159 }
3160 btrfs_set_header_nritems(leaf, nritems + nr);
3161 btrfs_mark_buffer_dirty(leaf);
3162
3163 ret = 0;
3164 if (slot == 0) {
3165 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3166 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3167 }
3168
3169 if (btrfs_leaf_free_space(root, leaf) < 0) {
3170 btrfs_print_leaf(root, leaf);
3171 BUG();
3172 }
3173out:
3174 if (!ret)
3175 ret = nr;
3176 return ret;
3177}
3178
3179/*
3180 * Given a key and some data, insert items into the tree.
3181 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003182 */
Chris Mason9c583092008-01-29 15:15:18 -05003183int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003184 struct btrfs_root *root,
3185 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003186 struct btrfs_key *cpu_key, u32 *data_size,
3187 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003188{
Chris Mason5f39d392007-10-15 16:14:19 -04003189 struct extent_buffer *leaf;
3190 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003191 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003192 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003193 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003194 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003195 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003196 u32 total_size = 0;
3197 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003198 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003199 struct btrfs_disk_key disk_key;
3200
Chris Mason9c583092008-01-29 15:15:18 -05003201 for (i = 0; i < nr; i++) {
3202 total_data += data_size[i];
3203 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003204
Josef Bacik7b128762008-07-24 12:17:14 -04003205 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003206 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003207 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003208 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003209 if (ret < 0)
3210 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003211
Chris Mason62e27492007-03-15 12:56:47 -04003212 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003213 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003214
Chris Mason5f39d392007-10-15 16:14:19 -04003215 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003216 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003217
Chris Masonf25956c2008-09-12 15:32:53 -04003218 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003219 btrfs_print_leaf(root, leaf);
3220 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003221 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003222 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003223 }
Chris Mason5f39d392007-10-15 16:14:19 -04003224
Chris Mason62e27492007-03-15 12:56:47 -04003225 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003226 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003227
Chris Masonbe0e5c02007-01-26 15:51:26 -05003228 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003229 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003230
Chris Mason5f39d392007-10-15 16:14:19 -04003231 if (old_data < data_end) {
3232 btrfs_print_leaf(root, leaf);
3233 printk("slot %d old_data %d data_end %d\n",
3234 slot, old_data, data_end);
3235 BUG_ON(1);
3236 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003237 /*
3238 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3239 */
3240 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003241 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003242 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003243 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003244
Chris Mason5f39d392007-10-15 16:14:19 -04003245 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003246 if (!leaf->map_token) {
3247 map_extent_buffer(leaf, (unsigned long)item,
3248 sizeof(struct btrfs_item),
3249 &leaf->map_token, &leaf->kaddr,
3250 &leaf->map_start, &leaf->map_len,
3251 KM_USER1);
3252 }
3253
Chris Mason5f39d392007-10-15 16:14:19 -04003254 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003255 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003256 }
Chris Masondb945352007-10-15 16:15:53 -04003257 if (leaf->map_token) {
3258 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3259 leaf->map_token = NULL;
3260 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003261
3262 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003263 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003264 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003265 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003266
3267 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003268 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003269 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003270 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003271 data_end = old_data;
3272 }
Chris Mason5f39d392007-10-15 16:14:19 -04003273
Chris Mason62e27492007-03-15 12:56:47 -04003274 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003275 for (i = 0; i < nr; i++) {
3276 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3277 btrfs_set_item_key(leaf, &disk_key, slot + i);
3278 item = btrfs_item_nr(leaf, slot + i);
3279 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3280 data_end -= data_size[i];
3281 btrfs_set_item_size(leaf, item, data_size[i]);
3282 }
3283 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003284 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003285
3286 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003287 if (slot == 0) {
3288 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003289 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003290 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003291
Chris Mason5f39d392007-10-15 16:14:19 -04003292 if (btrfs_leaf_free_space(root, leaf) < 0) {
3293 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003294 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003295 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003296out:
Chris Mason62e27492007-03-15 12:56:47 -04003297 return ret;
3298}
3299
3300/*
3301 * Given a key and some data, insert an item into the tree.
3302 * This does all the path init required, making room in the tree if needed.
3303 */
Chris Masone089f052007-03-16 16:20:31 -04003304int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3305 *root, struct btrfs_key *cpu_key, void *data, u32
3306 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003307{
3308 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003309 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003310 struct extent_buffer *leaf;
3311 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003312
Chris Mason2c90e5d2007-04-02 10:50:19 -04003313 path = btrfs_alloc_path();
3314 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003315 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003316 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003317 leaf = path->nodes[0];
3318 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3319 write_extent_buffer(leaf, data, ptr, data_size);
3320 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003321 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003322 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003323 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003324}
3325
Chris Mason74123bd2007-02-02 11:05:29 -05003326/*
Chris Mason5de08d72007-02-24 06:24:44 -05003327 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003328 *
Chris Masond352ac62008-09-29 15:18:18 -04003329 * the tree should have been previously balanced so the deletion does not
3330 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003331 */
Chris Masone089f052007-03-16 16:20:31 -04003332static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3333 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003334{
Chris Mason5f39d392007-10-15 16:14:19 -04003335 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003336 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003337 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003338 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003339
Chris Mason5f39d392007-10-15 16:14:19 -04003340 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05003341 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003342 memmove_extent_buffer(parent,
3343 btrfs_node_key_ptr_offset(slot),
3344 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003345 sizeof(struct btrfs_key_ptr) *
3346 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003347 }
Chris Mason7518a232007-03-12 12:01:18 -04003348 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003349 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003350 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003351 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003352 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003353 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003354 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003355 struct btrfs_disk_key disk_key;
3356
3357 btrfs_node_key(parent, &disk_key, 0);
3358 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003359 if (wret)
3360 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003361 }
Chris Masond6025572007-03-30 14:27:56 -04003362 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003363 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003364}
3365
Chris Mason74123bd2007-02-02 11:05:29 -05003366/*
Chris Mason323ac952008-10-01 19:05:46 -04003367 * a helper function to delete the leaf pointed to by path->slots[1] and
3368 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3369 * already know it, it is faster to have them pass it down than to
3370 * read it out of the node again.
3371 *
3372 * This deletes the pointer in path->nodes[1] and frees the leaf
3373 * block extent. zero is returned if it all worked out, < 0 otherwise.
3374 *
3375 * The path must have already been setup for deleting the leaf, including
3376 * all the proper balancing. path->nodes[1] must be locked.
3377 */
3378noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3379 struct btrfs_root *root,
3380 struct btrfs_path *path, u64 bytenr)
3381{
3382 int ret;
3383 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3384
3385 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3386 if (ret)
3387 return ret;
3388
3389 ret = btrfs_free_extent(trans, root, bytenr,
3390 btrfs_level_size(root, 0),
3391 path->nodes[1]->start,
3392 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003393 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003394 return ret;
3395}
3396/*
Chris Mason74123bd2007-02-02 11:05:29 -05003397 * delete the item at the leaf level in path. If that empties
3398 * the leaf, remove it from the tree
3399 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003400int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3401 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003402{
Chris Mason5f39d392007-10-15 16:14:19 -04003403 struct extent_buffer *leaf;
3404 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003405 int last_off;
3406 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003407 int ret = 0;
3408 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003409 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003410 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003411
Chris Mason5f39d392007-10-15 16:14:19 -04003412 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003413 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3414
3415 for (i = 0; i < nr; i++)
3416 dsize += btrfs_item_size_nr(leaf, slot + i);
3417
Chris Mason5f39d392007-10-15 16:14:19 -04003418 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003419
Chris Mason85e21ba2008-01-29 15:11:36 -05003420 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003421 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003422
3423 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003424 data_end + dsize,
3425 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003426 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003427
Chris Mason85e21ba2008-01-29 15:11:36 -05003428 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003429 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003430
Chris Mason5f39d392007-10-15 16:14:19 -04003431 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003432 if (!leaf->map_token) {
3433 map_extent_buffer(leaf, (unsigned long)item,
3434 sizeof(struct btrfs_item),
3435 &leaf->map_token, &leaf->kaddr,
3436 &leaf->map_start, &leaf->map_len,
3437 KM_USER1);
3438 }
Chris Mason5f39d392007-10-15 16:14:19 -04003439 ioff = btrfs_item_offset(leaf, item);
3440 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003441 }
Chris Masondb945352007-10-15 16:15:53 -04003442
3443 if (leaf->map_token) {
3444 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3445 leaf->map_token = NULL;
3446 }
3447
Chris Mason5f39d392007-10-15 16:14:19 -04003448 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003449 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003450 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003451 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003452 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003453 btrfs_set_header_nritems(leaf, nritems - nr);
3454 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003455
Chris Mason74123bd2007-02-02 11:05:29 -05003456 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003457 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003458 if (leaf == root->node) {
3459 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003460 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003461 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3462 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003463 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003464 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003465 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003466 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003467 struct btrfs_disk_key disk_key;
3468
3469 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003470 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003471 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003472 if (wret)
3473 ret = wret;
3474 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003475
Chris Mason74123bd2007-02-02 11:05:29 -05003476 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003477 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003478 /* push_leaf_left fixes the path.
3479 * make sure the path still points to our leaf
3480 * for possible call to del_ptr below
3481 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003482 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003483 extent_buffer_get(leaf);
3484
Chris Mason85e21ba2008-01-29 15:11:36 -05003485 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003486 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003487 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003488
3489 if (path->nodes[0] == leaf &&
3490 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003491 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003492 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003493 ret = wret;
3494 }
Chris Mason5f39d392007-10-15 16:14:19 -04003495
3496 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003497 path->slots[1] = slot;
3498 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3499 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003500 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003501 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003502 /* if we're still in the path, make sure
3503 * we're dirty. Otherwise, one of the
3504 * push_leaf functions must have already
3505 * dirtied this buffer
3506 */
3507 if (path->nodes[0] == leaf)
3508 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003509 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003510 }
Chris Masond5719762007-03-23 10:01:08 -04003511 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003512 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003513 }
3514 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003515 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003516}
3517
Chris Mason97571fd2007-02-24 13:39:08 -05003518/*
Chris Mason925baed2008-06-25 16:01:30 -04003519 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003520 * returns 0 if it found something or 1 if there are no lesser leaves.
3521 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003522 *
3523 * This may release the path, and so you may lose any locks held at the
3524 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003525 */
3526int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3527{
Chris Mason925baed2008-06-25 16:01:30 -04003528 struct btrfs_key key;
3529 struct btrfs_disk_key found_key;
3530 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003531
Chris Mason925baed2008-06-25 16:01:30 -04003532 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003533
Chris Mason925baed2008-06-25 16:01:30 -04003534 if (key.offset > 0)
3535 key.offset--;
3536 else if (key.type > 0)
3537 key.type--;
3538 else if (key.objectid > 0)
3539 key.objectid--;
3540 else
3541 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003542
Chris Mason925baed2008-06-25 16:01:30 -04003543 btrfs_release_path(root, path);
3544 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3545 if (ret < 0)
3546 return ret;
3547 btrfs_item_key(path->nodes[0], &found_key, 0);
3548 ret = comp_keys(&found_key, &key);
3549 if (ret < 0)
3550 return 0;
3551 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003552}
3553
Chris Mason3f157a22008-06-25 16:01:31 -04003554/*
3555 * A helper function to walk down the tree starting at min_key, and looking
3556 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003557 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003558 *
3559 * This does not cow, but it does stuff the starting key it finds back
3560 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3561 * key and get a writable path.
3562 *
3563 * This does lock as it descends, and path->keep_locks should be set
3564 * to 1 by the caller.
3565 *
3566 * This honors path->lowest_level to prevent descent past a given level
3567 * of the tree.
3568 *
Chris Masond352ac62008-09-29 15:18:18 -04003569 * min_trans indicates the oldest transaction that you are interested
3570 * in walking through. Any nodes or leaves older than min_trans are
3571 * skipped over (without reading them).
3572 *
Chris Mason3f157a22008-06-25 16:01:31 -04003573 * returns zero if something useful was found, < 0 on error and 1 if there
3574 * was nothing in the tree that matched the search criteria.
3575 */
3576int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003577 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003578 struct btrfs_path *path, int cache_only,
3579 u64 min_trans)
3580{
3581 struct extent_buffer *cur;
3582 struct btrfs_key found_key;
3583 int slot;
Yan96524802008-07-24 12:19:49 -04003584 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003585 u32 nritems;
3586 int level;
3587 int ret = 1;
3588
Chris Mason934d3752008-12-08 16:43:10 -05003589 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003590again:
3591 cur = btrfs_lock_root_node(root);
3592 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003593 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003594 path->nodes[level] = cur;
3595 path->locks[level] = 1;
3596
3597 if (btrfs_header_generation(cur) < min_trans) {
3598 ret = 1;
3599 goto out;
3600 }
3601 while(1) {
3602 nritems = btrfs_header_nritems(cur);
3603 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003604 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003605
Chris Mason323ac952008-10-01 19:05:46 -04003606 /* at the lowest level, we're done, setup the path and exit */
3607 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003608 if (slot >= nritems)
3609 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003610 ret = 0;
3611 path->slots[level] = slot;
3612 btrfs_item_key_to_cpu(cur, &found_key, slot);
3613 goto out;
3614 }
Yan96524802008-07-24 12:19:49 -04003615 if (sret && slot > 0)
3616 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003617 /*
3618 * check this node pointer against the cache_only and
3619 * min_trans parameters. If it isn't in cache or is too
3620 * old, skip to the next one.
3621 */
3622 while(slot < nritems) {
3623 u64 blockptr;
3624 u64 gen;
3625 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003626 struct btrfs_disk_key disk_key;
3627
Chris Mason3f157a22008-06-25 16:01:31 -04003628 blockptr = btrfs_node_blockptr(cur, slot);
3629 gen = btrfs_node_ptr_generation(cur, slot);
3630 if (gen < min_trans) {
3631 slot++;
3632 continue;
3633 }
3634 if (!cache_only)
3635 break;
3636
Chris Masone02119d2008-09-05 16:13:11 -04003637 if (max_key) {
3638 btrfs_node_key(cur, &disk_key, slot);
3639 if (comp_keys(&disk_key, max_key) >= 0) {
3640 ret = 1;
3641 goto out;
3642 }
3643 }
3644
Chris Mason3f157a22008-06-25 16:01:31 -04003645 tmp = btrfs_find_tree_block(root, blockptr,
3646 btrfs_level_size(root, level - 1));
3647
3648 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3649 free_extent_buffer(tmp);
3650 break;
3651 }
3652 if (tmp)
3653 free_extent_buffer(tmp);
3654 slot++;
3655 }
Chris Masone02119d2008-09-05 16:13:11 -04003656find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003657 /*
3658 * we didn't find a candidate key in this node, walk forward
3659 * and find another one
3660 */
3661 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003662 path->slots[level] = slot;
3663 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003664 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003665 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003666 btrfs_release_path(root, path);
3667 goto again;
3668 } else {
3669 goto out;
3670 }
3671 }
3672 /* save our key for returning back */
3673 btrfs_node_key_to_cpu(cur, &found_key, slot);
3674 path->slots[level] = slot;
3675 if (level == path->lowest_level) {
3676 ret = 0;
3677 unlock_up(path, level, 1);
3678 goto out;
3679 }
3680 cur = read_node_slot(root, cur, slot);
3681
3682 btrfs_tree_lock(cur);
3683 path->locks[level - 1] = 1;
3684 path->nodes[level - 1] = cur;
3685 unlock_up(path, level, 1);
3686 }
3687out:
3688 if (ret == 0)
3689 memcpy(min_key, &found_key, sizeof(found_key));
3690 return ret;
3691}
3692
3693/*
3694 * this is similar to btrfs_next_leaf, but does not try to preserve
3695 * and fixup the path. It looks for and returns the next key in the
3696 * tree based on the current path and the cache_only and min_trans
3697 * parameters.
3698 *
3699 * 0 is returned if another key is found, < 0 if there are any errors
3700 * and 1 is returned if there are no higher keys in the tree
3701 *
3702 * path->keep_locks should be set to 1 on the search made before
3703 * calling this function.
3704 */
Chris Masone7a84562008-06-25 16:01:31 -04003705int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003706 struct btrfs_key *key, int lowest_level,
3707 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003708{
3709 int level = lowest_level;
3710 int slot;
3711 struct extent_buffer *c;
3712
Chris Mason934d3752008-12-08 16:43:10 -05003713 WARN_ON(!path->keep_locks);
Chris Masone7a84562008-06-25 16:01:31 -04003714 while(level < BTRFS_MAX_LEVEL) {
3715 if (!path->nodes[level])
3716 return 1;
3717
3718 slot = path->slots[level] + 1;
3719 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003720next:
Chris Masone7a84562008-06-25 16:01:31 -04003721 if (slot >= btrfs_header_nritems(c)) {
3722 level++;
3723 if (level == BTRFS_MAX_LEVEL) {
3724 return 1;
3725 }
3726 continue;
3727 }
3728 if (level == 0)
3729 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003730 else {
3731 u64 blockptr = btrfs_node_blockptr(c, slot);
3732 u64 gen = btrfs_node_ptr_generation(c, slot);
3733
3734 if (cache_only) {
3735 struct extent_buffer *cur;
3736 cur = btrfs_find_tree_block(root, blockptr,
3737 btrfs_level_size(root, level - 1));
3738 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3739 slot++;
3740 if (cur)
3741 free_extent_buffer(cur);
3742 goto next;
3743 }
3744 free_extent_buffer(cur);
3745 }
3746 if (gen < min_trans) {
3747 slot++;
3748 goto next;
3749 }
Chris Masone7a84562008-06-25 16:01:31 -04003750 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003751 }
Chris Masone7a84562008-06-25 16:01:31 -04003752 return 0;
3753 }
3754 return 1;
3755}
3756
Chris Mason7bb86312007-12-11 09:25:06 -05003757/*
Chris Mason925baed2008-06-25 16:01:30 -04003758 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003759 * returns 0 if it found something or 1 if there are no greater leaves.
3760 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003761 */
Chris Mason234b63a2007-03-13 10:46:10 -04003762int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003763{
3764 int slot;
3765 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003766 struct extent_buffer *c;
3767 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003768 struct btrfs_key key;
3769 u32 nritems;
3770 int ret;
3771
3772 nritems = btrfs_header_nritems(path->nodes[0]);
3773 if (nritems == 0) {
3774 return 1;
3775 }
3776
3777 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3778
Chris Mason925baed2008-06-25 16:01:30 -04003779 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003780 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003781 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3782 path->keep_locks = 0;
3783
3784 if (ret < 0)
3785 return ret;
3786
Chris Masona2135012008-06-25 16:01:30 -04003787 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003788 /*
3789 * by releasing the path above we dropped all our locks. A balance
3790 * could have added more items next to the key that used to be
3791 * at the very end of the block. So, check again here and
3792 * advance the path if there are now more items available.
3793 */
Chris Masona2135012008-06-25 16:01:30 -04003794 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003795 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003796 goto done;
3797 }
Chris Masond97e63b2007-02-20 16:40:44 -05003798
Chris Mason234b63a2007-03-13 10:46:10 -04003799 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003800 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003801 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003802
Chris Masond97e63b2007-02-20 16:40:44 -05003803 slot = path->slots[level] + 1;
3804 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003805 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003806 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003807 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003808 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003809 }
Chris Masond97e63b2007-02-20 16:40:44 -05003810 continue;
3811 }
Chris Mason5f39d392007-10-15 16:14:19 -04003812
Chris Mason925baed2008-06-25 16:01:30 -04003813 if (next) {
3814 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003815 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003816 }
Chris Mason5f39d392007-10-15 16:14:19 -04003817
Chris Mason0bd40a72008-07-17 12:54:43 -04003818 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3819 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003820 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003821
Chris Masonca7a79a2008-05-12 12:59:19 -04003822 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003823 if (!path->skip_locking) {
3824 WARN_ON(!btrfs_tree_locked(c));
3825 btrfs_tree_lock(next);
3826 }
Chris Masond97e63b2007-02-20 16:40:44 -05003827 break;
3828 }
3829 path->slots[level] = slot;
3830 while(1) {
3831 level--;
3832 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003833 if (path->locks[level])
3834 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003835 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003836 path->nodes[level] = next;
3837 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003838 if (!path->skip_locking)
3839 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003840 if (!level)
3841 break;
Chris Mason925baed2008-06-25 16:01:30 -04003842 if (level == 1 && path->locks[1] && path->reada)
3843 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003844 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003845 if (!path->skip_locking) {
3846 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3847 btrfs_tree_lock(next);
3848 }
Chris Masond97e63b2007-02-20 16:40:44 -05003849 }
Chris Mason925baed2008-06-25 16:01:30 -04003850done:
3851 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003852 return 0;
3853}
Chris Mason0b86a832008-03-24 15:01:56 -04003854
Chris Mason3f157a22008-06-25 16:01:31 -04003855/*
3856 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3857 * searching until it gets past min_objectid or finds an item of 'type'
3858 *
3859 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3860 */
Chris Mason0b86a832008-03-24 15:01:56 -04003861int btrfs_previous_item(struct btrfs_root *root,
3862 struct btrfs_path *path, u64 min_objectid,
3863 int type)
3864{
3865 struct btrfs_key found_key;
3866 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003867 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003868 int ret;
3869
3870 while(1) {
3871 if (path->slots[0] == 0) {
3872 ret = btrfs_prev_leaf(root, path);
3873 if (ret != 0)
3874 return ret;
3875 } else {
3876 path->slots[0]--;
3877 }
3878 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003879 nritems = btrfs_header_nritems(leaf);
3880 if (nritems == 0)
3881 return 1;
3882 if (path->slots[0] == nritems)
3883 path->slots[0]--;
3884
Chris Mason0b86a832008-03-24 15:01:56 -04003885 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3886 if (found_key.type == type)
3887 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003888 if (found_key.objectid < min_objectid)
3889 break;
3890 if (found_key.objectid == min_objectid &&
3891 found_key.type < type)
3892 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003893 }
3894 return 1;
3895}