blob: 7fad2e3ad6ffef6a3b7491b160877a591a7796dc [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 Mason459931e2008-12-10 09:10:46 -05001515 if ((p->search_for_split || ins_len > 0) &&
1516 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001517 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001518 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001519 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001520 if (sret) {
1521 ret = sret;
1522 goto done;
1523 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001524 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001525 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001526 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001527 int sret = balance_level(trans, root, p,
1528 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001529 if (sret) {
1530 ret = sret;
1531 goto done;
1532 }
Chris Masonbb803952007-03-01 12:04:21 -05001533 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001534 if (!b) {
1535 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001536 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001537 }
Chris Masonbb803952007-03-01 12:04:21 -05001538 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001539 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001540 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001541 unlock_up(p, level, lowest_unlock);
1542
Chris Mason9f3a7422007-08-07 15:52:19 -04001543 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001544 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001545 ret = 0;
1546 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001547 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001548
Chris Mason594a24e2008-06-25 16:01:30 -04001549 blocknr = btrfs_node_blockptr(b, slot);
1550 gen = btrfs_node_ptr_generation(b, slot);
1551 blocksize = btrfs_level_size(root, level - 1);
1552
1553 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1554 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001555 b = tmp;
1556 } else {
1557 /*
1558 * reduce lock contention at high levels
1559 * of the btree by dropping locks before
1560 * we read.
1561 */
1562 if (level > 1) {
1563 btrfs_release_path(NULL, p);
1564 if (tmp)
1565 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001566 if (should_reada)
1567 reada_for_search(root, p,
1568 level, slot,
1569 key->objectid);
1570
Chris Mason594a24e2008-06-25 16:01:30 -04001571 tmp = read_tree_block(root, blocknr,
1572 blocksize, gen);
1573 if (tmp)
1574 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001575 goto again;
1576 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001577 if (tmp)
1578 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001579 if (should_reada)
1580 reada_for_search(root, p,
1581 level, slot,
1582 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001583 b = read_node_slot(root, b, slot);
1584 }
1585 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001586 if (!p->skip_locking)
1587 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001588 } else {
1589 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001590 if (ins_len > 0 &&
1591 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001592 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001593 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001594 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001595 if (sret) {
1596 ret = sret;
1597 goto done;
1598 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001599 }
Chris Mason459931e2008-12-10 09:10:46 -05001600 if (!p->search_for_split)
1601 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001602 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001603 }
1604 }
Chris Mason65b51a02008-08-01 15:11:20 -04001605 ret = 1;
1606done:
1607 if (prealloc_block.objectid) {
1608 btrfs_free_reserved_extent(root,
1609 prealloc_block.objectid,
1610 prealloc_block.offset);
1611 }
1612
1613 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001614}
1615
Zheng Yan1a40e232008-09-26 10:09:34 -04001616int btrfs_merge_path(struct btrfs_trans_handle *trans,
1617 struct btrfs_root *root,
1618 struct btrfs_key *node_keys,
1619 u64 *nodes, int lowest_level)
1620{
1621 struct extent_buffer *eb;
1622 struct extent_buffer *parent;
1623 struct btrfs_key key;
1624 u64 bytenr;
1625 u64 generation;
1626 u32 blocksize;
1627 int level;
1628 int slot;
1629 int key_match;
1630 int ret;
1631
1632 eb = btrfs_lock_root_node(root);
1633 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1634 BUG_ON(ret);
1635
1636 parent = eb;
1637 while (1) {
1638 level = btrfs_header_level(parent);
1639 if (level == 0 || level <= lowest_level)
1640 break;
1641
1642 ret = bin_search(parent, &node_keys[lowest_level], level,
1643 &slot);
1644 if (ret && slot > 0)
1645 slot--;
1646
1647 bytenr = btrfs_node_blockptr(parent, slot);
1648 if (nodes[level - 1] == bytenr)
1649 break;
1650
1651 blocksize = btrfs_level_size(root, level - 1);
1652 generation = btrfs_node_ptr_generation(parent, slot);
1653 btrfs_node_key_to_cpu(eb, &key, slot);
1654 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1655
Yan Zhengf82d02d2008-10-29 14:49:05 -04001656 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001657 eb = read_tree_block(root, bytenr, blocksize,
1658 generation);
1659 btrfs_tree_lock(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001660 }
1661
1662 /*
1663 * if node keys match and node pointer hasn't been modified
1664 * in the running transaction, we can merge the path. for
1665 * blocks owened by reloc trees, the node pointer check is
1666 * skipped, this is because these blocks are fully controlled
1667 * by the space balance code, no one else can modify them.
1668 */
1669 if (!nodes[level - 1] || !key_match ||
1670 (generation == trans->transid &&
1671 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1672 if (level == 1 || level == lowest_level + 1) {
1673 if (generation == trans->transid) {
1674 btrfs_tree_unlock(eb);
1675 free_extent_buffer(eb);
1676 }
1677 break;
1678 }
1679
1680 if (generation != trans->transid) {
1681 eb = read_tree_block(root, bytenr, blocksize,
1682 generation);
1683 btrfs_tree_lock(eb);
1684 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001685
1686 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1687 &eb, 0);
1688 BUG_ON(ret);
1689
Yan Zhengf82d02d2008-10-29 14:49:05 -04001690 if (root->root_key.objectid ==
1691 BTRFS_TREE_RELOC_OBJECTID) {
1692 if (!nodes[level - 1]) {
1693 nodes[level - 1] = eb->start;
1694 memcpy(&node_keys[level - 1], &key,
1695 sizeof(node_keys[0]));
1696 } else {
1697 WARN_ON(1);
1698 }
1699 }
1700
Zheng Yan1a40e232008-09-26 10:09:34 -04001701 btrfs_tree_unlock(parent);
1702 free_extent_buffer(parent);
1703 parent = eb;
1704 continue;
1705 }
1706
Zheng Yan1a40e232008-09-26 10:09:34 -04001707 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1708 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1709 btrfs_mark_buffer_dirty(parent);
1710
1711 ret = btrfs_inc_extent_ref(trans, root,
1712 nodes[level - 1],
1713 blocksize, parent->start,
1714 btrfs_header_owner(parent),
1715 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001716 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001717 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001718
1719 /*
1720 * If the block was created in the running transaction,
1721 * it's possible this is the last reference to it, so we
1722 * should drop the subtree.
1723 */
1724 if (generation == trans->transid) {
1725 ret = btrfs_drop_subtree(trans, root, eb, parent);
1726 BUG_ON(ret);
1727 btrfs_tree_unlock(eb);
1728 free_extent_buffer(eb);
1729 } else {
1730 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001731 blocksize, parent->start,
1732 btrfs_header_owner(parent),
1733 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001734 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001735 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001736 }
1737 break;
1738 }
1739 btrfs_tree_unlock(parent);
1740 free_extent_buffer(parent);
1741 return 0;
1742}
1743
Chris Mason74123bd2007-02-02 11:05:29 -05001744/*
1745 * adjust the pointers going up the tree, starting at level
1746 * making sure the right key of each node is points to 'key'.
1747 * This is used after shifting pointers to the left, so it stops
1748 * fixing up pointers when a given leaf/node is not in slot 0 of the
1749 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001750 *
1751 * If this fails to write a tree block, it returns -1, but continues
1752 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001753 */
Chris Mason5f39d392007-10-15 16:14:19 -04001754static int fixup_low_keys(struct btrfs_trans_handle *trans,
1755 struct btrfs_root *root, struct btrfs_path *path,
1756 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001757{
1758 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001759 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001760 struct extent_buffer *t;
1761
Chris Mason234b63a2007-03-13 10:46:10 -04001762 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001763 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001764 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001765 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001766 t = path->nodes[i];
1767 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001768 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769 if (tslot != 0)
1770 break;
1771 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001772 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773}
1774
Chris Mason74123bd2007-02-02 11:05:29 -05001775/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001776 * update item key.
1777 *
1778 * This function isn't completely safe. It's the caller's responsibility
1779 * that the new key won't break the order
1780 */
1781int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1782 struct btrfs_root *root, struct btrfs_path *path,
1783 struct btrfs_key *new_key)
1784{
1785 struct btrfs_disk_key disk_key;
1786 struct extent_buffer *eb;
1787 int slot;
1788
1789 eb = path->nodes[0];
1790 slot = path->slots[0];
1791 if (slot > 0) {
1792 btrfs_item_key(eb, &disk_key, slot - 1);
1793 if (comp_keys(&disk_key, new_key) >= 0)
1794 return -1;
1795 }
1796 if (slot < btrfs_header_nritems(eb) - 1) {
1797 btrfs_item_key(eb, &disk_key, slot + 1);
1798 if (comp_keys(&disk_key, new_key) <= 0)
1799 return -1;
1800 }
1801
1802 btrfs_cpu_key_to_disk(&disk_key, new_key);
1803 btrfs_set_item_key(eb, &disk_key, slot);
1804 btrfs_mark_buffer_dirty(eb);
1805 if (slot == 0)
1806 fixup_low_keys(trans, root, path, &disk_key, 1);
1807 return 0;
1808}
1809
1810/*
Chris Mason74123bd2007-02-02 11:05:29 -05001811 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001812 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001813 *
1814 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1815 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001816 */
Chris Mason98ed5172008-01-03 10:01:48 -05001817static int push_node_left(struct btrfs_trans_handle *trans,
1818 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001819 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001820{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001821 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001822 int src_nritems;
1823 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001824 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001825
Chris Mason5f39d392007-10-15 16:14:19 -04001826 src_nritems = btrfs_header_nritems(src);
1827 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001828 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001829 WARN_ON(btrfs_header_generation(src) != trans->transid);
1830 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001831
Chris Masonbce4eae2008-04-24 14:42:46 -04001832 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001833 return 1;
1834
Chris Masoneb60cea2007-02-02 09:18:22 -05001835 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001836 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001837 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001838
Chris Masonbce4eae2008-04-24 14:42:46 -04001839 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001840 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001841 if (push_items < src_nritems) {
1842 /* leave at least 8 pointers in the node if
1843 * we aren't going to empty it
1844 */
1845 if (src_nritems - push_items < 8) {
1846 if (push_items <= 8)
1847 return 1;
1848 push_items -= 8;
1849 }
1850 }
1851 } else
1852 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001853
Chris Mason5f39d392007-10-15 16:14:19 -04001854 copy_extent_buffer(dst, src,
1855 btrfs_node_key_ptr_offset(dst_nritems),
1856 btrfs_node_key_ptr_offset(0),
1857 push_items * sizeof(struct btrfs_key_ptr));
1858
Chris Masonbb803952007-03-01 12:04:21 -05001859 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001860 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1861 btrfs_node_key_ptr_offset(push_items),
1862 (src_nritems - push_items) *
1863 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001864 }
Chris Mason5f39d392007-10-15 16:14:19 -04001865 btrfs_set_header_nritems(src, src_nritems - push_items);
1866 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1867 btrfs_mark_buffer_dirty(src);
1868 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001869
1870 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1871 BUG_ON(ret);
1872
Chris Masonbb803952007-03-01 12:04:21 -05001873 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001874}
1875
Chris Mason97571fd2007-02-24 13:39:08 -05001876/*
Chris Mason79f95c82007-03-01 15:16:26 -05001877 * try to push data from one node into the next node right in the
1878 * tree.
1879 *
1880 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1881 * error, and > 0 if there was no room in the right hand block.
1882 *
1883 * this will only push up to 1/2 the contents of the left node over
1884 */
Chris Mason5f39d392007-10-15 16:14:19 -04001885static int balance_node_right(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 struct extent_buffer *dst,
1888 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001889{
Chris Mason79f95c82007-03-01 15:16:26 -05001890 int push_items = 0;
1891 int max_push;
1892 int src_nritems;
1893 int dst_nritems;
1894 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001895
Chris Mason7bb86312007-12-11 09:25:06 -05001896 WARN_ON(btrfs_header_generation(src) != trans->transid);
1897 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1898
Chris Mason5f39d392007-10-15 16:14:19 -04001899 src_nritems = btrfs_header_nritems(src);
1900 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001901 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001902 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001903 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001904 }
1905
1906 if (src_nritems < 4) {
1907 return 1;
1908 }
Chris Mason79f95c82007-03-01 15:16:26 -05001909
1910 max_push = src_nritems / 2 + 1;
1911 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001912 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001913 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001914 }
Yan252c38f2007-08-29 09:11:44 -04001915
Chris Mason79f95c82007-03-01 15:16:26 -05001916 if (max_push < push_items)
1917 push_items = max_push;
1918
Chris Mason5f39d392007-10-15 16:14:19 -04001919 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1920 btrfs_node_key_ptr_offset(0),
1921 (dst_nritems) *
1922 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001923
Chris Mason5f39d392007-10-15 16:14:19 -04001924 copy_extent_buffer(dst, src,
1925 btrfs_node_key_ptr_offset(0),
1926 btrfs_node_key_ptr_offset(src_nritems - push_items),
1927 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001928
Chris Mason5f39d392007-10-15 16:14:19 -04001929 btrfs_set_header_nritems(src, src_nritems - push_items);
1930 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001931
Chris Mason5f39d392007-10-15 16:14:19 -04001932 btrfs_mark_buffer_dirty(src);
1933 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001934
1935 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1936 BUG_ON(ret);
1937
Chris Mason79f95c82007-03-01 15:16:26 -05001938 return ret;
1939}
1940
1941/*
Chris Mason97571fd2007-02-24 13:39:08 -05001942 * helper function to insert a new root level in the tree.
1943 * A new node is allocated, and a single item is inserted to
1944 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001945 *
1946 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001947 */
Chris Mason98ed5172008-01-03 10:01:48 -05001948static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001949 struct btrfs_root *root,
1950 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001951{
Chris Mason7bb86312007-12-11 09:25:06 -05001952 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001953 struct extent_buffer *lower;
1954 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001955 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001956 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001957 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001958
1959 BUG_ON(path->nodes[level]);
1960 BUG_ON(path->nodes[level-1] != root->node);
1961
Chris Mason7bb86312007-12-11 09:25:06 -05001962 lower = path->nodes[level-1];
1963 if (level == 1)
1964 btrfs_item_key(lower, &lower_key, 0);
1965 else
1966 btrfs_node_key(lower, &lower_key, 0);
1967
Zheng Yan31840ae2008-09-23 13:14:14 -04001968 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1969 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001970 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001971 if (IS_ERR(c))
1972 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001973
Chris Mason5f39d392007-10-15 16:14:19 -04001974 memset_extent_buffer(c, 0, 0, root->nodesize);
1975 btrfs_set_header_nritems(c, 1);
1976 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001977 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001978 btrfs_set_header_generation(c, trans->transid);
1979 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001980
Chris Mason5f39d392007-10-15 16:14:19 -04001981 write_extent_buffer(c, root->fs_info->fsid,
1982 (unsigned long)btrfs_header_fsid(c),
1983 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001984
1985 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1986 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1987 BTRFS_UUID_SIZE);
1988
Chris Mason5f39d392007-10-15 16:14:19 -04001989 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001990 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001991 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001992 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001993
1994 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001995
1996 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001997
Chris Mason925baed2008-06-25 16:01:30 -04001998 spin_lock(&root->node_lock);
1999 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002000 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002001 spin_unlock(&root->node_lock);
2002
Zheng Yan31840ae2008-09-23 13:14:14 -04002003 ret = btrfs_update_extent_ref(trans, root, lower->start,
2004 lower->start, c->start,
2005 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002006 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002007 BUG_ON(ret);
2008
Chris Mason925baed2008-06-25 16:01:30 -04002009 /* the super has an extra ref to root->node */
2010 free_extent_buffer(old);
2011
Chris Mason0b86a832008-03-24 15:01:56 -04002012 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002013 extent_buffer_get(c);
2014 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002015 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002016 path->slots[level] = 0;
2017 return 0;
2018}
2019
Chris Mason74123bd2007-02-02 11:05:29 -05002020/*
2021 * worker function to insert a single pointer in a node.
2022 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002023 *
Chris Mason74123bd2007-02-02 11:05:29 -05002024 * slot and level indicate where you want the key to go, and
2025 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002026 *
2027 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002028 */
Chris Masone089f052007-03-16 16:20:31 -04002029static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2030 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002031 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002032{
Chris Mason5f39d392007-10-15 16:14:19 -04002033 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002034 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002035
2036 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002037 lower = path->nodes[level];
2038 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002039 if (slot > nritems)
2040 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002041 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002042 BUG();
2043 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002044 memmove_extent_buffer(lower,
2045 btrfs_node_key_ptr_offset(slot + 1),
2046 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002047 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002048 }
Chris Mason5f39d392007-10-15 16:14:19 -04002049 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002050 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002051 WARN_ON(trans->transid == 0);
2052 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002053 btrfs_set_header_nritems(lower, nritems + 1);
2054 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002055 return 0;
2056}
2057
Chris Mason97571fd2007-02-24 13:39:08 -05002058/*
2059 * split the node at the specified level in path in two.
2060 * The path is corrected to point to the appropriate node after the split
2061 *
2062 * Before splitting this tries to make some room in the node by pushing
2063 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002064 *
2065 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002066 */
Chris Masone02119d2008-09-05 16:13:11 -04002067static noinline int split_node(struct btrfs_trans_handle *trans,
2068 struct btrfs_root *root,
2069 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002070{
Chris Mason5f39d392007-10-15 16:14:19 -04002071 struct extent_buffer *c;
2072 struct extent_buffer *split;
2073 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002074 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002075 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002076 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002077 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002078
Chris Mason5f39d392007-10-15 16:14:19 -04002079 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002080 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002081 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002082 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002083 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002084 if (ret)
2085 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002086 } else {
2087 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002088 c = path->nodes[level];
2089 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002090 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002091 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002092 if (ret < 0)
2093 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002094 }
Chris Masone66f7092007-04-20 13:16:02 -04002095
Chris Mason5f39d392007-10-15 16:14:19 -04002096 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002097
Chris Mason925baed2008-06-25 16:01:30 -04002098 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002099 path->nodes[level + 1]->start,
2100 root->root_key.objectid,
2101 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002102 if (IS_ERR(split))
2103 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002104
Chris Mason5f39d392007-10-15 16:14:19 -04002105 btrfs_set_header_flags(split, btrfs_header_flags(c));
2106 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002107 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002108 btrfs_set_header_generation(split, trans->transid);
2109 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002110 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002111 write_extent_buffer(split, root->fs_info->fsid,
2112 (unsigned long)btrfs_header_fsid(split),
2113 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002114 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2115 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2116 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002117
Chris Mason7518a232007-03-12 12:01:18 -04002118 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002119
2120 copy_extent_buffer(split, c,
2121 btrfs_node_key_ptr_offset(0),
2122 btrfs_node_key_ptr_offset(mid),
2123 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2124 btrfs_set_header_nritems(split, c_nritems - mid);
2125 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002126 ret = 0;
2127
Chris Mason5f39d392007-10-15 16:14:19 -04002128 btrfs_mark_buffer_dirty(c);
2129 btrfs_mark_buffer_dirty(split);
2130
2131 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002132 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002133 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002134 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002135 if (wret)
2136 ret = wret;
2137
Zheng Yan31840ae2008-09-23 13:14:14 -04002138 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2139 BUG_ON(ret);
2140
Chris Mason5de08d72007-02-24 06:24:44 -05002141 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002142 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002143 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002144 free_extent_buffer(c);
2145 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002146 path->slots[level + 1] += 1;
2147 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002148 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002149 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002150 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002151 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002152}
2153
Chris Mason74123bd2007-02-02 11:05:29 -05002154/*
2155 * how many bytes are required to store the items in a leaf. start
2156 * and nr indicate which items in the leaf to check. This totals up the
2157 * space used both by the item structs and the item data
2158 */
Chris Mason5f39d392007-10-15 16:14:19 -04002159static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002160{
2161 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002162 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002163 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002164
2165 if (!nr)
2166 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002167 data_len = btrfs_item_end_nr(l, start);
2168 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002169 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002170 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002171 return data_len;
2172}
2173
Chris Mason74123bd2007-02-02 11:05:29 -05002174/*
Chris Masond4dbff92007-04-04 14:08:15 -04002175 * The space between the end of the leaf items and
2176 * the start of the leaf data. IOW, how much room
2177 * the leaf has left for both items and data
2178 */
Chris Masone02119d2008-09-05 16:13:11 -04002179int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2180 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002181{
Chris Mason5f39d392007-10-15 16:14:19 -04002182 int nritems = btrfs_header_nritems(leaf);
2183 int ret;
2184 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2185 if (ret < 0) {
2186 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002187 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002188 leaf_space_used(leaf, 0, nritems), nritems);
2189 }
2190 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002191}
2192
2193/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002194 * push some data in the path leaf to the right, trying to free up at
2195 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002196 *
2197 * returns 1 if the push failed because the other node didn't have enough
2198 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002199 */
Chris Masone089f052007-03-16 16:20:31 -04002200static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002201 *root, struct btrfs_path *path, int data_size,
2202 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002203{
Chris Mason5f39d392007-10-15 16:14:19 -04002204 struct extent_buffer *left = path->nodes[0];
2205 struct extent_buffer *right;
2206 struct extent_buffer *upper;
2207 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002208 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002209 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002210 int free_space;
2211 int push_space = 0;
2212 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002213 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002214 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002215 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002216 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002217 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002218 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002219 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002220
2221 slot = path->slots[1];
2222 if (!path->nodes[1]) {
2223 return 1;
2224 }
2225 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002226 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002227 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002228
Chris Masona2135012008-06-25 16:01:30 -04002229 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2230
Chris Masonca7a79a2008-05-12 12:59:19 -04002231 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002232 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002233 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002234 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002235 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002236
Chris Mason5f39d392007-10-15 16:14:19 -04002237 /* cow and double check */
2238 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002239 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002240 if (ret)
2241 goto out_unlock;
2242
Chris Mason5f39d392007-10-15 16:14:19 -04002243 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002244 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002245 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002246
2247 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002248 if (left_nritems == 0)
2249 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002250
Chris Mason34a38212007-11-07 13:31:03 -05002251 if (empty)
2252 nr = 0;
2253 else
2254 nr = 1;
2255
Zheng Yan31840ae2008-09-23 13:14:14 -04002256 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002257 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002258
Chris Mason34a38212007-11-07 13:31:03 -05002259 i = left_nritems - 1;
2260 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002261 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002262
Zheng Yan31840ae2008-09-23 13:14:14 -04002263 if (!empty && push_items > 0) {
2264 if (path->slots[0] > i)
2265 break;
2266 if (path->slots[0] == i) {
2267 int space = btrfs_leaf_free_space(root, left);
2268 if (space + push_space * 2 > free_space)
2269 break;
2270 }
2271 }
2272
Chris Mason00ec4c52007-02-24 12:47:20 -05002273 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002274 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002275
2276 if (!left->map_token) {
2277 map_extent_buffer(left, (unsigned long)item,
2278 sizeof(struct btrfs_item),
2279 &left->map_token, &left->kaddr,
2280 &left->map_start, &left->map_len,
2281 KM_USER1);
2282 }
2283
2284 this_item_size = btrfs_item_size(left, item);
2285 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002286 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002287
Chris Mason00ec4c52007-02-24 12:47:20 -05002288 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002289 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002290 if (i == 0)
2291 break;
2292 i--;
Chris Masondb945352007-10-15 16:15:53 -04002293 }
2294 if (left->map_token) {
2295 unmap_extent_buffer(left, left->map_token, KM_USER1);
2296 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002297 }
Chris Mason5f39d392007-10-15 16:14:19 -04002298
Chris Mason925baed2008-06-25 16:01:30 -04002299 if (push_items == 0)
2300 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002301
Chris Mason34a38212007-11-07 13:31:03 -05002302 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002303 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002304
Chris Mason00ec4c52007-02-24 12:47:20 -05002305 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002306 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002307
Chris Mason5f39d392007-10-15 16:14:19 -04002308 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002309 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002310
Chris Mason00ec4c52007-02-24 12:47:20 -05002311 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002312 data_end = leaf_data_end(root, right);
2313 memmove_extent_buffer(right,
2314 btrfs_leaf_data(right) + data_end - push_space,
2315 btrfs_leaf_data(right) + data_end,
2316 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2317
Chris Mason00ec4c52007-02-24 12:47:20 -05002318 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002319 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002320 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2321 btrfs_leaf_data(left) + leaf_data_end(root, left),
2322 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002323
2324 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2325 btrfs_item_nr_offset(0),
2326 right_nritems * sizeof(struct btrfs_item));
2327
Chris Mason00ec4c52007-02-24 12:47:20 -05002328 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002329 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2330 btrfs_item_nr_offset(left_nritems - push_items),
2331 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002332
2333 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002334 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002335 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002336 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002337 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002338 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002339 if (!right->map_token) {
2340 map_extent_buffer(right, (unsigned long)item,
2341 sizeof(struct btrfs_item),
2342 &right->map_token, &right->kaddr,
2343 &right->map_start, &right->map_len,
2344 KM_USER1);
2345 }
2346 push_space -= btrfs_item_size(right, item);
2347 btrfs_set_item_offset(right, item, push_space);
2348 }
2349
2350 if (right->map_token) {
2351 unmap_extent_buffer(right, right->map_token, KM_USER1);
2352 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002353 }
Chris Mason7518a232007-03-12 12:01:18 -04002354 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002355 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002356
Chris Mason34a38212007-11-07 13:31:03 -05002357 if (left_nritems)
2358 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002359 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002360
Zheng Yan31840ae2008-09-23 13:14:14 -04002361 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2362 BUG_ON(ret);
2363
Chris Mason5f39d392007-10-15 16:14:19 -04002364 btrfs_item_key(right, &disk_key, 0);
2365 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002366 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002367
Chris Mason00ec4c52007-02-24 12:47:20 -05002368 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002369 if (path->slots[0] >= left_nritems) {
2370 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002371 if (btrfs_header_nritems(path->nodes[0]) == 0)
2372 clean_tree_block(trans, root, path->nodes[0]);
2373 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002374 free_extent_buffer(path->nodes[0]);
2375 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002376 path->slots[1] += 1;
2377 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002378 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002379 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002380 }
2381 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002382
2383out_unlock:
2384 btrfs_tree_unlock(right);
2385 free_extent_buffer(right);
2386 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002387}
Chris Mason925baed2008-06-25 16:01:30 -04002388
Chris Mason00ec4c52007-02-24 12:47:20 -05002389/*
Chris Mason74123bd2007-02-02 11:05:29 -05002390 * push some data in the path leaf to the left, trying to free up at
2391 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2392 */
Chris Masone089f052007-03-16 16:20:31 -04002393static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002394 *root, struct btrfs_path *path, int data_size,
2395 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002396{
Chris Mason5f39d392007-10-15 16:14:19 -04002397 struct btrfs_disk_key disk_key;
2398 struct extent_buffer *right = path->nodes[0];
2399 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002400 int slot;
2401 int i;
2402 int free_space;
2403 int push_space = 0;
2404 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002405 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002406 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002407 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002408 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002409 int ret = 0;
2410 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002411 u32 this_item_size;
2412 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002413
2414 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002415 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002416 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002417 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002418 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002419
Chris Mason3685f792007-10-19 09:23:27 -04002420 right_nritems = btrfs_header_nritems(right);
2421 if (right_nritems == 0) {
2422 return 1;
2423 }
2424
Chris Masona2135012008-06-25 16:01:30 -04002425 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2426
Chris Masonca7a79a2008-05-12 12:59:19 -04002427 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002428 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002429 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002430 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002431 ret = 1;
2432 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002433 }
Chris Mason02217ed2007-03-02 16:08:05 -05002434
2435 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002436 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002437 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002438 if (ret) {
2439 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002440 ret = 1;
2441 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002442 }
Chris Mason3685f792007-10-19 09:23:27 -04002443
Chris Mason123abc82007-03-14 14:14:43 -04002444 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002445 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002446 ret = 1;
2447 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002448 }
2449
Chris Mason34a38212007-11-07 13:31:03 -05002450 if (empty)
2451 nr = right_nritems;
2452 else
2453 nr = right_nritems - 1;
2454
2455 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002456 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002457 if (!right->map_token) {
2458 map_extent_buffer(right, (unsigned long)item,
2459 sizeof(struct btrfs_item),
2460 &right->map_token, &right->kaddr,
2461 &right->map_start, &right->map_len,
2462 KM_USER1);
2463 }
2464
Zheng Yan31840ae2008-09-23 13:14:14 -04002465 if (!empty && push_items > 0) {
2466 if (path->slots[0] < i)
2467 break;
2468 if (path->slots[0] == i) {
2469 int space = btrfs_leaf_free_space(root, right);
2470 if (space + push_space * 2 > free_space)
2471 break;
2472 }
2473 }
2474
Chris Masonbe0e5c02007-01-26 15:51:26 -05002475 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002476 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002477
2478 this_item_size = btrfs_item_size(right, item);
2479 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002480 break;
Chris Masondb945352007-10-15 16:15:53 -04002481
Chris Masonbe0e5c02007-01-26 15:51:26 -05002482 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002483 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002484 }
Chris Masondb945352007-10-15 16:15:53 -04002485
2486 if (right->map_token) {
2487 unmap_extent_buffer(right, right->map_token, KM_USER1);
2488 right->map_token = NULL;
2489 }
2490
Chris Masonbe0e5c02007-01-26 15:51:26 -05002491 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002492 ret = 1;
2493 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002494 }
Chris Mason34a38212007-11-07 13:31:03 -05002495 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002496 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002497
Chris Masonbe0e5c02007-01-26 15:51:26 -05002498 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002499 copy_extent_buffer(left, right,
2500 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2501 btrfs_item_nr_offset(0),
2502 push_items * sizeof(struct btrfs_item));
2503
Chris Mason123abc82007-03-14 14:14:43 -04002504 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002505 btrfs_item_offset_nr(right, push_items -1);
2506
2507 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002508 leaf_data_end(root, left) - push_space,
2509 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002510 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002511 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002512 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002513 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002514
Chris Masondb945352007-10-15 16:15:53 -04002515 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002516 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002517 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002518
Chris Mason5f39d392007-10-15 16:14:19 -04002519 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002520 if (!left->map_token) {
2521 map_extent_buffer(left, (unsigned long)item,
2522 sizeof(struct btrfs_item),
2523 &left->map_token, &left->kaddr,
2524 &left->map_start, &left->map_len,
2525 KM_USER1);
2526 }
2527
Chris Mason5f39d392007-10-15 16:14:19 -04002528 ioff = btrfs_item_offset(left, item);
2529 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002530 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002531 }
Chris Mason5f39d392007-10-15 16:14:19 -04002532 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002533 if (left->map_token) {
2534 unmap_extent_buffer(left, left->map_token, KM_USER1);
2535 left->map_token = NULL;
2536 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002537
2538 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002539 if (push_items > right_nritems) {
2540 printk("push items %d nr %u\n", push_items, right_nritems);
2541 WARN_ON(1);
2542 }
Chris Mason5f39d392007-10-15 16:14:19 -04002543
Chris Mason34a38212007-11-07 13:31:03 -05002544 if (push_items < right_nritems) {
2545 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2546 leaf_data_end(root, right);
2547 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2548 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2549 btrfs_leaf_data(right) +
2550 leaf_data_end(root, right), push_space);
2551
2552 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002553 btrfs_item_nr_offset(push_items),
2554 (btrfs_header_nritems(right) - push_items) *
2555 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002556 }
Yaneef1c492007-11-26 10:58:13 -05002557 right_nritems -= push_items;
2558 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002559 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002560 for (i = 0; i < right_nritems; i++) {
2561 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002562
2563 if (!right->map_token) {
2564 map_extent_buffer(right, (unsigned long)item,
2565 sizeof(struct btrfs_item),
2566 &right->map_token, &right->kaddr,
2567 &right->map_start, &right->map_len,
2568 KM_USER1);
2569 }
2570
2571 push_space = push_space - btrfs_item_size(right, item);
2572 btrfs_set_item_offset(right, item, push_space);
2573 }
2574 if (right->map_token) {
2575 unmap_extent_buffer(right, right->map_token, KM_USER1);
2576 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002577 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002578
Chris Mason5f39d392007-10-15 16:14:19 -04002579 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002580 if (right_nritems)
2581 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002582
Zheng Yan31840ae2008-09-23 13:14:14 -04002583 ret = btrfs_update_ref(trans, root, right, left,
2584 old_left_nritems, push_items);
2585 BUG_ON(ret);
2586
Chris Mason5f39d392007-10-15 16:14:19 -04002587 btrfs_item_key(right, &disk_key, 0);
2588 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002589 if (wret)
2590 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002591
2592 /* then fixup the leaf pointer in the path */
2593 if (path->slots[0] < push_items) {
2594 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002595 if (btrfs_header_nritems(path->nodes[0]) == 0)
2596 clean_tree_block(trans, root, path->nodes[0]);
2597 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002598 free_extent_buffer(path->nodes[0]);
2599 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 path->slots[1] -= 1;
2601 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002602 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002603 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002604 path->slots[0] -= push_items;
2605 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002606 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002607 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002608out:
2609 btrfs_tree_unlock(left);
2610 free_extent_buffer(left);
2611 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002612}
2613
Chris Mason74123bd2007-02-02 11:05:29 -05002614/*
2615 * split the path's leaf in two, making sure there is at least data_size
2616 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002617 *
2618 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002619 */
Chris Masone02119d2008-09-05 16:13:11 -04002620static noinline int split_leaf(struct btrfs_trans_handle *trans,
2621 struct btrfs_root *root,
2622 struct btrfs_key *ins_key,
2623 struct btrfs_path *path, int data_size,
2624 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002625{
Chris Mason5f39d392007-10-15 16:14:19 -04002626 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002627 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002628 int mid;
2629 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002630 struct extent_buffer *right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002631 int data_copy_size;
2632 int rt_data_off;
2633 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002634 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002635 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002636 int double_split;
2637 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002638 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002639
Chris Mason40689472007-03-17 14:29:23 -04002640 /* first try to make some room by pushing left and right */
Chris Mason459931e2008-12-10 09:10:46 -05002641 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002642 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002643 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002644 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002645 }
2646 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002647 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002648 if (wret < 0)
2649 return wret;
2650 }
2651 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002652
Chris Mason3685f792007-10-19 09:23:27 -04002653 /* did the pushes work? */
Yan Zheng87b29b22008-12-17 10:21:48 -05002654 if (btrfs_leaf_free_space(root, l) >= data_size)
Chris Mason3685f792007-10-19 09:23:27 -04002655 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002656 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002657
Chris Mason5c680ed2007-02-22 11:39:13 -05002658 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002659 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002660 if (ret)
2661 return ret;
2662 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002663again:
2664 double_split = 0;
2665 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002666 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002667 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002668 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002669
Chris Mason925baed2008-06-25 16:01:30 -04002670 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002671 path->nodes[1]->start,
2672 root->root_key.objectid,
2673 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002674 if (IS_ERR(right)) {
2675 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002676 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002677 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002678
Chris Mason5f39d392007-10-15 16:14:19 -04002679 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002680 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002681 btrfs_set_header_generation(right, trans->transid);
2682 btrfs_set_header_owner(right, root->root_key.objectid);
2683 btrfs_set_header_level(right, 0);
2684 write_extent_buffer(right, root->fs_info->fsid,
2685 (unsigned long)btrfs_header_fsid(right),
2686 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002687
2688 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2689 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2690 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002691 if (mid <= slot) {
2692 if (nritems == 1 ||
Yan Zheng87b29b22008-12-17 10:21:48 -05002693 leaf_space_used(l, mid, nritems - mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002694 BTRFS_LEAF_DATA_SIZE(root)) {
2695 if (slot >= nritems) {
2696 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002697 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002698 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002699 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002700 path->slots[1] + 1, 1);
2701 if (wret)
2702 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002703
2704 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002705 free_extent_buffer(path->nodes[0]);
2706 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002707 path->slots[0] = 0;
2708 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002709 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002710 return ret;
2711 }
2712 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002713 if (mid != nritems &&
2714 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002715 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002716 double_split = 1;
2717 }
Chris Masond4dbff92007-04-04 14:08:15 -04002718 }
2719 } else {
Yan Zheng87b29b22008-12-17 10:21:48 -05002720 if (leaf_space_used(l, 0, mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002721 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason459931e2008-12-10 09:10:46 -05002722 if (!extend && data_size && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002723 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002724 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002725 wret = insert_ptr(trans, root, path,
2726 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002727 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002728 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002729 if (wret)
2730 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002731 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002732 free_extent_buffer(path->nodes[0]);
2733 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002734 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002735 if (path->slots[1] == 0) {
2736 wret = fixup_low_keys(trans, root,
2737 path, &disk_key, 1);
2738 if (wret)
2739 ret = wret;
2740 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002741 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002742 return ret;
Chris Mason459931e2008-12-10 09:10:46 -05002743 } else if ((extend || !data_size) && slot == 0) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002744 mid = 1;
2745 } else {
2746 mid = slot;
2747 if (mid != nritems &&
2748 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002749 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002750 double_split = 1;
2751 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002752 }
Chris Masond4dbff92007-04-04 14:08:15 -04002753 }
2754 }
Chris Mason5f39d392007-10-15 16:14:19 -04002755 nritems = nritems - mid;
2756 btrfs_set_header_nritems(right, nritems);
2757 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2758
2759 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2760 btrfs_item_nr_offset(mid),
2761 nritems * sizeof(struct btrfs_item));
2762
2763 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002764 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2765 data_copy_size, btrfs_leaf_data(l) +
2766 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002767
Chris Mason5f39d392007-10-15 16:14:19 -04002768 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2769 btrfs_item_end_nr(l, mid);
2770
2771 for (i = 0; i < nritems; i++) {
2772 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002773 u32 ioff;
2774
2775 if (!right->map_token) {
2776 map_extent_buffer(right, (unsigned long)item,
2777 sizeof(struct btrfs_item),
2778 &right->map_token, &right->kaddr,
2779 &right->map_start, &right->map_len,
2780 KM_USER1);
2781 }
2782
2783 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002784 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002785 }
Chris Mason74123bd2007-02-02 11:05:29 -05002786
Chris Masondb945352007-10-15 16:15:53 -04002787 if (right->map_token) {
2788 unmap_extent_buffer(right, right->map_token, KM_USER1);
2789 right->map_token = NULL;
2790 }
2791
Chris Mason5f39d392007-10-15 16:14:19 -04002792 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002793 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002794 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002795 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2796 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002797 if (wret)
2798 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002799
2800 btrfs_mark_buffer_dirty(right);
2801 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002802 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002803
Zheng Yan31840ae2008-09-23 13:14:14 -04002804 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2805 BUG_ON(ret);
2806
Chris Masonbe0e5c02007-01-26 15:51:26 -05002807 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002808 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002809 free_extent_buffer(path->nodes[0]);
2810 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002811 path->slots[0] -= mid;
2812 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002813 } else {
2814 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002815 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002816 }
Chris Mason5f39d392007-10-15 16:14:19 -04002817
Chris Masoneb60cea2007-02-02 09:18:22 -05002818 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002819
Chris Masoncc0c5532007-10-25 15:42:57 -04002820 if (double_split) {
2821 BUG_ON(num_doubles != 0);
2822 num_doubles++;
2823 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002824 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002825 return ret;
2826}
2827
Chris Masond352ac62008-09-29 15:18:18 -04002828/*
Chris Mason459931e2008-12-10 09:10:46 -05002829 * This function splits a single item into two items,
2830 * giving 'new_key' to the new item and splitting the
2831 * old one at split_offset (from the start of the item).
2832 *
2833 * The path may be released by this operation. After
2834 * the split, the path is pointing to the old item. The
2835 * new item is going to be in the same node as the old one.
2836 *
2837 * Note, the item being split must be smaller enough to live alone on
2838 * a tree block with room for one extra struct btrfs_item
2839 *
2840 * This allows us to split the item in place, keeping a lock on the
2841 * leaf the entire time.
2842 */
2843int btrfs_split_item(struct btrfs_trans_handle *trans,
2844 struct btrfs_root *root,
2845 struct btrfs_path *path,
2846 struct btrfs_key *new_key,
2847 unsigned long split_offset)
2848{
2849 u32 item_size;
2850 struct extent_buffer *leaf;
2851 struct btrfs_key orig_key;
2852 struct btrfs_item *item;
2853 struct btrfs_item *new_item;
2854 int ret = 0;
2855 int slot;
2856 u32 nritems;
2857 u32 orig_offset;
2858 struct btrfs_disk_key disk_key;
2859 char *buf;
2860
2861 leaf = path->nodes[0];
2862 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2863 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2864 goto split;
2865
2866 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2867 btrfs_release_path(root, path);
2868
2869 path->search_for_split = 1;
2870 path->keep_locks = 1;
2871
2872 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2873 path->search_for_split = 0;
2874
2875 /* if our item isn't there or got smaller, return now */
2876 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2877 path->slots[0])) {
2878 path->keep_locks = 0;
2879 return -EAGAIN;
2880 }
2881
Yan Zheng87b29b22008-12-17 10:21:48 -05002882 ret = split_leaf(trans, root, &orig_key, path,
2883 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05002884 path->keep_locks = 0;
2885 BUG_ON(ret);
2886
Chris Mason459931e2008-12-10 09:10:46 -05002887 leaf = path->nodes[0];
Chris Mason42dc7ba2008-12-15 11:44:56 -05002888 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05002889
2890split:
2891 item = btrfs_item_nr(leaf, path->slots[0]);
2892 orig_offset = btrfs_item_offset(leaf, item);
2893 item_size = btrfs_item_size(leaf, item);
2894
2895
2896 buf = kmalloc(item_size, GFP_NOFS);
2897 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2898 path->slots[0]), item_size);
2899 slot = path->slots[0] + 1;
2900 leaf = path->nodes[0];
2901
2902 nritems = btrfs_header_nritems(leaf);
2903
2904 if (slot != nritems) {
2905 /* shift the items */
2906 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2907 btrfs_item_nr_offset(slot),
2908 (nritems - slot) * sizeof(struct btrfs_item));
2909
2910 }
2911
2912 btrfs_cpu_key_to_disk(&disk_key, new_key);
2913 btrfs_set_item_key(leaf, &disk_key, slot);
2914
2915 new_item = btrfs_item_nr(leaf, slot);
2916
2917 btrfs_set_item_offset(leaf, new_item, orig_offset);
2918 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2919
2920 btrfs_set_item_offset(leaf, item,
2921 orig_offset + item_size - split_offset);
2922 btrfs_set_item_size(leaf, item, split_offset);
2923
2924 btrfs_set_header_nritems(leaf, nritems + 1);
2925
2926 /* write the data for the start of the original item */
2927 write_extent_buffer(leaf, buf,
2928 btrfs_item_ptr_offset(leaf, path->slots[0]),
2929 split_offset);
2930
2931 /* write the data for the new item */
2932 write_extent_buffer(leaf, buf + split_offset,
2933 btrfs_item_ptr_offset(leaf, slot),
2934 item_size - split_offset);
2935 btrfs_mark_buffer_dirty(leaf);
2936
2937 ret = 0;
2938 if (btrfs_leaf_free_space(root, leaf) < 0) {
2939 btrfs_print_leaf(root, leaf);
2940 BUG();
2941 }
2942 kfree(buf);
2943 return ret;
2944}
2945
2946/*
Chris Masond352ac62008-09-29 15:18:18 -04002947 * make the item pointed to by the path smaller. new_size indicates
2948 * how small to make it, and from_end tells us if we just chop bytes
2949 * off the end of the item or if we shift the item to chop bytes off
2950 * the front.
2951 */
Chris Masonb18c6682007-04-17 13:26:50 -04002952int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2953 struct btrfs_root *root,
2954 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002955 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002956{
2957 int ret = 0;
2958 int slot;
2959 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002960 struct extent_buffer *leaf;
2961 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002962 u32 nritems;
2963 unsigned int data_end;
2964 unsigned int old_data_start;
2965 unsigned int old_size;
2966 unsigned int size_diff;
2967 int i;
2968
2969 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002970 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002971 slot = path->slots[0];
2972
2973 old_size = btrfs_item_size_nr(leaf, slot);
2974 if (old_size == new_size)
2975 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002976
Chris Mason5f39d392007-10-15 16:14:19 -04002977 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002978 data_end = leaf_data_end(root, leaf);
2979
Chris Mason5f39d392007-10-15 16:14:19 -04002980 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002981
Chris Masonb18c6682007-04-17 13:26:50 -04002982 size_diff = old_size - new_size;
2983
2984 BUG_ON(slot < 0);
2985 BUG_ON(slot >= nritems);
2986
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 }
3002
Chris Mason5f39d392007-10-15 16:14:19 -04003003 ioff = btrfs_item_offset(leaf, item);
3004 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003005 }
Chris Masondb945352007-10-15 16:15:53 -04003006
3007 if (leaf->map_token) {
3008 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3009 leaf->map_token = NULL;
3010 }
3011
Chris Masonb18c6682007-04-17 13:26:50 -04003012 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003013 if (from_end) {
3014 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3015 data_end + size_diff, btrfs_leaf_data(leaf) +
3016 data_end, old_data_start + new_size - data_end);
3017 } else {
3018 struct btrfs_disk_key disk_key;
3019 u64 offset;
3020
3021 btrfs_item_key(leaf, &disk_key, slot);
3022
3023 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3024 unsigned long ptr;
3025 struct btrfs_file_extent_item *fi;
3026
3027 fi = btrfs_item_ptr(leaf, slot,
3028 struct btrfs_file_extent_item);
3029 fi = (struct btrfs_file_extent_item *)(
3030 (unsigned long)fi - size_diff);
3031
3032 if (btrfs_file_extent_type(leaf, fi) ==
3033 BTRFS_FILE_EXTENT_INLINE) {
3034 ptr = btrfs_item_ptr_offset(leaf, slot);
3035 memmove_extent_buffer(leaf, ptr,
3036 (unsigned long)fi,
3037 offsetof(struct btrfs_file_extent_item,
3038 disk_bytenr));
3039 }
3040 }
3041
3042 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3043 data_end + size_diff, btrfs_leaf_data(leaf) +
3044 data_end, old_data_start - data_end);
3045
3046 offset = btrfs_disk_key_offset(&disk_key);
3047 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3048 btrfs_set_item_key(leaf, &disk_key, slot);
3049 if (slot == 0)
3050 fixup_low_keys(trans, root, path, &disk_key, 1);
3051 }
Chris Mason5f39d392007-10-15 16:14:19 -04003052
3053 item = btrfs_item_nr(leaf, slot);
3054 btrfs_set_item_size(leaf, item, new_size);
3055 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003056
3057 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003058 if (btrfs_leaf_free_space(root, leaf) < 0) {
3059 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003060 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003061 }
Chris Masonb18c6682007-04-17 13:26:50 -04003062 return ret;
3063}
3064
Chris Masond352ac62008-09-29 15:18:18 -04003065/*
3066 * make the item pointed to by the path bigger, data_size is the new size.
3067 */
Chris Mason5f39d392007-10-15 16:14:19 -04003068int btrfs_extend_item(struct btrfs_trans_handle *trans,
3069 struct btrfs_root *root, struct btrfs_path *path,
3070 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003071{
3072 int ret = 0;
3073 int slot;
3074 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003075 struct extent_buffer *leaf;
3076 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003077 u32 nritems;
3078 unsigned int data_end;
3079 unsigned int old_data;
3080 unsigned int old_size;
3081 int i;
3082
3083 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003084 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003085
Chris Mason5f39d392007-10-15 16:14:19 -04003086 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003087 data_end = leaf_data_end(root, leaf);
3088
Chris Mason5f39d392007-10-15 16:14:19 -04003089 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3090 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003091 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003092 }
Chris Mason6567e832007-04-16 09:22:45 -04003093 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003094 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003095
3096 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003097 if (slot >= nritems) {
3098 btrfs_print_leaf(root, leaf);
3099 printk("slot %d too large, nritems %d\n", slot, nritems);
3100 BUG_ON(1);
3101 }
Chris Mason6567e832007-04-16 09:22:45 -04003102
3103 /*
3104 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3105 */
3106 /* first correct the data pointers */
3107 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003108 u32 ioff;
3109 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003110
3111 if (!leaf->map_token) {
3112 map_extent_buffer(leaf, (unsigned long)item,
3113 sizeof(struct btrfs_item),
3114 &leaf->map_token, &leaf->kaddr,
3115 &leaf->map_start, &leaf->map_len,
3116 KM_USER1);
3117 }
Chris Mason5f39d392007-10-15 16:14:19 -04003118 ioff = btrfs_item_offset(leaf, item);
3119 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003120 }
Chris Mason5f39d392007-10-15 16:14:19 -04003121
Chris Masondb945352007-10-15 16:15:53 -04003122 if (leaf->map_token) {
3123 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3124 leaf->map_token = NULL;
3125 }
3126
Chris Mason6567e832007-04-16 09:22:45 -04003127 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003128 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003129 data_end - data_size, btrfs_leaf_data(leaf) +
3130 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003131
Chris Mason6567e832007-04-16 09:22:45 -04003132 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003133 old_size = btrfs_item_size_nr(leaf, slot);
3134 item = btrfs_item_nr(leaf, slot);
3135 btrfs_set_item_size(leaf, item, old_size + data_size);
3136 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003137
3138 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003139 if (btrfs_leaf_free_space(root, leaf) < 0) {
3140 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003141 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003142 }
Chris Mason6567e832007-04-16 09:22:45 -04003143 return ret;
3144}
3145
Chris Mason74123bd2007-02-02 11:05:29 -05003146/*
Chris Masond352ac62008-09-29 15:18:18 -04003147 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003148 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003149 * Returns the number of keys that were inserted.
3150 */
3151int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3152 struct btrfs_root *root,
3153 struct btrfs_path *path,
3154 struct btrfs_key *cpu_key, u32 *data_size,
3155 int nr)
3156{
3157 struct extent_buffer *leaf;
3158 struct btrfs_item *item;
3159 int ret = 0;
3160 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003161 int i;
3162 u32 nritems;
3163 u32 total_data = 0;
3164 u32 total_size = 0;
3165 unsigned int data_end;
3166 struct btrfs_disk_key disk_key;
3167 struct btrfs_key found_key;
3168
Yan Zheng87b29b22008-12-17 10:21:48 -05003169 for (i = 0; i < nr; i++) {
3170 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3171 BTRFS_LEAF_DATA_SIZE(root)) {
3172 break;
3173 nr = i;
3174 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003175 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003176 total_size += data_size[i] + sizeof(struct btrfs_item);
3177 }
3178 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003179
Josef Bacikf3465ca2008-11-12 14:19:50 -05003180 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3181 if (ret == 0)
3182 return -EEXIST;
3183 if (ret < 0)
3184 goto out;
3185
Josef Bacikf3465ca2008-11-12 14:19:50 -05003186 leaf = path->nodes[0];
3187
3188 nritems = btrfs_header_nritems(leaf);
3189 data_end = leaf_data_end(root, leaf);
3190
3191 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3192 for (i = nr; i >= 0; i--) {
3193 total_data -= data_size[i];
3194 total_size -= data_size[i] + sizeof(struct btrfs_item);
3195 if (total_size < btrfs_leaf_free_space(root, leaf))
3196 break;
3197 }
3198 nr = i;
3199 }
3200
3201 slot = path->slots[0];
3202 BUG_ON(slot < 0);
3203
3204 if (slot != nritems) {
3205 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3206
3207 item = btrfs_item_nr(leaf, slot);
3208 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3209
3210 /* figure out how many keys we can insert in here */
3211 total_data = data_size[0];
3212 for (i = 1; i < nr; i++) {
3213 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3214 break;
3215 total_data += data_size[i];
3216 }
3217 nr = i;
3218
3219 if (old_data < data_end) {
3220 btrfs_print_leaf(root, leaf);
3221 printk("slot %d old_data %d data_end %d\n",
3222 slot, old_data, data_end);
3223 BUG_ON(1);
3224 }
3225 /*
3226 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3227 */
3228 /* first correct the data pointers */
3229 WARN_ON(leaf->map_token);
3230 for (i = slot; i < nritems; i++) {
3231 u32 ioff;
3232
3233 item = btrfs_item_nr(leaf, i);
3234 if (!leaf->map_token) {
3235 map_extent_buffer(leaf, (unsigned long)item,
3236 sizeof(struct btrfs_item),
3237 &leaf->map_token, &leaf->kaddr,
3238 &leaf->map_start, &leaf->map_len,
3239 KM_USER1);
3240 }
3241
3242 ioff = btrfs_item_offset(leaf, item);
3243 btrfs_set_item_offset(leaf, item, ioff - total_data);
3244 }
3245 if (leaf->map_token) {
3246 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3247 leaf->map_token = NULL;
3248 }
3249
3250 /* shift the items */
3251 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3252 btrfs_item_nr_offset(slot),
3253 (nritems - slot) * sizeof(struct btrfs_item));
3254
3255 /* shift the data */
3256 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3257 data_end - total_data, btrfs_leaf_data(leaf) +
3258 data_end, old_data - data_end);
3259 data_end = old_data;
3260 } else {
3261 /*
3262 * this sucks but it has to be done, if we are inserting at
3263 * the end of the leaf only insert 1 of the items, since we
3264 * have no way of knowing whats on the next leaf and we'd have
3265 * to drop our current locks to figure it out
3266 */
3267 nr = 1;
3268 }
3269
3270 /* setup the item for the new data */
3271 for (i = 0; i < nr; i++) {
3272 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3273 btrfs_set_item_key(leaf, &disk_key, slot + i);
3274 item = btrfs_item_nr(leaf, slot + i);
3275 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3276 data_end -= data_size[i];
3277 btrfs_set_item_size(leaf, item, data_size[i]);
3278 }
3279 btrfs_set_header_nritems(leaf, nritems + nr);
3280 btrfs_mark_buffer_dirty(leaf);
3281
3282 ret = 0;
3283 if (slot == 0) {
3284 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3285 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3286 }
3287
3288 if (btrfs_leaf_free_space(root, leaf) < 0) {
3289 btrfs_print_leaf(root, leaf);
3290 BUG();
3291 }
3292out:
3293 if (!ret)
3294 ret = nr;
3295 return ret;
3296}
3297
3298/*
3299 * Given a key and some data, insert items into the tree.
3300 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003301 */
Chris Mason9c583092008-01-29 15:15:18 -05003302int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003303 struct btrfs_root *root,
3304 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003305 struct btrfs_key *cpu_key, u32 *data_size,
3306 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003307{
Chris Mason5f39d392007-10-15 16:14:19 -04003308 struct extent_buffer *leaf;
3309 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003310 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003311 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003312 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003313 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003314 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003315 u32 total_size = 0;
3316 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003317 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003318 struct btrfs_disk_key disk_key;
3319
Chris Mason9c583092008-01-29 15:15:18 -05003320 for (i = 0; i < nr; i++) {
3321 total_data += data_size[i];
3322 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003323
Josef Bacik7b128762008-07-24 12:17:14 -04003324 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003325 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003326 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003327 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003328 if (ret < 0)
3329 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003330
Chris Mason62e27492007-03-15 12:56:47 -04003331 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003332 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003333
Chris Mason5f39d392007-10-15 16:14:19 -04003334 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003335 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003336
Chris Masonf25956c2008-09-12 15:32:53 -04003337 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003338 btrfs_print_leaf(root, leaf);
3339 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003340 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003341 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003342 }
Chris Mason5f39d392007-10-15 16:14:19 -04003343
Chris Mason62e27492007-03-15 12:56:47 -04003344 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003345 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003346
Chris Masonbe0e5c02007-01-26 15:51:26 -05003347 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003348 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003349
Chris Mason5f39d392007-10-15 16:14:19 -04003350 if (old_data < data_end) {
3351 btrfs_print_leaf(root, leaf);
3352 printk("slot %d old_data %d data_end %d\n",
3353 slot, old_data, data_end);
3354 BUG_ON(1);
3355 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003356 /*
3357 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3358 */
3359 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003360 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003361 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003362 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003363
Chris Mason5f39d392007-10-15 16:14:19 -04003364 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003365 if (!leaf->map_token) {
3366 map_extent_buffer(leaf, (unsigned long)item,
3367 sizeof(struct btrfs_item),
3368 &leaf->map_token, &leaf->kaddr,
3369 &leaf->map_start, &leaf->map_len,
3370 KM_USER1);
3371 }
3372
Chris Mason5f39d392007-10-15 16:14:19 -04003373 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003374 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003375 }
Chris Masondb945352007-10-15 16:15:53 -04003376 if (leaf->map_token) {
3377 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3378 leaf->map_token = NULL;
3379 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003380
3381 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003382 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003383 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003384 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003385
3386 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003387 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003388 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003389 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003390 data_end = old_data;
3391 }
Chris Mason5f39d392007-10-15 16:14:19 -04003392
Chris Mason62e27492007-03-15 12:56:47 -04003393 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003394 for (i = 0; i < nr; i++) {
3395 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3396 btrfs_set_item_key(leaf, &disk_key, slot + i);
3397 item = btrfs_item_nr(leaf, slot + i);
3398 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3399 data_end -= data_size[i];
3400 btrfs_set_item_size(leaf, item, data_size[i]);
3401 }
3402 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003403 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003404
3405 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003406 if (slot == 0) {
3407 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003408 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003409 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003410
Chris Mason5f39d392007-10-15 16:14:19 -04003411 if (btrfs_leaf_free_space(root, leaf) < 0) {
3412 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003413 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003414 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003415out:
Chris Mason62e27492007-03-15 12:56:47 -04003416 return ret;
3417}
3418
3419/*
3420 * Given a key and some data, insert an item into the tree.
3421 * This does all the path init required, making room in the tree if needed.
3422 */
Chris Masone089f052007-03-16 16:20:31 -04003423int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3424 *root, struct btrfs_key *cpu_key, void *data, u32
3425 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003426{
3427 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003428 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003429 struct extent_buffer *leaf;
3430 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003431
Chris Mason2c90e5d2007-04-02 10:50:19 -04003432 path = btrfs_alloc_path();
3433 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003434 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003435 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003436 leaf = path->nodes[0];
3437 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3438 write_extent_buffer(leaf, data, ptr, data_size);
3439 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003440 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003441 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003442 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003443}
3444
Chris Mason74123bd2007-02-02 11:05:29 -05003445/*
Chris Mason5de08d72007-02-24 06:24:44 -05003446 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003447 *
Chris Masond352ac62008-09-29 15:18:18 -04003448 * the tree should have been previously balanced so the deletion does not
3449 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003450 */
Chris Masone089f052007-03-16 16:20:31 -04003451static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3452 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003453{
Chris Mason5f39d392007-10-15 16:14:19 -04003454 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003455 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003456 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003457 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003458
Chris Mason5f39d392007-10-15 16:14:19 -04003459 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05003460 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003461 memmove_extent_buffer(parent,
3462 btrfs_node_key_ptr_offset(slot),
3463 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003464 sizeof(struct btrfs_key_ptr) *
3465 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003466 }
Chris Mason7518a232007-03-12 12:01:18 -04003467 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003468 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003469 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003470 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003471 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003472 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003473 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003474 struct btrfs_disk_key disk_key;
3475
3476 btrfs_node_key(parent, &disk_key, 0);
3477 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003478 if (wret)
3479 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003480 }
Chris Masond6025572007-03-30 14:27:56 -04003481 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003482 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003483}
3484
Chris Mason74123bd2007-02-02 11:05:29 -05003485/*
Chris Mason323ac952008-10-01 19:05:46 -04003486 * a helper function to delete the leaf pointed to by path->slots[1] and
3487 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3488 * already know it, it is faster to have them pass it down than to
3489 * read it out of the node again.
3490 *
3491 * This deletes the pointer in path->nodes[1] and frees the leaf
3492 * block extent. zero is returned if it all worked out, < 0 otherwise.
3493 *
3494 * The path must have already been setup for deleting the leaf, including
3495 * all the proper balancing. path->nodes[1] must be locked.
3496 */
3497noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3498 struct btrfs_root *root,
3499 struct btrfs_path *path, u64 bytenr)
3500{
3501 int ret;
3502 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3503
3504 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3505 if (ret)
3506 return ret;
3507
3508 ret = btrfs_free_extent(trans, root, bytenr,
3509 btrfs_level_size(root, 0),
3510 path->nodes[1]->start,
3511 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003512 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003513 return ret;
3514}
3515/*
Chris Mason74123bd2007-02-02 11:05:29 -05003516 * delete the item at the leaf level in path. If that empties
3517 * the leaf, remove it from the tree
3518 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003519int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3520 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003521{
Chris Mason5f39d392007-10-15 16:14:19 -04003522 struct extent_buffer *leaf;
3523 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003524 int last_off;
3525 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003526 int ret = 0;
3527 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003528 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003529 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003530
Chris Mason5f39d392007-10-15 16:14:19 -04003531 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003532 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3533
3534 for (i = 0; i < nr; i++)
3535 dsize += btrfs_item_size_nr(leaf, slot + i);
3536
Chris Mason5f39d392007-10-15 16:14:19 -04003537 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003538
Chris Mason85e21ba2008-01-29 15:11:36 -05003539 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003540 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003541
3542 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003543 data_end + dsize,
3544 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003545 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003546
Chris Mason85e21ba2008-01-29 15:11:36 -05003547 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003548 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003549
Chris Mason5f39d392007-10-15 16:14:19 -04003550 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003551 if (!leaf->map_token) {
3552 map_extent_buffer(leaf, (unsigned long)item,
3553 sizeof(struct btrfs_item),
3554 &leaf->map_token, &leaf->kaddr,
3555 &leaf->map_start, &leaf->map_len,
3556 KM_USER1);
3557 }
Chris Mason5f39d392007-10-15 16:14:19 -04003558 ioff = btrfs_item_offset(leaf, item);
3559 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003560 }
Chris Masondb945352007-10-15 16:15:53 -04003561
3562 if (leaf->map_token) {
3563 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3564 leaf->map_token = NULL;
3565 }
3566
Chris Mason5f39d392007-10-15 16:14:19 -04003567 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003568 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003569 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003570 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003571 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003572 btrfs_set_header_nritems(leaf, nritems - nr);
3573 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003574
Chris Mason74123bd2007-02-02 11:05:29 -05003575 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003576 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003577 if (leaf == root->node) {
3578 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003579 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003580 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3581 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003582 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003583 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003584 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003585 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003586 struct btrfs_disk_key disk_key;
3587
3588 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003589 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003590 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003591 if (wret)
3592 ret = wret;
3593 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003594
Chris Mason74123bd2007-02-02 11:05:29 -05003595 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003596 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003597 /* push_leaf_left fixes the path.
3598 * make sure the path still points to our leaf
3599 * for possible call to del_ptr below
3600 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003601 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003602 extent_buffer_get(leaf);
3603
Chris Mason85e21ba2008-01-29 15:11:36 -05003604 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003605 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003606 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003607
3608 if (path->nodes[0] == leaf &&
3609 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003610 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003611 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003612 ret = wret;
3613 }
Chris Mason5f39d392007-10-15 16:14:19 -04003614
3615 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003616 path->slots[1] = slot;
3617 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3618 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003619 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003620 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003621 /* if we're still in the path, make sure
3622 * we're dirty. Otherwise, one of the
3623 * push_leaf functions must have already
3624 * dirtied this buffer
3625 */
3626 if (path->nodes[0] == leaf)
3627 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003628 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003629 }
Chris Masond5719762007-03-23 10:01:08 -04003630 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003631 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632 }
3633 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003634 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003635}
3636
Chris Mason97571fd2007-02-24 13:39:08 -05003637/*
Chris Mason925baed2008-06-25 16:01:30 -04003638 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003639 * returns 0 if it found something or 1 if there are no lesser leaves.
3640 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003641 *
3642 * This may release the path, and so you may lose any locks held at the
3643 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003644 */
3645int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3646{
Chris Mason925baed2008-06-25 16:01:30 -04003647 struct btrfs_key key;
3648 struct btrfs_disk_key found_key;
3649 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003650
Chris Mason925baed2008-06-25 16:01:30 -04003651 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003652
Chris Mason925baed2008-06-25 16:01:30 -04003653 if (key.offset > 0)
3654 key.offset--;
3655 else if (key.type > 0)
3656 key.type--;
3657 else if (key.objectid > 0)
3658 key.objectid--;
3659 else
3660 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003661
Chris Mason925baed2008-06-25 16:01:30 -04003662 btrfs_release_path(root, path);
3663 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3664 if (ret < 0)
3665 return ret;
3666 btrfs_item_key(path->nodes[0], &found_key, 0);
3667 ret = comp_keys(&found_key, &key);
3668 if (ret < 0)
3669 return 0;
3670 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003671}
3672
Chris Mason3f157a22008-06-25 16:01:31 -04003673/*
3674 * A helper function to walk down the tree starting at min_key, and looking
3675 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003676 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003677 *
3678 * This does not cow, but it does stuff the starting key it finds back
3679 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3680 * key and get a writable path.
3681 *
3682 * This does lock as it descends, and path->keep_locks should be set
3683 * to 1 by the caller.
3684 *
3685 * This honors path->lowest_level to prevent descent past a given level
3686 * of the tree.
3687 *
Chris Masond352ac62008-09-29 15:18:18 -04003688 * min_trans indicates the oldest transaction that you are interested
3689 * in walking through. Any nodes or leaves older than min_trans are
3690 * skipped over (without reading them).
3691 *
Chris Mason3f157a22008-06-25 16:01:31 -04003692 * returns zero if something useful was found, < 0 on error and 1 if there
3693 * was nothing in the tree that matched the search criteria.
3694 */
3695int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003696 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003697 struct btrfs_path *path, int cache_only,
3698 u64 min_trans)
3699{
3700 struct extent_buffer *cur;
3701 struct btrfs_key found_key;
3702 int slot;
Yan96524802008-07-24 12:19:49 -04003703 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003704 u32 nritems;
3705 int level;
3706 int ret = 1;
3707
Chris Mason934d3752008-12-08 16:43:10 -05003708 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003709again:
3710 cur = btrfs_lock_root_node(root);
3711 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003712 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003713 path->nodes[level] = cur;
3714 path->locks[level] = 1;
3715
3716 if (btrfs_header_generation(cur) < min_trans) {
3717 ret = 1;
3718 goto out;
3719 }
3720 while(1) {
3721 nritems = btrfs_header_nritems(cur);
3722 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003723 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003724
Chris Mason323ac952008-10-01 19:05:46 -04003725 /* at the lowest level, we're done, setup the path and exit */
3726 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003727 if (slot >= nritems)
3728 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003729 ret = 0;
3730 path->slots[level] = slot;
3731 btrfs_item_key_to_cpu(cur, &found_key, slot);
3732 goto out;
3733 }
Yan96524802008-07-24 12:19:49 -04003734 if (sret && slot > 0)
3735 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003736 /*
3737 * check this node pointer against the cache_only and
3738 * min_trans parameters. If it isn't in cache or is too
3739 * old, skip to the next one.
3740 */
3741 while(slot < nritems) {
3742 u64 blockptr;
3743 u64 gen;
3744 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003745 struct btrfs_disk_key disk_key;
3746
Chris Mason3f157a22008-06-25 16:01:31 -04003747 blockptr = btrfs_node_blockptr(cur, slot);
3748 gen = btrfs_node_ptr_generation(cur, slot);
3749 if (gen < min_trans) {
3750 slot++;
3751 continue;
3752 }
3753 if (!cache_only)
3754 break;
3755
Chris Masone02119d2008-09-05 16:13:11 -04003756 if (max_key) {
3757 btrfs_node_key(cur, &disk_key, slot);
3758 if (comp_keys(&disk_key, max_key) >= 0) {
3759 ret = 1;
3760 goto out;
3761 }
3762 }
3763
Chris Mason3f157a22008-06-25 16:01:31 -04003764 tmp = btrfs_find_tree_block(root, blockptr,
3765 btrfs_level_size(root, level - 1));
3766
3767 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3768 free_extent_buffer(tmp);
3769 break;
3770 }
3771 if (tmp)
3772 free_extent_buffer(tmp);
3773 slot++;
3774 }
Chris Masone02119d2008-09-05 16:13:11 -04003775find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003776 /*
3777 * we didn't find a candidate key in this node, walk forward
3778 * and find another one
3779 */
3780 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003781 path->slots[level] = slot;
3782 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003783 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003784 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003785 btrfs_release_path(root, path);
3786 goto again;
3787 } else {
3788 goto out;
3789 }
3790 }
3791 /* save our key for returning back */
3792 btrfs_node_key_to_cpu(cur, &found_key, slot);
3793 path->slots[level] = slot;
3794 if (level == path->lowest_level) {
3795 ret = 0;
3796 unlock_up(path, level, 1);
3797 goto out;
3798 }
3799 cur = read_node_slot(root, cur, slot);
3800
3801 btrfs_tree_lock(cur);
3802 path->locks[level - 1] = 1;
3803 path->nodes[level - 1] = cur;
3804 unlock_up(path, level, 1);
3805 }
3806out:
3807 if (ret == 0)
3808 memcpy(min_key, &found_key, sizeof(found_key));
3809 return ret;
3810}
3811
3812/*
3813 * this is similar to btrfs_next_leaf, but does not try to preserve
3814 * and fixup the path. It looks for and returns the next key in the
3815 * tree based on the current path and the cache_only and min_trans
3816 * parameters.
3817 *
3818 * 0 is returned if another key is found, < 0 if there are any errors
3819 * and 1 is returned if there are no higher keys in the tree
3820 *
3821 * path->keep_locks should be set to 1 on the search made before
3822 * calling this function.
3823 */
Chris Masone7a84562008-06-25 16:01:31 -04003824int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003825 struct btrfs_key *key, int lowest_level,
3826 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003827{
3828 int level = lowest_level;
3829 int slot;
3830 struct extent_buffer *c;
3831
Chris Mason934d3752008-12-08 16:43:10 -05003832 WARN_ON(!path->keep_locks);
Chris Masone7a84562008-06-25 16:01:31 -04003833 while(level < BTRFS_MAX_LEVEL) {
3834 if (!path->nodes[level])
3835 return 1;
3836
3837 slot = path->slots[level] + 1;
3838 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003839next:
Chris Masone7a84562008-06-25 16:01:31 -04003840 if (slot >= btrfs_header_nritems(c)) {
3841 level++;
3842 if (level == BTRFS_MAX_LEVEL) {
3843 return 1;
3844 }
3845 continue;
3846 }
3847 if (level == 0)
3848 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003849 else {
3850 u64 blockptr = btrfs_node_blockptr(c, slot);
3851 u64 gen = btrfs_node_ptr_generation(c, slot);
3852
3853 if (cache_only) {
3854 struct extent_buffer *cur;
3855 cur = btrfs_find_tree_block(root, blockptr,
3856 btrfs_level_size(root, level - 1));
3857 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3858 slot++;
3859 if (cur)
3860 free_extent_buffer(cur);
3861 goto next;
3862 }
3863 free_extent_buffer(cur);
3864 }
3865 if (gen < min_trans) {
3866 slot++;
3867 goto next;
3868 }
Chris Masone7a84562008-06-25 16:01:31 -04003869 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003870 }
Chris Masone7a84562008-06-25 16:01:31 -04003871 return 0;
3872 }
3873 return 1;
3874}
3875
Chris Mason7bb86312007-12-11 09:25:06 -05003876/*
Chris Mason925baed2008-06-25 16:01:30 -04003877 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003878 * returns 0 if it found something or 1 if there are no greater leaves.
3879 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003880 */
Chris Mason234b63a2007-03-13 10:46:10 -04003881int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003882{
3883 int slot;
3884 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003885 struct extent_buffer *c;
3886 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003887 struct btrfs_key key;
3888 u32 nritems;
3889 int ret;
3890
3891 nritems = btrfs_header_nritems(path->nodes[0]);
3892 if (nritems == 0) {
3893 return 1;
3894 }
3895
3896 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3897
Chris Mason925baed2008-06-25 16:01:30 -04003898 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003899 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003900 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3901 path->keep_locks = 0;
3902
3903 if (ret < 0)
3904 return ret;
3905
Chris Masona2135012008-06-25 16:01:30 -04003906 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003907 /*
3908 * by releasing the path above we dropped all our locks. A balance
3909 * could have added more items next to the key that used to be
3910 * at the very end of the block. So, check again here and
3911 * advance the path if there are now more items available.
3912 */
Chris Masona2135012008-06-25 16:01:30 -04003913 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003914 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003915 goto done;
3916 }
Chris Masond97e63b2007-02-20 16:40:44 -05003917
Chris Mason234b63a2007-03-13 10:46:10 -04003918 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003919 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003920 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003921
Chris Masond97e63b2007-02-20 16:40:44 -05003922 slot = path->slots[level] + 1;
3923 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003924 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003925 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003926 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003927 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003928 }
Chris Masond97e63b2007-02-20 16:40:44 -05003929 continue;
3930 }
Chris Mason5f39d392007-10-15 16:14:19 -04003931
Chris Mason925baed2008-06-25 16:01:30 -04003932 if (next) {
3933 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003934 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003935 }
Chris Mason5f39d392007-10-15 16:14:19 -04003936
Chris Mason0bd40a72008-07-17 12:54:43 -04003937 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3938 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003939 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003940
Chris Masonca7a79a2008-05-12 12:59:19 -04003941 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003942 if (!path->skip_locking) {
3943 WARN_ON(!btrfs_tree_locked(c));
3944 btrfs_tree_lock(next);
3945 }
Chris Masond97e63b2007-02-20 16:40:44 -05003946 break;
3947 }
3948 path->slots[level] = slot;
3949 while(1) {
3950 level--;
3951 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003952 if (path->locks[level])
3953 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003954 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003955 path->nodes[level] = next;
3956 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003957 if (!path->skip_locking)
3958 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003959 if (!level)
3960 break;
Chris Mason925baed2008-06-25 16:01:30 -04003961 if (level == 1 && path->locks[1] && path->reada)
3962 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003963 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003964 if (!path->skip_locking) {
3965 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3966 btrfs_tree_lock(next);
3967 }
Chris Masond97e63b2007-02-20 16:40:44 -05003968 }
Chris Mason925baed2008-06-25 16:01:30 -04003969done:
3970 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003971 return 0;
3972}
Chris Mason0b86a832008-03-24 15:01:56 -04003973
Chris Mason3f157a22008-06-25 16:01:31 -04003974/*
3975 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3976 * searching until it gets past min_objectid or finds an item of 'type'
3977 *
3978 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3979 */
Chris Mason0b86a832008-03-24 15:01:56 -04003980int btrfs_previous_item(struct btrfs_root *root,
3981 struct btrfs_path *path, u64 min_objectid,
3982 int type)
3983{
3984 struct btrfs_key found_key;
3985 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003986 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003987 int ret;
3988
3989 while(1) {
3990 if (path->slots[0] == 0) {
3991 ret = btrfs_prev_leaf(root, path);
3992 if (ret != 0)
3993 return ret;
3994 } else {
3995 path->slots[0]--;
3996 }
3997 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003998 nritems = btrfs_header_nritems(leaf);
3999 if (nritems == 0)
4000 return 1;
4001 if (path->slots[0] == nritems)
4002 path->slots[0]--;
4003
Chris Mason0b86a832008-03-24 15:01:56 -04004004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4005 if (found_key.type == type)
4006 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004007 if (found_key.objectid < min_objectid)
4008 break;
4009 if (found_key.objectid == min_objectid &&
4010 found_key.type < type)
4011 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004012 }
4013 return 1;
4014}