blob: 2b3ffa707eae36e4c4e4330d3cec728e562d2e5a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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 Mason9a8dd152007-02-23 08:38:36 -050024
Chris Masone089f052007-03-16 16:20:31 -040025static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_path *path, int level);
27static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040028 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040029 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040030static int push_node_left(struct btrfs_trans_handle *trans,
31 struct btrfs_root *root, struct extent_buffer *dst,
32 struct extent_buffer *src);
33static int balance_node_right(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root,
35 struct extent_buffer *dst_buf,
36 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040037static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
38 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050039
Chris Masondf24a2b2007-04-04 09:36:31 -040040inline void btrfs_init_path(struct btrfs_path *p)
41{
42 memset(p, 0, sizeof(*p));
43}
44
Chris Mason2c90e5d2007-04-02 10:50:19 -040045struct btrfs_path *btrfs_alloc_path(void)
46{
Chris Masondf24a2b2007-04-04 09:36:31 -040047 struct btrfs_path *path;
48 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040049 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040050 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040051 path->reada = 1;
52 }
Chris Masondf24a2b2007-04-04 09:36:31 -040053 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040054}
55
56void btrfs_free_path(struct btrfs_path *p)
57{
Chris Masondf24a2b2007-04-04 09:36:31 -040058 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040059 kmem_cache_free(btrfs_path_cachep, p);
60}
61
Chris Mason234b63a2007-03-13 10:46:10 -040062void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050063{
64 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040065 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050066 if (!p->nodes[i])
67 break;
Chris Mason5f39d392007-10-15 16:14:19 -040068 free_extent_buffer(p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050069 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050070 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050071}
72
Chris Masonbe20aa92007-12-17 20:14:01 -050073int btrfs_copy_root(struct btrfs_trans_handle *trans,
74 struct btrfs_root *root,
75 struct extent_buffer *buf,
76 struct extent_buffer **cow_ret, u64 new_root_objectid)
77{
78 struct extent_buffer *cow;
79 u32 nritems;
80 int ret = 0;
81 int level;
82 struct btrfs_key first_key;
Chris Mason4aec2b52007-12-18 16:25:45 -050083 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -050084
Chris Mason4aec2b52007-12-18 16:25:45 -050085 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
86 if (!new_root)
87 return -ENOMEM;
88
89 memcpy(new_root, root, sizeof(*new_root));
90 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -050091
92 WARN_ON(root->ref_cows && trans->transid !=
93 root->fs_info->running_transaction->transid);
94 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
95
96 level = btrfs_header_level(buf);
97 nritems = btrfs_header_nritems(buf);
98 if (nritems) {
99 if (level == 0)
100 btrfs_item_key_to_cpu(buf, &first_key, 0);
101 else
102 btrfs_node_key_to_cpu(buf, &first_key, 0);
103 } else {
104 first_key.objectid = 0;
105 }
Chris Mason4aec2b52007-12-18 16:25:45 -0500106 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
Chris Masonbe20aa92007-12-17 20:14:01 -0500107 new_root_objectid,
108 trans->transid, first_key.objectid,
109 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500110 if (IS_ERR(cow)) {
111 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500112 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500113 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500114
115 copy_extent_buffer(cow, buf, 0, 0, cow->len);
116 btrfs_set_header_bytenr(cow, cow->start);
117 btrfs_set_header_generation(cow, trans->transid);
118 btrfs_set_header_owner(cow, new_root_objectid);
119
120 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Chris Mason4aec2b52007-12-18 16:25:45 -0500121 ret = btrfs_inc_ref(trans, new_root, buf);
122 kfree(new_root);
123
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 if (ret)
125 return ret;
126
127 btrfs_mark_buffer_dirty(cow);
128 *cow_ret = cow;
129 return 0;
130}
131
132int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400133 struct btrfs_root *root,
134 struct extent_buffer *buf,
135 struct extent_buffer *parent, int parent_slot,
136 struct extent_buffer **cow_ret,
137 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400138{
Chris Mason7bb86312007-12-11 09:25:06 -0500139 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400140 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500141 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400142 int ret = 0;
143 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500144 int level;
145 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400146
Chris Mason7bb86312007-12-11 09:25:06 -0500147 if (root->ref_cows) {
148 root_gen = trans->transid;
149 } else {
150 root_gen = 0;
151 }
152
153 WARN_ON(root->ref_cows && trans->transid !=
154 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400155 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400156
Chris Mason7bb86312007-12-11 09:25:06 -0500157 level = btrfs_header_level(buf);
158 nritems = btrfs_header_nritems(buf);
159 if (nritems) {
160 if (level == 0)
161 btrfs_item_key_to_cpu(buf, &first_key, 0);
162 else
163 btrfs_node_key_to_cpu(buf, &first_key, 0);
164 } else {
165 first_key.objectid = 0;
166 }
167 cow = __btrfs_alloc_free_block(trans, root, buf->len,
168 root->root_key.objectid,
169 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400170 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400171 if (IS_ERR(cow))
172 return PTR_ERR(cow);
173
Chris Mason5f39d392007-10-15 16:14:19 -0400174 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400175 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400176 btrfs_set_header_generation(cow, trans->transid);
177 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400178
Chris Mason5f39d392007-10-15 16:14:19 -0400179 WARN_ON(btrfs_header_generation(buf) > trans->transid);
180 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400181 different_trans = 1;
182 ret = btrfs_inc_ref(trans, root, buf);
183 if (ret)
184 return ret;
185 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400186 clean_tree_block(trans, root, buf);
187 }
188
189 if (buf == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -0500190 root_gen = btrfs_header_generation(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400191 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400192 extent_buffer_get(cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400193 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400194 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500195 buf->len, root->root_key.objectid,
196 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400197 }
Chris Mason5f39d392007-10-15 16:14:19 -0400198 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400199 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500200 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400201 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400202 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500203 WARN_ON(trans->transid == 0);
204 btrfs_set_node_ptr_generation(parent, parent_slot,
205 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400206 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400207 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500208 btrfs_free_extent(trans, root, buf->start, buf->len,
209 btrfs_header_owner(parent), root_gen,
210 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400211 }
Chris Mason5f39d392007-10-15 16:14:19 -0400212 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400213 btrfs_mark_buffer_dirty(cow);
214 *cow_ret = cow;
215 return 0;
216}
217
Chris Mason5f39d392007-10-15 16:14:19 -0400218int btrfs_cow_block(struct btrfs_trans_handle *trans,
219 struct btrfs_root *root, struct extent_buffer *buf,
220 struct extent_buffer *parent, int parent_slot,
221 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500222{
Chris Mason6702ed42007-08-07 16:15:09 -0400223 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400224 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400225 if (trans->transaction != root->fs_info->running_transaction) {
226 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
227 root->fs_info->running_transaction->transid);
228 WARN_ON(1);
229 }
230 if (trans->transid != root->fs_info->generation) {
231 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
232 root->fs_info->generation);
233 WARN_ON(1);
234 }
Chris Mason5f39d392007-10-15 16:14:19 -0400235 if (btrfs_header_generation(buf) == trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -0500236 *cow_ret = buf;
237 return 0;
238 }
Chris Mason6702ed42007-08-07 16:15:09 -0400239
Chris Masondb945352007-10-15 16:15:53 -0400240 search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400241 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400242 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400243 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400244}
245
Chris Mason6b800532007-10-15 16:17:34 -0400246static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400247{
Chris Mason6b800532007-10-15 16:17:34 -0400248 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400249 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400250 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400251 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500252 return 0;
253}
254
Chris Mason081e9572007-11-06 10:26:24 -0500255/*
256 * compare two keys in a memcmp fashion
257 */
258static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
259{
260 struct btrfs_key k1;
261
262 btrfs_disk_key_to_cpu(&k1, disk);
263
264 if (k1.objectid > k2->objectid)
265 return 1;
266 if (k1.objectid < k2->objectid)
267 return -1;
268 if (k1.type > k2->type)
269 return 1;
270 if (k1.type < k2->type)
271 return -1;
272 if (k1.offset > k2->offset)
273 return 1;
274 if (k1.offset < k2->offset)
275 return -1;
276 return 0;
277}
278
279
Chris Mason6702ed42007-08-07 16:15:09 -0400280int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400281 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400282 int start_slot, int cache_only, u64 *last_ret,
283 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400284{
Chris Mason6b800532007-10-15 16:17:34 -0400285 struct extent_buffer *cur;
286 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400287 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400288 u64 search_start = *last_ret;
289 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400290 u64 other;
291 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400292 int end_slot;
293 int i;
294 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400295 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400296 int uptodate;
297 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500298 int progress_passed = 0;
299 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400300
Chris Mason5708b952007-10-25 15:43:18 -0400301 parent_level = btrfs_header_level(parent);
302 if (cache_only && parent_level != 1)
303 return 0;
304
Chris Mason6702ed42007-08-07 16:15:09 -0400305 if (trans->transaction != root->fs_info->running_transaction) {
306 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
307 root->fs_info->running_transaction->transid);
308 WARN_ON(1);
309 }
310 if (trans->transid != root->fs_info->generation) {
311 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
312 root->fs_info->generation);
313 WARN_ON(1);
314 }
Chris Mason86479a02007-09-10 19:58:16 -0400315
Chris Mason6b800532007-10-15 16:17:34 -0400316 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400317 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400318 end_slot = parent_nritems;
319
320 if (parent_nritems == 1)
321 return 0;
322
323 for (i = start_slot; i < end_slot; i++) {
324 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400325
Chris Mason5708b952007-10-25 15:43:18 -0400326 if (!parent->map_token) {
327 map_extent_buffer(parent,
328 btrfs_node_key_ptr_offset(i),
329 sizeof(struct btrfs_key_ptr),
330 &parent->map_token, &parent->kaddr,
331 &parent->map_start, &parent->map_len,
332 KM_USER1);
333 }
Chris Mason081e9572007-11-06 10:26:24 -0500334 btrfs_node_key(parent, &disk_key, i);
335 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
336 continue;
337
338 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400339 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400340 if (last_block == 0)
341 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400342
Chris Mason6702ed42007-08-07 16:15:09 -0400343 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400344 other = btrfs_node_blockptr(parent, i - 1);
345 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400346 }
Chris Mason5708b952007-10-25 15:43:18 -0400347 if (close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400348 other = btrfs_node_blockptr(parent, i + 1);
349 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400350 }
Chris Masone9d0b132007-08-10 14:06:19 -0400351 if (close) {
352 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400353 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400354 }
Chris Mason5708b952007-10-25 15:43:18 -0400355 if (parent->map_token) {
356 unmap_extent_buffer(parent, parent->map_token,
357 KM_USER1);
358 parent->map_token = NULL;
359 }
Chris Mason6702ed42007-08-07 16:15:09 -0400360
Chris Mason6b800532007-10-15 16:17:34 -0400361 cur = btrfs_find_tree_block(root, blocknr, blocksize);
362 if (cur)
363 uptodate = btrfs_buffer_uptodate(cur);
364 else
365 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400366 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400367 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400368 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400369 continue;
370 }
Chris Mason6b800532007-10-15 16:17:34 -0400371 if (!cur) {
372 cur = read_tree_block(root, blocknr,
373 blocksize);
374 } else if (!uptodate) {
375 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400376 }
Chris Mason6702ed42007-08-07 16:15:09 -0400377 }
Chris Masone9d0b132007-08-10 14:06:19 -0400378 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400379 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400380
Chris Mason6b800532007-10-15 16:17:34 -0400381 err = __btrfs_cow_block(trans, root, cur, parent, i,
382 &tmp, search_start,
383 min(16 * blocksize,
384 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400385 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400386 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400387 break;
Yan252c38f2007-08-29 09:11:44 -0400388 }
Chris Mason6b800532007-10-15 16:17:34 -0400389 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400390 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400391 *last_ret = search_start;
392 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400393 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400394 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400395 }
Chris Mason5708b952007-10-25 15:43:18 -0400396 if (parent->map_token) {
397 unmap_extent_buffer(parent, parent->map_token,
398 KM_USER1);
399 parent->map_token = NULL;
400 }
Chris Mason6702ed42007-08-07 16:15:09 -0400401 return err;
402}
403
Chris Mason74123bd2007-02-02 11:05:29 -0500404/*
405 * The leaf data grows from end-to-front in the node.
406 * this returns the address of the start of the last item,
407 * which is the stop of the leaf data stack
408 */
Chris Mason123abc82007-03-14 14:14:43 -0400409static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400410 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500411{
Chris Mason5f39d392007-10-15 16:14:19 -0400412 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500413 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400414 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400415 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500416}
417
Chris Mason123abc82007-03-14 14:14:43 -0400418static int check_node(struct btrfs_root *root, struct btrfs_path *path,
419 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500420{
Chris Mason5f39d392007-10-15 16:14:19 -0400421 struct extent_buffer *parent = NULL;
422 struct extent_buffer *node = path->nodes[level];
423 struct btrfs_disk_key parent_key;
424 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500425 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400426 int slot;
427 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400428 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500429
430 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400431 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400432
Chris Mason8d7be552007-05-10 11:24:42 -0400433 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400434 BUG_ON(nritems == 0);
435 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400436 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400437 btrfs_node_key(parent, &parent_key, parent_slot);
438 btrfs_node_key(node, &node_key, 0);
439 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400440 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400441 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400442 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500443 }
Chris Mason123abc82007-03-14 14:14:43 -0400444 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400445 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400446 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
447 btrfs_node_key(node, &node_key, slot);
448 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400449 }
450 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400451 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
452 btrfs_node_key(node, &node_key, slot);
453 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500454 }
455 return 0;
456}
457
Chris Mason123abc82007-03-14 14:14:43 -0400458static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
459 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500460{
Chris Mason5f39d392007-10-15 16:14:19 -0400461 struct extent_buffer *leaf = path->nodes[level];
462 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500463 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400464 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400465 struct btrfs_disk_key parent_key;
466 struct btrfs_disk_key leaf_key;
467 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400468
Chris Mason5f39d392007-10-15 16:14:19 -0400469 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500470
471 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400472 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400473
474 if (nritems == 0)
475 return 0;
476
477 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400478 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400479 btrfs_node_key(parent, &parent_key, parent_slot);
480 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400481
Chris Mason5f39d392007-10-15 16:14:19 -0400482 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400483 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400484 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400485 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500486 }
Chris Mason5f39d392007-10-15 16:14:19 -0400487#if 0
488 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
489 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
490 btrfs_item_key(leaf, &leaf_key, i);
491 if (comp_keys(&leaf_key, &cpukey) >= 0) {
492 btrfs_print_leaf(root, leaf);
493 printk("slot %d offset bad key\n", i);
494 BUG_ON(1);
495 }
496 if (btrfs_item_offset_nr(leaf, i) !=
497 btrfs_item_end_nr(leaf, i + 1)) {
498 btrfs_print_leaf(root, leaf);
499 printk("slot %d offset bad\n", i);
500 BUG_ON(1);
501 }
502 if (i == 0) {
503 if (btrfs_item_offset_nr(leaf, i) +
504 btrfs_item_size_nr(leaf, i) !=
505 BTRFS_LEAF_DATA_SIZE(root)) {
506 btrfs_print_leaf(root, leaf);
507 printk("slot %d first offset bad\n", i);
508 BUG_ON(1);
509 }
510 }
511 }
512 if (nritems > 0) {
513 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
514 btrfs_print_leaf(root, leaf);
515 printk("slot %d bad size \n", nritems - 1);
516 BUG_ON(1);
517 }
518 }
519#endif
520 if (slot != 0 && slot < nritems - 1) {
521 btrfs_item_key(leaf, &leaf_key, slot);
522 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
523 if (comp_keys(&leaf_key, &cpukey) <= 0) {
524 btrfs_print_leaf(root, leaf);
525 printk("slot %d offset bad key\n", slot);
526 BUG_ON(1);
527 }
528 if (btrfs_item_offset_nr(leaf, slot - 1) !=
529 btrfs_item_end_nr(leaf, slot)) {
530 btrfs_print_leaf(root, leaf);
531 printk("slot %d offset bad\n", slot);
532 BUG_ON(1);
533 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500534 }
Chris Mason8d7be552007-05-10 11:24:42 -0400535 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400536 btrfs_item_key(leaf, &leaf_key, slot);
537 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
538 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
539 if (btrfs_item_offset_nr(leaf, slot) !=
540 btrfs_item_end_nr(leaf, slot + 1)) {
541 btrfs_print_leaf(root, leaf);
542 printk("slot %d offset bad\n", slot);
543 BUG_ON(1);
544 }
Chris Mason8d7be552007-05-10 11:24:42 -0400545 }
Chris Mason5f39d392007-10-15 16:14:19 -0400546 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
547 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500548 return 0;
549}
550
Chris Mason123abc82007-03-14 14:14:43 -0400551static int check_block(struct btrfs_root *root, struct btrfs_path *path,
552 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500553{
Chris Mason810191f2007-10-15 16:18:55 -0400554 return 0;
Chris Masondb945352007-10-15 16:15:53 -0400555#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400556 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400557
Chris Mason479965d2007-10-15 16:14:27 -0400558 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
559 (unsigned long)btrfs_header_fsid(buf),
560 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400561 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400562 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400563 }
Chris Masondb945352007-10-15 16:15:53 -0400564#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500565 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400566 return check_leaf(root, path, level);
567 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500568}
569
Chris Mason74123bd2007-02-02 11:05:29 -0500570/*
Chris Mason5f39d392007-10-15 16:14:19 -0400571 * search for key in the extent_buffer. The items start at offset p,
572 * and they are item_size apart. There are 'max' items in p.
573 *
Chris Mason74123bd2007-02-02 11:05:29 -0500574 * the slot in the array is returned via slot, and it points to
575 * the place where you would insert key if it is not found in
576 * the array.
577 *
578 * slot may point to max if the key is bigger than all of the keys
579 */
Chris Mason5f39d392007-10-15 16:14:19 -0400580static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
581 int item_size, struct btrfs_key *key,
582 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500583{
584 int low = 0;
585 int high = max;
586 int mid;
587 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400588 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400589 struct btrfs_disk_key unaligned;
590 unsigned long offset;
591 char *map_token = NULL;
592 char *kaddr = NULL;
593 unsigned long map_start = 0;
594 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400595 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500596
597 while(low < high) {
598 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400599 offset = p + mid * item_size;
600
601 if (!map_token || offset < map_start ||
602 (offset + sizeof(struct btrfs_disk_key)) >
603 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400604 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400605 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400606 map_token = NULL;
607 }
608 err = map_extent_buffer(eb, offset,
609 sizeof(struct btrfs_disk_key),
610 &map_token, &kaddr,
611 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400612
Chris Mason479965d2007-10-15 16:14:27 -0400613 if (!err) {
614 tmp = (struct btrfs_disk_key *)(kaddr + offset -
615 map_start);
616 } else {
617 read_extent_buffer(eb, &unaligned,
618 offset, sizeof(unaligned));
619 tmp = &unaligned;
620 }
621
Chris Mason5f39d392007-10-15 16:14:19 -0400622 } else {
623 tmp = (struct btrfs_disk_key *)(kaddr + offset -
624 map_start);
625 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500626 ret = comp_keys(tmp, key);
627
628 if (ret < 0)
629 low = mid + 1;
630 else if (ret > 0)
631 high = mid;
632 else {
633 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400634 if (map_token)
635 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500636 return 0;
637 }
638 }
639 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400640 if (map_token)
641 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500642 return 1;
643}
644
Chris Mason97571fd2007-02-24 13:39:08 -0500645/*
646 * simple bin_search frontend that does the right thing for
647 * leaves vs nodes
648 */
Chris Mason5f39d392007-10-15 16:14:19 -0400649static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
650 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500651{
Chris Mason5f39d392007-10-15 16:14:19 -0400652 if (level == 0) {
653 return generic_bin_search(eb,
654 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400655 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400656 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400657 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500658 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400659 return generic_bin_search(eb,
660 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400661 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400662 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400663 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500664 }
665 return -1;
666}
667
Chris Mason5f39d392007-10-15 16:14:19 -0400668static struct extent_buffer *read_node_slot(struct btrfs_root *root,
669 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500670{
Chris Masonbb803952007-03-01 12:04:21 -0500671 if (slot < 0)
672 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400673 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500674 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400675 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
676 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500677}
678
Chris Masone089f052007-03-16 16:20:31 -0400679static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
680 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500681{
Chris Mason5f39d392007-10-15 16:14:19 -0400682 struct extent_buffer *right = NULL;
683 struct extent_buffer *mid;
684 struct extent_buffer *left = NULL;
685 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500686 int ret = 0;
687 int wret;
688 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500689 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400690 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500691 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500692
693 if (level == 0)
694 return 0;
695
Chris Mason5f39d392007-10-15 16:14:19 -0400696 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500697 WARN_ON(btrfs_header_generation(mid) != trans->transid);
698
Chris Mason1d4f8a02007-03-13 09:28:32 -0400699 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500700
Chris Mason234b63a2007-03-13 10:46:10 -0400701 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400702 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500703 pslot = path->slots[level + 1];
704
Chris Mason40689472007-03-17 14:29:23 -0400705 /*
706 * deal with the case where there is only one pointer in the root
707 * by promoting the node below to a root
708 */
Chris Mason5f39d392007-10-15 16:14:19 -0400709 if (!parent) {
710 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500711
Chris Mason5f39d392007-10-15 16:14:19 -0400712 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500713 return 0;
714
715 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400716 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500717 BUG_ON(!child);
718 root->node = child;
719 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400720 clean_tree_block(trans, root, mid);
721 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500722 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400723 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500724 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
725 root->root_key.objectid,
726 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500727 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400728 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400729 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500730 }
Chris Mason5f39d392007-10-15 16:14:19 -0400731 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400732 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500733 return 0;
734
Chris Mason5f39d392007-10-15 16:14:19 -0400735 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400736 err_on_enospc = 1;
737
Chris Mason5f39d392007-10-15 16:14:19 -0400738 left = read_node_slot(root, parent, pslot - 1);
739 if (left) {
740 wret = btrfs_cow_block(trans, root, left,
741 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400742 if (wret) {
743 ret = wret;
744 goto enospc;
745 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400746 }
Chris Mason5f39d392007-10-15 16:14:19 -0400747 right = read_node_slot(root, parent, pslot + 1);
748 if (right) {
749 wret = btrfs_cow_block(trans, root, right,
750 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400751 if (wret) {
752 ret = wret;
753 goto enospc;
754 }
755 }
756
757 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400758 if (left) {
759 orig_slot += btrfs_header_nritems(left);
760 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500761 if (wret < 0)
762 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400763 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400764 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500765 }
Chris Mason79f95c82007-03-01 15:16:26 -0500766
767 /*
768 * then try to empty the right most buffer into the middle
769 */
Chris Mason5f39d392007-10-15 16:14:19 -0400770 if (right) {
771 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400772 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500773 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400774 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400775 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500776 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400777 u32 blocksize = right->len;
778
Chris Mason5f39d392007-10-15 16:14:19 -0400779 clean_tree_block(trans, root, right);
780 wait_on_tree_block_writeback(root, right);
781 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500782 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400783 wret = del_ptr(trans, root, path, level + 1, pslot +
784 1);
Chris Masonbb803952007-03-01 12:04:21 -0500785 if (wret)
786 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400787 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500788 blocksize,
789 btrfs_header_owner(parent),
790 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500791 if (wret)
792 ret = wret;
793 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400794 struct btrfs_disk_key right_key;
795 btrfs_node_key(right, &right_key, 0);
796 btrfs_set_node_key(parent, &right_key, pslot + 1);
797 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500798 }
799 }
Chris Mason5f39d392007-10-15 16:14:19 -0400800 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500801 /*
802 * we're not allowed to leave a node with one item in the
803 * tree during a delete. A deletion from lower in the tree
804 * could try to delete the only pointer in this node.
805 * So, pull some keys from the left.
806 * There has to be a left pointer at this point because
807 * otherwise we would have pulled some pointers from the
808 * right
809 */
Chris Mason5f39d392007-10-15 16:14:19 -0400810 BUG_ON(!left);
811 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400812 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500813 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400814 goto enospc;
815 }
Chris Mason79f95c82007-03-01 15:16:26 -0500816 BUG_ON(wret == 1);
817 }
Chris Mason5f39d392007-10-15 16:14:19 -0400818 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500819 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500820 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400821 u64 bytenr = mid->start;
822 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400823 clean_tree_block(trans, root, mid);
824 wait_on_tree_block_writeback(root, mid);
825 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500826 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400827 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500828 if (wret)
829 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500830 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
831 btrfs_header_owner(parent),
832 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500833 if (wret)
834 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500835 } else {
836 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400837 struct btrfs_disk_key mid_key;
838 btrfs_node_key(mid, &mid_key, 0);
839 btrfs_set_node_key(parent, &mid_key, pslot);
840 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500841 }
Chris Masonbb803952007-03-01 12:04:21 -0500842
Chris Mason79f95c82007-03-01 15:16:26 -0500843 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400844 if (left) {
845 if (btrfs_header_nritems(left) > orig_slot) {
846 extent_buffer_get(left);
847 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500848 path->slots[level + 1] -= 1;
849 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400850 if (mid)
851 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500852 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400853 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500854 path->slots[level] = orig_slot;
855 }
856 }
Chris Mason79f95c82007-03-01 15:16:26 -0500857 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400858 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400859 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400860 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500861 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400862enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400863 if (right)
864 free_extent_buffer(right);
865 if (left)
866 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500867 return ret;
868}
869
Chris Masone66f7092007-04-20 13:16:02 -0400870/* returns zero if the push worked, non-zero otherwise */
871static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
872 struct btrfs_root *root,
873 struct btrfs_path *path, int level)
874{
Chris Mason5f39d392007-10-15 16:14:19 -0400875 struct extent_buffer *right = NULL;
876 struct extent_buffer *mid;
877 struct extent_buffer *left = NULL;
878 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400879 int ret = 0;
880 int wret;
881 int pslot;
882 int orig_slot = path->slots[level];
883 u64 orig_ptr;
884
885 if (level == 0)
886 return 1;
887
Chris Mason5f39d392007-10-15 16:14:19 -0400888 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500889 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -0400890 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
891
892 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400893 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400894 pslot = path->slots[level + 1];
895
Chris Mason5f39d392007-10-15 16:14:19 -0400896 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400897 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400898
Chris Mason5f39d392007-10-15 16:14:19 -0400899 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400900
901 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400902 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400903 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400904 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400905 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
906 wret = 1;
907 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400908 ret = btrfs_cow_block(trans, root, left, parent,
909 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400910 if (ret)
911 wret = 1;
912 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400913 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400914 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400915 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400916 }
Chris Masone66f7092007-04-20 13:16:02 -0400917 if (wret < 0)
918 ret = wret;
919 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400920 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400921 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400922 btrfs_node_key(mid, &disk_key, 0);
923 btrfs_set_node_key(parent, &disk_key, pslot);
924 btrfs_mark_buffer_dirty(parent);
925 if (btrfs_header_nritems(left) > orig_slot) {
926 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400927 path->slots[level + 1] -= 1;
928 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400929 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400930 } else {
931 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400932 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400933 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400934 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400935 }
Chris Masone66f7092007-04-20 13:16:02 -0400936 return 0;
937 }
Chris Mason5f39d392007-10-15 16:14:19 -0400938 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400939 }
Chris Mason5f39d392007-10-15 16:14:19 -0400940 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400941
942 /*
943 * then try to empty the right most buffer into the middle
944 */
Chris Mason5f39d392007-10-15 16:14:19 -0400945 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400946 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400947 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400948 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
949 wret = 1;
950 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400951 ret = btrfs_cow_block(trans, root, right,
952 parent, pslot + 1,
953 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400954 if (ret)
955 wret = 1;
956 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400957 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400958 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400959 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400960 }
Chris Masone66f7092007-04-20 13:16:02 -0400961 if (wret < 0)
962 ret = wret;
963 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400964 struct btrfs_disk_key disk_key;
965
966 btrfs_node_key(right, &disk_key, 0);
967 btrfs_set_node_key(parent, &disk_key, pslot + 1);
968 btrfs_mark_buffer_dirty(parent);
969
970 if (btrfs_header_nritems(mid) <= orig_slot) {
971 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -0400972 path->slots[level + 1] += 1;
973 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -0400974 btrfs_header_nritems(mid);
975 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400976 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400977 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400978 }
Chris Masone66f7092007-04-20 13:16:02 -0400979 return 0;
980 }
Chris Mason5f39d392007-10-15 16:14:19 -0400981 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400982 }
Chris Masone66f7092007-04-20 13:16:02 -0400983 return 1;
984}
985
Chris Mason74123bd2007-02-02 11:05:29 -0500986/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400987 * readahead one full node of leaves
988 */
989static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -0500990 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -0400991{
Chris Mason5f39d392007-10-15 16:14:19 -0400992 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -0500993 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -0400994 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -0400995 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -0400996 u64 lowest_read;
997 u64 highest_read;
998 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -0400999 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001000 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001001 u32 nr;
1002 u32 blocksize;
1003 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001004
Chris Masona6b6e752007-10-15 16:22:39 -04001005 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001006 return;
1007
Chris Mason6702ed42007-08-07 16:15:09 -04001008 if (!path->nodes[level])
1009 return;
1010
Chris Mason5f39d392007-10-15 16:14:19 -04001011 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -04001012 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001013 blocksize = btrfs_level_size(root, level - 1);
1014 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001015 if (eb) {
1016 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001017 return;
1018 }
1019
Chris Mason6b800532007-10-15 16:17:34 -04001020 highest_read = search;
1021 lowest_read = search;
1022
Chris Mason5f39d392007-10-15 16:14:19 -04001023 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001024 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001025 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001026 if (direction < 0) {
1027 if (nr == 0)
1028 break;
1029 nr--;
1030 } else if (direction > 0) {
1031 nr++;
1032 if (nr >= nritems)
1033 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001034 }
Chris Mason01f46652007-12-21 16:24:26 -05001035 if (path->reada < 0 && objectid) {
1036 btrfs_node_key(node, &disk_key, nr);
1037 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1038 break;
1039 }
Chris Mason6b800532007-10-15 16:17:34 -04001040 search = btrfs_node_blockptr(node, nr);
1041 if ((search >= lowest_read && search <= highest_read) ||
1042 (search < lowest_read && lowest_read - search <= 32768) ||
1043 (search > highest_read && search - highest_read <= 32768)) {
1044 readahead_tree_block(root, search, blocksize);
1045 nread += blocksize;
1046 }
1047 nscan++;
1048 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1049 break;
1050 if(nread > (1024 * 1024) || nscan > 128)
1051 break;
1052
1053 if (search < lowest_read)
1054 lowest_read = search;
1055 if (search > highest_read)
1056 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001057 }
1058}
1059/*
Chris Mason74123bd2007-02-02 11:05:29 -05001060 * look for key in the tree. path is filled in with nodes along the way
1061 * if key is found, we return zero and you can find the item in the leaf
1062 * level of the path (level 0)
1063 *
1064 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001065 * be inserted, and 1 is returned. If there are other errors during the
1066 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001067 *
1068 * if ins_len > 0, nodes and leaves will be split as we walk down the
1069 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1070 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001071 */
Chris Masone089f052007-03-16 16:20:31 -04001072int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1073 *root, struct btrfs_key *key, struct btrfs_path *p, int
1074 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001075{
Chris Mason5f39d392007-10-15 16:14:19 -04001076 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -04001077 u64 bytenr;
Chris Mason74493f72007-12-11 09:25:06 -05001078 u64 ptr_gen;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001079 int slot;
1080 int ret;
1081 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001082 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -04001083 u8 lowest_level = 0;
1084
Chris Mason6702ed42007-08-07 16:15:09 -04001085 lowest_level = p->lowest_level;
1086 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001087 WARN_ON(p->nodes[0] != NULL);
1088 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -05001089again:
1090 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001091 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001092 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001093 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001094 if (cow) {
1095 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001096 wret = btrfs_cow_block(trans, root, b,
1097 p->nodes[level + 1],
1098 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001099 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001100 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001101 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001102 return wret;
1103 }
Chris Mason02217ed2007-03-02 16:08:05 -05001104 }
1105 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001106 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001107 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001108 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001109 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001110 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001111 if (ret)
1112 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001113 ret = bin_search(b, key, level, &slot);
1114 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001115 if (ret && slot > 0)
1116 slot -= 1;
1117 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001118 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001119 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001120 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001121 BUG_ON(sret > 0);
1122 if (sret)
1123 return sret;
1124 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001125 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001126 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001127 int sret = balance_level(trans, root, p,
1128 level);
Chris Masonbb803952007-03-01 12:04:21 -05001129 if (sret)
1130 return sret;
1131 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001132 if (!b) {
1133 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001134 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001135 }
Chris Masonbb803952007-03-01 12:04:21 -05001136 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001137 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001138 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001139 /* this is only true while dropping a snapshot */
1140 if (level == lowest_level)
1141 break;
Chris Masondb945352007-10-15 16:15:53 -04001142 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason74493f72007-12-11 09:25:06 -05001143 ptr_gen = btrfs_node_ptr_generation(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001144 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001145 reada_for_search(root, p, level, slot,
1146 key->objectid);
Chris Masondb945352007-10-15 16:15:53 -04001147 b = read_tree_block(root, bytenr,
1148 btrfs_level_size(root, level - 1));
Chris Mason74493f72007-12-11 09:25:06 -05001149 if (ptr_gen != btrfs_header_generation(b)) {
1150 printk("block %llu bad gen wanted %llu "
1151 "found %llu\n",
1152 (unsigned long long)b->start,
1153 (unsigned long long)ptr_gen,
1154 (unsigned long long)btrfs_header_generation(b));
1155 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001156 } else {
1157 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001158 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001159 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001160 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001161 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001162 BUG_ON(sret > 0);
1163 if (sret)
1164 return sret;
1165 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001166 return ret;
1167 }
1168 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001169 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001170}
1171
Chris Mason74123bd2007-02-02 11:05:29 -05001172/*
1173 * adjust the pointers going up the tree, starting at level
1174 * making sure the right key of each node is points to 'key'.
1175 * This is used after shifting pointers to the left, so it stops
1176 * fixing up pointers when a given leaf/node is not in slot 0 of the
1177 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001178 *
1179 * If this fails to write a tree block, it returns -1, but continues
1180 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001181 */
Chris Mason5f39d392007-10-15 16:14:19 -04001182static int fixup_low_keys(struct btrfs_trans_handle *trans,
1183 struct btrfs_root *root, struct btrfs_path *path,
1184 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001185{
1186 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001187 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001188 struct extent_buffer *t;
1189
Chris Mason234b63a2007-03-13 10:46:10 -04001190 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001191 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001192 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001193 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001194 t = path->nodes[i];
1195 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001196 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001197 if (tslot != 0)
1198 break;
1199 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001200 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001201}
1202
Chris Mason74123bd2007-02-02 11:05:29 -05001203/*
1204 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001205 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001206 *
1207 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1208 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001209 */
Chris Masone089f052007-03-16 16:20:31 -04001210static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason5f39d392007-10-15 16:14:19 -04001211 *root, struct extent_buffer *dst,
1212 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001213{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001214 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001215 int src_nritems;
1216 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001217 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001218
Chris Mason5f39d392007-10-15 16:14:19 -04001219 src_nritems = btrfs_header_nritems(src);
1220 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001221 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001222 WARN_ON(btrfs_header_generation(src) != trans->transid);
1223 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001224
Chris Masoneb60cea2007-02-02 09:18:22 -05001225 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001226 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001227 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001228
1229 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001230 push_items = src_nritems;
1231
Chris Mason5f39d392007-10-15 16:14:19 -04001232 copy_extent_buffer(dst, src,
1233 btrfs_node_key_ptr_offset(dst_nritems),
1234 btrfs_node_key_ptr_offset(0),
1235 push_items * sizeof(struct btrfs_key_ptr));
1236
Chris Masonbb803952007-03-01 12:04:21 -05001237 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001238 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1239 btrfs_node_key_ptr_offset(push_items),
1240 (src_nritems - push_items) *
1241 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001242 }
Chris Mason5f39d392007-10-15 16:14:19 -04001243 btrfs_set_header_nritems(src, src_nritems - push_items);
1244 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1245 btrfs_mark_buffer_dirty(src);
1246 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001247 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001248}
1249
Chris Mason97571fd2007-02-24 13:39:08 -05001250/*
Chris Mason79f95c82007-03-01 15:16:26 -05001251 * try to push data from one node into the next node right in the
1252 * tree.
1253 *
1254 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1255 * error, and > 0 if there was no room in the right hand block.
1256 *
1257 * this will only push up to 1/2 the contents of the left node over
1258 */
Chris Mason5f39d392007-10-15 16:14:19 -04001259static int balance_node_right(struct btrfs_trans_handle *trans,
1260 struct btrfs_root *root,
1261 struct extent_buffer *dst,
1262 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001263{
Chris Mason79f95c82007-03-01 15:16:26 -05001264 int push_items = 0;
1265 int max_push;
1266 int src_nritems;
1267 int dst_nritems;
1268 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001269
Chris Mason7bb86312007-12-11 09:25:06 -05001270 WARN_ON(btrfs_header_generation(src) != trans->transid);
1271 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1272
Chris Mason5f39d392007-10-15 16:14:19 -04001273 src_nritems = btrfs_header_nritems(src);
1274 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001275 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001276 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001277 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001278
1279 max_push = src_nritems / 2 + 1;
1280 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001281 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001282 return 1;
Yan252c38f2007-08-29 09:11:44 -04001283
Chris Mason79f95c82007-03-01 15:16:26 -05001284 if (max_push < push_items)
1285 push_items = max_push;
1286
Chris Mason5f39d392007-10-15 16:14:19 -04001287 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1288 btrfs_node_key_ptr_offset(0),
1289 (dst_nritems) *
1290 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001291
Chris Mason5f39d392007-10-15 16:14:19 -04001292 copy_extent_buffer(dst, src,
1293 btrfs_node_key_ptr_offset(0),
1294 btrfs_node_key_ptr_offset(src_nritems - push_items),
1295 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001296
Chris Mason5f39d392007-10-15 16:14:19 -04001297 btrfs_set_header_nritems(src, src_nritems - push_items);
1298 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001299
Chris Mason5f39d392007-10-15 16:14:19 -04001300 btrfs_mark_buffer_dirty(src);
1301 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001302 return ret;
1303}
1304
1305/*
Chris Mason97571fd2007-02-24 13:39:08 -05001306 * helper function to insert a new root level in the tree.
1307 * A new node is allocated, and a single item is inserted to
1308 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001309 *
1310 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001311 */
Chris Mason5f39d392007-10-15 16:14:19 -04001312static int insert_new_root(struct btrfs_trans_handle *trans,
1313 struct btrfs_root *root,
1314 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001315{
Chris Mason7bb86312007-12-11 09:25:06 -05001316 u64 root_gen;
1317 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001318 struct extent_buffer *lower;
1319 struct extent_buffer *c;
1320 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001321
1322 BUG_ON(path->nodes[level]);
1323 BUG_ON(path->nodes[level-1] != root->node);
1324
Chris Mason7bb86312007-12-11 09:25:06 -05001325 if (root->ref_cows)
1326 root_gen = trans->transid;
1327 else
1328 root_gen = 0;
1329
1330 lower = path->nodes[level-1];
1331 if (level == 1)
1332 btrfs_item_key(lower, &lower_key, 0);
1333 else
1334 btrfs_node_key(lower, &lower_key, 0);
1335
1336 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1337 root->root_key.objectid,
1338 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001339 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001340 if (IS_ERR(c))
1341 return PTR_ERR(c);
1342 memset_extent_buffer(c, 0, 0, root->nodesize);
1343 btrfs_set_header_nritems(c, 1);
1344 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001345 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001346 btrfs_set_header_generation(c, trans->transid);
1347 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001348
Chris Mason5f39d392007-10-15 16:14:19 -04001349 write_extent_buffer(c, root->fs_info->fsid,
1350 (unsigned long)btrfs_header_fsid(c),
1351 BTRFS_FSID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001352 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001353 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001354 lower_gen = btrfs_header_generation(lower);
1355 WARN_ON(lower_gen == 0);
1356
1357 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001358
1359 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001360
Chris Mason5c680ed2007-02-22 11:39:13 -05001361 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001362 free_extent_buffer(root->node);
1363 root->node = c;
1364 extent_buffer_get(c);
1365 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001366 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001367
1368 if (root->ref_cows && lower_gen != trans->transid) {
1369 struct btrfs_path *back_path = btrfs_alloc_path();
1370 int ret;
1371 ret = btrfs_insert_extent_backref(trans,
1372 root->fs_info->extent_root,
1373 path, lower->start,
1374 root->root_key.objectid,
1375 trans->transid, 0, 0);
1376 BUG_ON(ret);
1377 btrfs_free_path(back_path);
1378 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001379 return 0;
1380}
1381
Chris Mason74123bd2007-02-02 11:05:29 -05001382/*
1383 * worker function to insert a single pointer in a node.
1384 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001385 *
Chris Mason74123bd2007-02-02 11:05:29 -05001386 * slot and level indicate where you want the key to go, and
1387 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001388 *
1389 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001390 */
Chris Masone089f052007-03-16 16:20:31 -04001391static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1392 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001393 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001394{
Chris Mason5f39d392007-10-15 16:14:19 -04001395 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001396 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001397
1398 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001399 lower = path->nodes[level];
1400 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001401 if (slot > nritems)
1402 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001403 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001404 BUG();
1405 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001406 memmove_extent_buffer(lower,
1407 btrfs_node_key_ptr_offset(slot + 1),
1408 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001409 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001410 }
Chris Mason5f39d392007-10-15 16:14:19 -04001411 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001412 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001413 WARN_ON(trans->transid == 0);
1414 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001415 btrfs_set_header_nritems(lower, nritems + 1);
1416 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001417 return 0;
1418}
1419
Chris Mason97571fd2007-02-24 13:39:08 -05001420/*
1421 * split the node at the specified level in path in two.
1422 * The path is corrected to point to the appropriate node after the split
1423 *
1424 * Before splitting this tries to make some room in the node by pushing
1425 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001426 *
1427 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001428 */
Chris Masone089f052007-03-16 16:20:31 -04001429static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1430 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001431{
Chris Mason7bb86312007-12-11 09:25:06 -05001432 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001433 struct extent_buffer *c;
1434 struct extent_buffer *split;
1435 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001436 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001437 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001438 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001439 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001440
Chris Mason5f39d392007-10-15 16:14:19 -04001441 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001442 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001443 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001444 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001445 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001446 if (ret)
1447 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001448 } else {
1449 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001450 c = path->nodes[level];
1451 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001452 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1453 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001454 if (ret < 0)
1455 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001456 }
Chris Masone66f7092007-04-20 13:16:02 -04001457
Chris Mason5f39d392007-10-15 16:14:19 -04001458 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001459 if (root->ref_cows)
1460 root_gen = trans->transid;
1461 else
1462 root_gen = 0;
1463
1464 btrfs_node_key(c, &disk_key, 0);
1465 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1466 root->root_key.objectid,
1467 root_gen,
1468 btrfs_disk_key_objectid(&disk_key),
1469 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001470 if (IS_ERR(split))
1471 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001472
Chris Mason5f39d392007-10-15 16:14:19 -04001473 btrfs_set_header_flags(split, btrfs_header_flags(c));
1474 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001475 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001476 btrfs_set_header_generation(split, trans->transid);
1477 btrfs_set_header_owner(split, root->root_key.objectid);
1478 write_extent_buffer(split, root->fs_info->fsid,
1479 (unsigned long)btrfs_header_fsid(split),
1480 BTRFS_FSID_SIZE);
1481
Chris Mason7518a232007-03-12 12:01:18 -04001482 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001483
1484 copy_extent_buffer(split, c,
1485 btrfs_node_key_ptr_offset(0),
1486 btrfs_node_key_ptr_offset(mid),
1487 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1488 btrfs_set_header_nritems(split, c_nritems - mid);
1489 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001490 ret = 0;
1491
Chris Mason5f39d392007-10-15 16:14:19 -04001492 btrfs_mark_buffer_dirty(c);
1493 btrfs_mark_buffer_dirty(split);
1494
1495 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001496 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001497 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001498 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001499 if (wret)
1500 ret = wret;
1501
Chris Mason5de08d72007-02-24 06:24:44 -05001502 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001503 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001504 free_extent_buffer(c);
1505 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001506 path->slots[level + 1] += 1;
1507 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001508 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001509 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001510 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001511}
1512
Chris Mason74123bd2007-02-02 11:05:29 -05001513/*
1514 * how many bytes are required to store the items in a leaf. start
1515 * and nr indicate which items in the leaf to check. This totals up the
1516 * space used both by the item structs and the item data
1517 */
Chris Mason5f39d392007-10-15 16:14:19 -04001518static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001519{
1520 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001521 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001522 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001523
1524 if (!nr)
1525 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001526 data_len = btrfs_item_end_nr(l, start);
1527 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001528 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001529 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001530 return data_len;
1531}
1532
Chris Mason74123bd2007-02-02 11:05:29 -05001533/*
Chris Masond4dbff92007-04-04 14:08:15 -04001534 * The space between the end of the leaf items and
1535 * the start of the leaf data. IOW, how much room
1536 * the leaf has left for both items and data
1537 */
Chris Mason5f39d392007-10-15 16:14:19 -04001538int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001539{
Chris Mason5f39d392007-10-15 16:14:19 -04001540 int nritems = btrfs_header_nritems(leaf);
1541 int ret;
1542 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1543 if (ret < 0) {
1544 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001545 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001546 leaf_space_used(leaf, 0, nritems), nritems);
1547 }
1548 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001549}
1550
1551/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001552 * push some data in the path leaf to the right, trying to free up at
1553 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001554 *
1555 * returns 1 if the push failed because the other node didn't have enough
1556 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001557 */
Chris Masone089f052007-03-16 16:20:31 -04001558static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001559 *root, struct btrfs_path *path, int data_size,
1560 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001561{
Chris Mason5f39d392007-10-15 16:14:19 -04001562 struct extent_buffer *left = path->nodes[0];
1563 struct extent_buffer *right;
1564 struct extent_buffer *upper;
1565 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001566 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001567 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001568 int free_space;
1569 int push_space = 0;
1570 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001571 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001572 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001573 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001574 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001575 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001576 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001577 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001578
1579 slot = path->slots[1];
1580 if (!path->nodes[1]) {
1581 return 1;
1582 }
1583 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001584 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001585 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001586
Chris Masondb945352007-10-15 16:15:53 -04001587 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1588 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001589 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001590 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001591 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001592 return 1;
1593 }
1594
Chris Mason5f39d392007-10-15 16:14:19 -04001595 /* cow and double check */
1596 ret = btrfs_cow_block(trans, root, right, upper,
1597 slot + 1, &right);
1598 if (ret) {
1599 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001600 return 1;
1601 }
Chris Mason5f39d392007-10-15 16:14:19 -04001602 free_space = btrfs_leaf_free_space(root, right);
1603 if (free_space < data_size + sizeof(struct btrfs_item)) {
1604 free_extent_buffer(right);
1605 return 1;
1606 }
1607
1608 left_nritems = btrfs_header_nritems(left);
1609 if (left_nritems == 0) {
1610 free_extent_buffer(right);
1611 return 1;
1612 }
1613
Chris Mason34a38212007-11-07 13:31:03 -05001614 if (empty)
1615 nr = 0;
1616 else
1617 nr = 1;
1618
1619 i = left_nritems - 1;
1620 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001621 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001622
Chris Mason00ec4c52007-02-24 12:47:20 -05001623 if (path->slots[0] == i)
1624 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001625
1626 if (!left->map_token) {
1627 map_extent_buffer(left, (unsigned long)item,
1628 sizeof(struct btrfs_item),
1629 &left->map_token, &left->kaddr,
1630 &left->map_start, &left->map_len,
1631 KM_USER1);
1632 }
1633
1634 this_item_size = btrfs_item_size(left, item);
1635 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001636 break;
1637 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001638 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001639 if (i == 0)
1640 break;
1641 i--;
Chris Masondb945352007-10-15 16:15:53 -04001642 }
1643 if (left->map_token) {
1644 unmap_extent_buffer(left, left->map_token, KM_USER1);
1645 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001646 }
Chris Mason5f39d392007-10-15 16:14:19 -04001647
Chris Mason00ec4c52007-02-24 12:47:20 -05001648 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001649 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001650 return 1;
1651 }
Chris Mason5f39d392007-10-15 16:14:19 -04001652
Chris Mason34a38212007-11-07 13:31:03 -05001653 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001654 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001655
Chris Mason00ec4c52007-02-24 12:47:20 -05001656 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001657 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001658
Chris Mason5f39d392007-10-15 16:14:19 -04001659 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001660 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001661
Chris Mason00ec4c52007-02-24 12:47:20 -05001662 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001663 data_end = leaf_data_end(root, right);
1664 memmove_extent_buffer(right,
1665 btrfs_leaf_data(right) + data_end - push_space,
1666 btrfs_leaf_data(right) + data_end,
1667 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1668
Chris Mason00ec4c52007-02-24 12:47:20 -05001669 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001670 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001671 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1672 btrfs_leaf_data(left) + leaf_data_end(root, left),
1673 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001674
1675 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1676 btrfs_item_nr_offset(0),
1677 right_nritems * sizeof(struct btrfs_item));
1678
Chris Mason00ec4c52007-02-24 12:47:20 -05001679 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001680 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1681 btrfs_item_nr_offset(left_nritems - push_items),
1682 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001683
1684 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001685 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001686 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001687 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001688 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001689 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001690 if (!right->map_token) {
1691 map_extent_buffer(right, (unsigned long)item,
1692 sizeof(struct btrfs_item),
1693 &right->map_token, &right->kaddr,
1694 &right->map_start, &right->map_len,
1695 KM_USER1);
1696 }
1697 push_space -= btrfs_item_size(right, item);
1698 btrfs_set_item_offset(right, item, push_space);
1699 }
1700
1701 if (right->map_token) {
1702 unmap_extent_buffer(right, right->map_token, KM_USER1);
1703 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001704 }
Chris Mason7518a232007-03-12 12:01:18 -04001705 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001706 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001707
Chris Mason34a38212007-11-07 13:31:03 -05001708 if (left_nritems)
1709 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001710 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001711
Chris Mason5f39d392007-10-15 16:14:19 -04001712 btrfs_item_key(right, &disk_key, 0);
1713 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001714 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001715
Chris Mason00ec4c52007-02-24 12:47:20 -05001716 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001717 if (path->slots[0] >= left_nritems) {
1718 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001719 free_extent_buffer(path->nodes[0]);
1720 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001721 path->slots[1] += 1;
1722 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001723 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001724 }
1725 return 0;
1726}
1727/*
Chris Mason74123bd2007-02-02 11:05:29 -05001728 * push some data in the path leaf to the left, trying to free up at
1729 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1730 */
Chris Masone089f052007-03-16 16:20:31 -04001731static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001732 *root, struct btrfs_path *path, int data_size,
1733 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001734{
Chris Mason5f39d392007-10-15 16:14:19 -04001735 struct btrfs_disk_key disk_key;
1736 struct extent_buffer *right = path->nodes[0];
1737 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001738 int slot;
1739 int i;
1740 int free_space;
1741 int push_space = 0;
1742 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001743 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001744 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001745 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001746 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001747 int ret = 0;
1748 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001749 u32 this_item_size;
1750 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001751
1752 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001753 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001754 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001755 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001756 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001757
Chris Mason3685f792007-10-19 09:23:27 -04001758 right_nritems = btrfs_header_nritems(right);
1759 if (right_nritems == 0) {
1760 return 1;
1761 }
1762
Chris Mason5f39d392007-10-15 16:14:19 -04001763 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001764 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001765 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001766 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001767 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001768 return 1;
1769 }
Chris Mason02217ed2007-03-02 16:08:05 -05001770
1771 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001772 ret = btrfs_cow_block(trans, root, left,
1773 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001774 if (ret) {
1775 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001776 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001777 return 1;
1778 }
Chris Mason3685f792007-10-19 09:23:27 -04001779
Chris Mason123abc82007-03-14 14:14:43 -04001780 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001781 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001782 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001783 return 1;
1784 }
1785
Chris Mason34a38212007-11-07 13:31:03 -05001786 if (empty)
1787 nr = right_nritems;
1788 else
1789 nr = right_nritems - 1;
1790
1791 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001792 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001793 if (!right->map_token) {
1794 map_extent_buffer(right, (unsigned long)item,
1795 sizeof(struct btrfs_item),
1796 &right->map_token, &right->kaddr,
1797 &right->map_start, &right->map_len,
1798 KM_USER1);
1799 }
1800
Chris Masonbe0e5c02007-01-26 15:51:26 -05001801 if (path->slots[0] == i)
1802 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001803
1804 this_item_size = btrfs_item_size(right, item);
1805 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001806 break;
Chris Masondb945352007-10-15 16:15:53 -04001807
Chris Masonbe0e5c02007-01-26 15:51:26 -05001808 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001809 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001810 }
Chris Masondb945352007-10-15 16:15:53 -04001811
1812 if (right->map_token) {
1813 unmap_extent_buffer(right, right->map_token, KM_USER1);
1814 right->map_token = NULL;
1815 }
1816
Chris Masonbe0e5c02007-01-26 15:51:26 -05001817 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001818 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001819 return 1;
1820 }
Chris Mason34a38212007-11-07 13:31:03 -05001821 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001822 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001823
Chris Masonbe0e5c02007-01-26 15:51:26 -05001824 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001825 copy_extent_buffer(left, right,
1826 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1827 btrfs_item_nr_offset(0),
1828 push_items * sizeof(struct btrfs_item));
1829
Chris Mason123abc82007-03-14 14:14:43 -04001830 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001831 btrfs_item_offset_nr(right, push_items -1);
1832
1833 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001834 leaf_data_end(root, left) - push_space,
1835 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001836 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001837 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001838 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001839 BUG_ON(old_left_nritems < 0);
1840
Chris Masondb945352007-10-15 16:15:53 -04001841 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001842 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001843 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001844
Chris Mason5f39d392007-10-15 16:14:19 -04001845 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001846 if (!left->map_token) {
1847 map_extent_buffer(left, (unsigned long)item,
1848 sizeof(struct btrfs_item),
1849 &left->map_token, &left->kaddr,
1850 &left->map_start, &left->map_len,
1851 KM_USER1);
1852 }
1853
Chris Mason5f39d392007-10-15 16:14:19 -04001854 ioff = btrfs_item_offset(left, item);
1855 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001856 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001857 }
Chris Mason5f39d392007-10-15 16:14:19 -04001858 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001859 if (left->map_token) {
1860 unmap_extent_buffer(left, left->map_token, KM_USER1);
1861 left->map_token = NULL;
1862 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001863
1864 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001865 if (push_items > right_nritems) {
1866 printk("push items %d nr %u\n", push_items, right_nritems);
1867 WARN_ON(1);
1868 }
Chris Mason5f39d392007-10-15 16:14:19 -04001869
Chris Mason34a38212007-11-07 13:31:03 -05001870 if (push_items < right_nritems) {
1871 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1872 leaf_data_end(root, right);
1873 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1874 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1875 btrfs_leaf_data(right) +
1876 leaf_data_end(root, right), push_space);
1877
1878 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001879 btrfs_item_nr_offset(push_items),
1880 (btrfs_header_nritems(right) - push_items) *
1881 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001882 }
Yaneef1c492007-11-26 10:58:13 -05001883 right_nritems -= push_items;
1884 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001885 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001886 for (i = 0; i < right_nritems; i++) {
1887 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001888
1889 if (!right->map_token) {
1890 map_extent_buffer(right, (unsigned long)item,
1891 sizeof(struct btrfs_item),
1892 &right->map_token, &right->kaddr,
1893 &right->map_start, &right->map_len,
1894 KM_USER1);
1895 }
1896
1897 push_space = push_space - btrfs_item_size(right, item);
1898 btrfs_set_item_offset(right, item, push_space);
1899 }
1900 if (right->map_token) {
1901 unmap_extent_buffer(right, right->map_token, KM_USER1);
1902 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001903 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001904
Chris Mason5f39d392007-10-15 16:14:19 -04001905 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001906 if (right_nritems)
1907 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001908
Chris Mason5f39d392007-10-15 16:14:19 -04001909 btrfs_item_key(right, &disk_key, 0);
1910 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001911 if (wret)
1912 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001913
1914 /* then fixup the leaf pointer in the path */
1915 if (path->slots[0] < push_items) {
1916 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001917 free_extent_buffer(path->nodes[0]);
1918 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001919 path->slots[1] -= 1;
1920 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001921 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001922 path->slots[0] -= push_items;
1923 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001924 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001925 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001926}
1927
Chris Mason74123bd2007-02-02 11:05:29 -05001928/*
1929 * split the path's leaf in two, making sure there is at least data_size
1930 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001931 *
1932 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001933 */
Chris Masone089f052007-03-16 16:20:31 -04001934static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001935 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001936 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001937{
Chris Mason7bb86312007-12-11 09:25:06 -05001938 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001939 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001940 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001941 int mid;
1942 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001943 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001944 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001945 int data_copy_size;
1946 int rt_data_off;
1947 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001948 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001949 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001950 int double_split;
1951 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001952 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001953
Chris Masoncc0c5532007-10-25 15:42:57 -04001954 if (extend)
1955 space_needed = data_size;
1956
Chris Mason7bb86312007-12-11 09:25:06 -05001957 if (root->ref_cows)
1958 root_gen = trans->transid;
1959 else
1960 root_gen = 0;
1961
Chris Mason40689472007-03-17 14:29:23 -04001962 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04001963 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05001964 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001965 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04001966 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04001967 }
1968 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05001969 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001970 if (wret < 0)
1971 return wret;
1972 }
1973 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05001974
Chris Mason3685f792007-10-19 09:23:27 -04001975 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04001976 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04001977 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04001978 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001979
Chris Mason5c680ed2007-02-22 11:39:13 -05001980 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001981 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001982 if (ret)
1983 return ret;
1984 }
Chris Masoncc0c5532007-10-25 15:42:57 -04001985again:
1986 double_split = 0;
1987 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001988 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001989 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001990 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001991
Chris Mason7bb86312007-12-11 09:25:06 -05001992 btrfs_item_key(l, &disk_key, 0);
1993
1994 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
1995 root->root_key.objectid,
1996 root_gen, disk_key.objectid, 0,
1997 l->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001998 if (IS_ERR(right))
1999 return PTR_ERR(right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002000
Chris Mason5f39d392007-10-15 16:14:19 -04002001 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002002 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002003 btrfs_set_header_generation(right, trans->transid);
2004 btrfs_set_header_owner(right, root->root_key.objectid);
2005 btrfs_set_header_level(right, 0);
2006 write_extent_buffer(right, root->fs_info->fsid,
2007 (unsigned long)btrfs_header_fsid(right),
2008 BTRFS_FSID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002009 if (mid <= slot) {
2010 if (nritems == 1 ||
2011 leaf_space_used(l, mid, nritems - mid) + space_needed >
2012 BTRFS_LEAF_DATA_SIZE(root)) {
2013 if (slot >= nritems) {
2014 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002015 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002016 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002017 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002018 path->slots[1] + 1, 1);
2019 if (wret)
2020 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002021 free_extent_buffer(path->nodes[0]);
2022 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002023 path->slots[0] = 0;
2024 path->slots[1] += 1;
2025 return ret;
2026 }
2027 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002028 if (mid != nritems &&
2029 leaf_space_used(l, mid, nritems - mid) +
2030 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2031 double_split = 1;
2032 }
Chris Masond4dbff92007-04-04 14:08:15 -04002033 }
2034 } else {
2035 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2036 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002037 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002038 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002039 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002040 wret = insert_ptr(trans, root, path,
2041 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002042 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002043 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002044 if (wret)
2045 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002046 free_extent_buffer(path->nodes[0]);
2047 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002048 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002049 if (path->slots[1] == 0) {
2050 wret = fixup_low_keys(trans, root,
2051 path, &disk_key, 1);
2052 if (wret)
2053 ret = wret;
2054 }
Chris Masond4dbff92007-04-04 14:08:15 -04002055 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002056 } else if (extend && slot == 0) {
2057 mid = 1;
2058 } else {
2059 mid = slot;
2060 if (mid != nritems &&
2061 leaf_space_used(l, mid, nritems - mid) +
2062 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2063 double_split = 1;
2064 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002065 }
Chris Masond4dbff92007-04-04 14:08:15 -04002066 }
2067 }
Chris Mason5f39d392007-10-15 16:14:19 -04002068 nritems = nritems - mid;
2069 btrfs_set_header_nritems(right, nritems);
2070 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2071
2072 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2073 btrfs_item_nr_offset(mid),
2074 nritems * sizeof(struct btrfs_item));
2075
2076 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002077 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2078 data_copy_size, btrfs_leaf_data(l) +
2079 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002080
Chris Mason5f39d392007-10-15 16:14:19 -04002081 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2082 btrfs_item_end_nr(l, mid);
2083
2084 for (i = 0; i < nritems; i++) {
2085 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002086 u32 ioff;
2087
2088 if (!right->map_token) {
2089 map_extent_buffer(right, (unsigned long)item,
2090 sizeof(struct btrfs_item),
2091 &right->map_token, &right->kaddr,
2092 &right->map_start, &right->map_len,
2093 KM_USER1);
2094 }
2095
2096 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002098 }
Chris Mason74123bd2007-02-02 11:05:29 -05002099
Chris Masondb945352007-10-15 16:15:53 -04002100 if (right->map_token) {
2101 unmap_extent_buffer(right, right->map_token, KM_USER1);
2102 right->map_token = NULL;
2103 }
2104
Chris Mason5f39d392007-10-15 16:14:19 -04002105 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002106 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002107 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002108 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2109 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002110 if (wret)
2111 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002112
2113 btrfs_mark_buffer_dirty(right);
2114 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002115 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002116
Chris Masonbe0e5c02007-01-26 15:51:26 -05002117 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002118 free_extent_buffer(path->nodes[0]);
2119 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002120 path->slots[0] -= mid;
2121 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002122 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002123 free_extent_buffer(right);
2124
Chris Masoneb60cea2007-02-02 09:18:22 -05002125 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002126
Chris Masoncc0c5532007-10-25 15:42:57 -04002127 if (double_split) {
2128 BUG_ON(num_doubles != 0);
2129 num_doubles++;
2130 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002131 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002132 return ret;
2133}
2134
Chris Masonb18c6682007-04-17 13:26:50 -04002135int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2136 struct btrfs_root *root,
2137 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002138 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002139{
2140 int ret = 0;
2141 int slot;
2142 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002143 struct extent_buffer *leaf;
2144 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002145 u32 nritems;
2146 unsigned int data_end;
2147 unsigned int old_data_start;
2148 unsigned int old_size;
2149 unsigned int size_diff;
2150 int i;
2151
2152 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002153 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002154 slot = path->slots[0];
2155
2156 old_size = btrfs_item_size_nr(leaf, slot);
2157 if (old_size == new_size)
2158 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002159
Chris Mason5f39d392007-10-15 16:14:19 -04002160 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002161 data_end = leaf_data_end(root, leaf);
2162
Chris Mason5f39d392007-10-15 16:14:19 -04002163 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002164
Chris Masonb18c6682007-04-17 13:26:50 -04002165 size_diff = old_size - new_size;
2166
2167 BUG_ON(slot < 0);
2168 BUG_ON(slot >= nritems);
2169
2170 /*
2171 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2172 */
2173 /* first correct the data pointers */
2174 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002175 u32 ioff;
2176 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002177
2178 if (!leaf->map_token) {
2179 map_extent_buffer(leaf, (unsigned long)item,
2180 sizeof(struct btrfs_item),
2181 &leaf->map_token, &leaf->kaddr,
2182 &leaf->map_start, &leaf->map_len,
2183 KM_USER1);
2184 }
2185
Chris Mason5f39d392007-10-15 16:14:19 -04002186 ioff = btrfs_item_offset(leaf, item);
2187 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002188 }
Chris Masondb945352007-10-15 16:15:53 -04002189
2190 if (leaf->map_token) {
2191 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2192 leaf->map_token = NULL;
2193 }
2194
Chris Masonb18c6682007-04-17 13:26:50 -04002195 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002196 if (from_end) {
2197 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2198 data_end + size_diff, btrfs_leaf_data(leaf) +
2199 data_end, old_data_start + new_size - data_end);
2200 } else {
2201 struct btrfs_disk_key disk_key;
2202 u64 offset;
2203
2204 btrfs_item_key(leaf, &disk_key, slot);
2205
2206 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2207 unsigned long ptr;
2208 struct btrfs_file_extent_item *fi;
2209
2210 fi = btrfs_item_ptr(leaf, slot,
2211 struct btrfs_file_extent_item);
2212 fi = (struct btrfs_file_extent_item *)(
2213 (unsigned long)fi - size_diff);
2214
2215 if (btrfs_file_extent_type(leaf, fi) ==
2216 BTRFS_FILE_EXTENT_INLINE) {
2217 ptr = btrfs_item_ptr_offset(leaf, slot);
2218 memmove_extent_buffer(leaf, ptr,
2219 (unsigned long)fi,
2220 offsetof(struct btrfs_file_extent_item,
2221 disk_bytenr));
2222 }
2223 }
2224
2225 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2226 data_end + size_diff, btrfs_leaf_data(leaf) +
2227 data_end, old_data_start - data_end);
2228
2229 offset = btrfs_disk_key_offset(&disk_key);
2230 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2231 btrfs_set_item_key(leaf, &disk_key, slot);
2232 if (slot == 0)
2233 fixup_low_keys(trans, root, path, &disk_key, 1);
2234 }
Chris Mason5f39d392007-10-15 16:14:19 -04002235
2236 item = btrfs_item_nr(leaf, slot);
2237 btrfs_set_item_size(leaf, item, new_size);
2238 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002239
2240 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002241 if (btrfs_leaf_free_space(root, leaf) < 0) {
2242 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002243 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002244 }
Chris Masonb18c6682007-04-17 13:26:50 -04002245 return ret;
2246}
2247
Chris Mason5f39d392007-10-15 16:14:19 -04002248int btrfs_extend_item(struct btrfs_trans_handle *trans,
2249 struct btrfs_root *root, struct btrfs_path *path,
2250 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002251{
2252 int ret = 0;
2253 int slot;
2254 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002255 struct extent_buffer *leaf;
2256 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002257 u32 nritems;
2258 unsigned int data_end;
2259 unsigned int old_data;
2260 unsigned int old_size;
2261 int i;
2262
2263 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002264 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002265
Chris Mason5f39d392007-10-15 16:14:19 -04002266 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002267 data_end = leaf_data_end(root, leaf);
2268
Chris Mason5f39d392007-10-15 16:14:19 -04002269 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2270 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002271 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002272 }
Chris Mason6567e832007-04-16 09:22:45 -04002273 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002274 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002275
2276 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002277 if (slot >= nritems) {
2278 btrfs_print_leaf(root, leaf);
2279 printk("slot %d too large, nritems %d\n", slot, nritems);
2280 BUG_ON(1);
2281 }
Chris Mason6567e832007-04-16 09:22:45 -04002282
2283 /*
2284 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2285 */
2286 /* first correct the data pointers */
2287 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002288 u32 ioff;
2289 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002290
2291 if (!leaf->map_token) {
2292 map_extent_buffer(leaf, (unsigned long)item,
2293 sizeof(struct btrfs_item),
2294 &leaf->map_token, &leaf->kaddr,
2295 &leaf->map_start, &leaf->map_len,
2296 KM_USER1);
2297 }
Chris Mason5f39d392007-10-15 16:14:19 -04002298 ioff = btrfs_item_offset(leaf, item);
2299 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002300 }
Chris Mason5f39d392007-10-15 16:14:19 -04002301
Chris Masondb945352007-10-15 16:15:53 -04002302 if (leaf->map_token) {
2303 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2304 leaf->map_token = NULL;
2305 }
2306
Chris Mason6567e832007-04-16 09:22:45 -04002307 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002308 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002309 data_end - data_size, btrfs_leaf_data(leaf) +
2310 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002311
Chris Mason6567e832007-04-16 09:22:45 -04002312 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002313 old_size = btrfs_item_size_nr(leaf, slot);
2314 item = btrfs_item_nr(leaf, slot);
2315 btrfs_set_item_size(leaf, item, old_size + data_size);
2316 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002317
2318 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002319 if (btrfs_leaf_free_space(root, leaf) < 0) {
2320 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002321 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002322 }
Chris Mason6567e832007-04-16 09:22:45 -04002323 return ret;
2324}
2325
Chris Mason74123bd2007-02-02 11:05:29 -05002326/*
2327 * Given a key and some data, insert an item into the tree.
2328 * This does all the path init required, making room in the tree if needed.
2329 */
Chris Mason5f39d392007-10-15 16:14:19 -04002330int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2331 struct btrfs_root *root,
2332 struct btrfs_path *path,
2333 struct btrfs_key *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002334{
Chris Mason5f39d392007-10-15 16:14:19 -04002335 struct extent_buffer *leaf;
2336 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002337 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002338 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002339 int slot_orig;
Chris Mason7518a232007-03-12 12:01:18 -04002340 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002341 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002342 struct btrfs_disk_key disk_key;
2343
2344 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002345
Chris Mason74123bd2007-02-02 11:05:29 -05002346 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002347 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002348 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002349
Chris Masone089f052007-03-16 16:20:31 -04002350 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002351 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002352 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002353 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002354 if (ret < 0)
2355 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002356
Chris Mason62e27492007-03-15 12:56:47 -04002357 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002358 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002359
Chris Mason5f39d392007-10-15 16:14:19 -04002360 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002361 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002362
Chris Mason123abc82007-03-14 14:14:43 -04002363 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04002364 sizeof(struct btrfs_item) + data_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002365 btrfs_print_leaf(root, leaf);
2366 printk("not enough freespace need %u have %d\n",
2367 data_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002368 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002369 }
Chris Mason5f39d392007-10-15 16:14:19 -04002370
Chris Mason62e27492007-03-15 12:56:47 -04002371 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002372 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002373
Chris Masonbe0e5c02007-01-26 15:51:26 -05002374 if (slot != nritems) {
2375 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002376 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002377
Chris Mason5f39d392007-10-15 16:14:19 -04002378 if (old_data < data_end) {
2379 btrfs_print_leaf(root, leaf);
2380 printk("slot %d old_data %d data_end %d\n",
2381 slot, old_data, data_end);
2382 BUG_ON(1);
2383 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002384 /*
2385 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2386 */
2387 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002388 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002389 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002390 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002391
Chris Mason5f39d392007-10-15 16:14:19 -04002392 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002393 if (!leaf->map_token) {
2394 map_extent_buffer(leaf, (unsigned long)item,
2395 sizeof(struct btrfs_item),
2396 &leaf->map_token, &leaf->kaddr,
2397 &leaf->map_start, &leaf->map_len,
2398 KM_USER1);
2399 }
2400
Chris Mason5f39d392007-10-15 16:14:19 -04002401 ioff = btrfs_item_offset(leaf, item);
2402 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason0783fcf2007-03-12 20:12:07 -04002403 }
Chris Masondb945352007-10-15 16:15:53 -04002404 if (leaf->map_token) {
2405 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2406 leaf->map_token = NULL;
2407 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002408
2409 /* shift the items */
Chris Mason5f39d392007-10-15 16:14:19 -04002410 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2411 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002412 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002413
2414 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002415 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002416 data_end - data_size, btrfs_leaf_data(leaf) +
2417 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002418 data_end = old_data;
2419 }
Chris Mason5f39d392007-10-15 16:14:19 -04002420
Chris Mason62e27492007-03-15 12:56:47 -04002421 /* setup the item for the new data */
Chris Mason5f39d392007-10-15 16:14:19 -04002422 btrfs_set_item_key(leaf, &disk_key, slot);
2423 item = btrfs_item_nr(leaf, slot);
2424 btrfs_set_item_offset(leaf, item, data_end - data_size);
2425 btrfs_set_item_size(leaf, item, data_size);
2426 btrfs_set_header_nritems(leaf, nritems + 1);
2427 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002428
2429 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05002430 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04002431 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002432
Chris Mason5f39d392007-10-15 16:14:19 -04002433 if (btrfs_leaf_free_space(root, leaf) < 0) {
2434 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002435 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002436 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002437out:
Chris Mason62e27492007-03-15 12:56:47 -04002438 return ret;
2439}
2440
2441/*
2442 * Given a key and some data, insert an item into the tree.
2443 * This does all the path init required, making room in the tree if needed.
2444 */
Chris Masone089f052007-03-16 16:20:31 -04002445int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2446 *root, struct btrfs_key *cpu_key, void *data, u32
2447 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002448{
2449 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002450 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002451 struct extent_buffer *leaf;
2452 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002453
Chris Mason2c90e5d2007-04-02 10:50:19 -04002454 path = btrfs_alloc_path();
2455 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002456 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002457 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002458 leaf = path->nodes[0];
2459 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2460 write_extent_buffer(leaf, data, ptr, data_size);
2461 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002462 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002463 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002464 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002465}
2466
Chris Mason74123bd2007-02-02 11:05:29 -05002467/*
Chris Mason5de08d72007-02-24 06:24:44 -05002468 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002469 *
2470 * If the delete empties a node, the node is removed from the tree,
2471 * continuing all the way the root if required. The root is converted into
2472 * a leaf if all the nodes are emptied.
2473 */
Chris Masone089f052007-03-16 16:20:31 -04002474static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2475 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002476{
Chris Mason5f39d392007-10-15 16:14:19 -04002477 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002478 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002479 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002480 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002481
Chris Mason5f39d392007-10-15 16:14:19 -04002482 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002483 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002484 memmove_extent_buffer(parent,
2485 btrfs_node_key_ptr_offset(slot),
2486 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002487 sizeof(struct btrfs_key_ptr) *
2488 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002489 }
Chris Mason7518a232007-03-12 12:01:18 -04002490 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002491 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002492 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002493 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002494 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002495 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002496 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002497 struct btrfs_disk_key disk_key;
2498
2499 btrfs_node_key(parent, &disk_key, 0);
2500 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002501 if (wret)
2502 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002503 }
Chris Masond6025572007-03-30 14:27:56 -04002504 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002505 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002506}
2507
Chris Mason74123bd2007-02-02 11:05:29 -05002508/*
2509 * delete the item at the leaf level in path. If that empties
2510 * the leaf, remove it from the tree
2511 */
Chris Masone089f052007-03-16 16:20:31 -04002512int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2513 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002514{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002516 struct extent_buffer *leaf;
2517 struct btrfs_item *item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002518 int doff;
2519 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002520 int ret = 0;
2521 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002522 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002523
Chris Mason5f39d392007-10-15 16:14:19 -04002524 leaf = path->nodes[0];
Chris Mason4920c9a2007-01-26 16:38:42 -05002525 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002526 doff = btrfs_item_offset_nr(leaf, slot);
2527 dsize = btrfs_item_size_nr(leaf, slot);
2528 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002529
Chris Mason7518a232007-03-12 12:01:18 -04002530 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002531 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002532 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002533
2534 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002535 data_end + dsize,
2536 btrfs_leaf_data(leaf) + data_end,
2537 doff - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002538
Chris Mason0783fcf2007-03-12 20:12:07 -04002539 for (i = slot + 1; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002540 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002541
Chris Mason5f39d392007-10-15 16:14:19 -04002542 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002543 if (!leaf->map_token) {
2544 map_extent_buffer(leaf, (unsigned long)item,
2545 sizeof(struct btrfs_item),
2546 &leaf->map_token, &leaf->kaddr,
2547 &leaf->map_start, &leaf->map_len,
2548 KM_USER1);
2549 }
Chris Mason5f39d392007-10-15 16:14:19 -04002550 ioff = btrfs_item_offset(leaf, item);
2551 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002552 }
Chris Masondb945352007-10-15 16:15:53 -04002553
2554 if (leaf->map_token) {
2555 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2556 leaf->map_token = NULL;
2557 }
2558
Chris Mason5f39d392007-10-15 16:14:19 -04002559 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2560 btrfs_item_nr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002561 sizeof(struct btrfs_item) *
2562 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 }
Chris Mason5f39d392007-10-15 16:14:19 -04002564 btrfs_set_header_nritems(leaf, nritems - 1);
Chris Mason7518a232007-03-12 12:01:18 -04002565 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002566
Chris Mason74123bd2007-02-02 11:05:29 -05002567 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002568 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002569 if (leaf == root->node) {
2570 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002571 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002572 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002573 clean_tree_block(trans, root, leaf);
2574 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002575 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002576 if (wret)
2577 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002578 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002579 leaf->start, leaf->len,
2580 btrfs_header_owner(path->nodes[1]),
2581 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002582 if (wret)
2583 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002584 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002585 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002586 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002587 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002588 struct btrfs_disk_key disk_key;
2589
2590 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002591 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002592 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002593 if (wret)
2594 ret = wret;
2595 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002596
Chris Mason74123bd2007-02-02 11:05:29 -05002597 /* delete the leaf if it is mostly empty */
Chris Mason7936ca32007-10-19 09:22:41 -04002598 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002599 /* push_leaf_left fixes the path.
2600 * make sure the path still points to our leaf
2601 * for possible call to del_ptr below
2602 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002603 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002604 extent_buffer_get(leaf);
2605
Chris Mason34a38212007-11-07 13:31:03 -05002606 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002607 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002608 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002609
2610 if (path->nodes[0] == leaf &&
2611 btrfs_header_nritems(leaf)) {
Chris Mason34a38212007-11-07 13:31:03 -05002612 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002613 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002614 ret = wret;
2615 }
Chris Mason5f39d392007-10-15 16:14:19 -04002616
2617 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002618 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002619 u64 bytenr = leaf->start;
2620 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002621
Chris Mason7bb86312007-12-11 09:25:06 -05002622 root_gen = btrfs_header_generation(
2623 path->nodes[1]);
2624
Chris Mason5f39d392007-10-15 16:14:19 -04002625 clean_tree_block(trans, root, leaf);
2626 wait_on_tree_block_writeback(root, leaf);
2627
Chris Masone089f052007-03-16 16:20:31 -04002628 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002629 if (wret)
2630 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002631
2632 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002633 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002634 blocksize,
2635 btrfs_header_owner(path->nodes[1]),
2636 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002637 if (wret)
2638 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002639 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002640 btrfs_mark_buffer_dirty(leaf);
2641 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002642 }
Chris Masond5719762007-03-23 10:01:08 -04002643 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002644 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002645 }
2646 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002647 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002648}
2649
Chris Mason97571fd2007-02-24 13:39:08 -05002650/*
Chris Mason7bb86312007-12-11 09:25:06 -05002651 * walk up the tree as far as required to find the previous leaf.
2652 * returns 0 if it found something or 1 if there are no lesser leaves.
2653 * returns < 0 on io errors.
2654 */
2655int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2656{
2657 int slot;
2658 int level = 1;
2659 u64 bytenr;
2660 struct extent_buffer *c;
2661 struct extent_buffer *next = NULL;
2662
2663 while(level < BTRFS_MAX_LEVEL) {
2664 if (!path->nodes[level])
2665 return 1;
2666
2667 slot = path->slots[level];
2668 c = path->nodes[level];
2669 if (slot == 0) {
2670 level++;
2671 if (level == BTRFS_MAX_LEVEL)
2672 return 1;
2673 continue;
2674 }
2675 slot--;
2676
2677 bytenr = btrfs_node_blockptr(c, slot);
2678 if (next)
2679 free_extent_buffer(next);
2680
Chris Mason7bb86312007-12-11 09:25:06 -05002681 next = read_tree_block(root, bytenr,
2682 btrfs_level_size(root, level - 1));
2683 break;
2684 }
2685 path->slots[level] = slot;
2686 while(1) {
2687 level--;
2688 c = path->nodes[level];
2689 free_extent_buffer(c);
2690 path->nodes[level] = next;
2691 path->slots[level] = 0;
2692 if (!level)
2693 break;
Chris Mason7bb86312007-12-11 09:25:06 -05002694 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2695 btrfs_level_size(root, level - 1));
2696 }
2697 return 0;
2698}
2699
2700/*
Chris Mason97571fd2007-02-24 13:39:08 -05002701 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002702 * returns 0 if it found something or 1 if there are no greater leaves.
2703 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002704 */
Chris Mason234b63a2007-03-13 10:46:10 -04002705int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002706{
2707 int slot;
2708 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002709 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002710 struct extent_buffer *c;
2711 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002712
Chris Mason234b63a2007-03-13 10:46:10 -04002713 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002714 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002715 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002716
Chris Masond97e63b2007-02-20 16:40:44 -05002717 slot = path->slots[level] + 1;
2718 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002719 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002720 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002721 if (level == BTRFS_MAX_LEVEL)
2722 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002723 continue;
2724 }
Chris Mason5f39d392007-10-15 16:14:19 -04002725
Chris Masondb945352007-10-15 16:15:53 -04002726 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002727 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002728 free_extent_buffer(next);
2729
Chris Mason6702ed42007-08-07 16:15:09 -04002730 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002731 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002732
Chris Masondb945352007-10-15 16:15:53 -04002733 next = read_tree_block(root, bytenr,
2734 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002735 break;
2736 }
2737 path->slots[level] = slot;
2738 while(1) {
2739 level--;
2740 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002741 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002742 path->nodes[level] = next;
2743 path->slots[level] = 0;
2744 if (!level)
2745 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002746 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002747 reada_for_search(root, path, level, 0, 0);
Chris Masondb945352007-10-15 16:15:53 -04002748 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2749 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002750 }
2751 return 0;
2752}