blob: c0782a5b04c5384390151a8fed6c75983207d6cc [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 Masoneb60cea2007-02-02 09:18:22 -050019#include "ctree.h"
20#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040021#include "transaction.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050022
Chris Masone089f052007-03-16 16:20:31 -040023static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
24 *root, struct btrfs_path *path, int level);
25static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040026 *root, struct btrfs_key *ins_key,
27 struct btrfs_path *path, int data_size);
Chris Masone089f052007-03-16 16:20:31 -040028static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040029 *root, struct buffer_head *dst, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -040030 *src);
31static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -040032 btrfs_root *root, struct buffer_head *dst_buf,
33 struct buffer_head *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040034static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
35 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050036
Chris Masondf24a2b2007-04-04 09:36:31 -040037inline void btrfs_init_path(struct btrfs_path *p)
38{
39 memset(p, 0, sizeof(*p));
40}
41
Chris Mason2c90e5d2007-04-02 10:50:19 -040042struct btrfs_path *btrfs_alloc_path(void)
43{
Chris Masondf24a2b2007-04-04 09:36:31 -040044 struct btrfs_path *path;
45 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040046 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040047 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040048 path->reada = 1;
49 }
Chris Masondf24a2b2007-04-04 09:36:31 -040050 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040051}
52
53void btrfs_free_path(struct btrfs_path *p)
54{
Chris Masondf24a2b2007-04-04 09:36:31 -040055 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040056 kmem_cache_free(btrfs_path_cachep, p);
57}
58
Chris Mason234b63a2007-03-13 10:46:10 -040059void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050060{
61 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040062 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050063 if (!p->nodes[i])
64 break;
Chris Mason234b63a2007-03-13 10:46:10 -040065 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050066 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050067 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050068}
69
Chris Mason6702ed42007-08-07 16:15:09 -040070static int __btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
71 *root, struct buffer_head *buf, struct buffer_head
72 *parent, int parent_slot, struct buffer_head
73 **cow_ret, u64 search_start, u64 empty_size)
74{
75 struct buffer_head *cow;
76 struct btrfs_node *cow_node;
77 int ret = 0;
78 int different_trans = 0;
79
80 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
81 WARN_ON(!buffer_uptodate(buf));
82 cow = btrfs_alloc_free_block(trans, root, search_start, empty_size);
83 if (IS_ERR(cow))
84 return PTR_ERR(cow);
85
86 cow_node = btrfs_buffer_node(cow);
87 if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
88 WARN_ON(1);
89
90 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
91 btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow));
92 btrfs_set_header_generation(&cow_node->header, trans->transid);
93 btrfs_set_header_owner(&cow_node->header, root->root_key.objectid);
94
95 WARN_ON(btrfs_header_generation(btrfs_buffer_header(buf)) >
96 trans->transid);
97 if (btrfs_header_generation(btrfs_buffer_header(buf)) !=
98 trans->transid) {
99 different_trans = 1;
100 ret = btrfs_inc_ref(trans, root, buf);
101 if (ret)
102 return ret;
103 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400104 clean_tree_block(trans, root, buf);
105 }
106
107 if (buf == root->node) {
108 root->node = cow;
109 get_bh(cow);
110 if (buf != root->commit_root) {
111 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
112 }
113 btrfs_block_release(root, buf);
114 } else {
115 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
116 bh_blocknr(cow));
117 btrfs_mark_buffer_dirty(parent);
118 WARN_ON(btrfs_header_generation(btrfs_buffer_header(parent)) !=
119 trans->transid);
120 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
121 }
122 btrfs_block_release(root, buf);
123 btrfs_mark_buffer_dirty(cow);
124 *cow_ret = cow;
125 return 0;
126}
127
128int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400129 *root, struct buffer_head *buf, struct buffer_head
130 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -0400131 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500132{
Chris Mason6702ed42007-08-07 16:15:09 -0400133 u64 search_start;
Chris Masonccd467d2007-06-28 15:57:36 -0400134 if (trans->transaction != root->fs_info->running_transaction) {
135 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
136 root->fs_info->running_transaction->transid);
137 WARN_ON(1);
138 }
139 if (trans->transid != root->fs_info->generation) {
140 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
141 root->fs_info->generation);
142 WARN_ON(1);
143 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400144 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
145 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
150 search_start = bh_blocknr(buf) & ~((u64)65535);
151 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 Mason2cc58cf2007-08-27 16:49:44 -0400164static int should_defrag_leaf(struct buffer_head *bh)
165{
166 struct btrfs_leaf *leaf = btrfs_buffer_leaf(bh);
167 struct btrfs_disk_key *key;
168 u32 nritems;
169
170 if (buffer_defrag(bh))
171 return 1;
172
173 nritems = btrfs_header_nritems(&leaf->header);
174 if (nritems == 0)
175 return 0;
176
177 key = &leaf->items[0].key;
178 if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY)
179 return 1;
180
181 key = &leaf->items[nritems-1].key;
182 if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY)
183 return 1;
184 if (nritems > 4) {
185 key = &leaf->items[nritems/2].key;
186 if (btrfs_disk_key_type(key) == BTRFS_DIR_ITEM_KEY)
187 return 1;
188 }
189 return 0;
190}
191
Chris Mason6702ed42007-08-07 16:15:09 -0400192int btrfs_realloc_node(struct btrfs_trans_handle *trans,
193 struct btrfs_root *root, struct buffer_head *parent,
Chris Masone9d0b132007-08-10 14:06:19 -0400194 int cache_only, u64 *last_ret)
Chris Mason6702ed42007-08-07 16:15:09 -0400195{
196 struct btrfs_node *parent_node;
197 struct buffer_head *cur_bh;
198 struct buffer_head *tmp_bh;
199 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400200 u64 search_start = *last_ret;
201 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400202 u64 other;
203 u32 parent_nritems;
204 int start_slot;
205 int end_slot;
206 int i;
207 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400208 int parent_level;
Chris Mason6702ed42007-08-07 16:15:09 -0400209
210 if (trans->transaction != root->fs_info->running_transaction) {
211 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
212 root->fs_info->running_transaction->transid);
213 WARN_ON(1);
214 }
215 if (trans->transid != root->fs_info->generation) {
216 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
217 root->fs_info->generation);
218 WARN_ON(1);
219 }
220 parent_node = btrfs_buffer_node(parent);
221 parent_nritems = btrfs_header_nritems(&parent_node->header);
Chris Masonf2183bd2007-08-10 14:42:37 -0400222 parent_level = btrfs_header_level(&parent_node->header);
Chris Mason6702ed42007-08-07 16:15:09 -0400223
224 start_slot = 0;
225 end_slot = parent_nritems;
226
227 if (parent_nritems == 1)
228 return 0;
229
230 for (i = start_slot; i < end_slot; i++) {
231 int close = 1;
232 blocknr = btrfs_node_blockptr(parent_node, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400233 if (last_block == 0)
234 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400235 if (i > 0) {
236 other = btrfs_node_blockptr(parent_node, i - 1);
237 close = close_blocks(blocknr, other);
238 }
239 if (close && i < end_slot - 1) {
240 other = btrfs_node_blockptr(parent_node, i + 1);
241 close = close_blocks(blocknr, other);
242 }
Chris Masone9d0b132007-08-10 14:06:19 -0400243 if (close) {
244 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400245 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400246 }
Chris Mason6702ed42007-08-07 16:15:09 -0400247
248 cur_bh = btrfs_find_tree_block(root, blocknr);
249 if (!cur_bh || !buffer_uptodate(cur_bh) ||
Chris Mason2cc58cf2007-08-27 16:49:44 -0400250 buffer_locked(cur_bh) ||
251 (parent_level != 1 && !buffer_defrag(cur_bh)) ||
252 (parent_level == 1 && !should_defrag_leaf(cur_bh))) {
Chris Mason6702ed42007-08-07 16:15:09 -0400253 if (cache_only) {
254 brelse(cur_bh);
255 continue;
256 }
Chris Masonf2183bd2007-08-10 14:42:37 -0400257 if (!cur_bh || !buffer_uptodate(cur_bh) ||
258 buffer_locked(cur_bh)) {
259 brelse(cur_bh);
260 cur_bh = read_tree_block(root, blocknr);
261 }
Chris Mason6702ed42007-08-07 16:15:09 -0400262 }
Chris Masone9d0b132007-08-10 14:06:19 -0400263 if (search_start == 0)
264 search_start = last_block & ~((u64)65535);
265
Chris Mason6702ed42007-08-07 16:15:09 -0400266 err = __btrfs_cow_block(trans, root, cur_bh, parent, i,
267 &tmp_bh, search_start,
268 min(8, end_slot - i));
269 if (err)
270 break;
271 search_start = bh_blocknr(tmp_bh);
Chris Masonf2183bd2007-08-10 14:42:37 -0400272 *last_ret = search_start;
273 if (parent_level == 1)
274 clear_buffer_defrag(tmp_bh);
Chris Mason6702ed42007-08-07 16:15:09 -0400275 brelse(tmp_bh);
276 }
277 return err;
278}
279
Chris Mason74123bd2007-02-02 11:05:29 -0500280/*
281 * The leaf data grows from end-to-front in the node.
282 * this returns the address of the start of the last item,
283 * which is the stop of the leaf data stack
284 */
Chris Mason123abc82007-03-14 14:14:43 -0400285static inline unsigned int leaf_data_end(struct btrfs_root *root,
286 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500287{
Chris Mason7518a232007-03-12 12:01:18 -0400288 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500289 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400290 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -0400291 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500292}
293
Chris Mason74123bd2007-02-02 11:05:29 -0500294/*
Chris Mason74123bd2007-02-02 11:05:29 -0500295 * compare two keys in a memcmp fashion
296 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400297static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500298{
Chris Masone2fa7222007-03-12 16:22:34 -0400299 struct btrfs_key k1;
300
301 btrfs_disk_key_to_cpu(&k1, disk);
302
303 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500304 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400305 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500306 return -1;
Chris Masonf254e522007-03-29 15:15:27 -0400307 if (k1.flags > k2->flags)
308 return 1;
309 if (k1.flags < k2->flags)
310 return -1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400311 if (k1.offset > k2->offset)
312 return 1;
313 if (k1.offset < k2->offset)
314 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500315 return 0;
316}
Chris Mason74123bd2007-02-02 11:05:29 -0500317
Chris Mason123abc82007-03-14 14:14:43 -0400318static int check_node(struct btrfs_root *root, struct btrfs_path *path,
319 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500320{
Chris Mason234b63a2007-03-13 10:46:10 -0400321 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400322 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500323 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400324 int slot;
325 struct btrfs_key cpukey;
Chris Mason7518a232007-03-12 12:01:18 -0400326 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500327
328 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400329 parent = btrfs_buffer_node(path->nodes[level + 1]);
Aneesha1f39632007-07-11 10:03:27 -0400330
Chris Mason8d7be552007-05-10 11:24:42 -0400331 slot = path->slots[level];
Chris Mason2cc58cf2007-08-27 16:49:44 -0400332 BUG_ON(!buffer_uptodate(path->nodes[level]));
Chris Mason7518a232007-03-12 12:01:18 -0400333 BUG_ON(nritems == 0);
334 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400335 struct btrfs_disk_key *parent_key;
Aneesha1f39632007-07-11 10:03:27 -0400336
337 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400338 parent_key = &parent->ptrs[parent_slot].key;
339 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400340 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400341 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400342 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500343 }
Chris Mason123abc82007-03-14 14:14:43 -0400344 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400345 if (slot != 0) {
346 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key);
347 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0);
348 }
349 if (slot < nritems - 1) {
350 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key);
351 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500352 }
353 return 0;
354}
355
Chris Mason123abc82007-03-14 14:14:43 -0400356static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
357 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500358{
Chris Masone20d96d2007-03-22 12:13:20 -0400359 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400360 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500361 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400362 int slot = path->slots[0];
363 struct btrfs_key cpukey;
364
Chris Mason7518a232007-03-12 12:01:18 -0400365 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500366
367 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400368 parent = btrfs_buffer_node(path->nodes[level + 1]);
Aneesha1f39632007-07-11 10:03:27 -0400369
Chris Mason123abc82007-03-14 14:14:43 -0400370 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400371
372 if (nritems == 0)
373 return 0;
374
375 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400376 struct btrfs_disk_key *parent_key;
Aneesha1f39632007-07-11 10:03:27 -0400377
378 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400379 parent_key = &parent->ptrs[parent_slot].key;
Chris Mason6702ed42007-08-07 16:15:09 -0400380
Chris Masonaa5d6be2007-02-28 16:35:06 -0500381 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400382 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400383 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400384 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500385 }
Chris Mason8d7be552007-05-10 11:24:42 -0400386 if (slot != 0) {
387 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key);
388 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0);
389 BUG_ON(btrfs_item_offset(leaf->items + slot - 1) !=
390 btrfs_item_end(leaf->items + slot));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500391 }
Chris Mason8d7be552007-05-10 11:24:42 -0400392 if (slot < nritems - 1) {
393 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key);
394 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0);
395 BUG_ON(btrfs_item_offset(leaf->items + slot) !=
396 btrfs_item_end(leaf->items + slot + 1));
397 }
398 BUG_ON(btrfs_item_offset(leaf->items) +
399 btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500400 return 0;
401}
402
Chris Mason123abc82007-03-14 14:14:43 -0400403static int check_block(struct btrfs_root *root, struct btrfs_path *path,
404 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500405{
Chris Mason3eb03142007-04-05 14:28:50 -0400406 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
407 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
408 sizeof(node->header.fsid)))
409 BUG();
Chris Masonaa5d6be2007-02-28 16:35:06 -0500410 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400411 return check_leaf(root, path, level);
412 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500413}
414
Chris Mason74123bd2007-02-02 11:05:29 -0500415/*
416 * search for key in the array p. items p are item_size apart
417 * and there are 'max' items in p
418 * the slot in the array is returned via slot, and it points to
419 * the place where you would insert key if it is not found in
420 * the array.
421 *
422 * slot may point to max if the key is bigger than all of the keys
423 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400424static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500425 int max, int *slot)
426{
427 int low = 0;
428 int high = max;
429 int mid;
430 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400431 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500432
433 while(low < high) {
434 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400435 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500436 ret = comp_keys(tmp, key);
437
438 if (ret < 0)
439 low = mid + 1;
440 else if (ret > 0)
441 high = mid;
442 else {
443 *slot = mid;
444 return 0;
445 }
446 }
447 *slot = low;
448 return 1;
449}
450
Chris Mason97571fd2007-02-24 13:39:08 -0500451/*
452 * simple bin_search frontend that does the right thing for
453 * leaves vs nodes
454 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400455static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500456{
Chris Mason7518a232007-03-12 12:01:18 -0400457 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400458 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400459 return generic_bin_search((void *)l->items,
460 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400461 key, btrfs_header_nritems(&c->header),
462 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500463 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400464 return generic_bin_search((void *)c->ptrs,
465 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400466 key, btrfs_header_nritems(&c->header),
467 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500468 }
469 return -1;
470}
471
Chris Masone20d96d2007-03-22 12:13:20 -0400472static struct buffer_head *read_node_slot(struct btrfs_root *root,
473 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500474 int slot)
475{
Chris Masone20d96d2007-03-22 12:13:20 -0400476 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500477 if (slot < 0)
478 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400479 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500480 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400481 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500482}
483
Chris Masone089f052007-03-16 16:20:31 -0400484static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
485 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500486{
Chris Masone20d96d2007-03-22 12:13:20 -0400487 struct buffer_head *right_buf;
488 struct buffer_head *mid_buf;
489 struct buffer_head *left_buf;
490 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400491 struct btrfs_node *right = NULL;
492 struct btrfs_node *mid;
493 struct btrfs_node *left = NULL;
494 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500495 int ret = 0;
496 int wret;
497 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500498 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400499 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500500 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500501
502 if (level == 0)
503 return 0;
504
505 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400506 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400507 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500508
Chris Mason234b63a2007-03-13 10:46:10 -0400509 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500510 parent_buf = path->nodes[level + 1];
511 pslot = path->slots[level + 1];
512
Chris Mason40689472007-03-17 14:29:23 -0400513 /*
514 * deal with the case where there is only one pointer in the root
515 * by promoting the node below to a root
516 */
Chris Masonbb803952007-03-01 12:04:21 -0500517 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400518 struct buffer_head *child;
Chris Mason7eccb902007-04-11 15:53:25 -0400519 u64 blocknr = bh_blocknr(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500520
Chris Mason7518a232007-03-12 12:01:18 -0400521 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500522 return 0;
523
524 /* promote the child to a root */
525 child = read_node_slot(root, mid_buf, 0);
526 BUG_ON(!child);
527 root->node = child;
528 path->nodes[level] = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400529 clean_tree_block(trans, root, mid_buf);
530 wait_on_buffer(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500531 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400532 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500533 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400534 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400535 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500536 }
Chris Masone20d96d2007-03-22 12:13:20 -0400537 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500538
Chris Mason123abc82007-03-14 14:14:43 -0400539 if (btrfs_header_nritems(&mid->header) >
540 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500541 return 0;
542
Chris Mason54aa1f42007-06-22 14:16:25 -0400543 if (btrfs_header_nritems(&mid->header) < 2)
544 err_on_enospc = 1;
545
Chris Masonbb803952007-03-01 12:04:21 -0500546 left_buf = read_node_slot(root, parent_buf, pslot - 1);
Chris Masonbb803952007-03-01 12:04:21 -0500547 if (left_buf) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400548 wret = btrfs_cow_block(trans, root, left_buf,
549 parent_buf, pslot - 1, &left_buf);
550 if (wret) {
551 ret = wret;
552 goto enospc;
553 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400554 }
555 right_buf = read_node_slot(root, parent_buf, pslot + 1);
556 if (right_buf) {
557 wret = btrfs_cow_block(trans, root, right_buf,
558 parent_buf, pslot + 1, &right_buf);
559 if (wret) {
560 ret = wret;
561 goto enospc;
562 }
563 }
564
565 /* first, try to make some room in the middle buffer */
566 if (left_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400567 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400568 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400569 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500570 if (wret < 0)
571 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400572 if (btrfs_header_nritems(&mid->header) < 2)
573 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500574 }
Chris Mason79f95c82007-03-01 15:16:26 -0500575
576 /*
577 * then try to empty the right most buffer into the middle
578 */
Chris Masonbb803952007-03-01 12:04:21 -0500579 if (right_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400580 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400581 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400582 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500583 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400584 if (btrfs_header_nritems(&right->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -0400585 u64 blocknr = bh_blocknr(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400586 clean_tree_block(trans, root, right_buf);
Chris Masond6025572007-03-30 14:27:56 -0400587 wait_on_buffer(right_buf);
588 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500589 right_buf = NULL;
590 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400591 wret = del_ptr(trans, root, path, level + 1, pslot +
592 1);
Chris Masonbb803952007-03-01 12:04:21 -0500593 if (wret)
594 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400595 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500596 if (wret)
597 ret = wret;
598 } else {
Chris Masond6025572007-03-30 14:27:56 -0400599 btrfs_memcpy(root, parent,
600 &parent->ptrs[pslot + 1].key,
601 &right->ptrs[0].key,
602 sizeof(struct btrfs_disk_key));
603 btrfs_mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500604 }
605 }
Chris Mason7518a232007-03-12 12:01:18 -0400606 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500607 /*
608 * we're not allowed to leave a node with one item in the
609 * tree during a delete. A deletion from lower in the tree
610 * could try to delete the only pointer in this node.
611 * So, pull some keys from the left.
612 * There has to be a left pointer at this point because
613 * otherwise we would have pulled some pointers from the
614 * right
615 */
616 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400617 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400618 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500619 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400620 goto enospc;
621 }
Chris Mason79f95c82007-03-01 15:16:26 -0500622 BUG_ON(wret == 1);
623 }
Chris Mason7518a232007-03-12 12:01:18 -0400624 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500625 /* we've managed to empty the middle node, drop it */
Chris Mason7eccb902007-04-11 15:53:25 -0400626 u64 blocknr = bh_blocknr(mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400627 clean_tree_block(trans, root, mid_buf);
Chris Masond6025572007-03-30 14:27:56 -0400628 wait_on_buffer(mid_buf);
629 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500630 mid_buf = NULL;
631 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400632 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500633 if (wret)
634 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400635 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500636 if (wret)
637 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500638 } else {
639 /* update the parent key to reflect our changes */
Chris Masond6025572007-03-30 14:27:56 -0400640 btrfs_memcpy(root, parent,
641 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
642 sizeof(struct btrfs_disk_key));
643 btrfs_mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500644 }
Chris Masonbb803952007-03-01 12:04:21 -0500645
Chris Mason79f95c82007-03-01 15:16:26 -0500646 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500647 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400648 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400649 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500650 path->nodes[level] = left_buf;
651 path->slots[level + 1] -= 1;
652 path->slots[level] = orig_slot;
653 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400654 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500655 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400656 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500657 path->slots[level] = orig_slot;
658 }
659 }
Chris Mason79f95c82007-03-01 15:16:26 -0500660 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400661 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400662 if (orig_ptr !=
663 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
664 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500665 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400666enospc:
Chris Masonbb803952007-03-01 12:04:21 -0500667 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400668 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500669 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400670 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500671 return ret;
672}
673
Chris Masone66f7092007-04-20 13:16:02 -0400674/* returns zero if the push worked, non-zero otherwise */
675static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
676 struct btrfs_root *root,
677 struct btrfs_path *path, int level)
678{
679 struct buffer_head *right_buf;
680 struct buffer_head *mid_buf;
681 struct buffer_head *left_buf;
682 struct buffer_head *parent_buf = NULL;
683 struct btrfs_node *right = NULL;
684 struct btrfs_node *mid;
685 struct btrfs_node *left = NULL;
686 struct btrfs_node *parent = NULL;
687 int ret = 0;
688 int wret;
689 int pslot;
690 int orig_slot = path->slots[level];
691 u64 orig_ptr;
692
693 if (level == 0)
694 return 1;
695
696 mid_buf = path->nodes[level];
697 mid = btrfs_buffer_node(mid_buf);
698 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
699
700 if (level < BTRFS_MAX_LEVEL - 1)
701 parent_buf = path->nodes[level + 1];
702 pslot = path->slots[level + 1];
703
704 if (!parent_buf)
705 return 1;
706 parent = btrfs_buffer_node(parent_buf);
707
708 left_buf = read_node_slot(root, parent_buf, pslot - 1);
709
710 /* first, try to make some room in the middle buffer */
711 if (left_buf) {
712 u32 left_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400713 left = btrfs_buffer_node(left_buf);
714 left_nr = btrfs_header_nritems(&left->header);
Chris Mason33ade1f2007-04-20 13:48:57 -0400715 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
716 wret = 1;
717 } else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400718 ret = btrfs_cow_block(trans, root, left_buf, parent_buf,
719 pslot - 1, &left_buf);
720 if (ret)
721 wret = 1;
722 else {
723 left = btrfs_buffer_node(left_buf);
724 wret = push_node_left(trans, root,
725 left_buf, mid_buf);
726 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400727 }
Chris Masone66f7092007-04-20 13:16:02 -0400728 if (wret < 0)
729 ret = wret;
730 if (wret == 0) {
731 orig_slot += left_nr;
732 btrfs_memcpy(root, parent,
733 &parent->ptrs[pslot].key,
734 &mid->ptrs[0].key,
735 sizeof(struct btrfs_disk_key));
736 btrfs_mark_buffer_dirty(parent_buf);
737 if (btrfs_header_nritems(&left->header) > orig_slot) {
738 path->nodes[level] = left_buf;
739 path->slots[level + 1] -= 1;
740 path->slots[level] = orig_slot;
741 btrfs_block_release(root, mid_buf);
742 } else {
743 orig_slot -=
744 btrfs_header_nritems(&left->header);
745 path->slots[level] = orig_slot;
746 btrfs_block_release(root, left_buf);
747 }
748 check_node(root, path, level);
749 return 0;
750 }
751 btrfs_block_release(root, left_buf);
752 }
753 right_buf = read_node_slot(root, parent_buf, pslot + 1);
754
755 /*
756 * then try to empty the right most buffer into the middle
757 */
758 if (right_buf) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400759 u32 right_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400760 right = btrfs_buffer_node(right_buf);
Chris Mason33ade1f2007-04-20 13:48:57 -0400761 right_nr = btrfs_header_nritems(&right->header);
762 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
763 wret = 1;
764 } else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400765 ret = btrfs_cow_block(trans, root, right_buf,
766 parent_buf, pslot + 1,
767 &right_buf);
768 if (ret)
769 wret = 1;
770 else {
771 right = btrfs_buffer_node(right_buf);
772 wret = balance_node_right(trans, root,
773 right_buf, mid_buf);
774 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400775 }
Chris Masone66f7092007-04-20 13:16:02 -0400776 if (wret < 0)
777 ret = wret;
778 if (wret == 0) {
779 btrfs_memcpy(root, parent,
780 &parent->ptrs[pslot + 1].key,
781 &right->ptrs[0].key,
782 sizeof(struct btrfs_disk_key));
783 btrfs_mark_buffer_dirty(parent_buf);
784 if (btrfs_header_nritems(&mid->header) <= orig_slot) {
785 path->nodes[level] = right_buf;
786 path->slots[level + 1] += 1;
787 path->slots[level] = orig_slot -
788 btrfs_header_nritems(&mid->header);
789 btrfs_block_release(root, mid_buf);
790 } else {
791 btrfs_block_release(root, right_buf);
792 }
793 check_node(root, path, level);
794 return 0;
795 }
796 btrfs_block_release(root, right_buf);
797 }
798 check_node(root, path, level);
799 return 1;
800}
801
Chris Mason74123bd2007-02-02 11:05:29 -0500802/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400803 * readahead one full node of leaves
804 */
805static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason6702ed42007-08-07 16:15:09 -0400806 int level, int slot)
Chris Mason3c69fae2007-08-07 15:52:22 -0400807{
808 struct btrfs_node *node;
809 int i;
810 u32 nritems;
811 u64 item_objectid;
812 u64 blocknr;
813 u64 search;
814 u64 cluster_start;
815 int ret;
816 int nread = 0;
817 int direction = path->reada;
818 struct radix_tree_root found;
819 unsigned long gang[8];
820 struct buffer_head *bh;
821
Chris Mason6702ed42007-08-07 16:15:09 -0400822 if (level == 0)
Chris Mason3c69fae2007-08-07 15:52:22 -0400823 return;
824
Chris Mason6702ed42007-08-07 16:15:09 -0400825 if (!path->nodes[level])
826 return;
827
828 node = btrfs_buffer_node(path->nodes[level]);
Chris Mason3c69fae2007-08-07 15:52:22 -0400829 search = btrfs_node_blockptr(node, slot);
830 bh = btrfs_find_tree_block(root, search);
831 if (bh) {
832 brelse(bh);
833 return;
834 }
835
836 init_bit_radix(&found);
837 nritems = btrfs_header_nritems(&node->header);
838 for (i = slot; i < nritems; i++) {
839 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
840 blocknr = btrfs_node_blockptr(node, i);
841 set_radix_bit(&found, blocknr);
842 }
843 if (direction > 0) {
844 cluster_start = search - 4;
845 if (cluster_start > search)
846 cluster_start = 0;
847 } else
848 cluster_start = search + 4;
849 while(1) {
850 ret = find_first_radix_bit(&found, gang, 0, ARRAY_SIZE(gang));
851 if (!ret)
852 break;
853 for (i = 0; i < ret; i++) {
854 blocknr = gang[i];
855 clear_radix_bit(&found, blocknr);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400856 if (path->reada == 1 && nread > 16)
Chris Mason3c69fae2007-08-07 15:52:22 -0400857 continue;
Chris Masonf2183bd2007-08-10 14:42:37 -0400858 if (close_blocks(cluster_start, blocknr)) {
Chris Mason3c69fae2007-08-07 15:52:22 -0400859 readahead_tree_block(root, blocknr);
860 nread++;
Chris Mason3c69fae2007-08-07 15:52:22 -0400861 cluster_start = blocknr;
Chris Mason3c69fae2007-08-07 15:52:22 -0400862 }
863 }
864 }
865}
866/*
Chris Mason74123bd2007-02-02 11:05:29 -0500867 * look for key in the tree. path is filled in with nodes along the way
868 * if key is found, we return zero and you can find the item in the leaf
869 * level of the path (level 0)
870 *
871 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500872 * be inserted, and 1 is returned. If there are other errors during the
873 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500874 *
875 * if ins_len > 0, nodes and leaves will be split as we walk down the
876 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
877 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500878 */
Chris Masone089f052007-03-16 16:20:31 -0400879int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
880 *root, struct btrfs_key *key, struct btrfs_path *p, int
881 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500882{
Chris Masone20d96d2007-03-22 12:13:20 -0400883 struct buffer_head *b;
884 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400885 struct btrfs_node *c;
Chris Mason3c69fae2007-08-07 15:52:22 -0400886 u64 blocknr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500887 int slot;
888 int ret;
889 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -0400890 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -0400891 u8 lowest_level = 0;
892
Chris Mason6702ed42007-08-07 16:15:09 -0400893 lowest_level = p->lowest_level;
894 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400895 WARN_ON(p->nodes[0] != NULL);
896 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500897again:
898 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400899 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500900 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400901 c = btrfs_buffer_node(b);
902 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500903 if (cow) {
904 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400905 wret = btrfs_cow_block(trans, root, b,
906 p->nodes[level + 1],
907 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400908 &cow_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400909 if (wret) {
910 btrfs_block_release(root, cow_buf);
911 return wret;
912 }
Chris Mason02217ed2007-03-02 16:08:05 -0500913 b = cow_buf;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400914 c = btrfs_buffer_node(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500915 }
916 BUG_ON(!cow && ins_len);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400917 if (level != btrfs_header_level(&c->header))
918 WARN_ON(1);
919 level = btrfs_header_level(&c->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500920 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400921 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500922 if (ret)
923 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500924 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400925 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500926 if (ret && slot > 0)
927 slot -= 1;
928 p->slots[level] = slot;
Chris Masond4dbff92007-04-04 14:08:15 -0400929 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
930 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -0400931 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500932 BUG_ON(sret > 0);
933 if (sret)
934 return sret;
935 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400936 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500937 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500938 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400939 int sret = balance_level(trans, root, p,
940 level);
Chris Masonbb803952007-03-01 12:04:21 -0500941 if (sret)
942 return sret;
943 b = p->nodes[level];
944 if (!b)
945 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400946 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500947 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400948 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500949 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400950 /* this is only true while dropping a snapshot */
951 if (level == lowest_level)
952 break;
Chris Mason3c69fae2007-08-07 15:52:22 -0400953 blocknr = btrfs_node_blockptr(c, slot);
Chris Mason6702ed42007-08-07 16:15:09 -0400954 if (should_reada)
955 reada_for_search(root, p, level, slot);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400956 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Mason3c69fae2007-08-07 15:52:22 -0400957
Chris Masonbe0e5c02007-01-26 15:51:26 -0500958 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400959 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500960 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400961 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400962 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -0400963 int sret = split_leaf(trans, root, key,
964 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500965 BUG_ON(sret > 0);
966 if (sret)
967 return sret;
968 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500969 return ret;
970 }
971 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500972 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500973}
974
Chris Mason74123bd2007-02-02 11:05:29 -0500975/*
976 * adjust the pointers going up the tree, starting at level
977 * making sure the right key of each node is points to 'key'.
978 * This is used after shifting pointers to the left, so it stops
979 * fixing up pointers when a given leaf/node is not in slot 0 of the
980 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500981 *
982 * If this fails to write a tree block, it returns -1, but continues
983 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500984 */
Chris Masone089f052007-03-16 16:20:31 -0400985static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
986 *root, struct btrfs_path *path, struct btrfs_disk_key
987 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500988{
989 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500990 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400991 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
992 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500993 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500994 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500995 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400996 t = btrfs_buffer_node(path->nodes[i]);
Chris Masond6025572007-03-30 14:27:56 -0400997 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
998 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500999 if (tslot != 0)
1000 break;
1001 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001002 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001003}
1004
Chris Mason74123bd2007-02-02 11:05:29 -05001005/*
1006 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001007 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001008 *
1009 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1010 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001011 */
Chris Masone089f052007-03-16 16:20:31 -04001012static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001013 *root, struct buffer_head *dst_buf, struct
1014 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001015{
Chris Masone20d96d2007-03-22 12:13:20 -04001016 struct btrfs_node *src = btrfs_buffer_node(src_buf);
1017 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001018 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001019 int src_nritems;
1020 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001021 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001022
Chris Mason7518a232007-03-12 12:01:18 -04001023 src_nritems = btrfs_header_nritems(&src->header);
1024 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -04001025 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001026
Chris Masoneb60cea2007-02-02 09:18:22 -05001027 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001028 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001029 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001030
1031 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001032 push_items = src_nritems;
1033
Chris Masond6025572007-03-30 14:27:56 -04001034 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
1035 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001036 if (push_items < src_nritems) {
Chris Masond6025572007-03-30 14:27:56 -04001037 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -04001038 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -04001039 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001040 }
Chris Mason7518a232007-03-12 12:01:18 -04001041 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
1042 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond6025572007-03-30 14:27:56 -04001043 btrfs_mark_buffer_dirty(src_buf);
1044 btrfs_mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001045 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001046}
1047
Chris Mason97571fd2007-02-24 13:39:08 -05001048/*
Chris Mason79f95c82007-03-01 15:16:26 -05001049 * try to push data from one node into the next node right in the
1050 * tree.
1051 *
1052 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1053 * error, and > 0 if there was no room in the right hand block.
1054 *
1055 * this will only push up to 1/2 the contents of the left node over
1056 */
Chris Masone089f052007-03-16 16:20:31 -04001057static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -04001058 btrfs_root *root, struct buffer_head *dst_buf,
1059 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -05001060{
Chris Masone20d96d2007-03-22 12:13:20 -04001061 struct btrfs_node *src = btrfs_buffer_node(src_buf);
1062 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -05001063 int push_items = 0;
1064 int max_push;
1065 int src_nritems;
1066 int dst_nritems;
1067 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001068
Chris Mason7518a232007-03-12 12:01:18 -04001069 src_nritems = btrfs_header_nritems(&src->header);
1070 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -04001071 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -05001072 if (push_items <= 0) {
1073 return 1;
1074 }
1075
1076 max_push = src_nritems / 2 + 1;
1077 /* don't try to empty the node */
1078 if (max_push > src_nritems)
1079 return 1;
1080 if (max_push < push_items)
1081 push_items = max_push;
1082
Chris Masond6025572007-03-30 14:27:56 -04001083 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
1084 dst_nritems * sizeof(struct btrfs_key_ptr));
1085
1086 btrfs_memcpy(root, dst, dst->ptrs,
1087 src->ptrs + src_nritems - push_items,
1088 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001089
Chris Mason7518a232007-03-12 12:01:18 -04001090 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
1091 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001092
Chris Masond6025572007-03-30 14:27:56 -04001093 btrfs_mark_buffer_dirty(src_buf);
1094 btrfs_mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -05001095 return ret;
1096}
1097
1098/*
Chris Mason97571fd2007-02-24 13:39:08 -05001099 * helper function to insert a new root level in the tree.
1100 * A new node is allocated, and a single item is inserted to
1101 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001102 *
1103 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001104 */
Chris Masone089f052007-03-16 16:20:31 -04001105static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
1106 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001107{
Chris Masone20d96d2007-03-22 12:13:20 -04001108 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001109 struct btrfs_node *lower;
1110 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -04001111 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001112
1113 BUG_ON(path->nodes[level]);
1114 BUG_ON(path->nodes[level-1] != root->node);
1115
Chris Mason6702ed42007-08-07 16:15:09 -04001116 t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001117 if (IS_ERR(t))
1118 return PTR_ERR(t);
Chris Masone20d96d2007-03-22 12:13:20 -04001119 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -04001120 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -04001121 btrfs_set_header_nritems(&c->header, 1);
1122 btrfs_set_header_level(&c->header, level);
Chris Mason7eccb902007-04-11 15:53:25 -04001123 btrfs_set_header_blocknr(&c->header, bh_blocknr(t));
Chris Mason7f5c1512007-03-23 15:56:19 -04001124 btrfs_set_header_generation(&c->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001125 btrfs_set_header_owner(&c->header, root->root_key.objectid);
Chris Masone20d96d2007-03-22 12:13:20 -04001126 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason3eb03142007-04-05 14:28:50 -04001127 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
1128 sizeof(c->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -04001129 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -04001130 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001131 else
Chris Mason123abc82007-03-14 14:14:43 -04001132 lower_key = &lower->ptrs[0].key;
Chris Masond6025572007-03-30 14:27:56 -04001133 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
1134 sizeof(struct btrfs_disk_key));
Chris Mason7eccb902007-04-11 15:53:25 -04001135 btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1]));
Chris Masond5719762007-03-23 10:01:08 -04001136
Chris Masond6025572007-03-30 14:27:56 -04001137 btrfs_mark_buffer_dirty(t);
Chris Masond5719762007-03-23 10:01:08 -04001138
Chris Mason5c680ed2007-02-22 11:39:13 -05001139 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -04001140 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -05001141 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -04001142 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001143 path->nodes[level] = t;
1144 path->slots[level] = 0;
1145 return 0;
1146}
1147
Chris Mason74123bd2007-02-02 11:05:29 -05001148/*
1149 * worker function to insert a single pointer in a node.
1150 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001151 *
Chris Mason74123bd2007-02-02 11:05:29 -05001152 * slot and level indicate where you want the key to go, and
1153 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001154 *
1155 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001156 */
Chris Masone089f052007-03-16 16:20:31 -04001157static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1158 *root, struct btrfs_path *path, struct btrfs_disk_key
1159 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001160{
Chris Mason234b63a2007-03-13 10:46:10 -04001161 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001162 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001163
1164 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -04001165 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -04001166 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -05001167 if (slot > nritems)
1168 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001169 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001170 BUG();
1171 if (slot != nritems) {
Chris Masond6025572007-03-30 14:27:56 -04001172 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
1173 lower->ptrs + slot,
1174 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001175 }
Chris Masond6025572007-03-30 14:27:56 -04001176 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
1177 key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -04001178 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -04001179 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001180 btrfs_mark_buffer_dirty(path->nodes[level]);
Chris Mason098f59c2007-05-11 11:33:21 -04001181 check_node(root, path, level);
Chris Mason74123bd2007-02-02 11:05:29 -05001182 return 0;
1183}
1184
Chris Mason97571fd2007-02-24 13:39:08 -05001185/*
1186 * split the node at the specified level in path in two.
1187 * The path is corrected to point to the appropriate node after the split
1188 *
1189 * Before splitting this tries to make some room in the node by pushing
1190 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001191 *
1192 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001193 */
Chris Masone089f052007-03-16 16:20:31 -04001194static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1195 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001196{
Chris Masone20d96d2007-03-22 12:13:20 -04001197 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001198 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -04001199 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -04001200 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001201 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001202 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001203 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001204 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001205
Chris Mason5c680ed2007-02-22 11:39:13 -05001206 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001207 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001208 if (t == root->node) {
1209 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001210 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001211 if (ret)
1212 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001213 } else {
1214 ret = push_nodes_for_insert(trans, root, path, level);
1215 t = path->nodes[level];
1216 c = btrfs_buffer_node(t);
1217 if (!ret &&
1218 btrfs_header_nritems(&c->header) <
1219 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1220 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001221 if (ret < 0)
1222 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001223 }
Chris Masone66f7092007-04-20 13:16:02 -04001224
Chris Mason7518a232007-03-12 12:01:18 -04001225 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason6702ed42007-08-07 16:15:09 -04001226 split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001227 if (IS_ERR(split_buffer))
1228 return PTR_ERR(split_buffer);
1229
Chris Masone20d96d2007-03-22 12:13:20 -04001230 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -04001231 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Mason9a6f11e2007-03-27 09:06:38 -04001232 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001233 btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001234 btrfs_set_header_generation(&split->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001235 btrfs_set_header_owner(&split->header, root->root_key.objectid);
Chris Mason3eb03142007-04-05 14:28:50 -04001236 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
1237 sizeof(split->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -04001238 mid = (c_nritems + 1) / 2;
Chris Masond6025572007-03-30 14:27:56 -04001239 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
1240 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -04001241 btrfs_set_header_nritems(&split->header, c_nritems - mid);
1242 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001243 ret = 0;
1244
Chris Masond6025572007-03-30 14:27:56 -04001245 btrfs_mark_buffer_dirty(t);
1246 btrfs_mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -04001247 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001248 bh_blocknr(split_buffer), path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001249 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001250 if (wret)
1251 ret = wret;
1252
Chris Mason5de08d72007-02-24 06:24:44 -05001253 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001254 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -04001255 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001256 path->nodes[level] = split_buffer;
1257 path->slots[level + 1] += 1;
1258 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001259 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001260 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001261 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001262}
1263
Chris Mason74123bd2007-02-02 11:05:29 -05001264/*
1265 * how many bytes are required to store the items in a leaf. start
1266 * and nr indicate which items in the leaf to check. This totals up the
1267 * space used both by the item structs and the item data
1268 */
Chris Mason234b63a2007-03-13 10:46:10 -04001269static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001270{
1271 int data_len;
Chris Masond4dbff92007-04-04 14:08:15 -04001272 int nritems = btrfs_header_nritems(&l->header);
1273 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001274
1275 if (!nr)
1276 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001277 data_len = btrfs_item_end(l->items + start);
1278 data_len = data_len - btrfs_item_offset(l->items + end);
1279 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001280 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001281 return data_len;
1282}
1283
Chris Mason74123bd2007-02-02 11:05:29 -05001284/*
Chris Masond4dbff92007-04-04 14:08:15 -04001285 * The space between the end of the leaf items and
1286 * the start of the leaf data. IOW, how much room
1287 * the leaf has left for both items and data
1288 */
1289int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
1290{
1291 int nritems = btrfs_header_nritems(&leaf->header);
1292 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1293}
1294
1295/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001296 * push some data in the path leaf to the right, trying to free up at
1297 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001298 *
1299 * returns 1 if the push failed because the other node didn't have enough
1300 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001301 */
Chris Masone089f052007-03-16 16:20:31 -04001302static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1303 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -05001304{
Chris Masone20d96d2007-03-22 12:13:20 -04001305 struct buffer_head *left_buf = path->nodes[0];
1306 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001307 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001308 struct buffer_head *right_buf;
1309 struct buffer_head *upper;
1310 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -05001311 int slot;
1312 int i;
1313 int free_space;
1314 int push_space = 0;
1315 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001316 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001317 u32 left_nritems;
1318 u32 right_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001319 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001320
1321 slot = path->slots[1];
1322 if (!path->nodes[1]) {
1323 return 1;
1324 }
1325 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001326 upper_node = btrfs_buffer_node(upper);
1327 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001328 return 1;
1329 }
Chris Masone20d96d2007-03-22 12:13:20 -04001330 right_buf = read_tree_block(root,
1331 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
1332 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001333 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001334 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001335 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001336 return 1;
1337 }
Chris Mason02217ed2007-03-02 16:08:05 -05001338 /* cow and double check */
Chris Mason54aa1f42007-06-22 14:16:25 -04001339 ret = btrfs_cow_block(trans, root, right_buf, upper,
1340 slot + 1, &right_buf);
1341 if (ret) {
1342 btrfs_block_release(root, right_buf);
1343 return 1;
1344 }
Chris Masone20d96d2007-03-22 12:13:20 -04001345 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001346 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001347 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001348 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -05001349 return 1;
1350 }
1351
Chris Mason7518a232007-03-12 12:01:18 -04001352 left_nritems = btrfs_header_nritems(&left->header);
Chris Masona429e512007-04-18 16:15:28 -04001353 if (left_nritems == 0) {
1354 btrfs_block_release(root, right_buf);
1355 return 1;
1356 }
1357 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001358 item = left->items + i;
1359 if (path->slots[0] == i)
1360 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001361 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1362 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001363 break;
1364 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001365 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -05001366 }
1367 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001368 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001369 return 1;
1370 }
Chris Masona429e512007-04-18 16:15:28 -04001371 if (push_items == left_nritems)
1372 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001373 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -05001374 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -04001375 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001376 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -05001377 /* make room in the right data area */
Chris Masond6025572007-03-30 14:27:56 -04001378 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1379 leaf_data_end(root, right) - push_space,
1380 btrfs_leaf_data(right) +
1381 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
1382 leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -05001383 /* copy from the left data area */
Chris Masond6025572007-03-30 14:27:56 -04001384 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
1385 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1386 btrfs_leaf_data(left) + leaf_data_end(root, left),
1387 push_space);
1388 btrfs_memmove(root, right, right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -04001389 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001390 /* copy the items from left to right */
Chris Masond6025572007-03-30 14:27:56 -04001391 btrfs_memcpy(root, right, right->items, left->items +
1392 left_nritems - push_items,
1393 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001394
1395 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001396 right_nritems += push_items;
1397 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001398 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001399 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001400 btrfs_set_item_offset(right->items + i, push_space -
1401 btrfs_item_size(right->items + i));
1402 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -05001403 }
Chris Mason7518a232007-03-12 12:01:18 -04001404 left_nritems -= push_items;
1405 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001406
Chris Masond6025572007-03-30 14:27:56 -04001407 btrfs_mark_buffer_dirty(left_buf);
1408 btrfs_mark_buffer_dirty(right_buf);
Chris Masona429e512007-04-18 16:15:28 -04001409
Chris Masond6025572007-03-30 14:27:56 -04001410 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -04001411 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond6025572007-03-30 14:27:56 -04001412 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001413
Chris Mason00ec4c52007-02-24 12:47:20 -05001414 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001415 if (path->slots[0] >= left_nritems) {
1416 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001417 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -05001418 path->nodes[0] = right_buf;
1419 path->slots[1] += 1;
1420 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001421 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001422 }
Chris Mason098f59c2007-05-11 11:33:21 -04001423 if (path->nodes[1])
1424 check_node(root, path, 1);
Chris Mason00ec4c52007-02-24 12:47:20 -05001425 return 0;
1426}
1427/*
Chris Mason74123bd2007-02-02 11:05:29 -05001428 * push some data in the path leaf to the left, trying to free up at
1429 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1430 */
Chris Masone089f052007-03-16 16:20:31 -04001431static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1432 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001433{
Chris Masone20d96d2007-03-22 12:13:20 -04001434 struct buffer_head *right_buf = path->nodes[0];
1435 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
1436 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001437 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001438 int slot;
1439 int i;
1440 int free_space;
1441 int push_space = 0;
1442 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001443 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001444 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001445 int ret = 0;
1446 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001447
1448 slot = path->slots[1];
1449 if (slot == 0) {
1450 return 1;
1451 }
1452 if (!path->nodes[1]) {
1453 return 1;
1454 }
Chris Masone20d96d2007-03-22 12:13:20 -04001455 t = read_tree_block(root,
1456 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
1457 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001458 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001459 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001460 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001461 return 1;
1462 }
Chris Mason02217ed2007-03-02 16:08:05 -05001463
1464 /* cow and double check */
Chris Mason54aa1f42007-06-22 14:16:25 -04001465 ret = btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
1466 if (ret) {
1467 /* we hit -ENOSPC, but it isn't fatal here */
1468 return 1;
1469 }
Chris Masone20d96d2007-03-22 12:13:20 -04001470 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001471 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001472 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001473 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -05001474 return 1;
1475 }
1476
Chris Masona429e512007-04-18 16:15:28 -04001477 if (btrfs_header_nritems(&right->header) == 0) {
1478 btrfs_block_release(root, t);
1479 return 1;
1480 }
1481
1482 for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001483 item = right->items + i;
1484 if (path->slots[0] == i)
1485 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001486 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1487 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001488 break;
1489 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001490 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001491 }
1492 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001493 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001494 return 1;
1495 }
Chris Masona429e512007-04-18 16:15:28 -04001496 if (push_items == btrfs_header_nritems(&right->header))
1497 WARN_ON(1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001498 /* push data from right to left */
Chris Masond6025572007-03-30 14:27:56 -04001499 btrfs_memcpy(root, left, left->items +
1500 btrfs_header_nritems(&left->header),
1501 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001502 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001503 btrfs_item_offset(right->items + push_items -1);
Chris Masond6025572007-03-30 14:27:56 -04001504 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1505 leaf_data_end(root, left) - push_space,
1506 btrfs_leaf_data(right) +
1507 btrfs_item_offset(right->items + push_items - 1),
1508 push_space);
Chris Mason7518a232007-03-12 12:01:18 -04001509 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001510 BUG_ON(old_left_nritems < 0);
1511
Chris Mason0783fcf2007-03-12 20:12:07 -04001512 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001513 u32 ioff = btrfs_item_offset(left->items + i);
1514 btrfs_set_item_offset(left->items + i, ioff -
1515 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001516 btrfs_item_offset(left->items +
1517 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001518 }
Chris Mason7518a232007-03-12 12:01:18 -04001519 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001520
1521 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -04001522 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -04001523 leaf_data_end(root, right);
Chris Masond6025572007-03-30 14:27:56 -04001524 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1525 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1526 btrfs_leaf_data(right) +
1527 leaf_data_end(root, right), push_space);
1528 btrfs_memmove(root, right, right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -04001529 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001530 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001531 btrfs_set_header_nritems(&right->header,
1532 btrfs_header_nritems(&right->header) -
1533 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001534 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001535
Chris Mason7518a232007-03-12 12:01:18 -04001536 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001537 btrfs_set_item_offset(right->items + i, push_space -
1538 btrfs_item_size(right->items + i));
1539 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001540 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001541
Chris Masond6025572007-03-30 14:27:56 -04001542 btrfs_mark_buffer_dirty(t);
1543 btrfs_mark_buffer_dirty(right_buf);
Chris Mason098f59c2007-05-11 11:33:21 -04001544
Chris Masone089f052007-03-16 16:20:31 -04001545 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001546 if (wret)
1547 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001548
1549 /* then fixup the leaf pointer in the path */
1550 if (path->slots[0] < push_items) {
1551 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001552 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001553 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001554 path->slots[1] -= 1;
1555 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001556 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001557 path->slots[0] -= push_items;
1558 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001559 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001560 if (path->nodes[1])
1561 check_node(root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001562 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001563}
1564
Chris Mason74123bd2007-02-02 11:05:29 -05001565/*
1566 * split the path's leaf in two, making sure there is at least data_size
1567 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001568 *
1569 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001570 */
Chris Masone089f052007-03-16 16:20:31 -04001571static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001572 *root, struct btrfs_key *ins_key,
1573 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001574{
Chris Masone20d96d2007-03-22 12:13:20 -04001575 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001576 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001577 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001578 int mid;
1579 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001580 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001581 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001582 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001583 int data_copy_size;
1584 int rt_data_off;
1585 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001586 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001587 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001588 int double_split = 0;
1589 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001590
Chris Mason40689472007-03-17 14:29:23 -04001591 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001592 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001593 if (wret < 0)
1594 return wret;
1595 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001596 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001597 if (wret < 0)
1598 return wret;
1599 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001600 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001601 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001602
1603 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001604 if (btrfs_leaf_free_space(root, l) >=
1605 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001606 return 0;
1607
Chris Mason5c680ed2007-02-22 11:39:13 -05001608 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001609 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001610 if (ret)
1611 return ret;
1612 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001613 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001614 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001615 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001616
Chris Mason6702ed42007-08-07 16:15:09 -04001617 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001618 if (IS_ERR(right_buffer))
1619 return PTR_ERR(right_buffer);
1620
Chris Masone20d96d2007-03-22 12:13:20 -04001621 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001622 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001623 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001624 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001625 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Mason7518a232007-03-12 12:01:18 -04001626 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001627 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1628 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001629 if (mid <= slot) {
1630 if (nritems == 1 ||
1631 leaf_space_used(l, mid, nritems - mid) + space_needed >
1632 BTRFS_LEAF_DATA_SIZE(root)) {
1633 if (slot >= nritems) {
1634 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1635 btrfs_set_header_nritems(&right->header, 0);
1636 wret = insert_ptr(trans, root, path,
1637 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001638 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001639 path->slots[1] + 1, 1);
1640 if (wret)
1641 ret = wret;
1642 btrfs_block_release(root, path->nodes[0]);
1643 path->nodes[0] = right_buffer;
1644 path->slots[0] = 0;
1645 path->slots[1] += 1;
1646 return ret;
1647 }
1648 mid = slot;
1649 double_split = 1;
1650 }
1651 } else {
1652 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1653 BTRFS_LEAF_DATA_SIZE(root)) {
1654 if (slot == 0) {
1655 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1656 btrfs_set_header_nritems(&right->header, 0);
1657 wret = insert_ptr(trans, root, path,
1658 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001659 bh_blocknr(right_buffer),
Chris Mason098f59c2007-05-11 11:33:21 -04001660 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001661 if (wret)
1662 ret = wret;
1663 btrfs_block_release(root, path->nodes[0]);
1664 path->nodes[0] = right_buffer;
1665 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04001666 if (path->slots[1] == 0) {
1667 wret = fixup_low_keys(trans, root,
1668 path, &disk_key, 1);
1669 if (wret)
1670 ret = wret;
1671 }
Chris Masond4dbff92007-04-04 14:08:15 -04001672 return ret;
1673 }
1674 mid = slot;
1675 double_split = 1;
1676 }
1677 }
1678 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Mason123abc82007-03-14 14:14:43 -04001679 data_copy_size = btrfs_item_end(l->items + mid) -
1680 leaf_data_end(root, l);
Chris Masond6025572007-03-30 14:27:56 -04001681 btrfs_memcpy(root, right, right->items, l->items + mid,
1682 (nritems - mid) * sizeof(struct btrfs_item));
1683 btrfs_memcpy(root, right,
1684 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1685 data_copy_size, btrfs_leaf_data(l) +
1686 leaf_data_end(root, l), data_copy_size);
Chris Mason123abc82007-03-14 14:14:43 -04001687 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1688 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001689
Chris Mason0783fcf2007-03-12 20:12:07 -04001690 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001691 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001692 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1693 }
Chris Mason74123bd2007-02-02 11:05:29 -05001694
Chris Mason7518a232007-03-12 12:01:18 -04001695 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001696 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001697 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001698 bh_blocknr(right_buffer), path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001699 if (wret)
1700 ret = wret;
Chris Masond6025572007-03-30 14:27:56 -04001701 btrfs_mark_buffer_dirty(right_buffer);
1702 btrfs_mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001703 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001704 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001705 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001706 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001707 path->slots[0] -= mid;
1708 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001709 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001710 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001711 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001712 check_node(root, path, 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001713
1714 if (!double_split)
1715 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001716 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001717 if (IS_ERR(right_buffer))
1718 return PTR_ERR(right_buffer);
1719
Chris Masond4dbff92007-04-04 14:08:15 -04001720 right = btrfs_buffer_leaf(right_buffer);
1721 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001722 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Masond4dbff92007-04-04 14:08:15 -04001723 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001724 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Masond4dbff92007-04-04 14:08:15 -04001725 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001726 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1727 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001728 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1729 btrfs_set_header_nritems(&right->header, 0);
1730 wret = insert_ptr(trans, root, path,
1731 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001732 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001733 path->slots[1], 1);
1734 if (wret)
1735 ret = wret;
Chris Masona429e512007-04-18 16:15:28 -04001736 if (path->slots[1] == 0) {
1737 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1738 if (wret)
1739 ret = wret;
1740 }
Chris Masond4dbff92007-04-04 14:08:15 -04001741 btrfs_block_release(root, path->nodes[0]);
1742 path->nodes[0] = right_buffer;
1743 path->slots[0] = 0;
1744 check_node(root, path, 1);
1745 check_leaf(root, path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001746 return ret;
1747}
1748
Chris Masonb18c6682007-04-17 13:26:50 -04001749int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1750 struct btrfs_root *root,
1751 struct btrfs_path *path,
1752 u32 new_size)
1753{
1754 int ret = 0;
1755 int slot;
1756 int slot_orig;
1757 struct btrfs_leaf *leaf;
1758 struct buffer_head *leaf_buf;
1759 u32 nritems;
1760 unsigned int data_end;
1761 unsigned int old_data_start;
1762 unsigned int old_size;
1763 unsigned int size_diff;
1764 int i;
1765
1766 slot_orig = path->slots[0];
1767 leaf_buf = path->nodes[0];
1768 leaf = btrfs_buffer_leaf(leaf_buf);
1769
1770 nritems = btrfs_header_nritems(&leaf->header);
1771 data_end = leaf_data_end(root, leaf);
1772
1773 slot = path->slots[0];
1774 old_data_start = btrfs_item_offset(leaf->items + slot);
1775 old_size = btrfs_item_size(leaf->items + slot);
1776 BUG_ON(old_size <= new_size);
1777 size_diff = old_size - new_size;
1778
1779 BUG_ON(slot < 0);
1780 BUG_ON(slot >= nritems);
1781
1782 /*
1783 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1784 */
1785 /* first correct the data pointers */
1786 for (i = slot; i < nritems; i++) {
1787 u32 ioff = btrfs_item_offset(leaf->items + i);
1788 btrfs_set_item_offset(leaf->items + i,
1789 ioff + size_diff);
1790 }
1791 /* shift the data */
Chris Masonb18c6682007-04-17 13:26:50 -04001792 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1793 data_end + size_diff, btrfs_leaf_data(leaf) +
1794 data_end, old_data_start + new_size - data_end);
1795 btrfs_set_item_size(leaf->items + slot, new_size);
1796 btrfs_mark_buffer_dirty(leaf_buf);
1797
1798 ret = 0;
1799 if (btrfs_leaf_free_space(root, leaf) < 0)
1800 BUG();
1801 check_leaf(root, path, 0);
1802 return ret;
1803}
1804
Chris Mason6567e832007-04-16 09:22:45 -04001805int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1806 *root, struct btrfs_path *path, u32 data_size)
1807{
1808 int ret = 0;
1809 int slot;
1810 int slot_orig;
1811 struct btrfs_leaf *leaf;
1812 struct buffer_head *leaf_buf;
1813 u32 nritems;
1814 unsigned int data_end;
1815 unsigned int old_data;
1816 unsigned int old_size;
1817 int i;
1818
1819 slot_orig = path->slots[0];
1820 leaf_buf = path->nodes[0];
1821 leaf = btrfs_buffer_leaf(leaf_buf);
1822
1823 nritems = btrfs_header_nritems(&leaf->header);
1824 data_end = leaf_data_end(root, leaf);
1825
1826 if (btrfs_leaf_free_space(root, leaf) < data_size)
1827 BUG();
1828 slot = path->slots[0];
1829 old_data = btrfs_item_end(leaf->items + slot);
1830
1831 BUG_ON(slot < 0);
1832 BUG_ON(slot >= nritems);
1833
1834 /*
1835 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1836 */
1837 /* first correct the data pointers */
1838 for (i = slot; i < nritems; i++) {
1839 u32 ioff = btrfs_item_offset(leaf->items + i);
1840 btrfs_set_item_offset(leaf->items + i,
1841 ioff - data_size);
1842 }
1843 /* shift the data */
1844 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1845 data_end - data_size, btrfs_leaf_data(leaf) +
1846 data_end, old_data - data_end);
1847 data_end = old_data;
1848 old_size = btrfs_item_size(leaf->items + slot);
1849 btrfs_set_item_size(leaf->items + slot, old_size + data_size);
1850 btrfs_mark_buffer_dirty(leaf_buf);
1851
1852 ret = 0;
1853 if (btrfs_leaf_free_space(root, leaf) < 0)
1854 BUG();
1855 check_leaf(root, path, 0);
1856 return ret;
1857}
1858
Chris Mason74123bd2007-02-02 11:05:29 -05001859/*
1860 * Given a key and some data, insert an item into the tree.
1861 * This does all the path init required, making room in the tree if needed.
1862 */
Chris Masone089f052007-03-16 16:20:31 -04001863int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1864 *root, struct btrfs_path *path, struct btrfs_key
1865 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001866{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001867 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001868 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001869 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001870 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001871 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001872 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001873 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001874 struct btrfs_disk_key disk_key;
1875
1876 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001877
Chris Mason74123bd2007-02-02 11:05:29 -05001878 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001879 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001880 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001881 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001882 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05001883 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001884 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001885 if (ret < 0)
1886 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001887
Chris Mason62e27492007-03-15 12:56:47 -04001888 slot_orig = path->slots[0];
1889 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001890 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001891
Chris Mason7518a232007-03-12 12:01:18 -04001892 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001893 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001894
Chris Mason123abc82007-03-14 14:14:43 -04001895 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04001896 sizeof(struct btrfs_item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001897 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04001898 }
Chris Mason62e27492007-03-15 12:56:47 -04001899 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001900 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001901 if (slot != nritems) {
1902 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001903 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001904
1905 /*
1906 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1907 */
1908 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001909 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001910 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001911 btrfs_set_item_offset(leaf->items + i,
1912 ioff - data_size);
1913 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001914
1915 /* shift the items */
Chris Masond6025572007-03-30 14:27:56 -04001916 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1917 leaf->items + slot,
1918 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001919
1920 /* shift the data */
Chris Masond6025572007-03-30 14:27:56 -04001921 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1922 data_end - data_size, btrfs_leaf_data(leaf) +
1923 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001924 data_end = old_data;
1925 }
Chris Mason62e27492007-03-15 12:56:47 -04001926 /* setup the item for the new data */
Chris Masond6025572007-03-30 14:27:56 -04001927 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1928 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001929 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1930 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001931 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001932 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001933
1934 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001935 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001936 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001937
Chris Mason123abc82007-03-14 14:14:43 -04001938 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001939 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001940 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001941out:
Chris Mason62e27492007-03-15 12:56:47 -04001942 return ret;
1943}
1944
1945/*
1946 * Given a key and some data, insert an item into the tree.
1947 * This does all the path init required, making room in the tree if needed.
1948 */
Chris Masone089f052007-03-16 16:20:31 -04001949int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1950 *root, struct btrfs_key *cpu_key, void *data, u32
1951 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001952{
1953 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001954 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -04001955 u8 *ptr;
1956
Chris Mason2c90e5d2007-04-02 10:50:19 -04001957 path = btrfs_alloc_path();
1958 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001959 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001960 if (!ret) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001961 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1962 path->slots[0], u8);
1963 btrfs_memcpy(root, path->nodes[0]->b_data,
Chris Masond6025572007-03-30 14:27:56 -04001964 ptr, data, data_size);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001965 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001966 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04001967 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001968 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001969}
1970
Chris Mason74123bd2007-02-02 11:05:29 -05001971/*
Chris Mason5de08d72007-02-24 06:24:44 -05001972 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001973 *
1974 * If the delete empties a node, the node is removed from the tree,
1975 * continuing all the way the root if required. The root is converted into
1976 * a leaf if all the nodes are emptied.
1977 */
Chris Masone089f052007-03-16 16:20:31 -04001978static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1979 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001980{
Chris Mason234b63a2007-03-13 10:46:10 -04001981 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001982 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001983 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001984 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001985 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001986
Chris Masone20d96d2007-03-22 12:13:20 -04001987 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001988 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001989 if (slot != nritems -1) {
Chris Masond6025572007-03-30 14:27:56 -04001990 btrfs_memmove(root, node, node->ptrs + slot,
1991 node->ptrs + slot + 1,
1992 sizeof(struct btrfs_key_ptr) *
1993 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001994 }
Chris Mason7518a232007-03-12 12:01:18 -04001995 nritems--;
1996 btrfs_set_header_nritems(&node->header, nritems);
1997 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001998 struct btrfs_header *header = btrfs_buffer_header(root->node);
1999 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002000 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04002001 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002002 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04002003 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04002004 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002005 if (wret)
2006 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002007 }
Chris Masond6025572007-03-30 14:27:56 -04002008 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002009 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002010}
2011
Chris Mason74123bd2007-02-02 11:05:29 -05002012/*
2013 * delete the item at the leaf level in path. If that empties
2014 * the leaf, remove it from the tree
2015 */
Chris Masone089f052007-03-16 16:20:31 -04002016int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2017 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002018{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002019 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04002020 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04002021 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002022 int doff;
2023 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002024 int ret = 0;
2025 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002026 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002027
Chris Masoneb60cea2007-02-02 09:18:22 -05002028 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04002029 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05002030 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04002031 doff = btrfs_item_offset(leaf->items + slot);
2032 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04002033 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002034
Chris Mason7518a232007-03-12 12:01:18 -04002035 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002036 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002037 int data_end = leaf_data_end(root, leaf);
Chris Masond6025572007-03-30 14:27:56 -04002038 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
2039 data_end + dsize,
2040 btrfs_leaf_data(leaf) + data_end,
2041 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002042 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04002043 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04002044 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
2045 }
Chris Masond6025572007-03-30 14:27:56 -04002046 btrfs_memmove(root, leaf, leaf->items + slot,
2047 leaf->items + slot + 1,
2048 sizeof(struct btrfs_item) *
2049 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002050 }
Chris Mason7518a232007-03-12 12:01:18 -04002051 btrfs_set_header_nritems(&leaf->header, nritems - 1);
2052 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05002053 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002054 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05002055 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04002056 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002057 } else {
Chris Masone089f052007-03-16 16:20:31 -04002058 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04002059 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002060 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002061 if (wret)
2062 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002063 wret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04002064 bh_blocknr(leaf_buf), 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002065 if (wret)
2066 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002067 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002068 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002069 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002070 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04002071 wret = fixup_low_keys(trans, root, path,
2072 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002073 if (wret)
2074 ret = wret;
2075 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002076
Chris Mason74123bd2007-02-02 11:05:29 -05002077 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04002078 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002079 /* push_leaf_left fixes the path.
2080 * make sure the path still points to our leaf
2081 * for possible call to del_ptr below
2082 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002083 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04002084 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002085 wret = push_leaf_left(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002086 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002087 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05002088 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04002089 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04002090 wret = push_leaf_right(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002091 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002092 ret = wret;
2093 }
Chris Mason7518a232007-03-12 12:01:18 -04002094 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -04002095 u64 blocknr = bh_blocknr(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002096 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04002097 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002098 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002099 if (wret)
2100 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04002101 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002102 wret = btrfs_free_extent(trans, root, blocknr,
2103 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002104 if (wret)
2105 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002106 } else {
Chris Masond6025572007-03-30 14:27:56 -04002107 btrfs_mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04002108 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002109 }
Chris Masond5719762007-03-23 10:01:08 -04002110 } else {
Chris Masond6025572007-03-30 14:27:56 -04002111 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002112 }
2113 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002114 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002115}
2116
Chris Mason97571fd2007-02-24 13:39:08 -05002117/*
2118 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002119 * returns 0 if it found something or 1 if there are no greater leaves.
2120 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002121 */
Chris Mason234b63a2007-03-13 10:46:10 -04002122int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002123{
2124 int slot;
2125 int level = 1;
2126 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04002127 struct buffer_head *c;
2128 struct btrfs_node *c_node;
2129 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002130
Chris Mason234b63a2007-03-13 10:46:10 -04002131 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002132 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002133 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002134 slot = path->slots[level] + 1;
2135 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04002136 c_node = btrfs_buffer_node(c);
2137 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002138 level++;
2139 continue;
2140 }
Chris Masone20d96d2007-03-22 12:13:20 -04002141 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002142 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04002143 btrfs_block_release(root, next);
Chris Mason6702ed42007-08-07 16:15:09 -04002144 if (path->reada)
2145 reada_for_search(root, path, level, slot);
Chris Masond97e63b2007-02-20 16:40:44 -05002146 next = read_tree_block(root, blocknr);
2147 break;
2148 }
2149 path->slots[level] = slot;
2150 while(1) {
2151 level--;
2152 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04002153 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05002154 path->nodes[level] = next;
2155 path->slots[level] = 0;
2156 if (!level)
2157 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002158 if (path->reada)
Yan32020612007-08-27 15:17:54 -04002159 reada_for_search(root, path, level, 0);
Chris Mason1d4f8a02007-03-13 09:28:32 -04002160 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04002161 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05002162 }
2163 return 0;
2164}