blob: f60920e8a0e061f7d4cd4eaebe51a483cceb4645 [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 Mason5f39d392007-10-15 16:14:19 -040019#include <linux/highmem.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,
29 struct btrfs_path *path, int data_size);
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 Mason5f39d392007-10-15 16:14:19 -040073static int __btrfs_cow_block(struct btrfs_trans_handle *trans,
74 struct btrfs_root *root,
75 struct extent_buffer *buf,
76 struct extent_buffer *parent, int parent_slot,
77 struct extent_buffer **cow_ret,
78 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -040079{
Chris Mason5f39d392007-10-15 16:14:19 -040080 struct extent_buffer *cow;
Chris Mason6702ed42007-08-07 16:15:09 -040081 int ret = 0;
82 int different_trans = 0;
83
84 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -040085
Chris Mason6702ed42007-08-07 16:15:09 -040086 cow = btrfs_alloc_free_block(trans, root, search_start, empty_size);
87 if (IS_ERR(cow))
88 return PTR_ERR(cow);
89
Chris Mason5f39d392007-10-15 16:14:19 -040090 if (buf->len != root->sectorsize || cow->len != root->sectorsize)
Chris Mason6702ed42007-08-07 16:15:09 -040091 WARN_ON(1);
92
Chris Mason5f39d392007-10-15 16:14:19 -040093 copy_extent_buffer(cow, buf, 0, 0, cow->len);
94 btrfs_set_header_blocknr(cow, extent_buffer_blocknr(cow));
95 btrfs_set_header_generation(cow, trans->transid);
96 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -040097
Chris Mason5f39d392007-10-15 16:14:19 -040098 WARN_ON(btrfs_header_generation(buf) > trans->transid);
99 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400100 different_trans = 1;
101 ret = btrfs_inc_ref(trans, root, buf);
102 if (ret)
103 return ret;
104 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400105 clean_tree_block(trans, root, buf);
106 }
107
108 if (buf == root->node) {
109 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400110 extent_buffer_get(cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400111 if (buf != root->commit_root) {
Chris Mason5f39d392007-10-15 16:14:19 -0400112 btrfs_free_extent(trans, root,
113 extent_buffer_blocknr(buf), 1, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400114 }
Chris Mason5f39d392007-10-15 16:14:19 -0400115 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400116 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400117 btrfs_set_node_blockptr(parent, parent_slot,
118 extent_buffer_blocknr(cow));
Chris Mason6702ed42007-08-07 16:15:09 -0400119 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400120 WARN_ON(btrfs_header_generation(parent) != trans->transid);
121 btrfs_free_extent(trans, root, extent_buffer_blocknr(buf),1,1);
Chris Mason6702ed42007-08-07 16:15:09 -0400122 }
Chris Mason5f39d392007-10-15 16:14:19 -0400123 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400124 btrfs_mark_buffer_dirty(cow);
125 *cow_ret = cow;
126 return 0;
127}
128
Chris Mason5f39d392007-10-15 16:14:19 -0400129int btrfs_cow_block(struct btrfs_trans_handle *trans,
130 struct btrfs_root *root, struct extent_buffer *buf,
131 struct extent_buffer *parent, int parent_slot,
132 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500133{
Chris Mason6702ed42007-08-07 16:15:09 -0400134 u64 search_start;
Chris Masonccd467d2007-06-28 15:57:36 -0400135 if (trans->transaction != root->fs_info->running_transaction) {
136 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
137 root->fs_info->running_transaction->transid);
138 WARN_ON(1);
139 }
140 if (trans->transid != root->fs_info->generation) {
141 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
142 root->fs_info->generation);
143 WARN_ON(1);
144 }
Chris Mason5f39d392007-10-15 16:14:19 -0400145 if (btrfs_header_generation(buf) == trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -0500146 *cow_ret = buf;
147 return 0;
148 }
Chris Mason6702ed42007-08-07 16:15:09 -0400149
Chris Mason5f39d392007-10-15 16:14:19 -0400150 search_start = extent_buffer_blocknr(buf) & ~((u64)65535);
Chris Mason6702ed42007-08-07 16:15:09 -0400151 return __btrfs_cow_block(trans, root, buf, parent,
152 parent_slot, cow_ret, search_start, 0);
153}
154
155static int close_blocks(u64 blocknr, u64 other)
156{
157 if (blocknr < other && other - blocknr < 8)
158 return 1;
159 if (blocknr > other && blocknr - other < 8)
160 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500161 return 0;
162}
163
Chris Mason5f39d392007-10-15 16:14:19 -0400164#if 0
165static int should_defrag_leaf(struct extent_buffer *eb)
Chris Mason2cc58cf2007-08-27 16:49:44 -0400166{
Chris Mason5f39d392007-10-15 16:14:19 -0400167 return 0;
168 struct btrfs_leaf *leaf = btrfs_buffer_leaf(eb);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400169 struct btrfs_disk_key *key;
170 u32 nritems;
171
172 if (buffer_defrag(bh))
173 return 1;
174
175 nritems = btrfs_header_nritems(&leaf->header);
176 if (nritems == 0)
177 return 0;
178
179 key = &leaf->items[0].key;
180 if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY)
181 return 1;
182
183 key = &leaf->items[nritems-1].key;
184 if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY)
185 return 1;
186 if (nritems > 4) {
187 key = &leaf->items[nritems/2].key;
188 if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY)
189 return 1;
190 }
191 return 0;
192}
Chris Mason5f39d392007-10-15 16:14:19 -0400193#endif
Chris Mason2cc58cf2007-08-27 16:49:44 -0400194
Chris Mason6702ed42007-08-07 16:15:09 -0400195int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400196 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masone9d0b132007-08-10 14:06:19 -0400197 int cache_only, u64 *last_ret)
Chris Mason6702ed42007-08-07 16:15:09 -0400198{
Chris Mason5f39d392007-10-15 16:14:19 -0400199 return 0;
200#if 0
Chris Mason6702ed42007-08-07 16:15:09 -0400201 struct btrfs_node *parent_node;
Chris Mason5f39d392007-10-15 16:14:19 -0400202 struct extent_buffer *cur_eb;
203 struct extent_buffer *tmp_eb;
Chris Mason6702ed42007-08-07 16:15:09 -0400204 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400205 u64 search_start = *last_ret;
206 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400207 u64 other;
208 u32 parent_nritems;
209 int start_slot;
210 int end_slot;
211 int i;
212 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400213 int parent_level;
Chris Mason6702ed42007-08-07 16:15:09 -0400214
215 if (trans->transaction != root->fs_info->running_transaction) {
216 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
217 root->fs_info->running_transaction->transid);
218 WARN_ON(1);
219 }
220 if (trans->transid != root->fs_info->generation) {
221 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
222 root->fs_info->generation);
223 WARN_ON(1);
224 }
Chris Mason86479a02007-09-10 19:58:16 -0400225 if (buffer_defrag_done(parent))
226 return 0;
227
Chris Mason6702ed42007-08-07 16:15:09 -0400228 parent_node = btrfs_buffer_node(parent);
229 parent_nritems = btrfs_header_nritems(&parent_node->header);
Chris Masonf2183bd2007-08-10 14:42:37 -0400230 parent_level = btrfs_header_level(&parent_node->header);
Chris Mason6702ed42007-08-07 16:15:09 -0400231
232 start_slot = 0;
233 end_slot = parent_nritems;
234
235 if (parent_nritems == 1)
236 return 0;
237
238 for (i = start_slot; i < end_slot; i++) {
239 int close = 1;
240 blocknr = btrfs_node_blockptr(parent_node, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400241 if (last_block == 0)
242 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400243 if (i > 0) {
244 other = btrfs_node_blockptr(parent_node, i - 1);
245 close = close_blocks(blocknr, other);
246 }
247 if (close && i < end_slot - 1) {
248 other = btrfs_node_blockptr(parent_node, i + 1);
249 close = close_blocks(blocknr, other);
250 }
Chris Masone9d0b132007-08-10 14:06:19 -0400251 if (close) {
252 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400253 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400254 }
Chris Mason6702ed42007-08-07 16:15:09 -0400255
256 cur_bh = btrfs_find_tree_block(root, blocknr);
257 if (!cur_bh || !buffer_uptodate(cur_bh) ||
Chris Mason2cc58cf2007-08-27 16:49:44 -0400258 buffer_locked(cur_bh) ||
259 (parent_level != 1 && !buffer_defrag(cur_bh)) ||
260 (parent_level == 1 && !should_defrag_leaf(cur_bh))) {
Chris Mason6702ed42007-08-07 16:15:09 -0400261 if (cache_only) {
262 brelse(cur_bh);
263 continue;
264 }
Chris Masonf2183bd2007-08-10 14:42:37 -0400265 if (!cur_bh || !buffer_uptodate(cur_bh) ||
266 buffer_locked(cur_bh)) {
267 brelse(cur_bh);
268 cur_bh = read_tree_block(root, blocknr);
269 }
Chris Mason6702ed42007-08-07 16:15:09 -0400270 }
Chris Masone9d0b132007-08-10 14:06:19 -0400271 if (search_start == 0)
272 search_start = last_block & ~((u64)65535);
273
Chris Mason6702ed42007-08-07 16:15:09 -0400274 err = __btrfs_cow_block(trans, root, cur_bh, parent, i,
275 &tmp_bh, search_start,
276 min(8, end_slot - i));
Yan252c38f2007-08-29 09:11:44 -0400277 if (err) {
278 brelse(cur_bh);
Chris Mason6702ed42007-08-07 16:15:09 -0400279 break;
Yan252c38f2007-08-29 09:11:44 -0400280 }
Chris Mason6702ed42007-08-07 16:15:09 -0400281 search_start = bh_blocknr(tmp_bh);
Chris Masonf2183bd2007-08-10 14:42:37 -0400282 *last_ret = search_start;
283 if (parent_level == 1)
284 clear_buffer_defrag(tmp_bh);
Chris Mason86479a02007-09-10 19:58:16 -0400285 set_buffer_defrag_done(tmp_bh);
Chris Mason6702ed42007-08-07 16:15:09 -0400286 brelse(tmp_bh);
287 }
288 return err;
Chris Mason5f39d392007-10-15 16:14:19 -0400289#endif
Chris Mason6702ed42007-08-07 16:15:09 -0400290}
291
Chris Mason74123bd2007-02-02 11:05:29 -0500292/*
293 * The leaf data grows from end-to-front in the node.
294 * this returns the address of the start of the last item,
295 * which is the stop of the leaf data stack
296 */
Chris Mason123abc82007-03-14 14:14:43 -0400297static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400298 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500299{
Chris Mason5f39d392007-10-15 16:14:19 -0400300 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500301 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400302 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400303 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500304}
305
Chris Mason74123bd2007-02-02 11:05:29 -0500306/*
Chris Mason74123bd2007-02-02 11:05:29 -0500307 * compare two keys in a memcmp fashion
308 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400309static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500310{
Chris Masone2fa7222007-03-12 16:22:34 -0400311 struct btrfs_key k1;
312
313 btrfs_disk_key_to_cpu(&k1, disk);
314
315 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500316 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400317 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500318 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -0400319 if (k1.type > k2->type)
Chris Masonf254e522007-03-29 15:15:27 -0400320 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400321 if (k1.type < k2->type)
Chris Masonf254e522007-03-29 15:15:27 -0400322 return -1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400323 if (k1.offset > k2->offset)
324 return 1;
325 if (k1.offset < k2->offset)
326 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500327 return 0;
328}
Chris Mason74123bd2007-02-02 11:05:29 -0500329
Chris Mason123abc82007-03-14 14:14:43 -0400330static int check_node(struct btrfs_root *root, struct btrfs_path *path,
331 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500332{
Chris Mason5f39d392007-10-15 16:14:19 -0400333 struct extent_buffer *parent = NULL;
334 struct extent_buffer *node = path->nodes[level];
335 struct btrfs_disk_key parent_key;
336 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500337 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400338 int slot;
339 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400340 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500341
342 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400343 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400344
Chris Mason8d7be552007-05-10 11:24:42 -0400345 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400346 BUG_ON(nritems == 0);
347 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400348 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400349 btrfs_node_key(parent, &parent_key, parent_slot);
350 btrfs_node_key(node, &node_key, 0);
351 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400352 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400353 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason5f39d392007-10-15 16:14:19 -0400354 btrfs_header_blocknr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500355 }
Chris Mason123abc82007-03-14 14:14:43 -0400356 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400357 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400358 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
359 btrfs_node_key(node, &node_key, slot);
360 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400361 }
362 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400363 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
364 btrfs_node_key(node, &node_key, slot);
365 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500366 }
367 return 0;
368}
369
Chris Mason123abc82007-03-14 14:14:43 -0400370static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
371 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500372{
Chris Mason5f39d392007-10-15 16:14:19 -0400373 struct extent_buffer *leaf = path->nodes[level];
374 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500375 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400376 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400377 struct btrfs_disk_key parent_key;
378 struct btrfs_disk_key leaf_key;
379 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400380
Chris Mason5f39d392007-10-15 16:14:19 -0400381 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500382
383 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400384 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400385
386 if (nritems == 0)
387 return 0;
388
389 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400390 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400391 btrfs_node_key(parent, &parent_key, parent_slot);
392 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400393
Chris Mason5f39d392007-10-15 16:14:19 -0400394 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400395 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400396 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason5f39d392007-10-15 16:14:19 -0400397 btrfs_header_blocknr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500398 }
Chris Mason5f39d392007-10-15 16:14:19 -0400399#if 0
400 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
401 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
402 btrfs_item_key(leaf, &leaf_key, i);
403 if (comp_keys(&leaf_key, &cpukey) >= 0) {
404 btrfs_print_leaf(root, leaf);
405 printk("slot %d offset bad key\n", i);
406 BUG_ON(1);
407 }
408 if (btrfs_item_offset_nr(leaf, i) !=
409 btrfs_item_end_nr(leaf, i + 1)) {
410 btrfs_print_leaf(root, leaf);
411 printk("slot %d offset bad\n", i);
412 BUG_ON(1);
413 }
414 if (i == 0) {
415 if (btrfs_item_offset_nr(leaf, i) +
416 btrfs_item_size_nr(leaf, i) !=
417 BTRFS_LEAF_DATA_SIZE(root)) {
418 btrfs_print_leaf(root, leaf);
419 printk("slot %d first offset bad\n", i);
420 BUG_ON(1);
421 }
422 }
423 }
424 if (nritems > 0) {
425 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
426 btrfs_print_leaf(root, leaf);
427 printk("slot %d bad size \n", nritems - 1);
428 BUG_ON(1);
429 }
430 }
431#endif
432 if (slot != 0 && slot < nritems - 1) {
433 btrfs_item_key(leaf, &leaf_key, slot);
434 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
435 if (comp_keys(&leaf_key, &cpukey) <= 0) {
436 btrfs_print_leaf(root, leaf);
437 printk("slot %d offset bad key\n", slot);
438 BUG_ON(1);
439 }
440 if (btrfs_item_offset_nr(leaf, slot - 1) !=
441 btrfs_item_end_nr(leaf, slot)) {
442 btrfs_print_leaf(root, leaf);
443 printk("slot %d offset bad\n", slot);
444 BUG_ON(1);
445 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500446 }
Chris Mason8d7be552007-05-10 11:24:42 -0400447 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400448 btrfs_item_key(leaf, &leaf_key, slot);
449 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
450 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
451 if (btrfs_item_offset_nr(leaf, slot) !=
452 btrfs_item_end_nr(leaf, slot + 1)) {
453 btrfs_print_leaf(root, leaf);
454 printk("slot %d offset bad\n", slot);
455 BUG_ON(1);
456 }
Chris Mason8d7be552007-05-10 11:24:42 -0400457 }
Chris Mason5f39d392007-10-15 16:14:19 -0400458 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
459 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500460 return 0;
461}
462
Chris Mason123abc82007-03-14 14:14:43 -0400463static int check_block(struct btrfs_root *root, struct btrfs_path *path,
464 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500465{
Chris Mason5f39d392007-10-15 16:14:19 -0400466 struct extent_buffer *buf = path->nodes[level];
467 char fsid[BTRFS_FSID_SIZE];
468
469 read_extent_buffer(buf, fsid, (unsigned long)btrfs_header_fsid(buf),
470 BTRFS_FSID_SIZE);
471
472 if (memcmp(fsid, root->fs_info->fsid, BTRFS_FSID_SIZE)) {
473 int i = 0;
474 printk("warning bad block %Lu\n", buf->start);
475 if (!btrfs_buffer_uptodate(buf)) {
476 WARN_ON(1);
477 }
478 for (i = 0; i < BTRFS_FSID_SIZE; i++) {
479 printk("%x:%x ", root->fs_info->fsid[i], fsid[i]);
480 }
481 printk("\n");
482 // BUG();
483 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500484 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400485 return check_leaf(root, path, level);
486 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500487}
488
Chris Mason74123bd2007-02-02 11:05:29 -0500489/*
Chris Mason5f39d392007-10-15 16:14:19 -0400490 * search for key in the extent_buffer. The items start at offset p,
491 * and they are item_size apart. There are 'max' items in p.
492 *
Chris Mason74123bd2007-02-02 11:05:29 -0500493 * the slot in the array is returned via slot, and it points to
494 * the place where you would insert key if it is not found in
495 * the array.
496 *
497 * slot may point to max if the key is bigger than all of the keys
498 */
Chris Mason5f39d392007-10-15 16:14:19 -0400499static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
500 int item_size, struct btrfs_key *key,
501 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500502{
503 int low = 0;
504 int high = max;
505 int mid;
506 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400507 struct btrfs_disk_key *tmp;
Chris Mason5f39d392007-10-15 16:14:19 -0400508 struct btrfs_disk_key unaligned;
509 unsigned long offset;
510 char *map_token = NULL;
511 char *kaddr = NULL;
512 unsigned long map_start = 0;
513 unsigned long map_len = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500514
515 while(low < high) {
516 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400517 offset = p + mid * item_size;
518
519 if (!map_token || offset < map_start ||
520 (offset + sizeof(struct btrfs_disk_key)) >
521 map_start + map_len) {
522 if (map_token)
523 unmap_extent_buffer(eb, map_token, KM_USER0);
524 map_extent_buffer(eb, offset, &map_token, &kaddr,
525 &map_start, &map_len, KM_USER0);
526
527 }
528 if (offset + sizeof(struct btrfs_disk_key) >
529 map_start + map_len) {
530 unmap_extent_buffer(eb, map_token, KM_USER0);
531 read_extent_buffer(eb, &unaligned,
532 offset, sizeof(unaligned));
533 map_token = NULL;
534 tmp = &unaligned;
535 } else {
536 tmp = (struct btrfs_disk_key *)(kaddr + offset -
537 map_start);
538 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500539 ret = comp_keys(tmp, key);
540
541 if (ret < 0)
542 low = mid + 1;
543 else if (ret > 0)
544 high = mid;
545 else {
546 *slot = mid;
Chris Mason5f39d392007-10-15 16:14:19 -0400547 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500548 return 0;
549 }
550 }
551 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400552 if (map_token)
553 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500554 return 1;
555}
556
Chris Mason97571fd2007-02-24 13:39:08 -0500557/*
558 * simple bin_search frontend that does the right thing for
559 * leaves vs nodes
560 */
Chris Mason5f39d392007-10-15 16:14:19 -0400561static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
562 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500563{
Chris Mason5f39d392007-10-15 16:14:19 -0400564 if (level == 0) {
565 return generic_bin_search(eb,
566 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400567 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400568 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400569 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500570 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400571 return generic_bin_search(eb,
572 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400573 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400574 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400575 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500576 }
577 return -1;
578}
579
Chris Mason5f39d392007-10-15 16:14:19 -0400580static struct extent_buffer *read_node_slot(struct btrfs_root *root,
581 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500582{
Chris Masonbb803952007-03-01 12:04:21 -0500583 if (slot < 0)
584 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400585 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500586 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400587 return read_tree_block(root, btrfs_node_blockptr(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500588}
589
Chris Masone089f052007-03-16 16:20:31 -0400590static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
591 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500592{
Chris Mason5f39d392007-10-15 16:14:19 -0400593 struct extent_buffer *right = NULL;
594 struct extent_buffer *mid;
595 struct extent_buffer *left = NULL;
596 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500597 int ret = 0;
598 int wret;
599 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500600 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400601 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500602 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500603
604 if (level == 0)
605 return 0;
606
Chris Mason5f39d392007-10-15 16:14:19 -0400607 mid = path->nodes[level];
Chris Mason1d4f8a02007-03-13 09:28:32 -0400608 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500609
Chris Mason234b63a2007-03-13 10:46:10 -0400610 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400611 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500612 pslot = path->slots[level + 1];
613
Chris Mason40689472007-03-17 14:29:23 -0400614 /*
615 * deal with the case where there is only one pointer in the root
616 * by promoting the node below to a root
617 */
Chris Mason5f39d392007-10-15 16:14:19 -0400618 if (!parent) {
619 struct extent_buffer *child;
620 u64 blocknr = extent_buffer_blocknr(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500621
Chris Mason5f39d392007-10-15 16:14:19 -0400622 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500623 return 0;
624
625 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400626 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500627 BUG_ON(!child);
628 root->node = child;
629 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400630 clean_tree_block(trans, root, mid);
631 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500632 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400633 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500634 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400635 free_extent_buffer(mid);
Chris Masone089f052007-03-16 16:20:31 -0400636 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500637 }
Chris Mason5f39d392007-10-15 16:14:19 -0400638 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400639 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500640 return 0;
641
Chris Mason5f39d392007-10-15 16:14:19 -0400642 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400643 err_on_enospc = 1;
644
Chris Mason5f39d392007-10-15 16:14:19 -0400645 left = read_node_slot(root, parent, pslot - 1);
646 if (left) {
647 wret = btrfs_cow_block(trans, root, left,
648 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400649 if (wret) {
650 ret = wret;
651 goto enospc;
652 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400653 }
Chris Mason5f39d392007-10-15 16:14:19 -0400654 right = read_node_slot(root, parent, pslot + 1);
655 if (right) {
656 wret = btrfs_cow_block(trans, root, right,
657 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400658 if (wret) {
659 ret = wret;
660 goto enospc;
661 }
662 }
663
664 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400665 if (left) {
666 orig_slot += btrfs_header_nritems(left);
667 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500668 if (wret < 0)
669 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400670 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400671 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500672 }
Chris Mason79f95c82007-03-01 15:16:26 -0500673
674 /*
675 * then try to empty the right most buffer into the middle
676 */
Chris Mason5f39d392007-10-15 16:14:19 -0400677 if (right) {
678 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400679 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500680 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400681 if (btrfs_header_nritems(right) == 0) {
682 u64 blocknr = extent_buffer_blocknr(right);
683 clean_tree_block(trans, root, right);
684 wait_on_tree_block_writeback(root, right);
685 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500686 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400687 wret = del_ptr(trans, root, path, level + 1, pslot +
688 1);
Chris Masonbb803952007-03-01 12:04:21 -0500689 if (wret)
690 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400691 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500692 if (wret)
693 ret = wret;
694 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400695 struct btrfs_disk_key right_key;
696 btrfs_node_key(right, &right_key, 0);
697 btrfs_set_node_key(parent, &right_key, pslot + 1);
698 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500699 }
700 }
Chris Mason5f39d392007-10-15 16:14:19 -0400701 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500702 /*
703 * we're not allowed to leave a node with one item in the
704 * tree during a delete. A deletion from lower in the tree
705 * could try to delete the only pointer in this node.
706 * So, pull some keys from the left.
707 * There has to be a left pointer at this point because
708 * otherwise we would have pulled some pointers from the
709 * right
710 */
Chris Mason5f39d392007-10-15 16:14:19 -0400711 BUG_ON(!left);
712 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400713 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500714 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400715 goto enospc;
716 }
Chris Mason79f95c82007-03-01 15:16:26 -0500717 BUG_ON(wret == 1);
718 }
Chris Mason5f39d392007-10-15 16:14:19 -0400719 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500720 /* we've managed to empty the middle node, drop it */
Chris Mason5f39d392007-10-15 16:14:19 -0400721 u64 blocknr = extent_buffer_blocknr(mid);
722 clean_tree_block(trans, root, mid);
723 wait_on_tree_block_writeback(root, mid);
724 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500725 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400726 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500727 if (wret)
728 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400729 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500730 if (wret)
731 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500732 } else {
733 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400734 struct btrfs_disk_key mid_key;
735 btrfs_node_key(mid, &mid_key, 0);
736 btrfs_set_node_key(parent, &mid_key, pslot);
737 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500738 }
Chris Masonbb803952007-03-01 12:04:21 -0500739
Chris Mason79f95c82007-03-01 15:16:26 -0500740 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400741 if (left) {
742 if (btrfs_header_nritems(left) > orig_slot) {
743 extent_buffer_get(left);
744 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500745 path->slots[level + 1] -= 1;
746 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400747 if (mid)
748 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500749 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400750 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500751 path->slots[level] = orig_slot;
752 }
753 }
Chris Mason79f95c82007-03-01 15:16:26 -0500754 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400755 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400756 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400757 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500758 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400759enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400760 if (right)
761 free_extent_buffer(right);
762 if (left)
763 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500764 return ret;
765}
766
Chris Masone66f7092007-04-20 13:16:02 -0400767/* returns zero if the push worked, non-zero otherwise */
768static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
769 struct btrfs_root *root,
770 struct btrfs_path *path, int level)
771{
Chris Mason5f39d392007-10-15 16:14:19 -0400772 struct extent_buffer *right = NULL;
773 struct extent_buffer *mid;
774 struct extent_buffer *left = NULL;
775 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400776 int ret = 0;
777 int wret;
778 int pslot;
779 int orig_slot = path->slots[level];
780 u64 orig_ptr;
781
782 if (level == 0)
783 return 1;
784
Chris Mason5f39d392007-10-15 16:14:19 -0400785 mid = path->nodes[level];
Chris Masone66f7092007-04-20 13:16:02 -0400786 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
787
788 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400789 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400790 pslot = path->slots[level + 1];
791
Chris Mason5f39d392007-10-15 16:14:19 -0400792 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400793 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400794
Chris Mason5f39d392007-10-15 16:14:19 -0400795 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400796
797 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400798 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400799 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400800 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400801 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
802 wret = 1;
803 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400804 ret = btrfs_cow_block(trans, root, left, parent,
805 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400806 if (ret)
807 wret = 1;
808 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400809 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400810 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400811 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400812 }
Chris Masone66f7092007-04-20 13:16:02 -0400813 if (wret < 0)
814 ret = wret;
815 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400816 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400817 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400818 btrfs_node_key(mid, &disk_key, 0);
819 btrfs_set_node_key(parent, &disk_key, pslot);
820 btrfs_mark_buffer_dirty(parent);
821 if (btrfs_header_nritems(left) > orig_slot) {
822 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400823 path->slots[level + 1] -= 1;
824 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400825 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400826 } else {
827 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400828 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400829 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400830 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400831 }
832 check_node(root, path, level);
833 return 0;
834 }
Chris Mason5f39d392007-10-15 16:14:19 -0400835 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400836 }
Chris Mason5f39d392007-10-15 16:14:19 -0400837 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400838
839 /*
840 * then try to empty the right most buffer into the middle
841 */
Chris Mason5f39d392007-10-15 16:14:19 -0400842 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400843 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400844 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400845 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
846 wret = 1;
847 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400848 ret = btrfs_cow_block(trans, root, right,
849 parent, pslot + 1,
850 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400851 if (ret)
852 wret = 1;
853 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400854 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400855 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400856 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400857 }
Chris Masone66f7092007-04-20 13:16:02 -0400858 if (wret < 0)
859 ret = wret;
860 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400861 struct btrfs_disk_key disk_key;
862
863 btrfs_node_key(right, &disk_key, 0);
864 btrfs_set_node_key(parent, &disk_key, pslot + 1);
865 btrfs_mark_buffer_dirty(parent);
866
867 if (btrfs_header_nritems(mid) <= orig_slot) {
868 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -0400869 path->slots[level + 1] += 1;
870 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -0400871 btrfs_header_nritems(mid);
872 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400873 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400874 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400875 }
876 check_node(root, path, level);
877 return 0;
878 }
Chris Mason5f39d392007-10-15 16:14:19 -0400879 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400880 }
881 check_node(root, path, level);
882 return 1;
883}
884
Chris Mason74123bd2007-02-02 11:05:29 -0500885/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400886 * readahead one full node of leaves
887 */
888static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason6702ed42007-08-07 16:15:09 -0400889 int level, int slot)
Chris Mason3c69fae2007-08-07 15:52:22 -0400890{
Chris Mason5f39d392007-10-15 16:14:19 -0400891 struct extent_buffer *node;
Chris Mason3c69fae2007-08-07 15:52:22 -0400892 int i;
893 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -0400894 u64 blocknr;
895 u64 search;
896 u64 cluster_start;
897 int ret;
898 int nread = 0;
899 int direction = path->reada;
900 struct radix_tree_root found;
901 unsigned long gang[8];
Chris Mason5f39d392007-10-15 16:14:19 -0400902 struct extent_buffer *eb;
Chris Mason3c69fae2007-08-07 15:52:22 -0400903
Chris Mason6702ed42007-08-07 16:15:09 -0400904 if (level == 0)
Chris Mason3c69fae2007-08-07 15:52:22 -0400905 return;
906
Chris Mason6702ed42007-08-07 16:15:09 -0400907 if (!path->nodes[level])
908 return;
909
Chris Mason5f39d392007-10-15 16:14:19 -0400910 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -0400911 search = btrfs_node_blockptr(node, slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400912 eb = btrfs_find_tree_block(root, search);
913 if (eb) {
914 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -0400915 return;
916 }
917
918 init_bit_radix(&found);
Chris Mason5f39d392007-10-15 16:14:19 -0400919 nritems = btrfs_header_nritems(node);
Chris Mason3c69fae2007-08-07 15:52:22 -0400920 for (i = slot; i < nritems; i++) {
Chris Mason3c69fae2007-08-07 15:52:22 -0400921 blocknr = btrfs_node_blockptr(node, i);
922 set_radix_bit(&found, blocknr);
923 }
924 if (direction > 0) {
925 cluster_start = search - 4;
926 if (cluster_start > search)
927 cluster_start = 0;
928 } else
929 cluster_start = search + 4;
930 while(1) {
931 ret = find_first_radix_bit(&found, gang, 0, ARRAY_SIZE(gang));
932 if (!ret)
933 break;
934 for (i = 0; i < ret; i++) {
935 blocknr = gang[i];
936 clear_radix_bit(&found, blocknr);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400937 if (path->reada == 1 && nread > 16)
Chris Mason3c69fae2007-08-07 15:52:22 -0400938 continue;
Chris Masonf2183bd2007-08-10 14:42:37 -0400939 if (close_blocks(cluster_start, blocknr)) {
Chris Mason3c69fae2007-08-07 15:52:22 -0400940 readahead_tree_block(root, blocknr);
941 nread++;
Chris Mason3c69fae2007-08-07 15:52:22 -0400942 cluster_start = blocknr;
Chris Mason3c69fae2007-08-07 15:52:22 -0400943 }
944 }
945 }
946}
947/*
Chris Mason74123bd2007-02-02 11:05:29 -0500948 * look for key in the tree. path is filled in with nodes along the way
949 * if key is found, we return zero and you can find the item in the leaf
950 * level of the path (level 0)
951 *
952 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500953 * be inserted, and 1 is returned. If there are other errors during the
954 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500955 *
956 * if ins_len > 0, nodes and leaves will be split as we walk down the
957 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
958 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500959 */
Chris Masone089f052007-03-16 16:20:31 -0400960int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
961 *root, struct btrfs_key *key, struct btrfs_path *p, int
962 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500963{
Chris Mason5f39d392007-10-15 16:14:19 -0400964 struct extent_buffer *b;
Chris Mason3c69fae2007-08-07 15:52:22 -0400965 u64 blocknr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500966 int slot;
967 int ret;
968 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -0400969 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -0400970 u8 lowest_level = 0;
971
Chris Mason6702ed42007-08-07 16:15:09 -0400972 lowest_level = p->lowest_level;
973 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400974 WARN_ON(p->nodes[0] != NULL);
975 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500976again:
977 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -0400978 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500979 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -0400980 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500981 if (cow) {
982 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400983 wret = btrfs_cow_block(trans, root, b,
984 p->nodes[level + 1],
985 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -0400986 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400988 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -0400989 return wret;
990 }
Chris Mason02217ed2007-03-02 16:08:05 -0500991 }
992 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400993 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400994 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -0400995 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500996 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400997 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500998 if (ret)
999 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001000 ret = bin_search(b, key, level, &slot);
1001 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001002 if (ret && slot > 0)
1003 slot -= 1;
1004 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001005 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001006 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001007 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001008 BUG_ON(sret > 0);
1009 if (sret)
1010 return sret;
1011 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001012 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001013 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001014 int sret = balance_level(trans, root, p,
1015 level);
Chris Masonbb803952007-03-01 12:04:21 -05001016 if (sret)
1017 return sret;
1018 b = p->nodes[level];
1019 if (!b)
1020 goto again;
Chris Masonbb803952007-03-01 12:04:21 -05001021 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001022 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001023 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001024 /* this is only true while dropping a snapshot */
1025 if (level == lowest_level)
1026 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001027 blocknr = btrfs_node_blockptr(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001028 if (should_reada)
1029 reada_for_search(root, p, level, slot);
Chris Mason5f39d392007-10-15 16:14:19 -04001030 b = read_tree_block(root, btrfs_node_blockptr(b, slot));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001031 } else {
1032 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001033 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001034 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001035 int sret = split_leaf(trans, root, key,
1036 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -05001037 BUG_ON(sret > 0);
1038 if (sret)
1039 return sret;
1040 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001041 return ret;
1042 }
1043 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001044 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001045}
1046
Chris Mason74123bd2007-02-02 11:05:29 -05001047/*
1048 * adjust the pointers going up the tree, starting at level
1049 * making sure the right key of each node is points to 'key'.
1050 * This is used after shifting pointers to the left, so it stops
1051 * fixing up pointers when a given leaf/node is not in slot 0 of the
1052 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001053 *
1054 * If this fails to write a tree block, it returns -1, but continues
1055 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001056 */
Chris Mason5f39d392007-10-15 16:14:19 -04001057static int fixup_low_keys(struct btrfs_trans_handle *trans,
1058 struct btrfs_root *root, struct btrfs_path *path,
1059 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001060{
1061 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001062 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001063 struct extent_buffer *t;
1064
Chris Mason234b63a2007-03-13 10:46:10 -04001065 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001066 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001067 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001068 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001069 t = path->nodes[i];
1070 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001071 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001072 if (tslot != 0)
1073 break;
1074 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001075 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001076}
1077
Chris Mason74123bd2007-02-02 11:05:29 -05001078/*
1079 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001080 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001081 *
1082 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1083 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001084 */
Chris Masone089f052007-03-16 16:20:31 -04001085static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason5f39d392007-10-15 16:14:19 -04001086 *root, struct extent_buffer *dst,
1087 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001088{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001089 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001090 int src_nritems;
1091 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001092 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001093
Chris Mason5f39d392007-10-15 16:14:19 -04001094 src_nritems = btrfs_header_nritems(src);
1095 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001096 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001097
Chris Masoneb60cea2007-02-02 09:18:22 -05001098 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001099 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001100 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001101
1102 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001103 push_items = src_nritems;
1104
Chris Mason5f39d392007-10-15 16:14:19 -04001105 copy_extent_buffer(dst, src,
1106 btrfs_node_key_ptr_offset(dst_nritems),
1107 btrfs_node_key_ptr_offset(0),
1108 push_items * sizeof(struct btrfs_key_ptr));
1109
Chris Masonbb803952007-03-01 12:04:21 -05001110 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001111 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1112 btrfs_node_key_ptr_offset(push_items),
1113 (src_nritems - push_items) *
1114 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001115 }
Chris Mason5f39d392007-10-15 16:14:19 -04001116 btrfs_set_header_nritems(src, src_nritems - push_items);
1117 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1118 btrfs_mark_buffer_dirty(src);
1119 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001120 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001121}
1122
Chris Mason97571fd2007-02-24 13:39:08 -05001123/*
Chris Mason79f95c82007-03-01 15:16:26 -05001124 * try to push data from one node into the next node right in the
1125 * tree.
1126 *
1127 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1128 * error, and > 0 if there was no room in the right hand block.
1129 *
1130 * this will only push up to 1/2 the contents of the left node over
1131 */
Chris Mason5f39d392007-10-15 16:14:19 -04001132static int balance_node_right(struct btrfs_trans_handle *trans,
1133 struct btrfs_root *root,
1134 struct extent_buffer *dst,
1135 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001136{
Chris Mason79f95c82007-03-01 15:16:26 -05001137 int push_items = 0;
1138 int max_push;
1139 int src_nritems;
1140 int dst_nritems;
1141 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001142
Chris Mason5f39d392007-10-15 16:14:19 -04001143 src_nritems = btrfs_header_nritems(src);
1144 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001145 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001146 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001147 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001148
1149 max_push = src_nritems / 2 + 1;
1150 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001151 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001152 return 1;
Yan252c38f2007-08-29 09:11:44 -04001153
Chris Mason79f95c82007-03-01 15:16:26 -05001154 if (max_push < push_items)
1155 push_items = max_push;
1156
Chris Mason5f39d392007-10-15 16:14:19 -04001157 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1158 btrfs_node_key_ptr_offset(0),
1159 (dst_nritems) *
1160 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001161
Chris Mason5f39d392007-10-15 16:14:19 -04001162 copy_extent_buffer(dst, src,
1163 btrfs_node_key_ptr_offset(0),
1164 btrfs_node_key_ptr_offset(src_nritems - push_items),
1165 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001166
Chris Mason5f39d392007-10-15 16:14:19 -04001167 btrfs_set_header_nritems(src, src_nritems - push_items);
1168 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001169
Chris Mason5f39d392007-10-15 16:14:19 -04001170 btrfs_mark_buffer_dirty(src);
1171 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001172 return ret;
1173}
1174
1175/*
Chris Mason97571fd2007-02-24 13:39:08 -05001176 * helper function to insert a new root level in the tree.
1177 * A new node is allocated, and a single item is inserted to
1178 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001179 *
1180 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001181 */
Chris Mason5f39d392007-10-15 16:14:19 -04001182static int insert_new_root(struct btrfs_trans_handle *trans,
1183 struct btrfs_root *root,
1184 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001185{
Chris Mason5f39d392007-10-15 16:14:19 -04001186 struct extent_buffer *lower;
1187 struct extent_buffer *c;
1188 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001189
1190 BUG_ON(path->nodes[level]);
1191 BUG_ON(path->nodes[level-1] != root->node);
1192
Chris Mason5f39d392007-10-15 16:14:19 -04001193 c = btrfs_alloc_free_block(trans, root,
1194 extent_buffer_blocknr(root->node), 0);
1195 if (IS_ERR(c))
1196 return PTR_ERR(c);
1197 memset_extent_buffer(c, 0, 0, root->nodesize);
1198 btrfs_set_header_nritems(c, 1);
1199 btrfs_set_header_level(c, level);
1200 btrfs_set_header_blocknr(c, extent_buffer_blocknr(c));
1201 btrfs_set_header_generation(c, trans->transid);
1202 btrfs_set_header_owner(c, root->root_key.objectid);
1203 lower = path->nodes[level-1];
Chris Masond5719762007-03-23 10:01:08 -04001204
Chris Mason5f39d392007-10-15 16:14:19 -04001205 write_extent_buffer(c, root->fs_info->fsid,
1206 (unsigned long)btrfs_header_fsid(c),
1207 BTRFS_FSID_SIZE);
1208 if (level == 1)
1209 btrfs_item_key(lower, &lower_key, 0);
1210 else
1211 btrfs_node_key(lower, &lower_key, 0);
1212 btrfs_set_node_key(c, &lower_key, 0);
1213 btrfs_set_node_blockptr(c, 0, extent_buffer_blocknr(lower));
1214
1215 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001216
Chris Mason5c680ed2007-02-22 11:39:13 -05001217 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001218 free_extent_buffer(root->node);
1219 root->node = c;
1220 extent_buffer_get(c);
1221 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001222 path->slots[level] = 0;
1223 return 0;
1224}
1225
Chris Mason74123bd2007-02-02 11:05:29 -05001226/*
1227 * worker function to insert a single pointer in a node.
1228 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001229 *
Chris Mason74123bd2007-02-02 11:05:29 -05001230 * slot and level indicate where you want the key to go, and
1231 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001232 *
1233 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001234 */
Chris Masone089f052007-03-16 16:20:31 -04001235static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1236 *root, struct btrfs_path *path, struct btrfs_disk_key
1237 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001238{
Chris Mason5f39d392007-10-15 16:14:19 -04001239 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001240 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001241
1242 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001243 lower = path->nodes[level];
1244 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001245 if (slot > nritems)
1246 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001247 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001248 BUG();
1249 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001250 memmove_extent_buffer(lower,
1251 btrfs_node_key_ptr_offset(slot + 1),
1252 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001253 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001254 }
Chris Mason5f39d392007-10-15 16:14:19 -04001255 btrfs_set_node_key(lower, key, slot);
Chris Mason1d4f8a02007-03-13 09:28:32 -04001256 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason5f39d392007-10-15 16:14:19 -04001257 btrfs_set_header_nritems(lower, nritems + 1);
1258 btrfs_mark_buffer_dirty(lower);
Chris Mason098f59c2007-05-11 11:33:21 -04001259 check_node(root, path, level);
Chris Mason74123bd2007-02-02 11:05:29 -05001260 return 0;
1261}
1262
Chris Mason97571fd2007-02-24 13:39:08 -05001263/*
1264 * split the node at the specified level in path in two.
1265 * The path is corrected to point to the appropriate node after the split
1266 *
1267 * Before splitting this tries to make some room in the node by pushing
1268 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001269 *
1270 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001271 */
Chris Masone089f052007-03-16 16:20:31 -04001272static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1273 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001274{
Chris Mason5f39d392007-10-15 16:14:19 -04001275 struct extent_buffer *c;
1276 struct extent_buffer *split;
1277 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001278 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001279 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001280 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001281 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001282
Chris Mason5f39d392007-10-15 16:14:19 -04001283 c = path->nodes[level];
1284 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001285 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001286 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001287 if (ret)
1288 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001289 } else {
1290 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001291 c = path->nodes[level];
1292 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001293 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1294 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001295 if (ret < 0)
1296 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001297 }
Chris Masone66f7092007-04-20 13:16:02 -04001298
Chris Mason5f39d392007-10-15 16:14:19 -04001299 c_nritems = btrfs_header_nritems(c);
1300 split = btrfs_alloc_free_block(trans, root,
1301 extent_buffer_blocknr(c), 0);
1302 if (IS_ERR(split))
1303 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001304
Chris Mason5f39d392007-10-15 16:14:19 -04001305 btrfs_set_header_flags(split, btrfs_header_flags(c));
1306 btrfs_set_header_level(split, btrfs_header_level(c));
1307 btrfs_set_header_blocknr(split, extent_buffer_blocknr(split));
1308 btrfs_set_header_generation(split, trans->transid);
1309 btrfs_set_header_owner(split, root->root_key.objectid);
1310 write_extent_buffer(split, root->fs_info->fsid,
1311 (unsigned long)btrfs_header_fsid(split),
1312 BTRFS_FSID_SIZE);
1313
Chris Mason7518a232007-03-12 12:01:18 -04001314 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001315
1316 copy_extent_buffer(split, c,
1317 btrfs_node_key_ptr_offset(0),
1318 btrfs_node_key_ptr_offset(mid),
1319 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1320 btrfs_set_header_nritems(split, c_nritems - mid);
1321 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001322 ret = 0;
1323
Chris Mason5f39d392007-10-15 16:14:19 -04001324 btrfs_mark_buffer_dirty(c);
1325 btrfs_mark_buffer_dirty(split);
1326
1327 btrfs_node_key(split, &disk_key, 0);
1328 wret = insert_ptr(trans, root, path, &disk_key,
1329 extent_buffer_blocknr(split),
1330 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001331 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001332 if (wret)
1333 ret = wret;
1334
Chris Mason5de08d72007-02-24 06:24:44 -05001335 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001336 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001337 free_extent_buffer(c);
1338 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001339 path->slots[level + 1] += 1;
1340 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001341 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001342 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001343 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001344}
1345
Chris Mason74123bd2007-02-02 11:05:29 -05001346/*
1347 * how many bytes are required to store the items in a leaf. start
1348 * and nr indicate which items in the leaf to check. This totals up the
1349 * space used both by the item structs and the item data
1350 */
Chris Mason5f39d392007-10-15 16:14:19 -04001351static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001352{
1353 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001354 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001355 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001356
1357 if (!nr)
1358 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001359 data_len = btrfs_item_end_nr(l, start);
1360 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001361 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001362 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001363 return data_len;
1364}
1365
Chris Mason74123bd2007-02-02 11:05:29 -05001366/*
Chris Masond4dbff92007-04-04 14:08:15 -04001367 * The space between the end of the leaf items and
1368 * the start of the leaf data. IOW, how much room
1369 * the leaf has left for both items and data
1370 */
Chris Mason5f39d392007-10-15 16:14:19 -04001371int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001372{
Chris Mason5f39d392007-10-15 16:14:19 -04001373 int nritems = btrfs_header_nritems(leaf);
1374 int ret;
1375 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1376 if (ret < 0) {
1377 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1378 ret, BTRFS_LEAF_DATA_SIZE(root),
1379 leaf_space_used(leaf, 0, nritems), nritems);
1380 }
1381 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001382}
1383
1384/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001385 * push some data in the path leaf to the right, trying to free up at
1386 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001387 *
1388 * returns 1 if the push failed because the other node didn't have enough
1389 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001390 */
Chris Masone089f052007-03-16 16:20:31 -04001391static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1392 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -05001393{
Chris Mason5f39d392007-10-15 16:14:19 -04001394 struct extent_buffer *left = path->nodes[0];
1395 struct extent_buffer *right;
1396 struct extent_buffer *upper;
1397 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001398 int slot;
1399 int i;
1400 int free_space;
1401 int push_space = 0;
1402 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001403 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001404 u32 left_nritems;
1405 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001406 u32 data_end;
Chris Mason54aa1f42007-06-22 14:16:25 -04001407 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001408
1409 slot = path->slots[1];
1410 if (!path->nodes[1]) {
1411 return 1;
1412 }
1413 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001414 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001415 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001416
1417 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1));
Chris Mason123abc82007-03-14 14:14:43 -04001418 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001419 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001420 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001421 return 1;
1422 }
1423
Chris Mason5f39d392007-10-15 16:14:19 -04001424 /* cow and double check */
1425 ret = btrfs_cow_block(trans, root, right, upper,
1426 slot + 1, &right);
1427 if (ret) {
1428 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001429 return 1;
1430 }
Chris Mason5f39d392007-10-15 16:14:19 -04001431 free_space = btrfs_leaf_free_space(root, right);
1432 if (free_space < data_size + sizeof(struct btrfs_item)) {
1433 free_extent_buffer(right);
1434 return 1;
1435 }
1436
1437 left_nritems = btrfs_header_nritems(left);
1438 if (left_nritems == 0) {
1439 free_extent_buffer(right);
1440 return 1;
1441 }
1442
Chris Masona429e512007-04-18 16:15:28 -04001443 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason5f39d392007-10-15 16:14:19 -04001444 item = btrfs_item_nr(left, i);
Chris Mason00ec4c52007-02-24 12:47:20 -05001445 if (path->slots[0] == i)
1446 push_space += data_size + sizeof(*item);
Chris Mason5f39d392007-10-15 16:14:19 -04001447 if (btrfs_item_size(left, item) + sizeof(*item) + push_space >
Chris Mason0783fcf2007-03-12 20:12:07 -04001448 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001449 break;
1450 push_items++;
Chris Mason5f39d392007-10-15 16:14:19 -04001451 push_space += btrfs_item_size(left, item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -05001452 }
Chris Mason5f39d392007-10-15 16:14:19 -04001453
Chris Mason00ec4c52007-02-24 12:47:20 -05001454 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001455 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001456 return 1;
1457 }
Chris Mason5f39d392007-10-15 16:14:19 -04001458
Chris Masona429e512007-04-18 16:15:28 -04001459 if (push_items == left_nritems)
1460 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001461
Chris Mason00ec4c52007-02-24 12:47:20 -05001462 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001463 right_nritems = btrfs_header_nritems(right);
1464 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001465 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001466
Chris Mason00ec4c52007-02-24 12:47:20 -05001467 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001468 data_end = leaf_data_end(root, right);
1469 memmove_extent_buffer(right,
1470 btrfs_leaf_data(right) + data_end - push_space,
1471 btrfs_leaf_data(right) + data_end,
1472 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1473
Chris Mason00ec4c52007-02-24 12:47:20 -05001474 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001475 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001476 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1477 btrfs_leaf_data(left) + leaf_data_end(root, left),
1478 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001479
1480 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1481 btrfs_item_nr_offset(0),
1482 right_nritems * sizeof(struct btrfs_item));
1483
Chris Mason00ec4c52007-02-24 12:47:20 -05001484 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001485 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1486 btrfs_item_nr_offset(left_nritems - push_items),
1487 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001488
1489 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001490 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001491 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001492 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001493 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001494 item = btrfs_item_nr(right, i);
1495 btrfs_set_item_offset(right, item, push_space -
1496 btrfs_item_size(right, item));
1497 push_space = btrfs_item_offset(right, item);
Chris Mason00ec4c52007-02-24 12:47:20 -05001498 }
Chris Mason7518a232007-03-12 12:01:18 -04001499 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001500 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001501
Chris Mason5f39d392007-10-15 16:14:19 -04001502 btrfs_mark_buffer_dirty(left);
1503 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001504
Chris Mason5f39d392007-10-15 16:14:19 -04001505 btrfs_item_key(right, &disk_key, 0);
1506 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001507 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001508
Chris Mason00ec4c52007-02-24 12:47:20 -05001509 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001510 if (path->slots[0] >= left_nritems) {
1511 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001512 free_extent_buffer(path->nodes[0]);
1513 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001514 path->slots[1] += 1;
1515 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001516 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001517 }
Chris Mason098f59c2007-05-11 11:33:21 -04001518 if (path->nodes[1])
1519 check_node(root, path, 1);
Chris Mason00ec4c52007-02-24 12:47:20 -05001520 return 0;
1521}
1522/*
Chris Mason74123bd2007-02-02 11:05:29 -05001523 * push some data in the path leaf to the left, trying to free up at
1524 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1525 */
Chris Masone089f052007-03-16 16:20:31 -04001526static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1527 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001528{
Chris Mason5f39d392007-10-15 16:14:19 -04001529 struct btrfs_disk_key disk_key;
1530 struct extent_buffer *right = path->nodes[0];
1531 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001532 int slot;
1533 int i;
1534 int free_space;
1535 int push_space = 0;
1536 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001537 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001538 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001539 u32 right_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001540 int ret = 0;
1541 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001542
1543 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001544 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001545 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001546 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001547 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001548
1549 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
1550 slot - 1));
Chris Mason123abc82007-03-14 14:14:43 -04001551 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001552 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001553 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001554 return 1;
1555 }
Chris Mason02217ed2007-03-02 16:08:05 -05001556
1557 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001558 ret = btrfs_cow_block(trans, root, left,
1559 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001560 if (ret) {
1561 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001562 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001563 return 1;
1564 }
Chris Mason123abc82007-03-14 14:14:43 -04001565 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001566 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001567 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001568 return 1;
1569 }
1570
Chris Mason5f39d392007-10-15 16:14:19 -04001571 right_nritems = btrfs_header_nritems(right);
1572 if (right_nritems == 0) {
1573 free_extent_buffer(left);
Chris Masona429e512007-04-18 16:15:28 -04001574 return 1;
1575 }
1576
Chris Mason5f39d392007-10-15 16:14:19 -04001577 for (i = 0; i < right_nritems - 1; i++) {
1578 item = btrfs_item_nr(right, i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001579 if (path->slots[0] == i)
1580 push_space += data_size + sizeof(*item);
Chris Mason5f39d392007-10-15 16:14:19 -04001581 if (btrfs_item_size(right, item) + sizeof(*item) + push_space >
Chris Mason0783fcf2007-03-12 20:12:07 -04001582 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001583 break;
1584 push_items++;
Chris Mason5f39d392007-10-15 16:14:19 -04001585 push_space += btrfs_item_size(right, item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001586 }
1587 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001588 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001589 return 1;
1590 }
Chris Mason5f39d392007-10-15 16:14:19 -04001591 if (push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001592 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001593
Chris Masonbe0e5c02007-01-26 15:51:26 -05001594 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001595 copy_extent_buffer(left, right,
1596 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1597 btrfs_item_nr_offset(0),
1598 push_items * sizeof(struct btrfs_item));
1599
Chris Mason123abc82007-03-14 14:14:43 -04001600 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001601 btrfs_item_offset_nr(right, push_items -1);
1602
1603 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001604 leaf_data_end(root, left) - push_space,
1605 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001606 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001607 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001608 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001609 BUG_ON(old_left_nritems < 0);
1610
Chris Mason0783fcf2007-03-12 20:12:07 -04001611 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001612 u32 ioff;
1613 item = btrfs_item_nr(left, i);
1614 ioff = btrfs_item_offset(left, item);
1615 btrfs_set_item_offset(left, item,
1616 ioff - (BTRFS_LEAF_DATA_SIZE(root) -
1617 btrfs_item_offset_nr(left, old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001618 }
Chris Mason5f39d392007-10-15 16:14:19 -04001619 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001620
1621 /* fixup right node */
Chris Mason5f39d392007-10-15 16:14:19 -04001622 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1623 leaf_data_end(root, right);
1624 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1625 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1626 btrfs_leaf_data(right) +
1627 leaf_data_end(root, right), push_space);
1628
1629 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1630 btrfs_item_nr_offset(push_items),
1631 (btrfs_header_nritems(right) - push_items) *
1632 sizeof(struct btrfs_item));
1633
1634 right_nritems = btrfs_header_nritems(right) - push_items;
1635 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001636 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001637
Chris Mason5f39d392007-10-15 16:14:19 -04001638 for (i = 0; i < right_nritems; i++) {
1639 item = btrfs_item_nr(right, i);
1640 btrfs_set_item_offset(right, item, push_space -
1641 btrfs_item_size(right, item));
1642 push_space = btrfs_item_offset(right, item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001643 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001644
Chris Mason5f39d392007-10-15 16:14:19 -04001645 btrfs_mark_buffer_dirty(left);
1646 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001647
Chris Mason5f39d392007-10-15 16:14:19 -04001648 btrfs_item_key(right, &disk_key, 0);
1649 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001650 if (wret)
1651 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001652
1653 /* then fixup the leaf pointer in the path */
1654 if (path->slots[0] < push_items) {
1655 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001656 free_extent_buffer(path->nodes[0]);
1657 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001658 path->slots[1] -= 1;
1659 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001660 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001661 path->slots[0] -= push_items;
1662 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001663 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001664 if (path->nodes[1])
1665 check_node(root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001666 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001667}
1668
Chris Mason74123bd2007-02-02 11:05:29 -05001669/*
1670 * split the path's leaf in two, making sure there is at least data_size
1671 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001672 *
1673 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001674 */
Chris Masone089f052007-03-16 16:20:31 -04001675static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001676 *root, struct btrfs_key *ins_key,
1677 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001678{
Chris Mason5f39d392007-10-15 16:14:19 -04001679 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001680 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001681 int mid;
1682 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001683 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001684 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001685 int data_copy_size;
1686 int rt_data_off;
1687 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001688 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001689 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001690 int double_split = 0;
1691 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001692
Chris Mason40689472007-03-17 14:29:23 -04001693 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001694 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001695 if (wret < 0)
1696 return wret;
1697 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001698 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001699 if (wret < 0)
1700 return wret;
1701 }
Chris Mason5f39d392007-10-15 16:14:19 -04001702 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05001703
1704 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001705 if (btrfs_leaf_free_space(root, l) >=
1706 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001707 return 0;
1708
Chris Mason5c680ed2007-02-22 11:39:13 -05001709 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001710 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001711 if (ret)
1712 return ret;
1713 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001714 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001715 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001716 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001717
Chris Mason5f39d392007-10-15 16:14:19 -04001718 right = btrfs_alloc_free_block(trans, root,
1719 extent_buffer_blocknr(l), 0);
1720 if (IS_ERR(right))
1721 return PTR_ERR(right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001722
Chris Mason5f39d392007-10-15 16:14:19 -04001723 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
1724 btrfs_set_header_blocknr(right, extent_buffer_blocknr(right));
1725 btrfs_set_header_generation(right, trans->transid);
1726 btrfs_set_header_owner(right, root->root_key.objectid);
1727 btrfs_set_header_level(right, 0);
1728 write_extent_buffer(right, root->fs_info->fsid,
1729 (unsigned long)btrfs_header_fsid(right),
1730 BTRFS_FSID_SIZE);
1731
Chris Masond4dbff92007-04-04 14:08:15 -04001732 if (mid <= slot) {
1733 if (nritems == 1 ||
1734 leaf_space_used(l, mid, nritems - mid) + space_needed >
1735 BTRFS_LEAF_DATA_SIZE(root)) {
1736 if (slot >= nritems) {
1737 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001738 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001739 wret = insert_ptr(trans, root, path,
1740 &disk_key,
Chris Mason5f39d392007-10-15 16:14:19 -04001741 extent_buffer_blocknr(right),
Chris Masond4dbff92007-04-04 14:08:15 -04001742 path->slots[1] + 1, 1);
1743 if (wret)
1744 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001745 free_extent_buffer(path->nodes[0]);
1746 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001747 path->slots[0] = 0;
1748 path->slots[1] += 1;
1749 return ret;
1750 }
1751 mid = slot;
1752 double_split = 1;
1753 }
1754 } else {
1755 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1756 BTRFS_LEAF_DATA_SIZE(root)) {
1757 if (slot == 0) {
1758 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001759 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001760 wret = insert_ptr(trans, root, path,
1761 &disk_key,
Chris Mason5f39d392007-10-15 16:14:19 -04001762 extent_buffer_blocknr(right),
Chris Mason098f59c2007-05-11 11:33:21 -04001763 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001764 if (wret)
1765 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001766 free_extent_buffer(path->nodes[0]);
1767 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001768 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04001769 if (path->slots[1] == 0) {
1770 wret = fixup_low_keys(trans, root,
1771 path, &disk_key, 1);
1772 if (wret)
1773 ret = wret;
1774 }
Chris Masond4dbff92007-04-04 14:08:15 -04001775 return ret;
1776 }
1777 mid = slot;
1778 double_split = 1;
1779 }
1780 }
Chris Mason5f39d392007-10-15 16:14:19 -04001781 nritems = nritems - mid;
1782 btrfs_set_header_nritems(right, nritems);
1783 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
1784
1785 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1786 btrfs_item_nr_offset(mid),
1787 nritems * sizeof(struct btrfs_item));
1788
1789 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04001790 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1791 data_copy_size, btrfs_leaf_data(l) +
1792 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05001793
Chris Mason5f39d392007-10-15 16:14:19 -04001794 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1795 btrfs_item_end_nr(l, mid);
1796
1797 for (i = 0; i < nritems; i++) {
1798 struct btrfs_item *item = btrfs_item_nr(right, i);
1799 u32 ioff = btrfs_item_offset(right, item);
1800 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04001801 }
Chris Mason74123bd2007-02-02 11:05:29 -05001802
Chris Mason5f39d392007-10-15 16:14:19 -04001803 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001804 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001805 btrfs_item_key(right, &disk_key, 0);
1806 wret = insert_ptr(trans, root, path, &disk_key,
1807 extent_buffer_blocknr(right), path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001808 if (wret)
1809 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001810
1811 btrfs_mark_buffer_dirty(right);
1812 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001813 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04001814
Chris Masonbe0e5c02007-01-26 15:51:26 -05001815 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04001816 free_extent_buffer(path->nodes[0]);
1817 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818 path->slots[0] -= mid;
1819 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001820 } else
Chris Mason5f39d392007-10-15 16:14:19 -04001821 free_extent_buffer(right);
1822
Chris Masoneb60cea2007-02-02 09:18:22 -05001823 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001824 check_node(root, path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001825 check_leaf(root, path, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001826
1827 if (!double_split)
1828 return ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001829
Chris Mason5f39d392007-10-15 16:14:19 -04001830 right = btrfs_alloc_free_block(trans, root,
1831 extent_buffer_blocknr(l), 0);
1832 if (IS_ERR(right))
1833 return PTR_ERR(right);
1834
1835 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
1836 btrfs_set_header_blocknr(right, extent_buffer_blocknr(right));
1837 btrfs_set_header_generation(right, trans->transid);
1838 btrfs_set_header_owner(right, root->root_key.objectid);
1839 btrfs_set_header_level(right, 0);
1840 write_extent_buffer(right, root->fs_info->fsid,
1841 (unsigned long)btrfs_header_fsid(right),
1842 BTRFS_FSID_SIZE);
1843
Chris Masond4dbff92007-04-04 14:08:15 -04001844 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001845 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001846 wret = insert_ptr(trans, root, path,
1847 &disk_key,
Chris Mason5f39d392007-10-15 16:14:19 -04001848 extent_buffer_blocknr(right),
Chris Masond4dbff92007-04-04 14:08:15 -04001849 path->slots[1], 1);
1850 if (wret)
1851 ret = wret;
Chris Masona429e512007-04-18 16:15:28 -04001852 if (path->slots[1] == 0) {
1853 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1854 if (wret)
1855 ret = wret;
1856 }
Chris Mason5f39d392007-10-15 16:14:19 -04001857 free_extent_buffer(path->nodes[0]);
1858 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001859 path->slots[0] = 0;
1860 check_node(root, path, 1);
1861 check_leaf(root, path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001862 return ret;
1863}
1864
Chris Masonb18c6682007-04-17 13:26:50 -04001865int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1866 struct btrfs_root *root,
1867 struct btrfs_path *path,
1868 u32 new_size)
1869{
1870 int ret = 0;
1871 int slot;
1872 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04001873 struct extent_buffer *leaf;
1874 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04001875 u32 nritems;
1876 unsigned int data_end;
1877 unsigned int old_data_start;
1878 unsigned int old_size;
1879 unsigned int size_diff;
1880 int i;
1881
1882 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001883 leaf = path->nodes[0];
Chris Masonb18c6682007-04-17 13:26:50 -04001884
Chris Mason5f39d392007-10-15 16:14:19 -04001885 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04001886 data_end = leaf_data_end(root, leaf);
1887
1888 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001889 old_data_start = btrfs_item_offset_nr(leaf, slot);
1890 old_size = btrfs_item_size_nr(leaf, slot);
Chris Masonb18c6682007-04-17 13:26:50 -04001891 BUG_ON(old_size <= new_size);
1892 size_diff = old_size - new_size;
1893
1894 BUG_ON(slot < 0);
1895 BUG_ON(slot >= nritems);
1896
1897 /*
1898 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1899 */
1900 /* first correct the data pointers */
1901 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001902 u32 ioff;
1903 item = btrfs_item_nr(leaf, i);
1904 ioff = btrfs_item_offset(leaf, item);
1905 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04001906 }
1907 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04001908 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masonb18c6682007-04-17 13:26:50 -04001909 data_end + size_diff, btrfs_leaf_data(leaf) +
1910 data_end, old_data_start + new_size - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04001911
1912 item = btrfs_item_nr(leaf, slot);
1913 btrfs_set_item_size(leaf, item, new_size);
1914 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04001915
1916 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001917 if (btrfs_leaf_free_space(root, leaf) < 0) {
1918 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04001919 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001920 }
Chris Masonb18c6682007-04-17 13:26:50 -04001921 check_leaf(root, path, 0);
1922 return ret;
1923}
1924
Chris Mason5f39d392007-10-15 16:14:19 -04001925int btrfs_extend_item(struct btrfs_trans_handle *trans,
1926 struct btrfs_root *root, struct btrfs_path *path,
1927 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04001928{
1929 int ret = 0;
1930 int slot;
1931 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04001932 struct extent_buffer *leaf;
1933 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04001934 u32 nritems;
1935 unsigned int data_end;
1936 unsigned int old_data;
1937 unsigned int old_size;
1938 int i;
1939
1940 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001941 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04001942
Chris Mason5f39d392007-10-15 16:14:19 -04001943 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04001944 data_end = leaf_data_end(root, leaf);
1945
Chris Mason5f39d392007-10-15 16:14:19 -04001946 if (btrfs_leaf_free_space(root, leaf) < data_size) {
1947 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04001948 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001949 }
Chris Mason6567e832007-04-16 09:22:45 -04001950 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001951 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04001952
1953 BUG_ON(slot < 0);
1954 BUG_ON(slot >= nritems);
1955
1956 /*
1957 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1958 */
1959 /* first correct the data pointers */
1960 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001961 u32 ioff;
1962 item = btrfs_item_nr(leaf, i);
1963 ioff = btrfs_item_offset(leaf, item);
1964 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04001965 }
Chris Mason5f39d392007-10-15 16:14:19 -04001966
Chris Mason6567e832007-04-16 09:22:45 -04001967 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04001968 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04001969 data_end - data_size, btrfs_leaf_data(leaf) +
1970 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04001971
Chris Mason6567e832007-04-16 09:22:45 -04001972 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04001973 old_size = btrfs_item_size_nr(leaf, slot);
1974 item = btrfs_item_nr(leaf, slot);
1975 btrfs_set_item_size(leaf, item, old_size + data_size);
1976 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04001977
1978 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001979 if (btrfs_leaf_free_space(root, leaf) < 0) {
1980 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04001981 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001982 }
Chris Mason6567e832007-04-16 09:22:45 -04001983 check_leaf(root, path, 0);
1984 return ret;
1985}
1986
Chris Mason74123bd2007-02-02 11:05:29 -05001987/*
1988 * Given a key and some data, insert an item into the tree.
1989 * This does all the path init required, making room in the tree if needed.
1990 */
Chris Mason5f39d392007-10-15 16:14:19 -04001991int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
1992 struct btrfs_root *root,
1993 struct btrfs_path *path,
1994 struct btrfs_key *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001995{
Chris Mason5f39d392007-10-15 16:14:19 -04001996 struct extent_buffer *leaf;
1997 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001998 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001999 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002000 int slot_orig;
Chris Mason7518a232007-03-12 12:01:18 -04002001 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002002 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002003 struct btrfs_disk_key disk_key;
2004
2005 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002006
Chris Mason74123bd2007-02-02 11:05:29 -05002007 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002008 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002009 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002010
Chris Masone089f052007-03-16 16:20:31 -04002011 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002012 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002013 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002014 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002015 if (ret < 0)
2016 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002017
Chris Mason62e27492007-03-15 12:56:47 -04002018 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002019 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002020
Chris Mason5f39d392007-10-15 16:14:19 -04002021 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002022 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002023
Chris Mason123abc82007-03-14 14:14:43 -04002024 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04002025 sizeof(struct btrfs_item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002026 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002027 }
Chris Mason5f39d392007-10-15 16:14:19 -04002028
Chris Mason62e27492007-03-15 12:56:47 -04002029 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002030 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002031
Chris Masonbe0e5c02007-01-26 15:51:26 -05002032 if (slot != nritems) {
2033 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002034 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002035
Chris Mason5f39d392007-10-15 16:14:19 -04002036 if (old_data < data_end) {
2037 btrfs_print_leaf(root, leaf);
2038 printk("slot %d old_data %d data_end %d\n",
2039 slot, old_data, data_end);
2040 BUG_ON(1);
2041 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002042 /*
2043 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2044 */
2045 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04002046 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002047 u32 ioff;
2048 item = btrfs_item_nr(leaf, i);
2049 ioff = btrfs_item_offset(leaf, item);
2050 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason0783fcf2007-03-12 20:12:07 -04002051 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002052
2053 /* shift the items */
Chris Mason5f39d392007-10-15 16:14:19 -04002054 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2055 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002056 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002057
2058 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002059 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002060 data_end - data_size, btrfs_leaf_data(leaf) +
2061 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002062 data_end = old_data;
2063 }
Chris Mason5f39d392007-10-15 16:14:19 -04002064
Chris Mason62e27492007-03-15 12:56:47 -04002065 /* setup the item for the new data */
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_set_item_key(leaf, &disk_key, slot);
2067 item = btrfs_item_nr(leaf, slot);
2068 btrfs_set_item_offset(leaf, item, data_end - data_size);
2069 btrfs_set_item_size(leaf, item, data_size);
2070 btrfs_set_header_nritems(leaf, nritems + 1);
2071 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002072
2073 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05002074 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04002075 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002076
Chris Mason5f39d392007-10-15 16:14:19 -04002077 if (btrfs_leaf_free_space(root, leaf) < 0) {
2078 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002079 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002080 }
Chris Mason62e27492007-03-15 12:56:47 -04002081 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05002082out:
Chris Mason62e27492007-03-15 12:56:47 -04002083 return ret;
2084}
2085
2086/*
2087 * Given a key and some data, insert an item into the tree.
2088 * This does all the path init required, making room in the tree if needed.
2089 */
Chris Masone089f052007-03-16 16:20:31 -04002090int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2091 *root, struct btrfs_key *cpu_key, void *data, u32
2092 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002093{
2094 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002095 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002096 struct extent_buffer *leaf;
2097 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002098
Chris Mason2c90e5d2007-04-02 10:50:19 -04002099 path = btrfs_alloc_path();
2100 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002101 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002102 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002103 leaf = path->nodes[0];
2104 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2105 write_extent_buffer(leaf, data, ptr, data_size);
2106 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002107 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002108 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002109 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002110}
2111
Chris Mason74123bd2007-02-02 11:05:29 -05002112/*
Chris Mason5de08d72007-02-24 06:24:44 -05002113 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002114 *
2115 * If the delete empties a node, the node is removed from the tree,
2116 * continuing all the way the root if required. The root is converted into
2117 * a leaf if all the nodes are emptied.
2118 */
Chris Masone089f052007-03-16 16:20:31 -04002119static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2120 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002121{
Chris Mason5f39d392007-10-15 16:14:19 -04002122 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002123 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002124 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002125 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002126
Chris Mason5f39d392007-10-15 16:14:19 -04002127 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002128 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002129 memmove_extent_buffer(parent,
2130 btrfs_node_key_ptr_offset(slot),
2131 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002132 sizeof(struct btrfs_key_ptr) *
2133 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002134 }
Chris Mason7518a232007-03-12 12:01:18 -04002135 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002136 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002137 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002138 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002139 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002140 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002141 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002142 struct btrfs_disk_key disk_key;
2143
2144 btrfs_node_key(parent, &disk_key, 0);
2145 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002146 if (wret)
2147 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002148 }
Chris Masond6025572007-03-30 14:27:56 -04002149 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002150 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002151}
2152
Chris Mason74123bd2007-02-02 11:05:29 -05002153/*
2154 * delete the item at the leaf level in path. If that empties
2155 * the leaf, remove it from the tree
2156 */
Chris Masone089f052007-03-16 16:20:31 -04002157int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2158 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002159{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002160 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002161 struct extent_buffer *leaf;
2162 struct btrfs_item *item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002163 int doff;
2164 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002165 int ret = 0;
2166 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002167 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168
Chris Mason5f39d392007-10-15 16:14:19 -04002169 leaf = path->nodes[0];
Chris Mason4920c9a2007-01-26 16:38:42 -05002170 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002171 doff = btrfs_item_offset_nr(leaf, slot);
2172 dsize = btrfs_item_size_nr(leaf, slot);
2173 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002174
Chris Mason7518a232007-03-12 12:01:18 -04002175 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002176 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002177 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002178
2179 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002180 data_end + dsize,
2181 btrfs_leaf_data(leaf) + data_end,
2182 doff - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002183
Chris Mason0783fcf2007-03-12 20:12:07 -04002184 for (i = slot + 1; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002185 u32 ioff;
2186 item = btrfs_item_nr(leaf, i);
2187 ioff = btrfs_item_offset(leaf, item);
2188 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002189 }
Chris Mason5f39d392007-10-15 16:14:19 -04002190 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2191 btrfs_item_nr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002192 sizeof(struct btrfs_item) *
2193 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002194 }
Chris Mason5f39d392007-10-15 16:14:19 -04002195 btrfs_set_header_nritems(leaf, nritems - 1);
Chris Mason7518a232007-03-12 12:01:18 -04002196 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002197
Chris Mason74123bd2007-02-02 11:05:29 -05002198 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002199 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002200 if (leaf == root->node) {
2201 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002202 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002203 clean_tree_block(trans, root, leaf);
2204 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002205 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002206 if (wret)
2207 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002208 wret = btrfs_free_extent(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002209 extent_buffer_blocknr(leaf),
2210 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002211 if (wret)
2212 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002213 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002214 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002215 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002216 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002217 struct btrfs_disk_key disk_key;
2218
2219 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002220 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002221 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002222 if (wret)
2223 ret = wret;
2224 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002225
Chris Mason74123bd2007-02-02 11:05:29 -05002226 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04002227 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002228 /* push_leaf_left fixes the path.
2229 * make sure the path still points to our leaf
2230 * for possible call to del_ptr below
2231 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002232 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002233 extent_buffer_get(leaf);
2234
Chris Masone089f052007-03-16 16:20:31 -04002235 wret = push_leaf_left(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002236 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002237 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002238
2239 if (path->nodes[0] == leaf &&
2240 btrfs_header_nritems(leaf)) {
Chris Masone089f052007-03-16 16:20:31 -04002241 wret = push_leaf_right(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002242 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002243 ret = wret;
2244 }
Chris Mason5f39d392007-10-15 16:14:19 -04002245
2246 if (btrfs_header_nritems(leaf) == 0) {
2247 u64 blocknr = extent_buffer_blocknr(leaf);
2248
2249 clean_tree_block(trans, root, leaf);
2250 wait_on_tree_block_writeback(root, leaf);
2251
Chris Masone089f052007-03-16 16:20:31 -04002252 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002253 if (wret)
2254 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002255
2256 free_extent_buffer(leaf);
Chris Masone089f052007-03-16 16:20:31 -04002257 wret = btrfs_free_extent(trans, root, blocknr,
2258 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002259 if (wret)
2260 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002261 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002262 btrfs_mark_buffer_dirty(leaf);
2263 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002264 }
Chris Masond5719762007-03-23 10:01:08 -04002265 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002266 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002267 }
2268 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002269 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002270}
2271
Chris Mason97571fd2007-02-24 13:39:08 -05002272/*
2273 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002274 * returns 0 if it found something or 1 if there are no greater leaves.
2275 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002276 */
Chris Mason234b63a2007-03-13 10:46:10 -04002277int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002278{
2279 int slot;
2280 int level = 1;
2281 u64 blocknr;
Chris Mason5f39d392007-10-15 16:14:19 -04002282 struct extent_buffer *c;
2283 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002284
Chris Mason234b63a2007-03-13 10:46:10 -04002285 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002286 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002287 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002288
Chris Masond97e63b2007-02-20 16:40:44 -05002289 slot = path->slots[level] + 1;
2290 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002291 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002292 level++;
2293 continue;
2294 }
Chris Mason5f39d392007-10-15 16:14:19 -04002295
2296 blocknr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002297 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002298 free_extent_buffer(next);
2299
Chris Mason6702ed42007-08-07 16:15:09 -04002300 if (path->reada)
2301 reada_for_search(root, path, level, slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002302
Chris Masond97e63b2007-02-20 16:40:44 -05002303 next = read_tree_block(root, blocknr);
2304 break;
2305 }
2306 path->slots[level] = slot;
2307 while(1) {
2308 level--;
2309 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002310 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002311 path->nodes[level] = next;
2312 path->slots[level] = 0;
2313 if (!level)
2314 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002315 if (path->reada)
Yan32020612007-08-27 15:17:54 -04002316 reada_for_search(root, path, level, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002317 next = read_tree_block(root, btrfs_node_blockptr(next, 0));
Chris Masond97e63b2007-02-20 16:40:44 -05002318 }
2319 return 0;
2320}