blob: c7e47e77723ff413c5350a7949fdeb02500b3a4b [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);
46 if (path)
47 btrfs_init_path(path);
48 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040049}
50
51void btrfs_free_path(struct btrfs_path *p)
52{
Chris Masondf24a2b2007-04-04 09:36:31 -040053 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040054 kmem_cache_free(btrfs_path_cachep, p);
55}
56
Chris Mason234b63a2007-03-13 10:46:10 -040057void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050058{
59 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040060 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050061 if (!p->nodes[i])
62 break;
Chris Mason234b63a2007-03-13 10:46:10 -040063 btrfs_block_release(root, p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050064 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050065 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050066}
67
Chris Mason6702ed42007-08-07 16:15:09 -040068static int __btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
69 *root, struct buffer_head *buf, struct buffer_head
70 *parent, int parent_slot, struct buffer_head
71 **cow_ret, u64 search_start, u64 empty_size)
72{
73 struct buffer_head *cow;
74 struct btrfs_node *cow_node;
75 int ret = 0;
76 int different_trans = 0;
77
78 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
79 WARN_ON(!buffer_uptodate(buf));
80 cow = btrfs_alloc_free_block(trans, root, search_start, empty_size);
81 if (IS_ERR(cow))
82 return PTR_ERR(cow);
83
84 cow_node = btrfs_buffer_node(cow);
85 if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
86 WARN_ON(1);
87
88 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
89 btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow));
90 btrfs_set_header_generation(&cow_node->header, trans->transid);
91 btrfs_set_header_owner(&cow_node->header, root->root_key.objectid);
92
93 WARN_ON(btrfs_header_generation(btrfs_buffer_header(buf)) >
94 trans->transid);
95 if (btrfs_header_generation(btrfs_buffer_header(buf)) !=
96 trans->transid) {
97 different_trans = 1;
98 ret = btrfs_inc_ref(trans, root, buf);
99 if (ret)
100 return ret;
101 } else {
102 WARN_ON(!root->ref_cows);
103 clean_tree_block(trans, root, buf);
104 }
105
106 if (buf == root->node) {
107 root->node = cow;
108 get_bh(cow);
109 if (buf != root->commit_root) {
110 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
111 }
112 btrfs_block_release(root, buf);
113 } else {
114 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
115 bh_blocknr(cow));
116 btrfs_mark_buffer_dirty(parent);
117 WARN_ON(btrfs_header_generation(btrfs_buffer_header(parent)) !=
118 trans->transid);
119 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
120 }
121 btrfs_block_release(root, buf);
122 btrfs_mark_buffer_dirty(cow);
123 *cow_ret = cow;
124 return 0;
125}
126
127int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400128 *root, struct buffer_head *buf, struct buffer_head
129 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -0400130 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500131{
Chris Mason6702ed42007-08-07 16:15:09 -0400132 u64 search_start;
Chris Masonccd467d2007-06-28 15:57:36 -0400133 if (trans->transaction != root->fs_info->running_transaction) {
134 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
135 root->fs_info->running_transaction->transid);
136 WARN_ON(1);
137 }
138 if (trans->transid != root->fs_info->generation) {
139 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
140 root->fs_info->generation);
141 WARN_ON(1);
142 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400143 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
144 trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -0500145 *cow_ret = buf;
146 return 0;
147 }
Chris Mason6702ed42007-08-07 16:15:09 -0400148
149 search_start = bh_blocknr(buf) & ~((u64)65535);
150 return __btrfs_cow_block(trans, root, buf, parent,
151 parent_slot, cow_ret, search_start, 0);
152}
153
154static int close_blocks(u64 blocknr, u64 other)
155{
156 if (blocknr < other && other - blocknr < 8)
157 return 1;
158 if (blocknr > other && blocknr - other < 8)
159 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500160 return 0;
161}
162
Chris Mason6702ed42007-08-07 16:15:09 -0400163int btrfs_realloc_node(struct btrfs_trans_handle *trans,
164 struct btrfs_root *root, struct buffer_head *parent,
165 int cache_only)
166{
167 struct btrfs_node *parent_node;
168 struct buffer_head *cur_bh;
169 struct buffer_head *tmp_bh;
170 u64 blocknr;
171 u64 search_start = 0;
172 u64 other;
173 u32 parent_nritems;
174 int start_slot;
175 int end_slot;
176 int i;
177 int err = 0;
178
179 if (trans->transaction != root->fs_info->running_transaction) {
180 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
181 root->fs_info->running_transaction->transid);
182 WARN_ON(1);
183 }
184 if (trans->transid != root->fs_info->generation) {
185 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
186 root->fs_info->generation);
187 WARN_ON(1);
188 }
189 parent_node = btrfs_buffer_node(parent);
190 parent_nritems = btrfs_header_nritems(&parent_node->header);
191
192 start_slot = 0;
193 end_slot = parent_nritems;
194
195 if (parent_nritems == 1)
196 return 0;
197
198 for (i = start_slot; i < end_slot; i++) {
199 int close = 1;
200 blocknr = btrfs_node_blockptr(parent_node, i);
201 if (i > 0) {
202 other = btrfs_node_blockptr(parent_node, i - 1);
203 close = close_blocks(blocknr, other);
204 }
205 if (close && i < end_slot - 1) {
206 other = btrfs_node_blockptr(parent_node, i + 1);
207 close = close_blocks(blocknr, other);
208 }
209 if (close)
210 continue;
211
212 cur_bh = btrfs_find_tree_block(root, blocknr);
213 if (!cur_bh || !buffer_uptodate(cur_bh) ||
214 buffer_locked(cur_bh)) {
215 if (cache_only) {
216 brelse(cur_bh);
217 continue;
218 }
219 brelse(cur_bh);
220 cur_bh = read_tree_block(root, blocknr);
221 }
222 if (search_start == 0) {
223 search_start = bh_blocknr(cur_bh) & ~((u64)65535);
224 }
225 err = __btrfs_cow_block(trans, root, cur_bh, parent, i,
226 &tmp_bh, search_start,
227 min(8, end_slot - i));
228 if (err)
229 break;
230 search_start = bh_blocknr(tmp_bh);
231 brelse(tmp_bh);
232 }
233 return err;
234}
235
Chris Mason74123bd2007-02-02 11:05:29 -0500236/*
237 * The leaf data grows from end-to-front in the node.
238 * this returns the address of the start of the last item,
239 * which is the stop of the leaf data stack
240 */
Chris Mason123abc82007-03-14 14:14:43 -0400241static inline unsigned int leaf_data_end(struct btrfs_root *root,
242 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500243{
Chris Mason7518a232007-03-12 12:01:18 -0400244 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500245 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400246 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -0400247 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500248}
249
Chris Mason74123bd2007-02-02 11:05:29 -0500250/*
Chris Mason74123bd2007-02-02 11:05:29 -0500251 * compare two keys in a memcmp fashion
252 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400253static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500254{
Chris Masone2fa7222007-03-12 16:22:34 -0400255 struct btrfs_key k1;
256
257 btrfs_disk_key_to_cpu(&k1, disk);
258
259 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500260 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400261 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500262 return -1;
Chris Masonf254e522007-03-29 15:15:27 -0400263 if (k1.flags > k2->flags)
264 return 1;
265 if (k1.flags < k2->flags)
266 return -1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400267 if (k1.offset > k2->offset)
268 return 1;
269 if (k1.offset < k2->offset)
270 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500271 return 0;
272}
Chris Mason74123bd2007-02-02 11:05:29 -0500273
Chris Mason123abc82007-03-14 14:14:43 -0400274static int check_node(struct btrfs_root *root, struct btrfs_path *path,
275 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500276{
Chris Mason234b63a2007-03-13 10:46:10 -0400277 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400278 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500279 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400280 int slot;
281 struct btrfs_key cpukey;
Chris Mason7518a232007-03-12 12:01:18 -0400282 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500283
284 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400285 parent = btrfs_buffer_node(path->nodes[level + 1]);
Aneesha1f396302007-07-11 10:03:27 -0400286
Chris Mason8d7be552007-05-10 11:24:42 -0400287 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400288 BUG_ON(nritems == 0);
289 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400290 struct btrfs_disk_key *parent_key;
Aneesha1f396302007-07-11 10:03:27 -0400291
292 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400293 parent_key = &parent->ptrs[parent_slot].key;
294 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400295 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400296 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400297 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500298 }
Chris Mason123abc82007-03-14 14:14:43 -0400299 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400300 if (slot != 0) {
301 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key);
302 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0);
303 }
304 if (slot < nritems - 1) {
305 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key);
306 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500307 }
308 return 0;
309}
310
Chris Mason123abc82007-03-14 14:14:43 -0400311static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
312 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500313{
Chris Masone20d96d2007-03-22 12:13:20 -0400314 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400315 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500316 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400317 int slot = path->slots[0];
318 struct btrfs_key cpukey;
319
Chris Mason7518a232007-03-12 12:01:18 -0400320 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500321
322 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400323 parent = btrfs_buffer_node(path->nodes[level + 1]);
Aneesha1f396302007-07-11 10:03:27 -0400324
Chris Mason123abc82007-03-14 14:14:43 -0400325 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400326
327 if (nritems == 0)
328 return 0;
329
330 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400331 struct btrfs_disk_key *parent_key;
Aneesha1f396302007-07-11 10:03:27 -0400332
333 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400334 parent_key = &parent->ptrs[parent_slot].key;
Chris Mason6702ed42007-08-07 16:15:09 -0400335
Chris Masonaa5d6be2007-02-28 16:35:06 -0500336 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400337 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400338 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400339 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500340 }
Chris Mason8d7be552007-05-10 11:24:42 -0400341 if (slot != 0) {
342 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key);
343 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0);
344 BUG_ON(btrfs_item_offset(leaf->items + slot - 1) !=
345 btrfs_item_end(leaf->items + slot));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500346 }
Chris Mason8d7be552007-05-10 11:24:42 -0400347 if (slot < nritems - 1) {
348 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key);
349 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0);
350 BUG_ON(btrfs_item_offset(leaf->items + slot) !=
351 btrfs_item_end(leaf->items + slot + 1));
352 }
353 BUG_ON(btrfs_item_offset(leaf->items) +
354 btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500355 return 0;
356}
357
Chris Mason123abc82007-03-14 14:14:43 -0400358static int check_block(struct btrfs_root *root, struct btrfs_path *path,
359 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500360{
Chris Mason3eb03142007-04-05 14:28:50 -0400361 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
362 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
363 sizeof(node->header.fsid)))
364 BUG();
Chris Masonaa5d6be2007-02-28 16:35:06 -0500365 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400366 return check_leaf(root, path, level);
367 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500368}
369
Chris Mason74123bd2007-02-02 11:05:29 -0500370/*
371 * search for key in the array p. items p are item_size apart
372 * and there are 'max' items in p
373 * the slot in the array is returned via slot, and it points to
374 * the place where you would insert key if it is not found in
375 * the array.
376 *
377 * slot may point to max if the key is bigger than all of the keys
378 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400379static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500380 int max, int *slot)
381{
382 int low = 0;
383 int high = max;
384 int mid;
385 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400386 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500387
388 while(low < high) {
389 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400390 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500391 ret = comp_keys(tmp, key);
392
393 if (ret < 0)
394 low = mid + 1;
395 else if (ret > 0)
396 high = mid;
397 else {
398 *slot = mid;
399 return 0;
400 }
401 }
402 *slot = low;
403 return 1;
404}
405
Chris Mason97571fd2007-02-24 13:39:08 -0500406/*
407 * simple bin_search frontend that does the right thing for
408 * leaves vs nodes
409 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400410static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500411{
Chris Mason7518a232007-03-12 12:01:18 -0400412 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400413 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400414 return generic_bin_search((void *)l->items,
415 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400416 key, btrfs_header_nritems(&c->header),
417 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500418 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400419 return generic_bin_search((void *)c->ptrs,
420 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400421 key, btrfs_header_nritems(&c->header),
422 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500423 }
424 return -1;
425}
426
Chris Masone20d96d2007-03-22 12:13:20 -0400427static struct buffer_head *read_node_slot(struct btrfs_root *root,
428 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500429 int slot)
430{
Chris Masone20d96d2007-03-22 12:13:20 -0400431 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500432 if (slot < 0)
433 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400434 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500435 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400436 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500437}
438
Chris Masone089f052007-03-16 16:20:31 -0400439static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
440 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500441{
Chris Masone20d96d2007-03-22 12:13:20 -0400442 struct buffer_head *right_buf;
443 struct buffer_head *mid_buf;
444 struct buffer_head *left_buf;
445 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400446 struct btrfs_node *right = NULL;
447 struct btrfs_node *mid;
448 struct btrfs_node *left = NULL;
449 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500450 int ret = 0;
451 int wret;
452 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500453 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400454 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500455 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500456
457 if (level == 0)
458 return 0;
459
460 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400461 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400462 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500463
Chris Mason234b63a2007-03-13 10:46:10 -0400464 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500465 parent_buf = path->nodes[level + 1];
466 pslot = path->slots[level + 1];
467
Chris Mason40689472007-03-17 14:29:23 -0400468 /*
469 * deal with the case where there is only one pointer in the root
470 * by promoting the node below to a root
471 */
Chris Masonbb803952007-03-01 12:04:21 -0500472 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400473 struct buffer_head *child;
Chris Mason7eccb902007-04-11 15:53:25 -0400474 u64 blocknr = bh_blocknr(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500475
Chris Mason7518a232007-03-12 12:01:18 -0400476 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500477 return 0;
478
479 /* promote the child to a root */
480 child = read_node_slot(root, mid_buf, 0);
481 BUG_ON(!child);
482 root->node = child;
483 path->nodes[level] = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400484 clean_tree_block(trans, root, mid_buf);
485 wait_on_buffer(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500486 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400487 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500488 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400489 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400490 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500491 }
Chris Masone20d96d2007-03-22 12:13:20 -0400492 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500493
Chris Mason123abc82007-03-14 14:14:43 -0400494 if (btrfs_header_nritems(&mid->header) >
495 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500496 return 0;
497
Chris Mason54aa1f42007-06-22 14:16:25 -0400498 if (btrfs_header_nritems(&mid->header) < 2)
499 err_on_enospc = 1;
500
Chris Masonbb803952007-03-01 12:04:21 -0500501 left_buf = read_node_slot(root, parent_buf, pslot - 1);
502 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500503
504 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500505 if (left_buf) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400506 wret = btrfs_cow_block(trans, root, left_buf,
507 parent_buf, pslot - 1, &left_buf);
508 if (wret) {
509 ret = wret;
510 goto enospc;
511 }
Chris Masone20d96d2007-03-22 12:13:20 -0400512 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400513 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400514 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500515 if (wret < 0)
516 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400517 if (btrfs_header_nritems(&mid->header) < 2)
518 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500519 }
Chris Mason79f95c82007-03-01 15:16:26 -0500520
521 /*
522 * then try to empty the right most buffer into the middle
523 */
Chris Masonbb803952007-03-01 12:04:21 -0500524 if (right_buf) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400525 wret = btrfs_cow_block(trans, root, right_buf,
526 parent_buf, pslot + 1, &right_buf);
527 if (wret) {
528 ret = wret;
529 goto enospc;
530 }
531
Chris Masone20d96d2007-03-22 12:13:20 -0400532 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400533 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400534 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500535 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400536 if (btrfs_header_nritems(&right->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -0400537 u64 blocknr = bh_blocknr(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400538 clean_tree_block(trans, root, right_buf);
Chris Masond6025572007-03-30 14:27:56 -0400539 wait_on_buffer(right_buf);
540 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500541 right_buf = NULL;
542 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400543 wret = del_ptr(trans, root, path, level + 1, pslot +
544 1);
Chris Masonbb803952007-03-01 12:04:21 -0500545 if (wret)
546 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400547 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500548 if (wret)
549 ret = wret;
550 } else {
Chris Masond6025572007-03-30 14:27:56 -0400551 btrfs_memcpy(root, parent,
552 &parent->ptrs[pslot + 1].key,
553 &right->ptrs[0].key,
554 sizeof(struct btrfs_disk_key));
555 btrfs_mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500556 }
557 }
Chris Mason7518a232007-03-12 12:01:18 -0400558 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500559 /*
560 * we're not allowed to leave a node with one item in the
561 * tree during a delete. A deletion from lower in the tree
562 * could try to delete the only pointer in this node.
563 * So, pull some keys from the left.
564 * There has to be a left pointer at this point because
565 * otherwise we would have pulled some pointers from the
566 * right
567 */
568 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400569 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400570 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500571 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400572 goto enospc;
573 }
Chris Mason79f95c82007-03-01 15:16:26 -0500574 BUG_ON(wret == 1);
575 }
Chris Mason7518a232007-03-12 12:01:18 -0400576 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500577 /* we've managed to empty the middle node, drop it */
Chris Mason7eccb902007-04-11 15:53:25 -0400578 u64 blocknr = bh_blocknr(mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400579 clean_tree_block(trans, root, mid_buf);
Chris Masond6025572007-03-30 14:27:56 -0400580 wait_on_buffer(mid_buf);
581 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500582 mid_buf = NULL;
583 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400584 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500585 if (wret)
586 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400587 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500588 if (wret)
589 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500590 } else {
591 /* update the parent key to reflect our changes */
Chris Masond6025572007-03-30 14:27:56 -0400592 btrfs_memcpy(root, parent,
593 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
594 sizeof(struct btrfs_disk_key));
595 btrfs_mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500596 }
Chris Masonbb803952007-03-01 12:04:21 -0500597
Chris Mason79f95c82007-03-01 15:16:26 -0500598 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500599 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400600 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400601 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500602 path->nodes[level] = left_buf;
603 path->slots[level + 1] -= 1;
604 path->slots[level] = orig_slot;
605 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400606 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500607 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400608 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500609 path->slots[level] = orig_slot;
610 }
611 }
Chris Mason79f95c82007-03-01 15:16:26 -0500612 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400613 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400614 if (orig_ptr !=
615 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
616 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500617 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400618enospc:
Chris Masonbb803952007-03-01 12:04:21 -0500619 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400620 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500621 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400622 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500623 return ret;
624}
625
Chris Masone66f7092007-04-20 13:16:02 -0400626/* returns zero if the push worked, non-zero otherwise */
627static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
628 struct btrfs_root *root,
629 struct btrfs_path *path, int level)
630{
631 struct buffer_head *right_buf;
632 struct buffer_head *mid_buf;
633 struct buffer_head *left_buf;
634 struct buffer_head *parent_buf = NULL;
635 struct btrfs_node *right = NULL;
636 struct btrfs_node *mid;
637 struct btrfs_node *left = NULL;
638 struct btrfs_node *parent = NULL;
639 int ret = 0;
640 int wret;
641 int pslot;
642 int orig_slot = path->slots[level];
643 u64 orig_ptr;
644
645 if (level == 0)
646 return 1;
647
648 mid_buf = path->nodes[level];
649 mid = btrfs_buffer_node(mid_buf);
650 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
651
652 if (level < BTRFS_MAX_LEVEL - 1)
653 parent_buf = path->nodes[level + 1];
654 pslot = path->slots[level + 1];
655
656 if (!parent_buf)
657 return 1;
658 parent = btrfs_buffer_node(parent_buf);
659
660 left_buf = read_node_slot(root, parent_buf, pslot - 1);
661
662 /* first, try to make some room in the middle buffer */
663 if (left_buf) {
664 u32 left_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400665 left = btrfs_buffer_node(left_buf);
666 left_nr = btrfs_header_nritems(&left->header);
Chris Mason33ade1f2007-04-20 13:48:57 -0400667 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
668 wret = 1;
669 } else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400670 ret = btrfs_cow_block(trans, root, left_buf, parent_buf,
671 pslot - 1, &left_buf);
672 if (ret)
673 wret = 1;
674 else {
675 left = btrfs_buffer_node(left_buf);
676 wret = push_node_left(trans, root,
677 left_buf, mid_buf);
678 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400679 }
Chris Masone66f7092007-04-20 13:16:02 -0400680 if (wret < 0)
681 ret = wret;
682 if (wret == 0) {
683 orig_slot += left_nr;
684 btrfs_memcpy(root, parent,
685 &parent->ptrs[pslot].key,
686 &mid->ptrs[0].key,
687 sizeof(struct btrfs_disk_key));
688 btrfs_mark_buffer_dirty(parent_buf);
689 if (btrfs_header_nritems(&left->header) > orig_slot) {
690 path->nodes[level] = left_buf;
691 path->slots[level + 1] -= 1;
692 path->slots[level] = orig_slot;
693 btrfs_block_release(root, mid_buf);
694 } else {
695 orig_slot -=
696 btrfs_header_nritems(&left->header);
697 path->slots[level] = orig_slot;
698 btrfs_block_release(root, left_buf);
699 }
700 check_node(root, path, level);
701 return 0;
702 }
703 btrfs_block_release(root, left_buf);
704 }
705 right_buf = read_node_slot(root, parent_buf, pslot + 1);
706
707 /*
708 * then try to empty the right most buffer into the middle
709 */
710 if (right_buf) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400711 u32 right_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400712 right = btrfs_buffer_node(right_buf);
Chris Mason33ade1f2007-04-20 13:48:57 -0400713 right_nr = btrfs_header_nritems(&right->header);
714 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
715 wret = 1;
716 } else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400717 ret = btrfs_cow_block(trans, root, right_buf,
718 parent_buf, pslot + 1,
719 &right_buf);
720 if (ret)
721 wret = 1;
722 else {
723 right = btrfs_buffer_node(right_buf);
724 wret = balance_node_right(trans, root,
725 right_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 btrfs_memcpy(root, parent,
732 &parent->ptrs[pslot + 1].key,
733 &right->ptrs[0].key,
734 sizeof(struct btrfs_disk_key));
735 btrfs_mark_buffer_dirty(parent_buf);
736 if (btrfs_header_nritems(&mid->header) <= orig_slot) {
737 path->nodes[level] = right_buf;
738 path->slots[level + 1] += 1;
739 path->slots[level] = orig_slot -
740 btrfs_header_nritems(&mid->header);
741 btrfs_block_release(root, mid_buf);
742 } else {
743 btrfs_block_release(root, right_buf);
744 }
745 check_node(root, path, level);
746 return 0;
747 }
748 btrfs_block_release(root, right_buf);
749 }
750 check_node(root, path, level);
751 return 1;
752}
753
Chris Mason74123bd2007-02-02 11:05:29 -0500754/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400755 * readahead one full node of leaves
756 */
757static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason6702ed42007-08-07 16:15:09 -0400758 int level, int slot)
Chris Mason3c69fae2007-08-07 15:52:22 -0400759{
760 struct btrfs_node *node;
761 int i;
762 u32 nritems;
763 u64 item_objectid;
764 u64 blocknr;
765 u64 search;
766 u64 cluster_start;
767 int ret;
768 int nread = 0;
769 int direction = path->reada;
770 struct radix_tree_root found;
771 unsigned long gang[8];
772 struct buffer_head *bh;
773
Chris Mason6702ed42007-08-07 16:15:09 -0400774 if (level == 0)
Chris Mason3c69fae2007-08-07 15:52:22 -0400775 return;
776
Chris Mason6702ed42007-08-07 16:15:09 -0400777 if (!path->nodes[level])
778 return;
779
780 node = btrfs_buffer_node(path->nodes[level]);
Chris Mason3c69fae2007-08-07 15:52:22 -0400781 search = btrfs_node_blockptr(node, slot);
782 bh = btrfs_find_tree_block(root, search);
783 if (bh) {
784 brelse(bh);
785 return;
786 }
787
788 init_bit_radix(&found);
789 nritems = btrfs_header_nritems(&node->header);
790 for (i = slot; i < nritems; i++) {
791 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
792 blocknr = btrfs_node_blockptr(node, i);
793 set_radix_bit(&found, blocknr);
794 }
795 if (direction > 0) {
796 cluster_start = search - 4;
797 if (cluster_start > search)
798 cluster_start = 0;
799 } else
800 cluster_start = search + 4;
801 while(1) {
802 ret = find_first_radix_bit(&found, gang, 0, ARRAY_SIZE(gang));
803 if (!ret)
804 break;
805 for (i = 0; i < ret; i++) {
806 blocknr = gang[i];
807 clear_radix_bit(&found, blocknr);
Chris Mason6702ed42007-08-07 16:15:09 -0400808 if (nread > 32)
Chris Mason3c69fae2007-08-07 15:52:22 -0400809 continue;
810 if (direction > 0 && cluster_start <= blocknr &&
811 cluster_start + 8 > blocknr) {
812 cluster_start = blocknr;
813 readahead_tree_block(root, blocknr);
814 nread++;
815 } else if (direction < 0 && cluster_start >= blocknr &&
816 blocknr + 8 > cluster_start) {
817 cluster_start = blocknr;
818 readahead_tree_block(root, blocknr);
819 nread++;
820 }
821 }
822 }
823}
824/*
Chris Mason74123bd2007-02-02 11:05:29 -0500825 * look for key in the tree. path is filled in with nodes along the way
826 * if key is found, we return zero and you can find the item in the leaf
827 * level of the path (level 0)
828 *
829 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500830 * be inserted, and 1 is returned. If there are other errors during the
831 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500832 *
833 * if ins_len > 0, nodes and leaves will be split as we walk down the
834 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
835 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500836 */
Chris Masone089f052007-03-16 16:20:31 -0400837int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
838 *root, struct btrfs_key *key, struct btrfs_path *p, int
839 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500840{
Chris Masone20d96d2007-03-22 12:13:20 -0400841 struct buffer_head *b;
842 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400843 struct btrfs_node *c;
Chris Mason3c69fae2007-08-07 15:52:22 -0400844 u64 blocknr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500845 int slot;
846 int ret;
847 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -0400848 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -0400849 u8 lowest_level = 0;
850
Chris Mason6702ed42007-08-07 16:15:09 -0400851 lowest_level = p->lowest_level;
852 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400853 WARN_ON(p->nodes[0] != NULL);
854 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500855again:
856 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400857 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500858 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400859 c = btrfs_buffer_node(b);
860 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500861 if (cow) {
862 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400863 wret = btrfs_cow_block(trans, root, b,
864 p->nodes[level + 1],
865 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400866 &cow_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400867 if (wret) {
868 btrfs_block_release(root, cow_buf);
869 return wret;
870 }
Chris Mason02217ed2007-03-02 16:08:05 -0500871 b = cow_buf;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400872 c = btrfs_buffer_node(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500873 }
874 BUG_ON(!cow && ins_len);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400875 if (level != btrfs_header_level(&c->header))
876 WARN_ON(1);
877 level = btrfs_header_level(&c->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500878 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400879 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500880 if (ret)
881 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500882 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400883 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500884 if (ret && slot > 0)
885 slot -= 1;
886 p->slots[level] = slot;
Chris Masond4dbff92007-04-04 14:08:15 -0400887 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
888 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -0400889 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500890 BUG_ON(sret > 0);
891 if (sret)
892 return sret;
893 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400894 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500895 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500896 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400897 int sret = balance_level(trans, root, p,
898 level);
Chris Masonbb803952007-03-01 12:04:21 -0500899 if (sret)
900 return sret;
901 b = p->nodes[level];
902 if (!b)
903 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400904 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500905 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400906 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500907 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400908 /* this is only true while dropping a snapshot */
909 if (level == lowest_level)
910 break;
Chris Mason3c69fae2007-08-07 15:52:22 -0400911 blocknr = btrfs_node_blockptr(c, slot);
Chris Mason6702ed42007-08-07 16:15:09 -0400912 if (should_reada)
913 reada_for_search(root, p, level, slot);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400914 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Mason3c69fae2007-08-07 15:52:22 -0400915
Chris Masonbe0e5c02007-01-26 15:51:26 -0500916 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400917 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500918 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400919 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400920 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -0400921 int sret = split_leaf(trans, root, key,
922 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500923 BUG_ON(sret > 0);
924 if (sret)
925 return sret;
926 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500927 return ret;
928 }
929 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500930 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500931}
932
Chris Mason74123bd2007-02-02 11:05:29 -0500933/*
934 * adjust the pointers going up the tree, starting at level
935 * making sure the right key of each node is points to 'key'.
936 * This is used after shifting pointers to the left, so it stops
937 * fixing up pointers when a given leaf/node is not in slot 0 of the
938 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500939 *
940 * If this fails to write a tree block, it returns -1, but continues
941 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500942 */
Chris Masone089f052007-03-16 16:20:31 -0400943static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
944 *root, struct btrfs_path *path, struct btrfs_disk_key
945 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500946{
947 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500948 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400949 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
950 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500951 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500952 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500953 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400954 t = btrfs_buffer_node(path->nodes[i]);
Chris Masond6025572007-03-30 14:27:56 -0400955 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
956 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500957 if (tslot != 0)
958 break;
959 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500960 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500961}
962
Chris Mason74123bd2007-02-02 11:05:29 -0500963/*
964 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500965 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500966 *
967 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
968 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500969 */
Chris Masone089f052007-03-16 16:20:31 -0400970static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400971 *root, struct buffer_head *dst_buf, struct
972 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500973{
Chris Masone20d96d2007-03-22 12:13:20 -0400974 struct btrfs_node *src = btrfs_buffer_node(src_buf);
975 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500976 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500977 int src_nritems;
978 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500979 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500980
Chris Mason7518a232007-03-12 12:01:18 -0400981 src_nritems = btrfs_header_nritems(&src->header);
982 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400983 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -0400984
Chris Masoneb60cea2007-02-02 09:18:22 -0500985 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500986 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500987 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500988
989 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500990 push_items = src_nritems;
991
Chris Masond6025572007-03-30 14:27:56 -0400992 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
993 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500994 if (push_items < src_nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400995 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -0400996 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -0400997 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500998 }
Chris Mason7518a232007-03-12 12:01:18 -0400999 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
1000 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond6025572007-03-30 14:27:56 -04001001 btrfs_mark_buffer_dirty(src_buf);
1002 btrfs_mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001003 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001004}
1005
Chris Mason97571fd2007-02-24 13:39:08 -05001006/*
Chris Mason79f95c82007-03-01 15:16:26 -05001007 * try to push data from one node into the next node right in the
1008 * tree.
1009 *
1010 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1011 * error, and > 0 if there was no room in the right hand block.
1012 *
1013 * this will only push up to 1/2 the contents of the left node over
1014 */
Chris Masone089f052007-03-16 16:20:31 -04001015static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -04001016 btrfs_root *root, struct buffer_head *dst_buf,
1017 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -05001018{
Chris Masone20d96d2007-03-22 12:13:20 -04001019 struct btrfs_node *src = btrfs_buffer_node(src_buf);
1020 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -05001021 int push_items = 0;
1022 int max_push;
1023 int src_nritems;
1024 int dst_nritems;
1025 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001026
Chris Mason7518a232007-03-12 12:01:18 -04001027 src_nritems = btrfs_header_nritems(&src->header);
1028 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -04001029 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -05001030 if (push_items <= 0) {
1031 return 1;
1032 }
1033
1034 max_push = src_nritems / 2 + 1;
1035 /* don't try to empty the node */
1036 if (max_push > src_nritems)
1037 return 1;
1038 if (max_push < push_items)
1039 push_items = max_push;
1040
Chris Masond6025572007-03-30 14:27:56 -04001041 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
1042 dst_nritems * sizeof(struct btrfs_key_ptr));
1043
1044 btrfs_memcpy(root, dst, dst->ptrs,
1045 src->ptrs + src_nritems - push_items,
1046 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001047
Chris Mason7518a232007-03-12 12:01:18 -04001048 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
1049 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001050
Chris Masond6025572007-03-30 14:27:56 -04001051 btrfs_mark_buffer_dirty(src_buf);
1052 btrfs_mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -05001053 return ret;
1054}
1055
1056/*
Chris Mason97571fd2007-02-24 13:39:08 -05001057 * helper function to insert a new root level in the tree.
1058 * A new node is allocated, and a single item is inserted to
1059 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001060 *
1061 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001062 */
Chris Masone089f052007-03-16 16:20:31 -04001063static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
1064 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001065{
Chris Masone20d96d2007-03-22 12:13:20 -04001066 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001067 struct btrfs_node *lower;
1068 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -04001069 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001070
1071 BUG_ON(path->nodes[level]);
1072 BUG_ON(path->nodes[level-1] != root->node);
1073
Chris Mason6702ed42007-08-07 16:15:09 -04001074 t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001075 if (IS_ERR(t))
1076 return PTR_ERR(t);
Chris Masone20d96d2007-03-22 12:13:20 -04001077 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -04001078 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -04001079 btrfs_set_header_nritems(&c->header, 1);
1080 btrfs_set_header_level(&c->header, level);
Chris Mason7eccb902007-04-11 15:53:25 -04001081 btrfs_set_header_blocknr(&c->header, bh_blocknr(t));
Chris Mason7f5c1512007-03-23 15:56:19 -04001082 btrfs_set_header_generation(&c->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001083 btrfs_set_header_owner(&c->header, root->root_key.objectid);
Chris Masone20d96d2007-03-22 12:13:20 -04001084 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason3eb03142007-04-05 14:28:50 -04001085 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
1086 sizeof(c->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -04001087 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -04001088 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001089 else
Chris Mason123abc82007-03-14 14:14:43 -04001090 lower_key = &lower->ptrs[0].key;
Chris Masond6025572007-03-30 14:27:56 -04001091 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
1092 sizeof(struct btrfs_disk_key));
Chris Mason7eccb902007-04-11 15:53:25 -04001093 btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1]));
Chris Masond5719762007-03-23 10:01:08 -04001094
Chris Masond6025572007-03-30 14:27:56 -04001095 btrfs_mark_buffer_dirty(t);
Chris Masond5719762007-03-23 10:01:08 -04001096
Chris Mason5c680ed2007-02-22 11:39:13 -05001097 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -04001098 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -05001099 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -04001100 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001101 path->nodes[level] = t;
1102 path->slots[level] = 0;
1103 return 0;
1104}
1105
Chris Mason74123bd2007-02-02 11:05:29 -05001106/*
1107 * worker function to insert a single pointer in a node.
1108 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001109 *
Chris Mason74123bd2007-02-02 11:05:29 -05001110 * slot and level indicate where you want the key to go, and
1111 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001112 *
1113 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001114 */
Chris Masone089f052007-03-16 16:20:31 -04001115static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1116 *root, struct btrfs_path *path, struct btrfs_disk_key
1117 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001118{
Chris Mason234b63a2007-03-13 10:46:10 -04001119 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001120 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001121
1122 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -04001123 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -04001124 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -05001125 if (slot > nritems)
1126 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001127 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001128 BUG();
1129 if (slot != nritems) {
Chris Masond6025572007-03-30 14:27:56 -04001130 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
1131 lower->ptrs + slot,
1132 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001133 }
Chris Masond6025572007-03-30 14:27:56 -04001134 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
1135 key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -04001136 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -04001137 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001138 btrfs_mark_buffer_dirty(path->nodes[level]);
Chris Mason098f59c2007-05-11 11:33:21 -04001139 check_node(root, path, level);
Chris Mason74123bd2007-02-02 11:05:29 -05001140 return 0;
1141}
1142
Chris Mason97571fd2007-02-24 13:39:08 -05001143/*
1144 * split the node at the specified level in path in two.
1145 * The path is corrected to point to the appropriate node after the split
1146 *
1147 * Before splitting this tries to make some room in the node by pushing
1148 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001149 *
1150 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001151 */
Chris Masone089f052007-03-16 16:20:31 -04001152static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1153 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001154{
Chris Masone20d96d2007-03-22 12:13:20 -04001155 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001156 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -04001157 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -04001158 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001159 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001160 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001161 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001162 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001163
Chris Mason5c680ed2007-02-22 11:39:13 -05001164 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001165 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001166 if (t == root->node) {
1167 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001168 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001169 if (ret)
1170 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001171 } else {
1172 ret = push_nodes_for_insert(trans, root, path, level);
1173 t = path->nodes[level];
1174 c = btrfs_buffer_node(t);
1175 if (!ret &&
1176 btrfs_header_nritems(&c->header) <
1177 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1178 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001179 if (ret < 0)
1180 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001181 }
Chris Masone66f7092007-04-20 13:16:02 -04001182
Chris Mason7518a232007-03-12 12:01:18 -04001183 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason6702ed42007-08-07 16:15:09 -04001184 split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001185 if (IS_ERR(split_buffer))
1186 return PTR_ERR(split_buffer);
1187
Chris Masone20d96d2007-03-22 12:13:20 -04001188 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -04001189 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Mason9a6f11e2007-03-27 09:06:38 -04001190 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001191 btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001192 btrfs_set_header_generation(&split->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001193 btrfs_set_header_owner(&split->header, root->root_key.objectid);
Chris Mason3eb03142007-04-05 14:28:50 -04001194 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
1195 sizeof(split->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -04001196 mid = (c_nritems + 1) / 2;
Chris Masond6025572007-03-30 14:27:56 -04001197 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
1198 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -04001199 btrfs_set_header_nritems(&split->header, c_nritems - mid);
1200 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001201 ret = 0;
1202
Chris Masond6025572007-03-30 14:27:56 -04001203 btrfs_mark_buffer_dirty(t);
1204 btrfs_mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -04001205 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001206 bh_blocknr(split_buffer), path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001207 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001208 if (wret)
1209 ret = wret;
1210
Chris Mason5de08d72007-02-24 06:24:44 -05001211 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001212 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -04001213 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001214 path->nodes[level] = split_buffer;
1215 path->slots[level + 1] += 1;
1216 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001217 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001218 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001219 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001220}
1221
Chris Mason74123bd2007-02-02 11:05:29 -05001222/*
1223 * how many bytes are required to store the items in a leaf. start
1224 * and nr indicate which items in the leaf to check. This totals up the
1225 * space used both by the item structs and the item data
1226 */
Chris Mason234b63a2007-03-13 10:46:10 -04001227static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001228{
1229 int data_len;
Chris Masond4dbff92007-04-04 14:08:15 -04001230 int nritems = btrfs_header_nritems(&l->header);
1231 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001232
1233 if (!nr)
1234 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001235 data_len = btrfs_item_end(l->items + start);
1236 data_len = data_len - btrfs_item_offset(l->items + end);
1237 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001238 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001239 return data_len;
1240}
1241
Chris Mason74123bd2007-02-02 11:05:29 -05001242/*
Chris Masond4dbff92007-04-04 14:08:15 -04001243 * The space between the end of the leaf items and
1244 * the start of the leaf data. IOW, how much room
1245 * the leaf has left for both items and data
1246 */
1247int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
1248{
1249 int nritems = btrfs_header_nritems(&leaf->header);
1250 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1251}
1252
1253/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001254 * push some data in the path leaf to the right, trying to free up at
1255 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001256 *
1257 * returns 1 if the push failed because the other node didn't have enough
1258 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001259 */
Chris Masone089f052007-03-16 16:20:31 -04001260static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1261 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -05001262{
Chris Masone20d96d2007-03-22 12:13:20 -04001263 struct buffer_head *left_buf = path->nodes[0];
1264 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001265 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001266 struct buffer_head *right_buf;
1267 struct buffer_head *upper;
1268 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -05001269 int slot;
1270 int i;
1271 int free_space;
1272 int push_space = 0;
1273 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001274 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001275 u32 left_nritems;
1276 u32 right_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001277 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001278
1279 slot = path->slots[1];
1280 if (!path->nodes[1]) {
1281 return 1;
1282 }
1283 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001284 upper_node = btrfs_buffer_node(upper);
1285 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001286 return 1;
1287 }
Chris Masone20d96d2007-03-22 12:13:20 -04001288 right_buf = read_tree_block(root,
1289 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
1290 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001291 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001292 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001293 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001294 return 1;
1295 }
Chris Mason02217ed2007-03-02 16:08:05 -05001296 /* cow and double check */
Chris Mason54aa1f42007-06-22 14:16:25 -04001297 ret = btrfs_cow_block(trans, root, right_buf, upper,
1298 slot + 1, &right_buf);
1299 if (ret) {
1300 btrfs_block_release(root, right_buf);
1301 return 1;
1302 }
Chris Masone20d96d2007-03-22 12:13:20 -04001303 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001304 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001305 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001306 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -05001307 return 1;
1308 }
1309
Chris Mason7518a232007-03-12 12:01:18 -04001310 left_nritems = btrfs_header_nritems(&left->header);
Chris Masona429e512007-04-18 16:15:28 -04001311 if (left_nritems == 0) {
1312 btrfs_block_release(root, right_buf);
1313 return 1;
1314 }
1315 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001316 item = left->items + i;
1317 if (path->slots[0] == i)
1318 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001319 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1320 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001321 break;
1322 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001323 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -05001324 }
1325 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001326 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001327 return 1;
1328 }
Chris Masona429e512007-04-18 16:15:28 -04001329 if (push_items == left_nritems)
1330 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001331 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -05001332 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -04001333 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001334 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -05001335 /* make room in the right data area */
Chris Masond6025572007-03-30 14:27:56 -04001336 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1337 leaf_data_end(root, right) - push_space,
1338 btrfs_leaf_data(right) +
1339 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
1340 leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -05001341 /* copy from the left data area */
Chris Masond6025572007-03-30 14:27:56 -04001342 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
1343 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1344 btrfs_leaf_data(left) + leaf_data_end(root, left),
1345 push_space);
1346 btrfs_memmove(root, right, right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -04001347 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001348 /* copy the items from left to right */
Chris Masond6025572007-03-30 14:27:56 -04001349 btrfs_memcpy(root, right, right->items, left->items +
1350 left_nritems - push_items,
1351 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001352
1353 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001354 right_nritems += push_items;
1355 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001356 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001357 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001358 btrfs_set_item_offset(right->items + i, push_space -
1359 btrfs_item_size(right->items + i));
1360 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -05001361 }
Chris Mason7518a232007-03-12 12:01:18 -04001362 left_nritems -= push_items;
1363 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001364
Chris Masond6025572007-03-30 14:27:56 -04001365 btrfs_mark_buffer_dirty(left_buf);
1366 btrfs_mark_buffer_dirty(right_buf);
Chris Masona429e512007-04-18 16:15:28 -04001367
Chris Masond6025572007-03-30 14:27:56 -04001368 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -04001369 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond6025572007-03-30 14:27:56 -04001370 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001371
Chris Mason00ec4c52007-02-24 12:47:20 -05001372 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001373 if (path->slots[0] >= left_nritems) {
1374 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001375 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -05001376 path->nodes[0] = right_buf;
1377 path->slots[1] += 1;
1378 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001379 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001380 }
Chris Mason098f59c2007-05-11 11:33:21 -04001381 if (path->nodes[1])
1382 check_node(root, path, 1);
Chris Mason00ec4c52007-02-24 12:47:20 -05001383 return 0;
1384}
1385/*
Chris Mason74123bd2007-02-02 11:05:29 -05001386 * push some data in the path leaf to the left, trying to free up at
1387 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1388 */
Chris Masone089f052007-03-16 16:20:31 -04001389static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1390 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001391{
Chris Masone20d96d2007-03-22 12:13:20 -04001392 struct buffer_head *right_buf = path->nodes[0];
1393 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
1394 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001395 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001396 int slot;
1397 int i;
1398 int free_space;
1399 int push_space = 0;
1400 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001401 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001402 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001403 int ret = 0;
1404 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001405
1406 slot = path->slots[1];
1407 if (slot == 0) {
1408 return 1;
1409 }
1410 if (!path->nodes[1]) {
1411 return 1;
1412 }
Chris Masone20d96d2007-03-22 12:13:20 -04001413 t = read_tree_block(root,
1414 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
1415 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001416 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001417 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001418 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001419 return 1;
1420 }
Chris Mason02217ed2007-03-02 16:08:05 -05001421
1422 /* cow and double check */
Chris Mason54aa1f42007-06-22 14:16:25 -04001423 ret = btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
1424 if (ret) {
1425 /* we hit -ENOSPC, but it isn't fatal here */
1426 return 1;
1427 }
Chris Masone20d96d2007-03-22 12:13:20 -04001428 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001429 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001430 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001431 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -05001432 return 1;
1433 }
1434
Chris Masona429e512007-04-18 16:15:28 -04001435 if (btrfs_header_nritems(&right->header) == 0) {
1436 btrfs_block_release(root, t);
1437 return 1;
1438 }
1439
1440 for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001441 item = right->items + i;
1442 if (path->slots[0] == i)
1443 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001444 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1445 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001446 break;
1447 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001448 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001449 }
1450 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001451 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001452 return 1;
1453 }
Chris Masona429e512007-04-18 16:15:28 -04001454 if (push_items == btrfs_header_nritems(&right->header))
1455 WARN_ON(1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001456 /* push data from right to left */
Chris Masond6025572007-03-30 14:27:56 -04001457 btrfs_memcpy(root, left, left->items +
1458 btrfs_header_nritems(&left->header),
1459 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001460 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001461 btrfs_item_offset(right->items + push_items -1);
Chris Masond6025572007-03-30 14:27:56 -04001462 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1463 leaf_data_end(root, left) - push_space,
1464 btrfs_leaf_data(right) +
1465 btrfs_item_offset(right->items + push_items - 1),
1466 push_space);
Chris Mason7518a232007-03-12 12:01:18 -04001467 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001468 BUG_ON(old_left_nritems < 0);
1469
Chris Mason0783fcf2007-03-12 20:12:07 -04001470 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001471 u32 ioff = btrfs_item_offset(left->items + i);
1472 btrfs_set_item_offset(left->items + i, ioff -
1473 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001474 btrfs_item_offset(left->items +
1475 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001476 }
Chris Mason7518a232007-03-12 12:01:18 -04001477 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001478
1479 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -04001480 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -04001481 leaf_data_end(root, right);
Chris Masond6025572007-03-30 14:27:56 -04001482 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1483 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1484 btrfs_leaf_data(right) +
1485 leaf_data_end(root, right), push_space);
1486 btrfs_memmove(root, right, right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -04001487 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001488 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001489 btrfs_set_header_nritems(&right->header,
1490 btrfs_header_nritems(&right->header) -
1491 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001492 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001493
Chris Mason7518a232007-03-12 12:01:18 -04001494 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001495 btrfs_set_item_offset(right->items + i, push_space -
1496 btrfs_item_size(right->items + i));
1497 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001498 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001499
Chris Masond6025572007-03-30 14:27:56 -04001500 btrfs_mark_buffer_dirty(t);
1501 btrfs_mark_buffer_dirty(right_buf);
Chris Mason098f59c2007-05-11 11:33:21 -04001502
Chris Masone089f052007-03-16 16:20:31 -04001503 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001504 if (wret)
1505 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001506
1507 /* then fixup the leaf pointer in the path */
1508 if (path->slots[0] < push_items) {
1509 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001510 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001511 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001512 path->slots[1] -= 1;
1513 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001514 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001515 path->slots[0] -= push_items;
1516 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001517 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001518 if (path->nodes[1])
1519 check_node(root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001520 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001521}
1522
Chris Mason74123bd2007-02-02 11:05:29 -05001523/*
1524 * split the path's leaf in two, making sure there is at least data_size
1525 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001526 *
1527 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001528 */
Chris Masone089f052007-03-16 16:20:31 -04001529static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001530 *root, struct btrfs_key *ins_key,
1531 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001532{
Chris Masone20d96d2007-03-22 12:13:20 -04001533 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001534 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001535 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001536 int mid;
1537 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001538 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001539 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001540 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001541 int data_copy_size;
1542 int rt_data_off;
1543 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001544 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001545 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001546 int double_split = 0;
1547 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001548
Chris Mason40689472007-03-17 14:29:23 -04001549 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001550 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001551 if (wret < 0)
1552 return wret;
1553 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001554 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001555 if (wret < 0)
1556 return wret;
1557 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001558 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001559 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001560
1561 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001562 if (btrfs_leaf_free_space(root, l) >=
1563 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001564 return 0;
1565
Chris Mason5c680ed2007-02-22 11:39:13 -05001566 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001567 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001568 if (ret)
1569 return ret;
1570 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001571 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001572 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001573 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001574
Chris Mason6702ed42007-08-07 16:15:09 -04001575 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001576 if (IS_ERR(right_buffer))
1577 return PTR_ERR(right_buffer);
1578
Chris Masone20d96d2007-03-22 12:13:20 -04001579 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001580 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001581 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001582 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001583 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Mason7518a232007-03-12 12:01:18 -04001584 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001585 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1586 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001587 if (mid <= slot) {
1588 if (nritems == 1 ||
1589 leaf_space_used(l, mid, nritems - mid) + space_needed >
1590 BTRFS_LEAF_DATA_SIZE(root)) {
1591 if (slot >= nritems) {
1592 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1593 btrfs_set_header_nritems(&right->header, 0);
1594 wret = insert_ptr(trans, root, path,
1595 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001596 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001597 path->slots[1] + 1, 1);
1598 if (wret)
1599 ret = wret;
1600 btrfs_block_release(root, path->nodes[0]);
1601 path->nodes[0] = right_buffer;
1602 path->slots[0] = 0;
1603 path->slots[1] += 1;
1604 return ret;
1605 }
1606 mid = slot;
1607 double_split = 1;
1608 }
1609 } else {
1610 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1611 BTRFS_LEAF_DATA_SIZE(root)) {
1612 if (slot == 0) {
1613 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1614 btrfs_set_header_nritems(&right->header, 0);
1615 wret = insert_ptr(trans, root, path,
1616 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001617 bh_blocknr(right_buffer),
Chris Mason098f59c2007-05-11 11:33:21 -04001618 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001619 if (wret)
1620 ret = wret;
1621 btrfs_block_release(root, path->nodes[0]);
1622 path->nodes[0] = right_buffer;
1623 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04001624 if (path->slots[1] == 0) {
1625 wret = fixup_low_keys(trans, root,
1626 path, &disk_key, 1);
1627 if (wret)
1628 ret = wret;
1629 }
Chris Masond4dbff92007-04-04 14:08:15 -04001630 return ret;
1631 }
1632 mid = slot;
1633 double_split = 1;
1634 }
1635 }
1636 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Mason123abc82007-03-14 14:14:43 -04001637 data_copy_size = btrfs_item_end(l->items + mid) -
1638 leaf_data_end(root, l);
Chris Masond6025572007-03-30 14:27:56 -04001639 btrfs_memcpy(root, right, right->items, l->items + mid,
1640 (nritems - mid) * sizeof(struct btrfs_item));
1641 btrfs_memcpy(root, right,
1642 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1643 data_copy_size, btrfs_leaf_data(l) +
1644 leaf_data_end(root, l), data_copy_size);
Chris Mason123abc82007-03-14 14:14:43 -04001645 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1646 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001647
Chris Mason0783fcf2007-03-12 20:12:07 -04001648 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001649 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001650 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1651 }
Chris Mason74123bd2007-02-02 11:05:29 -05001652
Chris Mason7518a232007-03-12 12:01:18 -04001653 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001654 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001655 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001656 bh_blocknr(right_buffer), path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001657 if (wret)
1658 ret = wret;
Chris Masond6025572007-03-30 14:27:56 -04001659 btrfs_mark_buffer_dirty(right_buffer);
1660 btrfs_mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001661 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001662 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001663 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001664 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001665 path->slots[0] -= mid;
1666 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001667 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001668 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001669 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001670 check_node(root, path, 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001671
1672 if (!double_split)
1673 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001674 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001675 if (IS_ERR(right_buffer))
1676 return PTR_ERR(right_buffer);
1677
Chris Masond4dbff92007-04-04 14:08:15 -04001678 right = btrfs_buffer_leaf(right_buffer);
1679 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001680 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Masond4dbff92007-04-04 14:08:15 -04001681 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001682 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Masond4dbff92007-04-04 14:08:15 -04001683 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001684 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1685 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001686 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1687 btrfs_set_header_nritems(&right->header, 0);
1688 wret = insert_ptr(trans, root, path,
1689 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001690 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001691 path->slots[1], 1);
1692 if (wret)
1693 ret = wret;
Chris Masona429e512007-04-18 16:15:28 -04001694 if (path->slots[1] == 0) {
1695 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1696 if (wret)
1697 ret = wret;
1698 }
Chris Masond4dbff92007-04-04 14:08:15 -04001699 btrfs_block_release(root, path->nodes[0]);
1700 path->nodes[0] = right_buffer;
1701 path->slots[0] = 0;
1702 check_node(root, path, 1);
1703 check_leaf(root, path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001704 return ret;
1705}
1706
Chris Masonb18c6682007-04-17 13:26:50 -04001707int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 struct btrfs_path *path,
1710 u32 new_size)
1711{
1712 int ret = 0;
1713 int slot;
1714 int slot_orig;
1715 struct btrfs_leaf *leaf;
1716 struct buffer_head *leaf_buf;
1717 u32 nritems;
1718 unsigned int data_end;
1719 unsigned int old_data_start;
1720 unsigned int old_size;
1721 unsigned int size_diff;
1722 int i;
1723
1724 slot_orig = path->slots[0];
1725 leaf_buf = path->nodes[0];
1726 leaf = btrfs_buffer_leaf(leaf_buf);
1727
1728 nritems = btrfs_header_nritems(&leaf->header);
1729 data_end = leaf_data_end(root, leaf);
1730
1731 slot = path->slots[0];
1732 old_data_start = btrfs_item_offset(leaf->items + slot);
1733 old_size = btrfs_item_size(leaf->items + slot);
1734 BUG_ON(old_size <= new_size);
1735 size_diff = old_size - new_size;
1736
1737 BUG_ON(slot < 0);
1738 BUG_ON(slot >= nritems);
1739
1740 /*
1741 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1742 */
1743 /* first correct the data pointers */
1744 for (i = slot; i < nritems; i++) {
1745 u32 ioff = btrfs_item_offset(leaf->items + i);
1746 btrfs_set_item_offset(leaf->items + i,
1747 ioff + size_diff);
1748 }
1749 /* shift the data */
Chris Masonb18c6682007-04-17 13:26:50 -04001750 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1751 data_end + size_diff, btrfs_leaf_data(leaf) +
1752 data_end, old_data_start + new_size - data_end);
1753 btrfs_set_item_size(leaf->items + slot, new_size);
1754 btrfs_mark_buffer_dirty(leaf_buf);
1755
1756 ret = 0;
1757 if (btrfs_leaf_free_space(root, leaf) < 0)
1758 BUG();
1759 check_leaf(root, path, 0);
1760 return ret;
1761}
1762
Chris Mason6567e832007-04-16 09:22:45 -04001763int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1764 *root, struct btrfs_path *path, u32 data_size)
1765{
1766 int ret = 0;
1767 int slot;
1768 int slot_orig;
1769 struct btrfs_leaf *leaf;
1770 struct buffer_head *leaf_buf;
1771 u32 nritems;
1772 unsigned int data_end;
1773 unsigned int old_data;
1774 unsigned int old_size;
1775 int i;
1776
1777 slot_orig = path->slots[0];
1778 leaf_buf = path->nodes[0];
1779 leaf = btrfs_buffer_leaf(leaf_buf);
1780
1781 nritems = btrfs_header_nritems(&leaf->header);
1782 data_end = leaf_data_end(root, leaf);
1783
1784 if (btrfs_leaf_free_space(root, leaf) < data_size)
1785 BUG();
1786 slot = path->slots[0];
1787 old_data = btrfs_item_end(leaf->items + slot);
1788
1789 BUG_ON(slot < 0);
1790 BUG_ON(slot >= nritems);
1791
1792 /*
1793 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1794 */
1795 /* first correct the data pointers */
1796 for (i = slot; i < nritems; i++) {
1797 u32 ioff = btrfs_item_offset(leaf->items + i);
1798 btrfs_set_item_offset(leaf->items + i,
1799 ioff - data_size);
1800 }
1801 /* shift the data */
1802 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1803 data_end - data_size, btrfs_leaf_data(leaf) +
1804 data_end, old_data - data_end);
1805 data_end = old_data;
1806 old_size = btrfs_item_size(leaf->items + slot);
1807 btrfs_set_item_size(leaf->items + slot, old_size + data_size);
1808 btrfs_mark_buffer_dirty(leaf_buf);
1809
1810 ret = 0;
1811 if (btrfs_leaf_free_space(root, leaf) < 0)
1812 BUG();
1813 check_leaf(root, path, 0);
1814 return ret;
1815}
1816
Chris Mason74123bd2007-02-02 11:05:29 -05001817/*
1818 * Given a key and some data, insert an item into the tree.
1819 * This does all the path init required, making room in the tree if needed.
1820 */
Chris Masone089f052007-03-16 16:20:31 -04001821int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1822 *root, struct btrfs_path *path, struct btrfs_key
1823 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001824{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001825 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001826 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001827 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001828 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001829 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001830 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001831 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001832 struct btrfs_disk_key disk_key;
1833
1834 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001835
Chris Mason74123bd2007-02-02 11:05:29 -05001836 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001837 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001838 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001839 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001840 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05001841 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001842 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001843 if (ret < 0)
1844 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001845
Chris Mason62e27492007-03-15 12:56:47 -04001846 slot_orig = path->slots[0];
1847 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001848 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001849
Chris Mason7518a232007-03-12 12:01:18 -04001850 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001851 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001852
Chris Mason123abc82007-03-14 14:14:43 -04001853 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04001854 sizeof(struct btrfs_item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001855 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04001856 }
Chris Mason62e27492007-03-15 12:56:47 -04001857 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001858 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001859 if (slot != nritems) {
1860 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001861 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001862
1863 /*
1864 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1865 */
1866 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001867 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001868 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001869 btrfs_set_item_offset(leaf->items + i,
1870 ioff - data_size);
1871 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001872
1873 /* shift the items */
Chris Masond6025572007-03-30 14:27:56 -04001874 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1875 leaf->items + slot,
1876 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001877
1878 /* shift the data */
Chris Masond6025572007-03-30 14:27:56 -04001879 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1880 data_end - data_size, btrfs_leaf_data(leaf) +
1881 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001882 data_end = old_data;
1883 }
Chris Mason62e27492007-03-15 12:56:47 -04001884 /* setup the item for the new data */
Chris Masond6025572007-03-30 14:27:56 -04001885 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1886 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001887 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1888 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001889 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001890 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001891
1892 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001893 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001894 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001895
Chris Mason123abc82007-03-14 14:14:43 -04001896 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001897 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001898 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001899out:
Chris Mason62e27492007-03-15 12:56:47 -04001900 return ret;
1901}
1902
1903/*
1904 * Given a key and some data, insert an item into the tree.
1905 * This does all the path init required, making room in the tree if needed.
1906 */
Chris Masone089f052007-03-16 16:20:31 -04001907int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1908 *root, struct btrfs_key *cpu_key, void *data, u32
1909 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001910{
1911 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001912 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -04001913 u8 *ptr;
1914
Chris Mason2c90e5d2007-04-02 10:50:19 -04001915 path = btrfs_alloc_path();
1916 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001917 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001918 if (!ret) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001919 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1920 path->slots[0], u8);
1921 btrfs_memcpy(root, path->nodes[0]->b_data,
Chris Masond6025572007-03-30 14:27:56 -04001922 ptr, data, data_size);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001923 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001924 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04001925 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001926 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001927}
1928
Chris Mason74123bd2007-02-02 11:05:29 -05001929/*
Chris Mason5de08d72007-02-24 06:24:44 -05001930 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001931 *
1932 * If the delete empties a node, the node is removed from the tree,
1933 * continuing all the way the root if required. The root is converted into
1934 * a leaf if all the nodes are emptied.
1935 */
Chris Masone089f052007-03-16 16:20:31 -04001936static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1937 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938{
Chris Mason234b63a2007-03-13 10:46:10 -04001939 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001940 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001941 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001942 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001943 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001944
Chris Masone20d96d2007-03-22 12:13:20 -04001945 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001946 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001947 if (slot != nritems -1) {
Chris Masond6025572007-03-30 14:27:56 -04001948 btrfs_memmove(root, node, node->ptrs + slot,
1949 node->ptrs + slot + 1,
1950 sizeof(struct btrfs_key_ptr) *
1951 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001952 }
Chris Mason7518a232007-03-12 12:01:18 -04001953 nritems--;
1954 btrfs_set_header_nritems(&node->header, nritems);
1955 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001956 struct btrfs_header *header = btrfs_buffer_header(root->node);
1957 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001958 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001959 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001960 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001961 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001962 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001963 if (wret)
1964 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001965 }
Chris Masond6025572007-03-30 14:27:56 -04001966 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001967 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001968}
1969
Chris Mason74123bd2007-02-02 11:05:29 -05001970/*
1971 * delete the item at the leaf level in path. If that empties
1972 * the leaf, remove it from the tree
1973 */
Chris Masone089f052007-03-16 16:20:31 -04001974int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1975 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001976{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001977 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001978 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001979 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001980 int doff;
1981 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001982 int ret = 0;
1983 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001984 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001985
Chris Masoneb60cea2007-02-02 09:18:22 -05001986 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001987 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001988 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001989 doff = btrfs_item_offset(leaf->items + slot);
1990 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001991 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001992
Chris Mason7518a232007-03-12 12:01:18 -04001993 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001994 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001995 int data_end = leaf_data_end(root, leaf);
Chris Masond6025572007-03-30 14:27:56 -04001996 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1997 data_end + dsize,
1998 btrfs_leaf_data(leaf) + data_end,
1999 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002000 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04002001 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04002002 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
2003 }
Chris Masond6025572007-03-30 14:27:56 -04002004 btrfs_memmove(root, leaf, leaf->items + slot,
2005 leaf->items + slot + 1,
2006 sizeof(struct btrfs_item) *
2007 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002008 }
Chris Mason7518a232007-03-12 12:01:18 -04002009 btrfs_set_header_nritems(&leaf->header, nritems - 1);
2010 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05002011 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002012 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05002013 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04002014 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002015 } else {
Chris Masone089f052007-03-16 16:20:31 -04002016 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04002017 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002018 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002019 if (wret)
2020 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002021 wret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04002022 bh_blocknr(leaf_buf), 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002023 if (wret)
2024 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002025 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002026 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002027 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002028 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04002029 wret = fixup_low_keys(trans, root, path,
2030 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002031 if (wret)
2032 ret = wret;
2033 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002034
Chris Mason74123bd2007-02-02 11:05:29 -05002035 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04002036 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002037 /* push_leaf_left fixes the path.
2038 * make sure the path still points to our leaf
2039 * for possible call to del_ptr below
2040 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002041 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04002042 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002043 wret = push_leaf_left(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002044 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002045 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05002046 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04002047 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04002048 wret = push_leaf_right(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002049 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002050 ret = wret;
2051 }
Chris Mason7518a232007-03-12 12:01:18 -04002052 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -04002053 u64 blocknr = bh_blocknr(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002054 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04002055 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002056 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002057 if (wret)
2058 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04002059 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002060 wret = btrfs_free_extent(trans, root, blocknr,
2061 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002062 if (wret)
2063 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002064 } else {
Chris Masond6025572007-03-30 14:27:56 -04002065 btrfs_mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04002066 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002067 }
Chris Masond5719762007-03-23 10:01:08 -04002068 } else {
Chris Masond6025572007-03-30 14:27:56 -04002069 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002070 }
2071 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002072 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002073}
2074
Chris Mason97571fd2007-02-24 13:39:08 -05002075/*
2076 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002077 * returns 0 if it found something or 1 if there are no greater leaves.
2078 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002079 */
Chris Mason234b63a2007-03-13 10:46:10 -04002080int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002081{
2082 int slot;
2083 int level = 1;
2084 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04002085 struct buffer_head *c;
2086 struct btrfs_node *c_node;
2087 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002088
Chris Mason234b63a2007-03-13 10:46:10 -04002089 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002090 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002091 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002092 slot = path->slots[level] + 1;
2093 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04002094 c_node = btrfs_buffer_node(c);
2095 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002096 level++;
2097 continue;
2098 }
Chris Masone20d96d2007-03-22 12:13:20 -04002099 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002100 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04002101 btrfs_block_release(root, next);
Chris Mason6702ed42007-08-07 16:15:09 -04002102 if (path->reada)
2103 reada_for_search(root, path, level, slot);
Chris Masond97e63b2007-02-20 16:40:44 -05002104 next = read_tree_block(root, blocknr);
2105 break;
2106 }
2107 path->slots[level] = slot;
2108 while(1) {
2109 level--;
2110 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04002111 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05002112 path->nodes[level] = next;
2113 path->slots[level] = 0;
2114 if (!level)
2115 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002116 if (path->reada)
2117 reada_for_search(root, path, level, slot);
Chris Mason1d4f8a02007-03-13 09:28:32 -04002118 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04002119 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05002120 }
2121 return 0;
2122}