blob: dd1c03aea2dfe3835f346296d592586748be69ac [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 */
Chris Masone02119d2008-09-05 16:13:11 -0400220int 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];
Aneesha1f39632007-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) {
Aneesha1f39632007-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) {
Aneesha1f39632007-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 }
816 err = map_extent_buffer(eb, offset,
817 sizeof(struct btrfs_disk_key),
818 &map_token, &kaddr,
819 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400820
Chris Mason479965d2007-10-15 16:14:27 -0400821 if (!err) {
822 tmp = (struct btrfs_disk_key *)(kaddr + offset -
823 map_start);
824 } else {
825 read_extent_buffer(eb, &unaligned,
826 offset, sizeof(unaligned));
827 tmp = &unaligned;
828 }
829
Chris Mason5f39d392007-10-15 16:14:19 -0400830 } else {
831 tmp = (struct btrfs_disk_key *)(kaddr + offset -
832 map_start);
833 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500834 ret = comp_keys(tmp, key);
835
836 if (ret < 0)
837 low = mid + 1;
838 else if (ret > 0)
839 high = mid;
840 else {
841 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400842 if (map_token)
843 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500844 return 0;
845 }
846 }
847 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400848 if (map_token)
849 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500850 return 1;
851}
852
Chris Mason97571fd2007-02-24 13:39:08 -0500853/*
854 * simple bin_search frontend that does the right thing for
855 * leaves vs nodes
856 */
Chris Mason5f39d392007-10-15 16:14:19 -0400857static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
858 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500859{
Chris Mason5f39d392007-10-15 16:14:19 -0400860 if (level == 0) {
861 return generic_bin_search(eb,
862 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400863 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400864 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400865 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500866 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400867 return generic_bin_search(eb,
868 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400869 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400870 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400871 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500872 }
873 return -1;
874}
875
Chris Masond352ac62008-09-29 15:18:18 -0400876/* given a node and slot number, this reads the blocks it points to. The
877 * extent buffer is returned with a reference taken (but unlocked).
878 * NULL is returned on error.
879 */
Chris Masone02119d2008-09-05 16:13:11 -0400880static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400881 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500882{
Chris Masonca7a79a2008-05-12 12:59:19 -0400883 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500884 if (slot < 0)
885 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400886 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500887 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400888
889 BUG_ON(level == 0);
890
Chris Masondb945352007-10-15 16:15:53 -0400891 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400892 btrfs_level_size(root, level - 1),
893 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500894}
895
Chris Masond352ac62008-09-29 15:18:18 -0400896/*
897 * node level balancing, used to make sure nodes are in proper order for
898 * item deletion. We balance from the top down, so we have to make sure
899 * that a deletion won't leave an node completely empty later on.
900 */
Chris Masone02119d2008-09-05 16:13:11 -0400901static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500902 struct btrfs_root *root,
903 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500904{
Chris Mason5f39d392007-10-15 16:14:19 -0400905 struct extent_buffer *right = NULL;
906 struct extent_buffer *mid;
907 struct extent_buffer *left = NULL;
908 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500909 int ret = 0;
910 int wret;
911 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500912 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400913 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500914 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500915
916 if (level == 0)
917 return 0;
918
Chris Mason5f39d392007-10-15 16:14:19 -0400919 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400920 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500921 WARN_ON(btrfs_header_generation(mid) != trans->transid);
922
Chris Mason1d4f8a02007-03-13 09:28:32 -0400923 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500924
Chris Mason234b63a2007-03-13 10:46:10 -0400925 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400926 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500927 pslot = path->slots[level + 1];
928
Chris Mason40689472007-03-17 14:29:23 -0400929 /*
930 * deal with the case where there is only one pointer in the root
931 * by promoting the node below to a root
932 */
Chris Mason5f39d392007-10-15 16:14:19 -0400933 if (!parent) {
934 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500935
Chris Mason5f39d392007-10-15 16:14:19 -0400936 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500937 return 0;
938
939 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400940 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400941 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500942 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400943 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500944 BUG_ON(ret);
945
Chris Mason925baed2008-06-25 16:01:30 -0400946 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500947 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400948 spin_unlock(&root->node_lock);
949
Zheng Yan31840ae2008-09-23 13:14:14 -0400950 ret = btrfs_update_extent_ref(trans, root, child->start,
951 mid->start, child->start,
952 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400953 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400954 BUG_ON(ret);
955
Chris Mason0b86a832008-03-24 15:01:56 -0400956 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400957 btrfs_tree_unlock(child);
958 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500959 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400960 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400961 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500962 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400963 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500964 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400965 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400966 btrfs_header_generation(mid),
967 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500968 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400969 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400970 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500971 }
Chris Mason5f39d392007-10-15 16:14:19 -0400972 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400973 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500974 return 0;
975
Chris Mason5f39d392007-10-15 16:14:19 -0400976 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400977 err_on_enospc = 1;
978
Chris Mason5f39d392007-10-15 16:14:19 -0400979 left = read_node_slot(root, parent, pslot - 1);
980 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400981 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400982 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400983 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400984 if (wret) {
985 ret = wret;
986 goto enospc;
987 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400988 }
Chris Mason5f39d392007-10-15 16:14:19 -0400989 right = read_node_slot(root, parent, pslot + 1);
990 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400991 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400992 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400993 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400994 if (wret) {
995 ret = wret;
996 goto enospc;
997 }
998 }
999
1000 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001001 if (left) {
1002 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001003 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001004 if (wret < 0)
1005 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001006 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -04001007 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001008 }
Chris Mason79f95c82007-03-01 15:16:26 -05001009
1010 /*
1011 * then try to empty the right most buffer into the middle
1012 */
Chris Mason5f39d392007-10-15 16:14:19 -04001013 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001014 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001015 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001016 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001017 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001018 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -05001019 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001020 u32 blocksize = right->len;
1021
Chris Mason5f39d392007-10-15 16:14:19 -04001022 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001023 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001024 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001025 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001026 wret = del_ptr(trans, root, path, level + 1, pslot +
1027 1);
Chris Masonbb803952007-03-01 12:04:21 -05001028 if (wret)
1029 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001030 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001031 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001032 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001033 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001034 if (wret)
1035 ret = wret;
1036 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001037 struct btrfs_disk_key right_key;
1038 btrfs_node_key(right, &right_key, 0);
1039 btrfs_set_node_key(parent, &right_key, pslot + 1);
1040 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001041 }
1042 }
Chris Mason5f39d392007-10-15 16:14:19 -04001043 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001044 /*
1045 * we're not allowed to leave a node with one item in the
1046 * tree during a delete. A deletion from lower in the tree
1047 * could try to delete the only pointer in this node.
1048 * So, pull some keys from the left.
1049 * There has to be a left pointer at this point because
1050 * otherwise we would have pulled some pointers from the
1051 * right
1052 */
Chris Mason5f39d392007-10-15 16:14:19 -04001053 BUG_ON(!left);
1054 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001055 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001056 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001057 goto enospc;
1058 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001059 if (wret == 1) {
1060 wret = push_node_left(trans, root, left, mid, 1);
1061 if (wret < 0)
1062 ret = wret;
1063 }
Chris Mason79f95c82007-03-01 15:16:26 -05001064 BUG_ON(wret == 1);
1065 }
Chris Mason5f39d392007-10-15 16:14:19 -04001066 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001067 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001068 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001069 u64 bytenr = mid->start;
1070 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001071
Chris Mason5f39d392007-10-15 16:14:19 -04001072 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001073 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001074 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001075 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001076 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001077 if (wret)
1078 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001079 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001080 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001081 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001082 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001083 if (wret)
1084 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001085 } else {
1086 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001087 struct btrfs_disk_key mid_key;
1088 btrfs_node_key(mid, &mid_key, 0);
1089 btrfs_set_node_key(parent, &mid_key, pslot);
1090 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001091 }
Chris Masonbb803952007-03-01 12:04:21 -05001092
Chris Mason79f95c82007-03-01 15:16:26 -05001093 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001094 if (left) {
1095 if (btrfs_header_nritems(left) > orig_slot) {
1096 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001097 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001098 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001099 path->slots[level + 1] -= 1;
1100 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001101 if (mid) {
1102 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001103 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001104 }
Chris Masonbb803952007-03-01 12:04:21 -05001105 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001106 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001107 path->slots[level] = orig_slot;
1108 }
1109 }
Chris Mason79f95c82007-03-01 15:16:26 -05001110 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001111 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001112 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001113 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001114 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001115enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001116 if (right) {
1117 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001118 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001119 }
1120 if (left) {
1121 if (path->nodes[level] != left)
1122 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001123 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001124 }
Chris Masonbb803952007-03-01 12:04:21 -05001125 return ret;
1126}
1127
Chris Masond352ac62008-09-29 15:18:18 -04001128/* Node balancing for insertion. Here we only split or push nodes around
1129 * when they are completely full. This is also done top down, so we
1130 * have to be pessimistic.
1131 */
Chris Mason98ed5172008-01-03 10:01:48 -05001132static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1133 struct btrfs_root *root,
1134 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001135{
Chris Mason5f39d392007-10-15 16:14:19 -04001136 struct extent_buffer *right = NULL;
1137 struct extent_buffer *mid;
1138 struct extent_buffer *left = NULL;
1139 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001140 int ret = 0;
1141 int wret;
1142 int pslot;
1143 int orig_slot = path->slots[level];
1144 u64 orig_ptr;
1145
1146 if (level == 0)
1147 return 1;
1148
Chris Mason5f39d392007-10-15 16:14:19 -04001149 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001150 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001151 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1152
1153 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001154 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001155 pslot = path->slots[level + 1];
1156
Chris Mason5f39d392007-10-15 16:14:19 -04001157 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001158 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001159
Chris Mason5f39d392007-10-15 16:14:19 -04001160 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001161
1162 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001163 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001164 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001165
1166 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001167 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001168 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1169 wret = 1;
1170 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001171 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001172 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001173 if (ret)
1174 wret = 1;
1175 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001176 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001177 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001178 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001179 }
Chris Masone66f7092007-04-20 13:16:02 -04001180 if (wret < 0)
1181 ret = wret;
1182 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001183 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001184 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001185 btrfs_node_key(mid, &disk_key, 0);
1186 btrfs_set_node_key(parent, &disk_key, pslot);
1187 btrfs_mark_buffer_dirty(parent);
1188 if (btrfs_header_nritems(left) > orig_slot) {
1189 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001190 path->slots[level + 1] -= 1;
1191 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001192 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001193 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001194 } else {
1195 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001196 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001197 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001198 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001199 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001200 }
Chris Masone66f7092007-04-20 13:16:02 -04001201 return 0;
1202 }
Chris Mason925baed2008-06-25 16:01:30 -04001203 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001204 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001205 }
Chris Mason925baed2008-06-25 16:01:30 -04001206 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001207
1208 /*
1209 * then try to empty the right most buffer into the middle
1210 */
Chris Mason5f39d392007-10-15 16:14:19 -04001211 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001212 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001213 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001214 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001215 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1216 wret = 1;
1217 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001218 ret = btrfs_cow_block(trans, root, right,
1219 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001220 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001221 if (ret)
1222 wret = 1;
1223 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001224 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001225 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001226 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001227 }
Chris Masone66f7092007-04-20 13:16:02 -04001228 if (wret < 0)
1229 ret = wret;
1230 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001231 struct btrfs_disk_key disk_key;
1232
1233 btrfs_node_key(right, &disk_key, 0);
1234 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1235 btrfs_mark_buffer_dirty(parent);
1236
1237 if (btrfs_header_nritems(mid) <= orig_slot) {
1238 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001239 path->slots[level + 1] += 1;
1240 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001241 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001242 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001243 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001244 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001245 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001246 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001247 }
Chris Masone66f7092007-04-20 13:16:02 -04001248 return 0;
1249 }
Chris Mason925baed2008-06-25 16:01:30 -04001250 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001251 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001252 }
Chris Masone66f7092007-04-20 13:16:02 -04001253 return 1;
1254}
1255
Chris Mason74123bd2007-02-02 11:05:29 -05001256/*
Chris Masond352ac62008-09-29 15:18:18 -04001257 * readahead one full node of leaves, finding things that are close
1258 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001259 */
Chris Masone02119d2008-09-05 16:13:11 -04001260static noinline void reada_for_search(struct btrfs_root *root,
1261 struct btrfs_path *path,
1262 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001263{
Chris Mason5f39d392007-10-15 16:14:19 -04001264 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001265 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001266 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001267 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001268 u64 lowest_read;
1269 u64 highest_read;
1270 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001271 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001272 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001273 u32 nr;
1274 u32 blocksize;
1275 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001276
Chris Masona6b6e752007-10-15 16:22:39 -04001277 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001278 return;
1279
Chris Mason6702ed42007-08-07 16:15:09 -04001280 if (!path->nodes[level])
1281 return;
1282
Chris Mason5f39d392007-10-15 16:14:19 -04001283 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001284
Chris Mason3c69fae2007-08-07 15:52:22 -04001285 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001286 blocksize = btrfs_level_size(root, level - 1);
1287 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001288 if (eb) {
1289 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001290 return;
1291 }
1292
Chris Mason6b800532007-10-15 16:17:34 -04001293 highest_read = search;
1294 lowest_read = search;
1295
Chris Mason5f39d392007-10-15 16:14:19 -04001296 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001297 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001298 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001299 if (direction < 0) {
1300 if (nr == 0)
1301 break;
1302 nr--;
1303 } else if (direction > 0) {
1304 nr++;
1305 if (nr >= nritems)
1306 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001307 }
Chris Mason01f46652007-12-21 16:24:26 -05001308 if (path->reada < 0 && objectid) {
1309 btrfs_node_key(node, &disk_key, nr);
1310 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1311 break;
1312 }
Chris Mason6b800532007-10-15 16:17:34 -04001313 search = btrfs_node_blockptr(node, nr);
1314 if ((search >= lowest_read && search <= highest_read) ||
Chris Mason6f3577b2008-11-13 09:59:36 -05001315 (search < lowest_read && lowest_read - search <= 16384) ||
1316 (search > highest_read && search - highest_read <= 16384)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001317 readahead_tree_block(root, search, blocksize,
1318 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001319 nread += blocksize;
1320 }
1321 nscan++;
Chris Mason6f3577b2008-11-13 09:59:36 -05001322 if (path->reada < 2 && (nread > (64 * 1024) || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001323 break;
Chris Mason6f3577b2008-11-13 09:59:36 -05001324 if(nread > (256 * 1024) || nscan > 128)
Chris Mason6b800532007-10-15 16:17:34 -04001325 break;
1326
1327 if (search < lowest_read)
1328 lowest_read = search;
1329 if (search > highest_read)
1330 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001331 }
1332}
Chris Mason925baed2008-06-25 16:01:30 -04001333
Chris Masond352ac62008-09-29 15:18:18 -04001334/*
1335 * when we walk down the tree, it is usually safe to unlock the higher layers in
1336 * the tree. The exceptions are when our path goes through slot 0, because operations
1337 * on the tree might require changing key pointers higher up in the tree.
1338 *
1339 * callers might also have set path->keep_locks, which tells this code to
1340 * keep the lock if the path points to the last slot in the block. This is
1341 * part of walking through the tree, and selecting the next slot in the higher
1342 * block.
1343 *
1344 * lowest_unlock sets the lowest level in the tree we're allowed to unlock.
1345 * so if lowest_unlock is 1, level 0 won't be unlocked
1346 */
Chris Masone02119d2008-09-05 16:13:11 -04001347static noinline void unlock_up(struct btrfs_path *path, int level,
1348 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001349{
1350 int i;
1351 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001352 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001353 struct extent_buffer *t;
1354
1355 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1356 if (!path->nodes[i])
1357 break;
1358 if (!path->locks[i])
1359 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001360 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001361 skip_level = i + 1;
1362 continue;
1363 }
Chris Mason051e1b92008-06-25 16:01:30 -04001364 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001365 u32 nritems;
1366 t = path->nodes[i];
1367 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001368 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001369 skip_level = i + 1;
1370 continue;
1371 }
1372 }
Chris Mason051e1b92008-06-25 16:01:30 -04001373 if (skip_level < i && i >= lowest_unlock)
1374 no_skips = 1;
1375
Chris Mason925baed2008-06-25 16:01:30 -04001376 t = path->nodes[i];
1377 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1378 btrfs_tree_unlock(t);
1379 path->locks[i] = 0;
1380 }
1381 }
1382}
1383
Chris Mason3c69fae2007-08-07 15:52:22 -04001384/*
Chris Mason74123bd2007-02-02 11:05:29 -05001385 * look for key in the tree. path is filled in with nodes along the way
1386 * if key is found, we return zero and you can find the item in the leaf
1387 * level of the path (level 0)
1388 *
1389 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001390 * be inserted, and 1 is returned. If there are other errors during the
1391 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001392 *
1393 * if ins_len > 0, nodes and leaves will be split as we walk down the
1394 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1395 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001396 */
Chris Masone089f052007-03-16 16:20:31 -04001397int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1398 *root, struct btrfs_key *key, struct btrfs_path *p, int
1399 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001400{
Chris Mason5f39d392007-10-15 16:14:19 -04001401 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001402 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001403 int slot;
1404 int ret;
1405 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001406 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001407 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001408 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001409 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001410 u64 blocknr;
1411 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001412 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001413
Chris Mason6702ed42007-08-07 16:15:09 -04001414 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001415 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001416 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001417
Chris Mason925baed2008-06-25 16:01:30 -04001418 if (ins_len < 0)
1419 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001420
1421 prealloc_block.objectid = 0;
1422
Chris Masonbb803952007-03-01 12:04:21 -05001423again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001424 if (p->skip_locking)
1425 b = btrfs_root_node(root);
1426 else
1427 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001428
Chris Masoneb60cea2007-02-02 09:18:22 -05001429 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001430 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001431
1432 /*
1433 * setup the path here so we can release it under lock
1434 * contention with the cow code
1435 */
1436 p->nodes[level] = b;
1437 if (!p->skip_locking)
1438 p->locks[level] = 1;
1439
Chris Mason02217ed2007-03-02 16:08:05 -05001440 if (cow) {
1441 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001442
1443 /* is a cow on this block not required */
1444 spin_lock(&root->fs_info->hash_lock);
1445 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001446 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001447 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1448 spin_unlock(&root->fs_info->hash_lock);
1449 goto cow_done;
1450 }
1451 spin_unlock(&root->fs_info->hash_lock);
1452
1453 /* ok, we have to cow, is our old prealloc the right
1454 * size?
1455 */
1456 if (prealloc_block.objectid &&
1457 prealloc_block.offset != b->len) {
1458 btrfs_free_reserved_extent(root,
1459 prealloc_block.objectid,
1460 prealloc_block.offset);
1461 prealloc_block.objectid = 0;
1462 }
1463
1464 /*
1465 * for higher level blocks, try not to allocate blocks
1466 * with the block and the parent locks held.
1467 */
1468 if (level > 1 && !prealloc_block.objectid &&
1469 btrfs_path_lock_waiting(p, level)) {
1470 u32 size = b->len;
1471 u64 hint = b->start;
1472
1473 btrfs_release_path(root, p);
1474 ret = btrfs_reserve_extent(trans, root,
1475 size, size, 0,
1476 hint, (u64)-1,
1477 &prealloc_block, 0);
1478 BUG_ON(ret);
1479 goto again;
1480 }
1481
Chris Masone20d96d2007-03-22 12:13:20 -04001482 wret = btrfs_cow_block(trans, root, b,
1483 p->nodes[level + 1],
1484 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001485 &b, prealloc_block.objectid);
1486 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001487 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001488 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001489 ret = wret;
1490 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001491 }
Chris Mason02217ed2007-03-02 16:08:05 -05001492 }
Chris Mason65b51a02008-08-01 15:11:20 -04001493cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001494 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001495 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001496 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001497 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001498
Chris Masoneb60cea2007-02-02 09:18:22 -05001499 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001500 if (!p->skip_locking)
1501 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001502
Chris Mason123abc82007-03-14 14:14:43 -04001503 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001504 if (ret) {
1505 ret = -1;
1506 goto done;
1507 }
Chris Mason925baed2008-06-25 16:01:30 -04001508
Chris Mason5f39d392007-10-15 16:14:19 -04001509 ret = bin_search(b, key, level, &slot);
1510 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001511 if (ret && slot > 0)
1512 slot -= 1;
1513 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001514 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001515 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001516 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001517 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001518 if (sret) {
1519 ret = sret;
1520 goto done;
1521 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001522 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001523 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001524 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001525 int sret = balance_level(trans, root, p,
1526 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001527 if (sret) {
1528 ret = sret;
1529 goto done;
1530 }
Chris Masonbb803952007-03-01 12:04:21 -05001531 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001532 if (!b) {
1533 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001534 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001535 }
Chris Masonbb803952007-03-01 12:04:21 -05001536 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001537 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001538 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001539 unlock_up(p, level, lowest_unlock);
1540
Chris Mason9f3a7422007-08-07 15:52:19 -04001541 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001542 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001543 ret = 0;
1544 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001545 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001546
Chris Mason594a24e2008-06-25 16:01:30 -04001547 blocknr = btrfs_node_blockptr(b, slot);
1548 gen = btrfs_node_ptr_generation(b, slot);
1549 blocksize = btrfs_level_size(root, level - 1);
1550
1551 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1552 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001553 b = tmp;
1554 } else {
1555 /*
1556 * reduce lock contention at high levels
1557 * of the btree by dropping locks before
1558 * we read.
1559 */
1560 if (level > 1) {
1561 btrfs_release_path(NULL, p);
1562 if (tmp)
1563 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001564 if (should_reada)
1565 reada_for_search(root, p,
1566 level, slot,
1567 key->objectid);
1568
Chris Mason594a24e2008-06-25 16:01:30 -04001569 tmp = read_tree_block(root, blocknr,
1570 blocksize, gen);
1571 if (tmp)
1572 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001573 goto again;
1574 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001575 if (tmp)
1576 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001577 if (should_reada)
1578 reada_for_search(root, p,
1579 level, slot,
1580 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001581 b = read_node_slot(root, b, slot);
1582 }
1583 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001584 if (!p->skip_locking)
1585 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001586 } else {
1587 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001588 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001589 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001590 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001591 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001592 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001593 if (sret) {
1594 ret = sret;
1595 goto done;
1596 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001597 }
Chris Mason925baed2008-06-25 16:01:30 -04001598 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001599 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001600 }
1601 }
Chris Mason65b51a02008-08-01 15:11:20 -04001602 ret = 1;
1603done:
1604 if (prealloc_block.objectid) {
1605 btrfs_free_reserved_extent(root,
1606 prealloc_block.objectid,
1607 prealloc_block.offset);
1608 }
1609
1610 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001611}
1612
Zheng Yan1a40e232008-09-26 10:09:34 -04001613int btrfs_merge_path(struct btrfs_trans_handle *trans,
1614 struct btrfs_root *root,
1615 struct btrfs_key *node_keys,
1616 u64 *nodes, int lowest_level)
1617{
1618 struct extent_buffer *eb;
1619 struct extent_buffer *parent;
1620 struct btrfs_key key;
1621 u64 bytenr;
1622 u64 generation;
1623 u32 blocksize;
1624 int level;
1625 int slot;
1626 int key_match;
1627 int ret;
1628
1629 eb = btrfs_lock_root_node(root);
1630 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1631 BUG_ON(ret);
1632
1633 parent = eb;
1634 while (1) {
1635 level = btrfs_header_level(parent);
1636 if (level == 0 || level <= lowest_level)
1637 break;
1638
1639 ret = bin_search(parent, &node_keys[lowest_level], level,
1640 &slot);
1641 if (ret && slot > 0)
1642 slot--;
1643
1644 bytenr = btrfs_node_blockptr(parent, slot);
1645 if (nodes[level - 1] == bytenr)
1646 break;
1647
1648 blocksize = btrfs_level_size(root, level - 1);
1649 generation = btrfs_node_ptr_generation(parent, slot);
1650 btrfs_node_key_to_cpu(eb, &key, slot);
1651 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1652
Yan Zhengf82d02d2008-10-29 14:49:05 -04001653 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001654 eb = read_tree_block(root, bytenr, blocksize,
1655 generation);
1656 btrfs_tree_lock(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001657 }
1658
1659 /*
1660 * if node keys match and node pointer hasn't been modified
1661 * in the running transaction, we can merge the path. for
1662 * blocks owened by reloc trees, the node pointer check is
1663 * skipped, this is because these blocks are fully controlled
1664 * by the space balance code, no one else can modify them.
1665 */
1666 if (!nodes[level - 1] || !key_match ||
1667 (generation == trans->transid &&
1668 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1669 if (level == 1 || level == lowest_level + 1) {
1670 if (generation == trans->transid) {
1671 btrfs_tree_unlock(eb);
1672 free_extent_buffer(eb);
1673 }
1674 break;
1675 }
1676
1677 if (generation != trans->transid) {
1678 eb = read_tree_block(root, bytenr, blocksize,
1679 generation);
1680 btrfs_tree_lock(eb);
1681 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001682
1683 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1684 &eb, 0);
1685 BUG_ON(ret);
1686
Yan Zhengf82d02d2008-10-29 14:49:05 -04001687 if (root->root_key.objectid ==
1688 BTRFS_TREE_RELOC_OBJECTID) {
1689 if (!nodes[level - 1]) {
1690 nodes[level - 1] = eb->start;
1691 memcpy(&node_keys[level - 1], &key,
1692 sizeof(node_keys[0]));
1693 } else {
1694 WARN_ON(1);
1695 }
1696 }
1697
Zheng Yan1a40e232008-09-26 10:09:34 -04001698 btrfs_tree_unlock(parent);
1699 free_extent_buffer(parent);
1700 parent = eb;
1701 continue;
1702 }
1703
Zheng Yan1a40e232008-09-26 10:09:34 -04001704 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1705 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1706 btrfs_mark_buffer_dirty(parent);
1707
1708 ret = btrfs_inc_extent_ref(trans, root,
1709 nodes[level - 1],
1710 blocksize, parent->start,
1711 btrfs_header_owner(parent),
1712 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001713 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001714 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001715
1716 /*
1717 * If the block was created in the running transaction,
1718 * it's possible this is the last reference to it, so we
1719 * should drop the subtree.
1720 */
1721 if (generation == trans->transid) {
1722 ret = btrfs_drop_subtree(trans, root, eb, parent);
1723 BUG_ON(ret);
1724 btrfs_tree_unlock(eb);
1725 free_extent_buffer(eb);
1726 } else {
1727 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001728 blocksize, parent->start,
1729 btrfs_header_owner(parent),
1730 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001731 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001732 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001733 }
1734 break;
1735 }
1736 btrfs_tree_unlock(parent);
1737 free_extent_buffer(parent);
1738 return 0;
1739}
1740
Chris Mason74123bd2007-02-02 11:05:29 -05001741/*
1742 * adjust the pointers going up the tree, starting at level
1743 * making sure the right key of each node is points to 'key'.
1744 * This is used after shifting pointers to the left, so it stops
1745 * fixing up pointers when a given leaf/node is not in slot 0 of the
1746 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001747 *
1748 * If this fails to write a tree block, it returns -1, but continues
1749 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001750 */
Chris Mason5f39d392007-10-15 16:14:19 -04001751static int fixup_low_keys(struct btrfs_trans_handle *trans,
1752 struct btrfs_root *root, struct btrfs_path *path,
1753 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001754{
1755 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001756 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001757 struct extent_buffer *t;
1758
Chris Mason234b63a2007-03-13 10:46:10 -04001759 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001760 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001761 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001762 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001763 t = path->nodes[i];
1764 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001765 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001766 if (tslot != 0)
1767 break;
1768 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001769 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001770}
1771
Chris Mason74123bd2007-02-02 11:05:29 -05001772/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001773 * update item key.
1774 *
1775 * This function isn't completely safe. It's the caller's responsibility
1776 * that the new key won't break the order
1777 */
1778int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1779 struct btrfs_root *root, struct btrfs_path *path,
1780 struct btrfs_key *new_key)
1781{
1782 struct btrfs_disk_key disk_key;
1783 struct extent_buffer *eb;
1784 int slot;
1785
1786 eb = path->nodes[0];
1787 slot = path->slots[0];
1788 if (slot > 0) {
1789 btrfs_item_key(eb, &disk_key, slot - 1);
1790 if (comp_keys(&disk_key, new_key) >= 0)
1791 return -1;
1792 }
1793 if (slot < btrfs_header_nritems(eb) - 1) {
1794 btrfs_item_key(eb, &disk_key, slot + 1);
1795 if (comp_keys(&disk_key, new_key) <= 0)
1796 return -1;
1797 }
1798
1799 btrfs_cpu_key_to_disk(&disk_key, new_key);
1800 btrfs_set_item_key(eb, &disk_key, slot);
1801 btrfs_mark_buffer_dirty(eb);
1802 if (slot == 0)
1803 fixup_low_keys(trans, root, path, &disk_key, 1);
1804 return 0;
1805}
1806
1807/*
Chris Mason74123bd2007-02-02 11:05:29 -05001808 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001809 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001810 *
1811 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1812 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001813 */
Chris Mason98ed5172008-01-03 10:01:48 -05001814static int push_node_left(struct btrfs_trans_handle *trans,
1815 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001816 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001817{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001819 int src_nritems;
1820 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001821 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001822
Chris Mason5f39d392007-10-15 16:14:19 -04001823 src_nritems = btrfs_header_nritems(src);
1824 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001825 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001826 WARN_ON(btrfs_header_generation(src) != trans->transid);
1827 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001828
Chris Masonbce4eae2008-04-24 14:42:46 -04001829 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001830 return 1;
1831
Chris Masoneb60cea2007-02-02 09:18:22 -05001832 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001833 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001834 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001835
Chris Masonbce4eae2008-04-24 14:42:46 -04001836 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001837 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001838 if (push_items < src_nritems) {
1839 /* leave at least 8 pointers in the node if
1840 * we aren't going to empty it
1841 */
1842 if (src_nritems - push_items < 8) {
1843 if (push_items <= 8)
1844 return 1;
1845 push_items -= 8;
1846 }
1847 }
1848 } else
1849 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001850
Chris Mason5f39d392007-10-15 16:14:19 -04001851 copy_extent_buffer(dst, src,
1852 btrfs_node_key_ptr_offset(dst_nritems),
1853 btrfs_node_key_ptr_offset(0),
1854 push_items * sizeof(struct btrfs_key_ptr));
1855
Chris Masonbb803952007-03-01 12:04:21 -05001856 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001857 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1858 btrfs_node_key_ptr_offset(push_items),
1859 (src_nritems - push_items) *
1860 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001861 }
Chris Mason5f39d392007-10-15 16:14:19 -04001862 btrfs_set_header_nritems(src, src_nritems - push_items);
1863 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1864 btrfs_mark_buffer_dirty(src);
1865 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001866
1867 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1868 BUG_ON(ret);
1869
Chris Masonbb803952007-03-01 12:04:21 -05001870 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001871}
1872
Chris Mason97571fd2007-02-24 13:39:08 -05001873/*
Chris Mason79f95c82007-03-01 15:16:26 -05001874 * try to push data from one node into the next node right in the
1875 * tree.
1876 *
1877 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1878 * error, and > 0 if there was no room in the right hand block.
1879 *
1880 * this will only push up to 1/2 the contents of the left node over
1881 */
Chris Mason5f39d392007-10-15 16:14:19 -04001882static int balance_node_right(struct btrfs_trans_handle *trans,
1883 struct btrfs_root *root,
1884 struct extent_buffer *dst,
1885 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001886{
Chris Mason79f95c82007-03-01 15:16:26 -05001887 int push_items = 0;
1888 int max_push;
1889 int src_nritems;
1890 int dst_nritems;
1891 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001892
Chris Mason7bb86312007-12-11 09:25:06 -05001893 WARN_ON(btrfs_header_generation(src) != trans->transid);
1894 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1895
Chris Mason5f39d392007-10-15 16:14:19 -04001896 src_nritems = btrfs_header_nritems(src);
1897 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001898 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001899 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001900 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001901 }
1902
1903 if (src_nritems < 4) {
1904 return 1;
1905 }
Chris Mason79f95c82007-03-01 15:16:26 -05001906
1907 max_push = src_nritems / 2 + 1;
1908 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001909 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001910 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001911 }
Yan252c38f2007-08-29 09:11:44 -04001912
Chris Mason79f95c82007-03-01 15:16:26 -05001913 if (max_push < push_items)
1914 push_items = max_push;
1915
Chris Mason5f39d392007-10-15 16:14:19 -04001916 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1917 btrfs_node_key_ptr_offset(0),
1918 (dst_nritems) *
1919 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001920
Chris Mason5f39d392007-10-15 16:14:19 -04001921 copy_extent_buffer(dst, src,
1922 btrfs_node_key_ptr_offset(0),
1923 btrfs_node_key_ptr_offset(src_nritems - push_items),
1924 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001925
Chris Mason5f39d392007-10-15 16:14:19 -04001926 btrfs_set_header_nritems(src, src_nritems - push_items);
1927 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001928
Chris Mason5f39d392007-10-15 16:14:19 -04001929 btrfs_mark_buffer_dirty(src);
1930 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001931
1932 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1933 BUG_ON(ret);
1934
Chris Mason79f95c82007-03-01 15:16:26 -05001935 return ret;
1936}
1937
1938/*
Chris Mason97571fd2007-02-24 13:39:08 -05001939 * helper function to insert a new root level in the tree.
1940 * A new node is allocated, and a single item is inserted to
1941 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001942 *
1943 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001944 */
Chris Mason98ed5172008-01-03 10:01:48 -05001945static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001946 struct btrfs_root *root,
1947 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001948{
Chris Mason7bb86312007-12-11 09:25:06 -05001949 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001950 struct extent_buffer *lower;
1951 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001952 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001953 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001954 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001955
1956 BUG_ON(path->nodes[level]);
1957 BUG_ON(path->nodes[level-1] != root->node);
1958
Chris Mason7bb86312007-12-11 09:25:06 -05001959 lower = path->nodes[level-1];
1960 if (level == 1)
1961 btrfs_item_key(lower, &lower_key, 0);
1962 else
1963 btrfs_node_key(lower, &lower_key, 0);
1964
Zheng Yan31840ae2008-09-23 13:14:14 -04001965 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1966 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001967 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001968 if (IS_ERR(c))
1969 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001970
Chris Mason5f39d392007-10-15 16:14:19 -04001971 memset_extent_buffer(c, 0, 0, root->nodesize);
1972 btrfs_set_header_nritems(c, 1);
1973 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001974 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001975 btrfs_set_header_generation(c, trans->transid);
1976 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001977
Chris Mason5f39d392007-10-15 16:14:19 -04001978 write_extent_buffer(c, root->fs_info->fsid,
1979 (unsigned long)btrfs_header_fsid(c),
1980 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001981
1982 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1983 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1984 BTRFS_UUID_SIZE);
1985
Chris Mason5f39d392007-10-15 16:14:19 -04001986 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001987 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001988 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001989 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001990
1991 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001992
1993 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001994
Chris Mason925baed2008-06-25 16:01:30 -04001995 spin_lock(&root->node_lock);
1996 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001997 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001998 spin_unlock(&root->node_lock);
1999
Zheng Yan31840ae2008-09-23 13:14:14 -04002000 ret = btrfs_update_extent_ref(trans, root, lower->start,
2001 lower->start, c->start,
2002 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002003 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002004 BUG_ON(ret);
2005
Chris Mason925baed2008-06-25 16:01:30 -04002006 /* the super has an extra ref to root->node */
2007 free_extent_buffer(old);
2008
Chris Mason0b86a832008-03-24 15:01:56 -04002009 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002010 extent_buffer_get(c);
2011 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002012 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002013 path->slots[level] = 0;
2014 return 0;
2015}
2016
Chris Mason74123bd2007-02-02 11:05:29 -05002017/*
2018 * worker function to insert a single pointer in a node.
2019 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002020 *
Chris Mason74123bd2007-02-02 11:05:29 -05002021 * slot and level indicate where you want the key to go, and
2022 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002023 *
2024 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002025 */
Chris Masone089f052007-03-16 16:20:31 -04002026static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2027 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002028 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002029{
Chris Mason5f39d392007-10-15 16:14:19 -04002030 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002031 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002032
2033 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002034 lower = path->nodes[level];
2035 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002036 if (slot > nritems)
2037 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002038 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002039 BUG();
2040 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002041 memmove_extent_buffer(lower,
2042 btrfs_node_key_ptr_offset(slot + 1),
2043 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002044 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002045 }
Chris Mason5f39d392007-10-15 16:14:19 -04002046 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002047 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002048 WARN_ON(trans->transid == 0);
2049 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002050 btrfs_set_header_nritems(lower, nritems + 1);
2051 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002052 return 0;
2053}
2054
Chris Mason97571fd2007-02-24 13:39:08 -05002055/*
2056 * split the node at the specified level in path in two.
2057 * The path is corrected to point to the appropriate node after the split
2058 *
2059 * Before splitting this tries to make some room in the node by pushing
2060 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002061 *
2062 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002063 */
Chris Masone02119d2008-09-05 16:13:11 -04002064static noinline int split_node(struct btrfs_trans_handle *trans,
2065 struct btrfs_root *root,
2066 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002067{
Chris Mason5f39d392007-10-15 16:14:19 -04002068 struct extent_buffer *c;
2069 struct extent_buffer *split;
2070 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002071 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002072 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002073 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002074 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002075
Chris Mason5f39d392007-10-15 16:14:19 -04002076 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002077 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002078 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002079 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002080 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002081 if (ret)
2082 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002083 } else {
2084 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002085 c = path->nodes[level];
2086 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002087 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002088 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002089 if (ret < 0)
2090 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002091 }
Chris Masone66f7092007-04-20 13:16:02 -04002092
Chris Mason5f39d392007-10-15 16:14:19 -04002093 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002094
Chris Mason925baed2008-06-25 16:01:30 -04002095 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002096 path->nodes[level + 1]->start,
2097 root->root_key.objectid,
2098 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002099 if (IS_ERR(split))
2100 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002101
Chris Mason5f39d392007-10-15 16:14:19 -04002102 btrfs_set_header_flags(split, btrfs_header_flags(c));
2103 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002104 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002105 btrfs_set_header_generation(split, trans->transid);
2106 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002107 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002108 write_extent_buffer(split, root->fs_info->fsid,
2109 (unsigned long)btrfs_header_fsid(split),
2110 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002111 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2112 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2113 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002114
Chris Mason7518a232007-03-12 12:01:18 -04002115 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002116
2117 copy_extent_buffer(split, c,
2118 btrfs_node_key_ptr_offset(0),
2119 btrfs_node_key_ptr_offset(mid),
2120 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2121 btrfs_set_header_nritems(split, c_nritems - mid);
2122 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002123 ret = 0;
2124
Chris Mason5f39d392007-10-15 16:14:19 -04002125 btrfs_mark_buffer_dirty(c);
2126 btrfs_mark_buffer_dirty(split);
2127
2128 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002129 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002130 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002131 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002132 if (wret)
2133 ret = wret;
2134
Zheng Yan31840ae2008-09-23 13:14:14 -04002135 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2136 BUG_ON(ret);
2137
Chris Mason5de08d72007-02-24 06:24:44 -05002138 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002139 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002140 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002141 free_extent_buffer(c);
2142 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002143 path->slots[level + 1] += 1;
2144 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002145 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002146 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002147 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002148 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002149}
2150
Chris Mason74123bd2007-02-02 11:05:29 -05002151/*
2152 * how many bytes are required to store the items in a leaf. start
2153 * and nr indicate which items in the leaf to check. This totals up the
2154 * space used both by the item structs and the item data
2155 */
Chris Mason5f39d392007-10-15 16:14:19 -04002156static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002157{
2158 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002159 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002160 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002161
2162 if (!nr)
2163 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002164 data_len = btrfs_item_end_nr(l, start);
2165 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002166 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002167 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168 return data_len;
2169}
2170
Chris Mason74123bd2007-02-02 11:05:29 -05002171/*
Chris Masond4dbff92007-04-04 14:08:15 -04002172 * The space between the end of the leaf items and
2173 * the start of the leaf data. IOW, how much room
2174 * the leaf has left for both items and data
2175 */
Chris Masone02119d2008-09-05 16:13:11 -04002176int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2177 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002178{
Chris Mason5f39d392007-10-15 16:14:19 -04002179 int nritems = btrfs_header_nritems(leaf);
2180 int ret;
2181 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2182 if (ret < 0) {
2183 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002184 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002185 leaf_space_used(leaf, 0, nritems), nritems);
2186 }
2187 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002188}
2189
2190/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002191 * push some data in the path leaf to the right, trying to free up at
2192 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002193 *
2194 * returns 1 if the push failed because the other node didn't have enough
2195 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002196 */
Chris Masone089f052007-03-16 16:20:31 -04002197static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002198 *root, struct btrfs_path *path, int data_size,
2199 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002200{
Chris Mason5f39d392007-10-15 16:14:19 -04002201 struct extent_buffer *left = path->nodes[0];
2202 struct extent_buffer *right;
2203 struct extent_buffer *upper;
2204 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002205 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002206 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002207 int free_space;
2208 int push_space = 0;
2209 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002210 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002211 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002212 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002213 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002214 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002215 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002216 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002217
2218 slot = path->slots[1];
2219 if (!path->nodes[1]) {
2220 return 1;
2221 }
2222 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002223 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002224 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002225
Chris Masona2135012008-06-25 16:01:30 -04002226 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2227
Chris Masonca7a79a2008-05-12 12:59:19 -04002228 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002229 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002230 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002231 if (free_space < data_size + sizeof(struct btrfs_item))
2232 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002233
Chris Mason5f39d392007-10-15 16:14:19 -04002234 /* cow and double check */
2235 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002236 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002237 if (ret)
2238 goto out_unlock;
2239
Chris Mason5f39d392007-10-15 16:14:19 -04002240 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002241 if (free_space < data_size + sizeof(struct btrfs_item))
2242 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002243
2244 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002245 if (left_nritems == 0)
2246 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002247
Chris Mason34a38212007-11-07 13:31:03 -05002248 if (empty)
2249 nr = 0;
2250 else
2251 nr = 1;
2252
Zheng Yan31840ae2008-09-23 13:14:14 -04002253 if (path->slots[0] >= left_nritems)
2254 push_space += data_size + sizeof(*item);
2255
Chris Mason34a38212007-11-07 13:31:03 -05002256 i = left_nritems - 1;
2257 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002258 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002259
Zheng Yan31840ae2008-09-23 13:14:14 -04002260 if (!empty && push_items > 0) {
2261 if (path->slots[0] > i)
2262 break;
2263 if (path->slots[0] == i) {
2264 int space = btrfs_leaf_free_space(root, left);
2265 if (space + push_space * 2 > free_space)
2266 break;
2267 }
2268 }
2269
Chris Mason00ec4c52007-02-24 12:47:20 -05002270 if (path->slots[0] == i)
2271 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002272
2273 if (!left->map_token) {
2274 map_extent_buffer(left, (unsigned long)item,
2275 sizeof(struct btrfs_item),
2276 &left->map_token, &left->kaddr,
2277 &left->map_start, &left->map_len,
2278 KM_USER1);
2279 }
2280
2281 this_item_size = btrfs_item_size(left, item);
2282 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002283 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002284
Chris Mason00ec4c52007-02-24 12:47:20 -05002285 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002286 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002287 if (i == 0)
2288 break;
2289 i--;
Chris Masondb945352007-10-15 16:15:53 -04002290 }
2291 if (left->map_token) {
2292 unmap_extent_buffer(left, left->map_token, KM_USER1);
2293 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002294 }
Chris Mason5f39d392007-10-15 16:14:19 -04002295
Chris Mason925baed2008-06-25 16:01:30 -04002296 if (push_items == 0)
2297 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002298
Chris Mason34a38212007-11-07 13:31:03 -05002299 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002300 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002301
Chris Mason00ec4c52007-02-24 12:47:20 -05002302 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002303 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002304
Chris Mason5f39d392007-10-15 16:14:19 -04002305 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002306 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002307
Chris Mason00ec4c52007-02-24 12:47:20 -05002308 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002309 data_end = leaf_data_end(root, right);
2310 memmove_extent_buffer(right,
2311 btrfs_leaf_data(right) + data_end - push_space,
2312 btrfs_leaf_data(right) + data_end,
2313 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2314
Chris Mason00ec4c52007-02-24 12:47:20 -05002315 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002316 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002317 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2318 btrfs_leaf_data(left) + leaf_data_end(root, left),
2319 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002320
2321 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2322 btrfs_item_nr_offset(0),
2323 right_nritems * sizeof(struct btrfs_item));
2324
Chris Mason00ec4c52007-02-24 12:47:20 -05002325 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002326 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2327 btrfs_item_nr_offset(left_nritems - push_items),
2328 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002329
2330 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002331 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002332 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002333 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002334 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002335 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002336 if (!right->map_token) {
2337 map_extent_buffer(right, (unsigned long)item,
2338 sizeof(struct btrfs_item),
2339 &right->map_token, &right->kaddr,
2340 &right->map_start, &right->map_len,
2341 KM_USER1);
2342 }
2343 push_space -= btrfs_item_size(right, item);
2344 btrfs_set_item_offset(right, item, push_space);
2345 }
2346
2347 if (right->map_token) {
2348 unmap_extent_buffer(right, right->map_token, KM_USER1);
2349 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002350 }
Chris Mason7518a232007-03-12 12:01:18 -04002351 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002352 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002353
Chris Mason34a38212007-11-07 13:31:03 -05002354 if (left_nritems)
2355 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002356 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002357
Zheng Yan31840ae2008-09-23 13:14:14 -04002358 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2359 BUG_ON(ret);
2360
Chris Mason5f39d392007-10-15 16:14:19 -04002361 btrfs_item_key(right, &disk_key, 0);
2362 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002363 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002364
Chris Mason00ec4c52007-02-24 12:47:20 -05002365 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002366 if (path->slots[0] >= left_nritems) {
2367 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002368 if (btrfs_header_nritems(path->nodes[0]) == 0)
2369 clean_tree_block(trans, root, path->nodes[0]);
2370 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002371 free_extent_buffer(path->nodes[0]);
2372 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002373 path->slots[1] += 1;
2374 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002375 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002376 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002377 }
2378 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002379
2380out_unlock:
2381 btrfs_tree_unlock(right);
2382 free_extent_buffer(right);
2383 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002384}
Chris Mason925baed2008-06-25 16:01:30 -04002385
Chris Mason00ec4c52007-02-24 12:47:20 -05002386/*
Chris Mason74123bd2007-02-02 11:05:29 -05002387 * push some data in the path leaf to the left, trying to free up at
2388 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2389 */
Chris Masone089f052007-03-16 16:20:31 -04002390static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002391 *root, struct btrfs_path *path, int data_size,
2392 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002393{
Chris Mason5f39d392007-10-15 16:14:19 -04002394 struct btrfs_disk_key disk_key;
2395 struct extent_buffer *right = path->nodes[0];
2396 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002397 int slot;
2398 int i;
2399 int free_space;
2400 int push_space = 0;
2401 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002402 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002403 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002404 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002405 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002406 int ret = 0;
2407 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002408 u32 this_item_size;
2409 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002410
2411 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002412 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002413 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002414 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002415 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002416
Chris Mason3685f792007-10-19 09:23:27 -04002417 right_nritems = btrfs_header_nritems(right);
2418 if (right_nritems == 0) {
2419 return 1;
2420 }
2421
Chris Masona2135012008-06-25 16:01:30 -04002422 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2423
Chris Masonca7a79a2008-05-12 12:59:19 -04002424 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002425 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002426 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002427 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002428 ret = 1;
2429 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002430 }
Chris Mason02217ed2007-03-02 16:08:05 -05002431
2432 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002433 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002434 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002435 if (ret) {
2436 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002437 ret = 1;
2438 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002439 }
Chris Mason3685f792007-10-19 09:23:27 -04002440
Chris Mason123abc82007-03-14 14:14:43 -04002441 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002442 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002443 ret = 1;
2444 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002445 }
2446
Chris Mason34a38212007-11-07 13:31:03 -05002447 if (empty)
2448 nr = right_nritems;
2449 else
2450 nr = right_nritems - 1;
2451
2452 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002453 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002454 if (!right->map_token) {
2455 map_extent_buffer(right, (unsigned long)item,
2456 sizeof(struct btrfs_item),
2457 &right->map_token, &right->kaddr,
2458 &right->map_start, &right->map_len,
2459 KM_USER1);
2460 }
2461
Zheng Yan31840ae2008-09-23 13:14:14 -04002462 if (!empty && push_items > 0) {
2463 if (path->slots[0] < i)
2464 break;
2465 if (path->slots[0] == i) {
2466 int space = btrfs_leaf_free_space(root, right);
2467 if (space + push_space * 2 > free_space)
2468 break;
2469 }
2470 }
2471
Chris Masonbe0e5c02007-01-26 15:51:26 -05002472 if (path->slots[0] == i)
2473 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002474
2475 this_item_size = btrfs_item_size(right, item);
2476 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002477 break;
Chris Masondb945352007-10-15 16:15:53 -04002478
Chris Masonbe0e5c02007-01-26 15:51:26 -05002479 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002480 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002481 }
Chris Masondb945352007-10-15 16:15:53 -04002482
2483 if (right->map_token) {
2484 unmap_extent_buffer(right, right->map_token, KM_USER1);
2485 right->map_token = NULL;
2486 }
2487
Chris Masonbe0e5c02007-01-26 15:51:26 -05002488 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002489 ret = 1;
2490 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002491 }
Chris Mason34a38212007-11-07 13:31:03 -05002492 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002493 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002494
Chris Masonbe0e5c02007-01-26 15:51:26 -05002495 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002496 copy_extent_buffer(left, right,
2497 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2498 btrfs_item_nr_offset(0),
2499 push_items * sizeof(struct btrfs_item));
2500
Chris Mason123abc82007-03-14 14:14:43 -04002501 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002502 btrfs_item_offset_nr(right, push_items -1);
2503
2504 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002505 leaf_data_end(root, left) - push_space,
2506 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002507 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002508 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002509 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002510 BUG_ON(old_left_nritems < 0);
2511
Chris Masondb945352007-10-15 16:15:53 -04002512 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002513 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002514 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002515
Chris Mason5f39d392007-10-15 16:14:19 -04002516 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002517 if (!left->map_token) {
2518 map_extent_buffer(left, (unsigned long)item,
2519 sizeof(struct btrfs_item),
2520 &left->map_token, &left->kaddr,
2521 &left->map_start, &left->map_len,
2522 KM_USER1);
2523 }
2524
Chris Mason5f39d392007-10-15 16:14:19 -04002525 ioff = btrfs_item_offset(left, item);
2526 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002527 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002528 }
Chris Mason5f39d392007-10-15 16:14:19 -04002529 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002530 if (left->map_token) {
2531 unmap_extent_buffer(left, left->map_token, KM_USER1);
2532 left->map_token = NULL;
2533 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002534
2535 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002536 if (push_items > right_nritems) {
2537 printk("push items %d nr %u\n", push_items, right_nritems);
2538 WARN_ON(1);
2539 }
Chris Mason5f39d392007-10-15 16:14:19 -04002540
Chris Mason34a38212007-11-07 13:31:03 -05002541 if (push_items < right_nritems) {
2542 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2543 leaf_data_end(root, right);
2544 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2545 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2546 btrfs_leaf_data(right) +
2547 leaf_data_end(root, right), push_space);
2548
2549 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002550 btrfs_item_nr_offset(push_items),
2551 (btrfs_header_nritems(right) - push_items) *
2552 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002553 }
Yaneef1c492007-11-26 10:58:13 -05002554 right_nritems -= push_items;
2555 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002556 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002557 for (i = 0; i < right_nritems; i++) {
2558 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002559
2560 if (!right->map_token) {
2561 map_extent_buffer(right, (unsigned long)item,
2562 sizeof(struct btrfs_item),
2563 &right->map_token, &right->kaddr,
2564 &right->map_start, &right->map_len,
2565 KM_USER1);
2566 }
2567
2568 push_space = push_space - btrfs_item_size(right, item);
2569 btrfs_set_item_offset(right, item, push_space);
2570 }
2571 if (right->map_token) {
2572 unmap_extent_buffer(right, right->map_token, KM_USER1);
2573 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002574 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002575
Chris Mason5f39d392007-10-15 16:14:19 -04002576 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002577 if (right_nritems)
2578 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002579
Zheng Yan31840ae2008-09-23 13:14:14 -04002580 ret = btrfs_update_ref(trans, root, right, left,
2581 old_left_nritems, push_items);
2582 BUG_ON(ret);
2583
Chris Mason5f39d392007-10-15 16:14:19 -04002584 btrfs_item_key(right, &disk_key, 0);
2585 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002586 if (wret)
2587 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002588
2589 /* then fixup the leaf pointer in the path */
2590 if (path->slots[0] < push_items) {
2591 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002592 if (btrfs_header_nritems(path->nodes[0]) == 0)
2593 clean_tree_block(trans, root, path->nodes[0]);
2594 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002595 free_extent_buffer(path->nodes[0]);
2596 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002597 path->slots[1] -= 1;
2598 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002599 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002600 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002601 path->slots[0] -= push_items;
2602 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002603 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002604 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002605out:
2606 btrfs_tree_unlock(left);
2607 free_extent_buffer(left);
2608 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609}
2610
Chris Mason74123bd2007-02-02 11:05:29 -05002611/*
2612 * split the path's leaf in two, making sure there is at least data_size
2613 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002614 *
2615 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002616 */
Chris Masone02119d2008-09-05 16:13:11 -04002617static noinline int split_leaf(struct btrfs_trans_handle *trans,
2618 struct btrfs_root *root,
2619 struct btrfs_key *ins_key,
2620 struct btrfs_path *path, int data_size,
2621 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002622{
Chris Mason5f39d392007-10-15 16:14:19 -04002623 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002624 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002625 int mid;
2626 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002627 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002628 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002629 int data_copy_size;
2630 int rt_data_off;
2631 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002632 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002633 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002634 int double_split;
2635 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002636 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002637
Chris Masoncc0c5532007-10-25 15:42:57 -04002638 if (extend)
2639 space_needed = data_size;
2640
Chris Mason40689472007-03-17 14:29:23 -04002641 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002642 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002643 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002644 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002645 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002646 }
2647 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002648 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002649 if (wret < 0)
2650 return wret;
2651 }
2652 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002653
Chris Mason3685f792007-10-19 09:23:27 -04002654 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002655 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002656 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002657 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002658
Chris Mason5c680ed2007-02-22 11:39:13 -05002659 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002660 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002661 if (ret)
2662 return ret;
2663 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002664again:
2665 double_split = 0;
2666 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002667 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002668 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002669 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002670
Chris Mason925baed2008-06-25 16:01:30 -04002671 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002672 path->nodes[1]->start,
2673 root->root_key.objectid,
2674 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002675 if (IS_ERR(right)) {
2676 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002678 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002679
Chris Mason5f39d392007-10-15 16:14:19 -04002680 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002681 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002682 btrfs_set_header_generation(right, trans->transid);
2683 btrfs_set_header_owner(right, root->root_key.objectid);
2684 btrfs_set_header_level(right, 0);
2685 write_extent_buffer(right, root->fs_info->fsid,
2686 (unsigned long)btrfs_header_fsid(right),
2687 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002688
2689 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2690 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2691 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002692 if (mid <= slot) {
2693 if (nritems == 1 ||
2694 leaf_space_used(l, mid, nritems - mid) + space_needed >
2695 BTRFS_LEAF_DATA_SIZE(root)) {
2696 if (slot >= nritems) {
2697 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002698 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002699 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002700 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002701 path->slots[1] + 1, 1);
2702 if (wret)
2703 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002704
2705 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002706 free_extent_buffer(path->nodes[0]);
2707 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002708 path->slots[0] = 0;
2709 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002710 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002711 return ret;
2712 }
2713 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002714 if (mid != nritems &&
2715 leaf_space_used(l, mid, nritems - mid) +
2716 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2717 double_split = 1;
2718 }
Chris Masond4dbff92007-04-04 14:08:15 -04002719 }
2720 } else {
2721 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2722 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002723 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002724 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002725 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002726 wret = insert_ptr(trans, root, path,
2727 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002728 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002729 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002730 if (wret)
2731 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002732 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002733 free_extent_buffer(path->nodes[0]);
2734 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002735 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002736 if (path->slots[1] == 0) {
2737 wret = fixup_low_keys(trans, root,
2738 path, &disk_key, 1);
2739 if (wret)
2740 ret = wret;
2741 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002742 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002743 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002744 } else if (extend && slot == 0) {
2745 mid = 1;
2746 } else {
2747 mid = slot;
2748 if (mid != nritems &&
2749 leaf_space_used(l, mid, nritems - mid) +
2750 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2751 double_split = 1;
2752 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002753 }
Chris Masond4dbff92007-04-04 14:08:15 -04002754 }
2755 }
Chris Mason5f39d392007-10-15 16:14:19 -04002756 nritems = nritems - mid;
2757 btrfs_set_header_nritems(right, nritems);
2758 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2759
2760 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2761 btrfs_item_nr_offset(mid),
2762 nritems * sizeof(struct btrfs_item));
2763
2764 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002765 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2766 data_copy_size, btrfs_leaf_data(l) +
2767 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002768
Chris Mason5f39d392007-10-15 16:14:19 -04002769 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2770 btrfs_item_end_nr(l, mid);
2771
2772 for (i = 0; i < nritems; i++) {
2773 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002774 u32 ioff;
2775
2776 if (!right->map_token) {
2777 map_extent_buffer(right, (unsigned long)item,
2778 sizeof(struct btrfs_item),
2779 &right->map_token, &right->kaddr,
2780 &right->map_start, &right->map_len,
2781 KM_USER1);
2782 }
2783
2784 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002785 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002786 }
Chris Mason74123bd2007-02-02 11:05:29 -05002787
Chris Masondb945352007-10-15 16:15:53 -04002788 if (right->map_token) {
2789 unmap_extent_buffer(right, right->map_token, KM_USER1);
2790 right->map_token = NULL;
2791 }
2792
Chris Mason5f39d392007-10-15 16:14:19 -04002793 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002794 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002795 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002796 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2797 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002798 if (wret)
2799 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002800
2801 btrfs_mark_buffer_dirty(right);
2802 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002803 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002804
Zheng Yan31840ae2008-09-23 13:14:14 -04002805 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2806 BUG_ON(ret);
2807
Chris Masonbe0e5c02007-01-26 15:51:26 -05002808 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002809 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002810 free_extent_buffer(path->nodes[0]);
2811 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002812 path->slots[0] -= mid;
2813 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002814 } else {
2815 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002816 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002817 }
Chris Mason5f39d392007-10-15 16:14:19 -04002818
Chris Masoneb60cea2007-02-02 09:18:22 -05002819 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002820
Chris Masoncc0c5532007-10-25 15:42:57 -04002821 if (double_split) {
2822 BUG_ON(num_doubles != 0);
2823 num_doubles++;
2824 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002825 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002826 return ret;
2827}
2828
Chris Masond352ac62008-09-29 15:18:18 -04002829/*
2830 * make the item pointed to by the path smaller. new_size indicates
2831 * how small to make it, and from_end tells us if we just chop bytes
2832 * off the end of the item or if we shift the item to chop bytes off
2833 * the front.
2834 */
Chris Masonb18c6682007-04-17 13:26:50 -04002835int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2836 struct btrfs_root *root,
2837 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002838 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002839{
2840 int ret = 0;
2841 int slot;
2842 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002843 struct extent_buffer *leaf;
2844 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002845 u32 nritems;
2846 unsigned int data_end;
2847 unsigned int old_data_start;
2848 unsigned int old_size;
2849 unsigned int size_diff;
2850 int i;
2851
2852 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002853 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002854 slot = path->slots[0];
2855
2856 old_size = btrfs_item_size_nr(leaf, slot);
2857 if (old_size == new_size)
2858 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002859
Chris Mason5f39d392007-10-15 16:14:19 -04002860 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002861 data_end = leaf_data_end(root, leaf);
2862
Chris Mason5f39d392007-10-15 16:14:19 -04002863 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002864
Chris Masonb18c6682007-04-17 13:26:50 -04002865 size_diff = old_size - new_size;
2866
2867 BUG_ON(slot < 0);
2868 BUG_ON(slot >= nritems);
2869
2870 /*
2871 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2872 */
2873 /* first correct the data pointers */
2874 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002875 u32 ioff;
2876 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002877
2878 if (!leaf->map_token) {
2879 map_extent_buffer(leaf, (unsigned long)item,
2880 sizeof(struct btrfs_item),
2881 &leaf->map_token, &leaf->kaddr,
2882 &leaf->map_start, &leaf->map_len,
2883 KM_USER1);
2884 }
2885
Chris Mason5f39d392007-10-15 16:14:19 -04002886 ioff = btrfs_item_offset(leaf, item);
2887 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002888 }
Chris Masondb945352007-10-15 16:15:53 -04002889
2890 if (leaf->map_token) {
2891 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2892 leaf->map_token = NULL;
2893 }
2894
Chris Masonb18c6682007-04-17 13:26:50 -04002895 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002896 if (from_end) {
2897 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2898 data_end + size_diff, btrfs_leaf_data(leaf) +
2899 data_end, old_data_start + new_size - data_end);
2900 } else {
2901 struct btrfs_disk_key disk_key;
2902 u64 offset;
2903
2904 btrfs_item_key(leaf, &disk_key, slot);
2905
2906 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2907 unsigned long ptr;
2908 struct btrfs_file_extent_item *fi;
2909
2910 fi = btrfs_item_ptr(leaf, slot,
2911 struct btrfs_file_extent_item);
2912 fi = (struct btrfs_file_extent_item *)(
2913 (unsigned long)fi - size_diff);
2914
2915 if (btrfs_file_extent_type(leaf, fi) ==
2916 BTRFS_FILE_EXTENT_INLINE) {
2917 ptr = btrfs_item_ptr_offset(leaf, slot);
2918 memmove_extent_buffer(leaf, ptr,
2919 (unsigned long)fi,
2920 offsetof(struct btrfs_file_extent_item,
2921 disk_bytenr));
2922 }
2923 }
2924
2925 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2926 data_end + size_diff, btrfs_leaf_data(leaf) +
2927 data_end, old_data_start - data_end);
2928
2929 offset = btrfs_disk_key_offset(&disk_key);
2930 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2931 btrfs_set_item_key(leaf, &disk_key, slot);
2932 if (slot == 0)
2933 fixup_low_keys(trans, root, path, &disk_key, 1);
2934 }
Chris Mason5f39d392007-10-15 16:14:19 -04002935
2936 item = btrfs_item_nr(leaf, slot);
2937 btrfs_set_item_size(leaf, item, new_size);
2938 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002939
2940 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002941 if (btrfs_leaf_free_space(root, leaf) < 0) {
2942 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002943 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002944 }
Chris Masonb18c6682007-04-17 13:26:50 -04002945 return ret;
2946}
2947
Chris Masond352ac62008-09-29 15:18:18 -04002948/*
2949 * make the item pointed to by the path bigger, data_size is the new size.
2950 */
Chris Mason5f39d392007-10-15 16:14:19 -04002951int btrfs_extend_item(struct btrfs_trans_handle *trans,
2952 struct btrfs_root *root, struct btrfs_path *path,
2953 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002954{
2955 int ret = 0;
2956 int slot;
2957 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002958 struct extent_buffer *leaf;
2959 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002960 u32 nritems;
2961 unsigned int data_end;
2962 unsigned int old_data;
2963 unsigned int old_size;
2964 int i;
2965
2966 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002967 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002968
Chris Mason5f39d392007-10-15 16:14:19 -04002969 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002970 data_end = leaf_data_end(root, leaf);
2971
Chris Mason5f39d392007-10-15 16:14:19 -04002972 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2973 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002974 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002975 }
Chris Mason6567e832007-04-16 09:22:45 -04002976 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002977 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002978
2979 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002980 if (slot >= nritems) {
2981 btrfs_print_leaf(root, leaf);
2982 printk("slot %d too large, nritems %d\n", slot, nritems);
2983 BUG_ON(1);
2984 }
Chris Mason6567e832007-04-16 09:22:45 -04002985
2986 /*
2987 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2988 */
2989 /* first correct the data pointers */
2990 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002991 u32 ioff;
2992 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002993
2994 if (!leaf->map_token) {
2995 map_extent_buffer(leaf, (unsigned long)item,
2996 sizeof(struct btrfs_item),
2997 &leaf->map_token, &leaf->kaddr,
2998 &leaf->map_start, &leaf->map_len,
2999 KM_USER1);
3000 }
Chris Mason5f39d392007-10-15 16:14:19 -04003001 ioff = btrfs_item_offset(leaf, item);
3002 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003003 }
Chris Mason5f39d392007-10-15 16:14:19 -04003004
Chris Masondb945352007-10-15 16:15:53 -04003005 if (leaf->map_token) {
3006 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3007 leaf->map_token = NULL;
3008 }
3009
Chris Mason6567e832007-04-16 09:22:45 -04003010 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003011 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003012 data_end - data_size, btrfs_leaf_data(leaf) +
3013 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003014
Chris Mason6567e832007-04-16 09:22:45 -04003015 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003016 old_size = btrfs_item_size_nr(leaf, slot);
3017 item = btrfs_item_nr(leaf, slot);
3018 btrfs_set_item_size(leaf, item, old_size + data_size);
3019 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003020
3021 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003022 if (btrfs_leaf_free_space(root, leaf) < 0) {
3023 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003024 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003025 }
Chris Mason6567e832007-04-16 09:22:45 -04003026 return ret;
3027}
3028
Chris Mason74123bd2007-02-02 11:05:29 -05003029/*
Chris Masond352ac62008-09-29 15:18:18 -04003030 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003031 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003032 * Returns the number of keys that were inserted.
3033 */
3034int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3035 struct btrfs_root *root,
3036 struct btrfs_path *path,
3037 struct btrfs_key *cpu_key, u32 *data_size,
3038 int nr)
3039{
3040 struct extent_buffer *leaf;
3041 struct btrfs_item *item;
3042 int ret = 0;
3043 int slot;
3044 int slot_orig;
3045 int i;
3046 u32 nritems;
3047 u32 total_data = 0;
3048 u32 total_size = 0;
3049 unsigned int data_end;
3050 struct btrfs_disk_key disk_key;
3051 struct btrfs_key found_key;
3052
3053 found_key.objectid = 0;
3054 nr = min_t(int, nr, BTRFS_NODEPTRS_PER_BLOCK(root));
3055
3056 for (i = 0; i < nr; i++)
3057 total_data += data_size[i];
3058
3059 total_data = min_t(u32, total_data, BTRFS_LEAF_DATA_SIZE(root));
3060 total_size = total_data + (nr * sizeof(struct btrfs_item));
3061 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3062 if (ret == 0)
3063 return -EEXIST;
3064 if (ret < 0)
3065 goto out;
3066
3067 slot_orig = path->slots[0];
3068 leaf = path->nodes[0];
3069
3070 nritems = btrfs_header_nritems(leaf);
3071 data_end = leaf_data_end(root, leaf);
3072
3073 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3074 for (i = nr; i >= 0; i--) {
3075 total_data -= data_size[i];
3076 total_size -= data_size[i] + sizeof(struct btrfs_item);
3077 if (total_size < btrfs_leaf_free_space(root, leaf))
3078 break;
3079 }
3080 nr = i;
3081 }
3082
3083 slot = path->slots[0];
3084 BUG_ON(slot < 0);
3085
3086 if (slot != nritems) {
3087 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3088
3089 item = btrfs_item_nr(leaf, slot);
3090 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3091
3092 /* figure out how many keys we can insert in here */
3093 total_data = data_size[0];
3094 for (i = 1; i < nr; i++) {
3095 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3096 break;
3097 total_data += data_size[i];
3098 }
3099 nr = i;
3100
3101 if (old_data < data_end) {
3102 btrfs_print_leaf(root, leaf);
3103 printk("slot %d old_data %d data_end %d\n",
3104 slot, old_data, data_end);
3105 BUG_ON(1);
3106 }
3107 /*
3108 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3109 */
3110 /* first correct the data pointers */
3111 WARN_ON(leaf->map_token);
3112 for (i = slot; i < nritems; i++) {
3113 u32 ioff;
3114
3115 item = btrfs_item_nr(leaf, i);
3116 if (!leaf->map_token) {
3117 map_extent_buffer(leaf, (unsigned long)item,
3118 sizeof(struct btrfs_item),
3119 &leaf->map_token, &leaf->kaddr,
3120 &leaf->map_start, &leaf->map_len,
3121 KM_USER1);
3122 }
3123
3124 ioff = btrfs_item_offset(leaf, item);
3125 btrfs_set_item_offset(leaf, item, ioff - total_data);
3126 }
3127 if (leaf->map_token) {
3128 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3129 leaf->map_token = NULL;
3130 }
3131
3132 /* shift the items */
3133 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3134 btrfs_item_nr_offset(slot),
3135 (nritems - slot) * sizeof(struct btrfs_item));
3136
3137 /* shift the data */
3138 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3139 data_end - total_data, btrfs_leaf_data(leaf) +
3140 data_end, old_data - data_end);
3141 data_end = old_data;
3142 } else {
3143 /*
3144 * this sucks but it has to be done, if we are inserting at
3145 * the end of the leaf only insert 1 of the items, since we
3146 * have no way of knowing whats on the next leaf and we'd have
3147 * to drop our current locks to figure it out
3148 */
3149 nr = 1;
3150 }
3151
3152 /* setup the item for the new data */
3153 for (i = 0; i < nr; i++) {
3154 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3155 btrfs_set_item_key(leaf, &disk_key, slot + i);
3156 item = btrfs_item_nr(leaf, slot + i);
3157 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3158 data_end -= data_size[i];
3159 btrfs_set_item_size(leaf, item, data_size[i]);
3160 }
3161 btrfs_set_header_nritems(leaf, nritems + nr);
3162 btrfs_mark_buffer_dirty(leaf);
3163
3164 ret = 0;
3165 if (slot == 0) {
3166 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3167 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3168 }
3169
3170 if (btrfs_leaf_free_space(root, leaf) < 0) {
3171 btrfs_print_leaf(root, leaf);
3172 BUG();
3173 }
3174out:
3175 if (!ret)
3176 ret = nr;
3177 return ret;
3178}
3179
3180/*
3181 * Given a key and some data, insert items into the tree.
3182 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003183 */
Chris Mason9c583092008-01-29 15:15:18 -05003184int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003185 struct btrfs_root *root,
3186 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003187 struct btrfs_key *cpu_key, u32 *data_size,
3188 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003189{
Chris Mason5f39d392007-10-15 16:14:19 -04003190 struct extent_buffer *leaf;
3191 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003192 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003193 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003194 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003195 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003196 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003197 u32 total_size = 0;
3198 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003199 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003200 struct btrfs_disk_key disk_key;
3201
Chris Mason9c583092008-01-29 15:15:18 -05003202 for (i = 0; i < nr; i++) {
3203 total_data += data_size[i];
3204 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003205
Josef Bacik7b128762008-07-24 12:17:14 -04003206 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003207 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003208 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003209 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003210 if (ret < 0)
3211 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003212
Chris Mason62e27492007-03-15 12:56:47 -04003213 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003214 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003215
Chris Mason5f39d392007-10-15 16:14:19 -04003216 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003217 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003218
Chris Masonf25956c2008-09-12 15:32:53 -04003219 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003220 btrfs_print_leaf(root, leaf);
3221 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003222 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003223 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003224 }
Chris Mason5f39d392007-10-15 16:14:19 -04003225
Chris Mason62e27492007-03-15 12:56:47 -04003226 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003227 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003228
Chris Masonbe0e5c02007-01-26 15:51:26 -05003229 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003230 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003231
Chris Mason5f39d392007-10-15 16:14:19 -04003232 if (old_data < data_end) {
3233 btrfs_print_leaf(root, leaf);
3234 printk("slot %d old_data %d data_end %d\n",
3235 slot, old_data, data_end);
3236 BUG_ON(1);
3237 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003238 /*
3239 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3240 */
3241 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003242 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003243 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003244 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003245
Chris Mason5f39d392007-10-15 16:14:19 -04003246 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003247 if (!leaf->map_token) {
3248 map_extent_buffer(leaf, (unsigned long)item,
3249 sizeof(struct btrfs_item),
3250 &leaf->map_token, &leaf->kaddr,
3251 &leaf->map_start, &leaf->map_len,
3252 KM_USER1);
3253 }
3254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003256 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003257 }
Chris Masondb945352007-10-15 16:15:53 -04003258 if (leaf->map_token) {
3259 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3260 leaf->map_token = NULL;
3261 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003262
3263 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003264 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003265 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003266 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003267
3268 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003269 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003270 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003271 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003272 data_end = old_data;
3273 }
Chris Mason5f39d392007-10-15 16:14:19 -04003274
Chris Mason62e27492007-03-15 12:56:47 -04003275 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003276 for (i = 0; i < nr; i++) {
3277 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3278 btrfs_set_item_key(leaf, &disk_key, slot + i);
3279 item = btrfs_item_nr(leaf, slot + i);
3280 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3281 data_end -= data_size[i];
3282 btrfs_set_item_size(leaf, item, data_size[i]);
3283 }
3284 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003285 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003286
3287 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003288 if (slot == 0) {
3289 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003290 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003291 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003292
Chris Mason5f39d392007-10-15 16:14:19 -04003293 if (btrfs_leaf_free_space(root, leaf) < 0) {
3294 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003295 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003296 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003297out:
Chris Mason62e27492007-03-15 12:56:47 -04003298 return ret;
3299}
3300
3301/*
3302 * Given a key and some data, insert an item into the tree.
3303 * This does all the path init required, making room in the tree if needed.
3304 */
Chris Masone089f052007-03-16 16:20:31 -04003305int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3306 *root, struct btrfs_key *cpu_key, void *data, u32
3307 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003308{
3309 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003310 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003311 struct extent_buffer *leaf;
3312 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003313
Chris Mason2c90e5d2007-04-02 10:50:19 -04003314 path = btrfs_alloc_path();
3315 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003316 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003317 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003318 leaf = path->nodes[0];
3319 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3320 write_extent_buffer(leaf, data, ptr, data_size);
3321 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003322 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003323 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003324 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003325}
3326
Chris Mason74123bd2007-02-02 11:05:29 -05003327/*
Chris Mason5de08d72007-02-24 06:24:44 -05003328 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003329 *
Chris Masond352ac62008-09-29 15:18:18 -04003330 * the tree should have been previously balanced so the deletion does not
3331 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003332 */
Chris Masone089f052007-03-16 16:20:31 -04003333static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3334 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003335{
Chris Mason5f39d392007-10-15 16:14:19 -04003336 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003337 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003338 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003339 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003340
Chris Mason5f39d392007-10-15 16:14:19 -04003341 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05003342 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003343 memmove_extent_buffer(parent,
3344 btrfs_node_key_ptr_offset(slot),
3345 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003346 sizeof(struct btrfs_key_ptr) *
3347 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003348 }
Chris Mason7518a232007-03-12 12:01:18 -04003349 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003350 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003351 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003352 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003353 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003354 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003355 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003356 struct btrfs_disk_key disk_key;
3357
3358 btrfs_node_key(parent, &disk_key, 0);
3359 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003360 if (wret)
3361 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003362 }
Chris Masond6025572007-03-30 14:27:56 -04003363 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003364 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003365}
3366
Chris Mason74123bd2007-02-02 11:05:29 -05003367/*
Chris Mason323ac952008-10-01 19:05:46 -04003368 * a helper function to delete the leaf pointed to by path->slots[1] and
3369 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3370 * already know it, it is faster to have them pass it down than to
3371 * read it out of the node again.
3372 *
3373 * This deletes the pointer in path->nodes[1] and frees the leaf
3374 * block extent. zero is returned if it all worked out, < 0 otherwise.
3375 *
3376 * The path must have already been setup for deleting the leaf, including
3377 * all the proper balancing. path->nodes[1] must be locked.
3378 */
3379noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3380 struct btrfs_root *root,
3381 struct btrfs_path *path, u64 bytenr)
3382{
3383 int ret;
3384 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3385
3386 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3387 if (ret)
3388 return ret;
3389
3390 ret = btrfs_free_extent(trans, root, bytenr,
3391 btrfs_level_size(root, 0),
3392 path->nodes[1]->start,
3393 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003394 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003395 return ret;
3396}
3397/*
Chris Mason74123bd2007-02-02 11:05:29 -05003398 * delete the item at the leaf level in path. If that empties
3399 * the leaf, remove it from the tree
3400 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003401int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3402 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003403{
Chris Mason5f39d392007-10-15 16:14:19 -04003404 struct extent_buffer *leaf;
3405 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003406 int last_off;
3407 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003408 int ret = 0;
3409 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003410 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003411 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003412
Chris Mason5f39d392007-10-15 16:14:19 -04003413 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003414 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3415
3416 for (i = 0; i < nr; i++)
3417 dsize += btrfs_item_size_nr(leaf, slot + i);
3418
Chris Mason5f39d392007-10-15 16:14:19 -04003419 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003420
Chris Mason85e21ba2008-01-29 15:11:36 -05003421 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003422 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003423
3424 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003425 data_end + dsize,
3426 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003427 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003428
Chris Mason85e21ba2008-01-29 15:11:36 -05003429 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003430 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003431
Chris Mason5f39d392007-10-15 16:14:19 -04003432 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003433 if (!leaf->map_token) {
3434 map_extent_buffer(leaf, (unsigned long)item,
3435 sizeof(struct btrfs_item),
3436 &leaf->map_token, &leaf->kaddr,
3437 &leaf->map_start, &leaf->map_len,
3438 KM_USER1);
3439 }
Chris Mason5f39d392007-10-15 16:14:19 -04003440 ioff = btrfs_item_offset(leaf, item);
3441 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003442 }
Chris Masondb945352007-10-15 16:15:53 -04003443
3444 if (leaf->map_token) {
3445 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3446 leaf->map_token = NULL;
3447 }
3448
Chris Mason5f39d392007-10-15 16:14:19 -04003449 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003450 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003451 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003452 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003453 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003454 btrfs_set_header_nritems(leaf, nritems - nr);
3455 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003456
Chris Mason74123bd2007-02-02 11:05:29 -05003457 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003458 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003459 if (leaf == root->node) {
3460 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003461 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003462 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3463 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003464 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003465 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003466 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003467 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003468 struct btrfs_disk_key disk_key;
3469
3470 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003471 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003472 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003473 if (wret)
3474 ret = wret;
3475 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003476
Chris Mason74123bd2007-02-02 11:05:29 -05003477 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003478 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003479 /* push_leaf_left fixes the path.
3480 * make sure the path still points to our leaf
3481 * for possible call to del_ptr below
3482 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003483 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003484 extent_buffer_get(leaf);
3485
Chris Mason85e21ba2008-01-29 15:11:36 -05003486 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003487 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003488 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003489
3490 if (path->nodes[0] == leaf &&
3491 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003492 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003493 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003494 ret = wret;
3495 }
Chris Mason5f39d392007-10-15 16:14:19 -04003496
3497 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003498 path->slots[1] = slot;
3499 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3500 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003501 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003502 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003503 /* if we're still in the path, make sure
3504 * we're dirty. Otherwise, one of the
3505 * push_leaf functions must have already
3506 * dirtied this buffer
3507 */
3508 if (path->nodes[0] == leaf)
3509 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003510 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003511 }
Chris Masond5719762007-03-23 10:01:08 -04003512 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003513 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003514 }
3515 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003516 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003517}
3518
Chris Mason97571fd2007-02-24 13:39:08 -05003519/*
Chris Mason925baed2008-06-25 16:01:30 -04003520 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003521 * returns 0 if it found something or 1 if there are no lesser leaves.
3522 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003523 *
3524 * This may release the path, and so you may lose any locks held at the
3525 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003526 */
3527int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3528{
Chris Mason925baed2008-06-25 16:01:30 -04003529 struct btrfs_key key;
3530 struct btrfs_disk_key found_key;
3531 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003532
Chris Mason925baed2008-06-25 16:01:30 -04003533 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003534
Chris Mason925baed2008-06-25 16:01:30 -04003535 if (key.offset > 0)
3536 key.offset--;
3537 else if (key.type > 0)
3538 key.type--;
3539 else if (key.objectid > 0)
3540 key.objectid--;
3541 else
3542 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003543
Chris Mason925baed2008-06-25 16:01:30 -04003544 btrfs_release_path(root, path);
3545 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3546 if (ret < 0)
3547 return ret;
3548 btrfs_item_key(path->nodes[0], &found_key, 0);
3549 ret = comp_keys(&found_key, &key);
3550 if (ret < 0)
3551 return 0;
3552 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003553}
3554
Chris Mason3f157a22008-06-25 16:01:31 -04003555/*
3556 * A helper function to walk down the tree starting at min_key, and looking
3557 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003558 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003559 *
3560 * This does not cow, but it does stuff the starting key it finds back
3561 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3562 * key and get a writable path.
3563 *
3564 * This does lock as it descends, and path->keep_locks should be set
3565 * to 1 by the caller.
3566 *
3567 * This honors path->lowest_level to prevent descent past a given level
3568 * of the tree.
3569 *
Chris Masond352ac62008-09-29 15:18:18 -04003570 * min_trans indicates the oldest transaction that you are interested
3571 * in walking through. Any nodes or leaves older than min_trans are
3572 * skipped over (without reading them).
3573 *
Chris Mason3f157a22008-06-25 16:01:31 -04003574 * returns zero if something useful was found, < 0 on error and 1 if there
3575 * was nothing in the tree that matched the search criteria.
3576 */
3577int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003578 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003579 struct btrfs_path *path, int cache_only,
3580 u64 min_trans)
3581{
3582 struct extent_buffer *cur;
3583 struct btrfs_key found_key;
3584 int slot;
Yan96524802008-07-24 12:19:49 -04003585 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003586 u32 nritems;
3587 int level;
3588 int ret = 1;
3589
3590again:
3591 cur = btrfs_lock_root_node(root);
3592 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003593 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003594 path->nodes[level] = cur;
3595 path->locks[level] = 1;
3596
3597 if (btrfs_header_generation(cur) < min_trans) {
3598 ret = 1;
3599 goto out;
3600 }
3601 while(1) {
3602 nritems = btrfs_header_nritems(cur);
3603 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003604 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003605
Chris Mason323ac952008-10-01 19:05:46 -04003606 /* at the lowest level, we're done, setup the path and exit */
3607 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003608 if (slot >= nritems)
3609 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003610 ret = 0;
3611 path->slots[level] = slot;
3612 btrfs_item_key_to_cpu(cur, &found_key, slot);
3613 goto out;
3614 }
Yan96524802008-07-24 12:19:49 -04003615 if (sret && slot > 0)
3616 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003617 /*
3618 * check this node pointer against the cache_only and
3619 * min_trans parameters. If it isn't in cache or is too
3620 * old, skip to the next one.
3621 */
3622 while(slot < nritems) {
3623 u64 blockptr;
3624 u64 gen;
3625 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003626 struct btrfs_disk_key disk_key;
3627
Chris Mason3f157a22008-06-25 16:01:31 -04003628 blockptr = btrfs_node_blockptr(cur, slot);
3629 gen = btrfs_node_ptr_generation(cur, slot);
3630 if (gen < min_trans) {
3631 slot++;
3632 continue;
3633 }
3634 if (!cache_only)
3635 break;
3636
Chris Masone02119d2008-09-05 16:13:11 -04003637 if (max_key) {
3638 btrfs_node_key(cur, &disk_key, slot);
3639 if (comp_keys(&disk_key, max_key) >= 0) {
3640 ret = 1;
3641 goto out;
3642 }
3643 }
3644
Chris Mason3f157a22008-06-25 16:01:31 -04003645 tmp = btrfs_find_tree_block(root, blockptr,
3646 btrfs_level_size(root, level - 1));
3647
3648 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3649 free_extent_buffer(tmp);
3650 break;
3651 }
3652 if (tmp)
3653 free_extent_buffer(tmp);
3654 slot++;
3655 }
Chris Masone02119d2008-09-05 16:13:11 -04003656find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003657 /*
3658 * we didn't find a candidate key in this node, walk forward
3659 * and find another one
3660 */
3661 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003662 path->slots[level] = slot;
3663 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003664 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003665 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003666 btrfs_release_path(root, path);
3667 goto again;
3668 } else {
3669 goto out;
3670 }
3671 }
3672 /* save our key for returning back */
3673 btrfs_node_key_to_cpu(cur, &found_key, slot);
3674 path->slots[level] = slot;
3675 if (level == path->lowest_level) {
3676 ret = 0;
3677 unlock_up(path, level, 1);
3678 goto out;
3679 }
3680 cur = read_node_slot(root, cur, slot);
3681
3682 btrfs_tree_lock(cur);
3683 path->locks[level - 1] = 1;
3684 path->nodes[level - 1] = cur;
3685 unlock_up(path, level, 1);
3686 }
3687out:
3688 if (ret == 0)
3689 memcpy(min_key, &found_key, sizeof(found_key));
3690 return ret;
3691}
3692
3693/*
3694 * this is similar to btrfs_next_leaf, but does not try to preserve
3695 * and fixup the path. It looks for and returns the next key in the
3696 * tree based on the current path and the cache_only and min_trans
3697 * parameters.
3698 *
3699 * 0 is returned if another key is found, < 0 if there are any errors
3700 * and 1 is returned if there are no higher keys in the tree
3701 *
3702 * path->keep_locks should be set to 1 on the search made before
3703 * calling this function.
3704 */
Chris Masone7a84562008-06-25 16:01:31 -04003705int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003706 struct btrfs_key *key, int lowest_level,
3707 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003708{
3709 int level = lowest_level;
3710 int slot;
3711 struct extent_buffer *c;
3712
3713 while(level < BTRFS_MAX_LEVEL) {
3714 if (!path->nodes[level])
3715 return 1;
3716
3717 slot = path->slots[level] + 1;
3718 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003719next:
Chris Masone7a84562008-06-25 16:01:31 -04003720 if (slot >= btrfs_header_nritems(c)) {
3721 level++;
3722 if (level == BTRFS_MAX_LEVEL) {
3723 return 1;
3724 }
3725 continue;
3726 }
3727 if (level == 0)
3728 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003729 else {
3730 u64 blockptr = btrfs_node_blockptr(c, slot);
3731 u64 gen = btrfs_node_ptr_generation(c, slot);
3732
3733 if (cache_only) {
3734 struct extent_buffer *cur;
3735 cur = btrfs_find_tree_block(root, blockptr,
3736 btrfs_level_size(root, level - 1));
3737 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3738 slot++;
3739 if (cur)
3740 free_extent_buffer(cur);
3741 goto next;
3742 }
3743 free_extent_buffer(cur);
3744 }
3745 if (gen < min_trans) {
3746 slot++;
3747 goto next;
3748 }
Chris Masone7a84562008-06-25 16:01:31 -04003749 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003750 }
Chris Masone7a84562008-06-25 16:01:31 -04003751 return 0;
3752 }
3753 return 1;
3754}
3755
Chris Mason7bb86312007-12-11 09:25:06 -05003756/*
Chris Mason925baed2008-06-25 16:01:30 -04003757 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003758 * returns 0 if it found something or 1 if there are no greater leaves.
3759 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003760 */
Chris Mason234b63a2007-03-13 10:46:10 -04003761int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003762{
3763 int slot;
3764 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003765 struct extent_buffer *c;
3766 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003767 struct btrfs_key key;
3768 u32 nritems;
3769 int ret;
3770
3771 nritems = btrfs_header_nritems(path->nodes[0]);
3772 if (nritems == 0) {
3773 return 1;
3774 }
3775
3776 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3777
Chris Mason925baed2008-06-25 16:01:30 -04003778 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003779 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003780 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3781 path->keep_locks = 0;
3782
3783 if (ret < 0)
3784 return ret;
3785
Chris Masona2135012008-06-25 16:01:30 -04003786 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003787 /*
3788 * by releasing the path above we dropped all our locks. A balance
3789 * could have added more items next to the key that used to be
3790 * at the very end of the block. So, check again here and
3791 * advance the path if there are now more items available.
3792 */
Chris Masona2135012008-06-25 16:01:30 -04003793 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003794 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003795 goto done;
3796 }
Chris Masond97e63b2007-02-20 16:40:44 -05003797
Chris Mason234b63a2007-03-13 10:46:10 -04003798 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003799 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003800 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003801
Chris Masond97e63b2007-02-20 16:40:44 -05003802 slot = path->slots[level] + 1;
3803 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003804 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003805 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003806 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003807 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003808 }
Chris Masond97e63b2007-02-20 16:40:44 -05003809 continue;
3810 }
Chris Mason5f39d392007-10-15 16:14:19 -04003811
Chris Mason925baed2008-06-25 16:01:30 -04003812 if (next) {
3813 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003814 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003815 }
Chris Mason5f39d392007-10-15 16:14:19 -04003816
Chris Mason0bd40a72008-07-17 12:54:43 -04003817 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3818 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003819 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003820
Chris Masonca7a79a2008-05-12 12:59:19 -04003821 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003822 if (!path->skip_locking) {
3823 WARN_ON(!btrfs_tree_locked(c));
3824 btrfs_tree_lock(next);
3825 }
Chris Masond97e63b2007-02-20 16:40:44 -05003826 break;
3827 }
3828 path->slots[level] = slot;
3829 while(1) {
3830 level--;
3831 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003832 if (path->locks[level])
3833 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003834 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003835 path->nodes[level] = next;
3836 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003837 if (!path->skip_locking)
3838 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003839 if (!level)
3840 break;
Chris Mason925baed2008-06-25 16:01:30 -04003841 if (level == 1 && path->locks[1] && path->reada)
3842 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003843 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003844 if (!path->skip_locking) {
3845 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3846 btrfs_tree_lock(next);
3847 }
Chris Masond97e63b2007-02-20 16:40:44 -05003848 }
Chris Mason925baed2008-06-25 16:01:30 -04003849done:
3850 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003851 return 0;
3852}
Chris Mason0b86a832008-03-24 15:01:56 -04003853
Chris Mason3f157a22008-06-25 16:01:31 -04003854/*
3855 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3856 * searching until it gets past min_objectid or finds an item of 'type'
3857 *
3858 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3859 */
Chris Mason0b86a832008-03-24 15:01:56 -04003860int btrfs_previous_item(struct btrfs_root *root,
3861 struct btrfs_path *path, u64 min_objectid,
3862 int type)
3863{
3864 struct btrfs_key found_key;
3865 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003866 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003867 int ret;
3868
3869 while(1) {
3870 if (path->slots[0] == 0) {
3871 ret = btrfs_prev_leaf(root, path);
3872 if (ret != 0)
3873 return ret;
3874 } else {
3875 path->slots[0]--;
3876 }
3877 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003878 nritems = btrfs_header_nritems(leaf);
3879 if (nritems == 0)
3880 return 1;
3881 if (path->slots[0] == nritems)
3882 path->slots[0]--;
3883
Chris Mason0b86a832008-03-24 15:01:56 -04003884 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3885 if (found_key.type == type)
3886 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003887 if (found_key.objectid < min_objectid)
3888 break;
3889 if (found_key.objectid == min_objectid &&
3890 found_key.type < type)
3891 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003892 }
3893 return 1;
3894}