blob: ee1ae00d2827a34dff02d93f557b3ba5df861c52 [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 {
Chris Mason6702ed42007-08-07 16:15:09 -0400102 clean_tree_block(trans, root, buf);
103 }
104
105 if (buf == root->node) {
106 root->node = cow;
107 get_bh(cow);
108 if (buf != root->commit_root) {
109 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
110 }
111 btrfs_block_release(root, buf);
112 } else {
113 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
114 bh_blocknr(cow));
115 btrfs_mark_buffer_dirty(parent);
116 WARN_ON(btrfs_header_generation(btrfs_buffer_header(parent)) !=
117 trans->transid);
118 btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1);
119 }
120 btrfs_block_release(root, buf);
121 btrfs_mark_buffer_dirty(cow);
122 *cow_ret = cow;
123 return 0;
124}
125
126int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400127 *root, struct buffer_head *buf, struct buffer_head
128 *parent, int parent_slot, struct buffer_head
Chris Masone089f052007-03-16 16:20:31 -0400129 **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500130{
Chris Mason6702ed42007-08-07 16:15:09 -0400131 u64 search_start;
Chris Masonccd467d2007-06-28 15:57:36 -0400132 if (trans->transaction != root->fs_info->running_transaction) {
133 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
134 root->fs_info->running_transaction->transid);
135 WARN_ON(1);
136 }
137 if (trans->transid != root->fs_info->generation) {
138 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
139 root->fs_info->generation);
140 WARN_ON(1);
141 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400142 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
143 trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -0500144 *cow_ret = buf;
145 return 0;
146 }
Chris Mason6702ed42007-08-07 16:15:09 -0400147
148 search_start = bh_blocknr(buf) & ~((u64)65535);
149 return __btrfs_cow_block(trans, root, buf, parent,
150 parent_slot, cow_ret, search_start, 0);
151}
152
153static int close_blocks(u64 blocknr, u64 other)
154{
155 if (blocknr < other && other - blocknr < 8)
156 return 1;
157 if (blocknr > other && blocknr - other < 8)
158 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500159 return 0;
160}
161
Chris Mason6702ed42007-08-07 16:15:09 -0400162int btrfs_realloc_node(struct btrfs_trans_handle *trans,
163 struct btrfs_root *root, struct buffer_head *parent,
Chris Masone9d0b132007-08-10 14:06:19 -0400164 int cache_only, u64 *last_ret)
Chris Mason6702ed42007-08-07 16:15:09 -0400165{
166 struct btrfs_node *parent_node;
167 struct buffer_head *cur_bh;
168 struct buffer_head *tmp_bh;
169 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400170 u64 search_start = *last_ret;
171 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400172 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);
Chris Masone9d0b132007-08-10 14:06:19 -0400201 if (last_block == 0)
202 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400203 if (i > 0) {
204 other = btrfs_node_blockptr(parent_node, i - 1);
205 close = close_blocks(blocknr, other);
206 }
207 if (close && i < end_slot - 1) {
208 other = btrfs_node_blockptr(parent_node, i + 1);
209 close = close_blocks(blocknr, other);
210 }
Chris Masone9d0b132007-08-10 14:06:19 -0400211 if (close) {
212 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400213 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400214 }
Chris Mason6702ed42007-08-07 16:15:09 -0400215
216 cur_bh = btrfs_find_tree_block(root, blocknr);
217 if (!cur_bh || !buffer_uptodate(cur_bh) ||
218 buffer_locked(cur_bh)) {
219 if (cache_only) {
220 brelse(cur_bh);
221 continue;
222 }
223 brelse(cur_bh);
224 cur_bh = read_tree_block(root, blocknr);
225 }
Chris Masone9d0b132007-08-10 14:06:19 -0400226 if (search_start == 0)
227 search_start = last_block & ~((u64)65535);
228
Chris Mason6702ed42007-08-07 16:15:09 -0400229 err = __btrfs_cow_block(trans, root, cur_bh, parent, i,
230 &tmp_bh, search_start,
231 min(8, end_slot - i));
232 if (err)
233 break;
234 search_start = bh_blocknr(tmp_bh);
235 brelse(tmp_bh);
236 }
237 return err;
238}
239
Chris Mason74123bd2007-02-02 11:05:29 -0500240/*
241 * The leaf data grows from end-to-front in the node.
242 * this returns the address of the start of the last item,
243 * which is the stop of the leaf data stack
244 */
Chris Mason123abc82007-03-14 14:14:43 -0400245static inline unsigned int leaf_data_end(struct btrfs_root *root,
246 struct btrfs_leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500247{
Chris Mason7518a232007-03-12 12:01:18 -0400248 u32 nr = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500249 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400250 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason0783fcf2007-03-12 20:12:07 -0400251 return btrfs_item_offset(leaf->items + nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500252}
253
Chris Mason74123bd2007-02-02 11:05:29 -0500254/*
Chris Mason74123bd2007-02-02 11:05:29 -0500255 * compare two keys in a memcmp fashion
256 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400257static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500258{
Chris Masone2fa7222007-03-12 16:22:34 -0400259 struct btrfs_key k1;
260
261 btrfs_disk_key_to_cpu(&k1, disk);
262
263 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500264 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400265 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500266 return -1;
Chris Masonf254e522007-03-29 15:15:27 -0400267 if (k1.flags > k2->flags)
268 return 1;
269 if (k1.flags < k2->flags)
270 return -1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400271 if (k1.offset > k2->offset)
272 return 1;
273 if (k1.offset < k2->offset)
274 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500275 return 0;
276}
Chris Mason74123bd2007-02-02 11:05:29 -0500277
Chris Mason123abc82007-03-14 14:14:43 -0400278static int check_node(struct btrfs_root *root, struct btrfs_path *path,
279 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500280{
Chris Mason234b63a2007-03-13 10:46:10 -0400281 struct btrfs_node *parent = NULL;
Chris Masone20d96d2007-03-22 12:13:20 -0400282 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500283 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400284 int slot;
285 struct btrfs_key cpukey;
Chris Mason7518a232007-03-12 12:01:18 -0400286 u32 nritems = btrfs_header_nritems(&node->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500287
288 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400289 parent = btrfs_buffer_node(path->nodes[level + 1]);
Aneesha1f396302007-07-11 10:03:27 -0400290
Chris Mason8d7be552007-05-10 11:24:42 -0400291 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400292 BUG_ON(nritems == 0);
293 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400294 struct btrfs_disk_key *parent_key;
Aneesha1f396302007-07-11 10:03:27 -0400295
296 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400297 parent_key = &parent->ptrs[parent_slot].key;
298 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400299 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400300 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400301 btrfs_header_blocknr(&node->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500302 }
Chris Mason123abc82007-03-14 14:14:43 -0400303 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400304 if (slot != 0) {
305 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot - 1].key);
306 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) <= 0);
307 }
308 if (slot < nritems - 1) {
309 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[slot + 1].key);
310 BUG_ON(comp_keys(&node->ptrs[slot].key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500311 }
312 return 0;
313}
314
Chris Mason123abc82007-03-14 14:14:43 -0400315static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
316 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500317{
Chris Masone20d96d2007-03-22 12:13:20 -0400318 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
Chris Mason234b63a2007-03-13 10:46:10 -0400319 struct btrfs_node *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500320 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400321 int slot = path->slots[0];
322 struct btrfs_key cpukey;
323
Chris Mason7518a232007-03-12 12:01:18 -0400324 u32 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500325
326 if (path->nodes[level + 1])
Chris Masone20d96d2007-03-22 12:13:20 -0400327 parent = btrfs_buffer_node(path->nodes[level + 1]);
Aneesha1f396302007-07-11 10:03:27 -0400328
Chris Mason123abc82007-03-14 14:14:43 -0400329 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason7518a232007-03-12 12:01:18 -0400330
331 if (nritems == 0)
332 return 0;
333
334 if (parent) {
Chris Masone2fa7222007-03-12 16:22:34 -0400335 struct btrfs_disk_key *parent_key;
Aneesha1f396302007-07-11 10:03:27 -0400336
337 parent_slot = path->slots[level + 1];
Chris Mason123abc82007-03-14 14:14:43 -0400338 parent_key = &parent->ptrs[parent_slot].key;
Chris Mason6702ed42007-08-07 16:15:09 -0400339
Chris Masonaa5d6be2007-02-28 16:35:06 -0500340 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
Chris Masone2fa7222007-03-12 16:22:34 -0400341 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400342 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Mason7518a232007-03-12 12:01:18 -0400343 btrfs_header_blocknr(&leaf->header));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500344 }
Chris Mason8d7be552007-05-10 11:24:42 -0400345 if (slot != 0) {
346 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot - 1].key);
347 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) <= 0);
348 BUG_ON(btrfs_item_offset(leaf->items + slot - 1) !=
349 btrfs_item_end(leaf->items + slot));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500350 }
Chris Mason8d7be552007-05-10 11:24:42 -0400351 if (slot < nritems - 1) {
352 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[slot + 1].key);
353 BUG_ON(comp_keys(&leaf->items[slot].key, &cpukey) >= 0);
354 BUG_ON(btrfs_item_offset(leaf->items + slot) !=
355 btrfs_item_end(leaf->items + slot + 1));
356 }
357 BUG_ON(btrfs_item_offset(leaf->items) +
358 btrfs_item_size(leaf->items) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500359 return 0;
360}
361
Chris Mason123abc82007-03-14 14:14:43 -0400362static int check_block(struct btrfs_root *root, struct btrfs_path *path,
363 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500364{
Chris Mason3eb03142007-04-05 14:28:50 -0400365 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
366 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
367 sizeof(node->header.fsid)))
368 BUG();
Chris Masonaa5d6be2007-02-28 16:35:06 -0500369 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400370 return check_leaf(root, path, level);
371 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500372}
373
Chris Mason74123bd2007-02-02 11:05:29 -0500374/*
375 * search for key in the array p. items p are item_size apart
376 * and there are 'max' items in p
377 * the slot in the array is returned via slot, and it points to
378 * the place where you would insert key if it is not found in
379 * the array.
380 *
381 * slot may point to max if the key is bigger than all of the keys
382 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400383static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500384 int max, int *slot)
385{
386 int low = 0;
387 int high = max;
388 int mid;
389 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400390 struct btrfs_disk_key *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500391
392 while(low < high) {
393 mid = (low + high) / 2;
Chris Masone2fa7222007-03-12 16:22:34 -0400394 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500395 ret = comp_keys(tmp, key);
396
397 if (ret < 0)
398 low = mid + 1;
399 else if (ret > 0)
400 high = mid;
401 else {
402 *slot = mid;
403 return 0;
404 }
405 }
406 *slot = low;
407 return 1;
408}
409
Chris Mason97571fd2007-02-24 13:39:08 -0500410/*
411 * simple bin_search frontend that does the right thing for
412 * leaves vs nodes
413 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400414static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500415{
Chris Mason7518a232007-03-12 12:01:18 -0400416 if (btrfs_is_leaf(c)) {
Chris Mason234b63a2007-03-13 10:46:10 -0400417 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Mason0783fcf2007-03-12 20:12:07 -0400418 return generic_bin_search((void *)l->items,
419 sizeof(struct btrfs_item),
Chris Mason7518a232007-03-12 12:01:18 -0400420 key, btrfs_header_nritems(&c->header),
421 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500422 } else {
Chris Mason123abc82007-03-14 14:14:43 -0400423 return generic_bin_search((void *)c->ptrs,
424 sizeof(struct btrfs_key_ptr),
Chris Mason7518a232007-03-12 12:01:18 -0400425 key, btrfs_header_nritems(&c->header),
426 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500427 }
428 return -1;
429}
430
Chris Masone20d96d2007-03-22 12:13:20 -0400431static struct buffer_head *read_node_slot(struct btrfs_root *root,
432 struct buffer_head *parent_buf,
Chris Masonbb803952007-03-01 12:04:21 -0500433 int slot)
434{
Chris Masone20d96d2007-03-22 12:13:20 -0400435 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500436 if (slot < 0)
437 return NULL;
Chris Mason7518a232007-03-12 12:01:18 -0400438 if (slot >= btrfs_header_nritems(&node->header))
Chris Masonbb803952007-03-01 12:04:21 -0500439 return NULL;
Chris Mason1d4f8a02007-03-13 09:28:32 -0400440 return read_tree_block(root, btrfs_node_blockptr(node, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500441}
442
Chris Masone089f052007-03-16 16:20:31 -0400443static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
444 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500445{
Chris Masone20d96d2007-03-22 12:13:20 -0400446 struct buffer_head *right_buf;
447 struct buffer_head *mid_buf;
448 struct buffer_head *left_buf;
449 struct buffer_head *parent_buf = NULL;
Chris Mason234b63a2007-03-13 10:46:10 -0400450 struct btrfs_node *right = NULL;
451 struct btrfs_node *mid;
452 struct btrfs_node *left = NULL;
453 struct btrfs_node *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500454 int ret = 0;
455 int wret;
456 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500457 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400458 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500459 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500460
461 if (level == 0)
462 return 0;
463
464 mid_buf = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400465 mid = btrfs_buffer_node(mid_buf);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400466 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500467
Chris Mason234b63a2007-03-13 10:46:10 -0400468 if (level < BTRFS_MAX_LEVEL - 1)
Chris Masonbb803952007-03-01 12:04:21 -0500469 parent_buf = path->nodes[level + 1];
470 pslot = path->slots[level + 1];
471
Chris Mason40689472007-03-17 14:29:23 -0400472 /*
473 * deal with the case where there is only one pointer in the root
474 * by promoting the node below to a root
475 */
Chris Masonbb803952007-03-01 12:04:21 -0500476 if (!parent_buf) {
Chris Masone20d96d2007-03-22 12:13:20 -0400477 struct buffer_head *child;
Chris Mason7eccb902007-04-11 15:53:25 -0400478 u64 blocknr = bh_blocknr(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500479
Chris Mason7518a232007-03-12 12:01:18 -0400480 if (btrfs_header_nritems(&mid->header) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500481 return 0;
482
483 /* promote the child to a root */
484 child = read_node_slot(root, mid_buf, 0);
485 BUG_ON(!child);
486 root->node = child;
487 path->nodes[level] = NULL;
Chris Masond6025572007-03-30 14:27:56 -0400488 clean_tree_block(trans, root, mid_buf);
489 wait_on_buffer(mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500490 /* once for the path */
Chris Mason234b63a2007-03-13 10:46:10 -0400491 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500492 /* once for the root ptr */
Chris Mason234b63a2007-03-13 10:46:10 -0400493 btrfs_block_release(root, mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400494 return btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500495 }
Chris Masone20d96d2007-03-22 12:13:20 -0400496 parent = btrfs_buffer_node(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500497
Chris Mason123abc82007-03-14 14:14:43 -0400498 if (btrfs_header_nritems(&mid->header) >
499 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500500 return 0;
501
Chris Mason54aa1f42007-06-22 14:16:25 -0400502 if (btrfs_header_nritems(&mid->header) < 2)
503 err_on_enospc = 1;
504
Chris Masonbb803952007-03-01 12:04:21 -0500505 left_buf = read_node_slot(root, parent_buf, pslot - 1);
506 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500507
508 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500509 if (left_buf) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400510 wret = btrfs_cow_block(trans, root, left_buf,
511 parent_buf, pslot - 1, &left_buf);
512 if (wret) {
513 ret = wret;
514 goto enospc;
515 }
Chris Masone20d96d2007-03-22 12:13:20 -0400516 left = btrfs_buffer_node(left_buf);
Chris Mason7518a232007-03-12 12:01:18 -0400517 orig_slot += btrfs_header_nritems(&left->header);
Chris Masone089f052007-03-16 16:20:31 -0400518 wret = push_node_left(trans, root, left_buf, mid_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500519 if (wret < 0)
520 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400521 if (btrfs_header_nritems(&mid->header) < 2)
522 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500523 }
Chris Mason79f95c82007-03-01 15:16:26 -0500524
525 /*
526 * then try to empty the right most buffer into the middle
527 */
Chris Masonbb803952007-03-01 12:04:21 -0500528 if (right_buf) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400529 wret = btrfs_cow_block(trans, root, right_buf,
530 parent_buf, pslot + 1, &right_buf);
531 if (wret) {
532 ret = wret;
533 goto enospc;
534 }
535
Chris Masone20d96d2007-03-22 12:13:20 -0400536 right = btrfs_buffer_node(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400537 wret = push_node_left(trans, root, mid_buf, right_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400538 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500539 ret = wret;
Chris Mason7518a232007-03-12 12:01:18 -0400540 if (btrfs_header_nritems(&right->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -0400541 u64 blocknr = bh_blocknr(right_buf);
Chris Masone089f052007-03-16 16:20:31 -0400542 clean_tree_block(trans, root, right_buf);
Chris Masond6025572007-03-30 14:27:56 -0400543 wait_on_buffer(right_buf);
544 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500545 right_buf = NULL;
546 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400547 wret = del_ptr(trans, root, path, level + 1, pslot +
548 1);
Chris Masonbb803952007-03-01 12:04:21 -0500549 if (wret)
550 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400551 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500552 if (wret)
553 ret = wret;
554 } else {
Chris Masond6025572007-03-30 14:27:56 -0400555 btrfs_memcpy(root, parent,
556 &parent->ptrs[pslot + 1].key,
557 &right->ptrs[0].key,
558 sizeof(struct btrfs_disk_key));
559 btrfs_mark_buffer_dirty(parent_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500560 }
561 }
Chris Mason7518a232007-03-12 12:01:18 -0400562 if (btrfs_header_nritems(&mid->header) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500563 /*
564 * we're not allowed to leave a node with one item in the
565 * tree during a delete. A deletion from lower in the tree
566 * could try to delete the only pointer in this node.
567 * So, pull some keys from the left.
568 * There has to be a left pointer at this point because
569 * otherwise we would have pulled some pointers from the
570 * right
571 */
572 BUG_ON(!left_buf);
Chris Masone089f052007-03-16 16:20:31 -0400573 wret = balance_node_right(trans, root, mid_buf, left_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400574 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500575 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400576 goto enospc;
577 }
Chris Mason79f95c82007-03-01 15:16:26 -0500578 BUG_ON(wret == 1);
579 }
Chris Mason7518a232007-03-12 12:01:18 -0400580 if (btrfs_header_nritems(&mid->header) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500581 /* we've managed to empty the middle node, drop it */
Chris Mason7eccb902007-04-11 15:53:25 -0400582 u64 blocknr = bh_blocknr(mid_buf);
Chris Masone089f052007-03-16 16:20:31 -0400583 clean_tree_block(trans, root, mid_buf);
Chris Masond6025572007-03-30 14:27:56 -0400584 wait_on_buffer(mid_buf);
585 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500586 mid_buf = NULL;
587 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400588 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500589 if (wret)
590 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -0400591 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500592 if (wret)
593 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500594 } else {
595 /* update the parent key to reflect our changes */
Chris Masond6025572007-03-30 14:27:56 -0400596 btrfs_memcpy(root, parent,
597 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
598 sizeof(struct btrfs_disk_key));
599 btrfs_mark_buffer_dirty(parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500600 }
Chris Masonbb803952007-03-01 12:04:21 -0500601
Chris Mason79f95c82007-03-01 15:16:26 -0500602 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500603 if (left_buf) {
Chris Mason7518a232007-03-12 12:01:18 -0400604 if (btrfs_header_nritems(&left->header) > orig_slot) {
Chris Masone20d96d2007-03-22 12:13:20 -0400605 get_bh(left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500606 path->nodes[level] = left_buf;
607 path->slots[level + 1] -= 1;
608 path->slots[level] = orig_slot;
609 if (mid_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400610 btrfs_block_release(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500611 } else {
Chris Mason7518a232007-03-12 12:01:18 -0400612 orig_slot -= btrfs_header_nritems(&left->header);
Chris Masonbb803952007-03-01 12:04:21 -0500613 path->slots[level] = orig_slot;
614 }
615 }
Chris Mason79f95c82007-03-01 15:16:26 -0500616 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400617 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400618 if (orig_ptr !=
619 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
620 path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500621 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400622enospc:
Chris Masonbb803952007-03-01 12:04:21 -0500623 if (right_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400624 btrfs_block_release(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500625 if (left_buf)
Chris Mason234b63a2007-03-13 10:46:10 -0400626 btrfs_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500627 return ret;
628}
629
Chris Masone66f7092007-04-20 13:16:02 -0400630/* returns zero if the push worked, non-zero otherwise */
631static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
632 struct btrfs_root *root,
633 struct btrfs_path *path, int level)
634{
635 struct buffer_head *right_buf;
636 struct buffer_head *mid_buf;
637 struct buffer_head *left_buf;
638 struct buffer_head *parent_buf = NULL;
639 struct btrfs_node *right = NULL;
640 struct btrfs_node *mid;
641 struct btrfs_node *left = NULL;
642 struct btrfs_node *parent = NULL;
643 int ret = 0;
644 int wret;
645 int pslot;
646 int orig_slot = path->slots[level];
647 u64 orig_ptr;
648
649 if (level == 0)
650 return 1;
651
652 mid_buf = path->nodes[level];
653 mid = btrfs_buffer_node(mid_buf);
654 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
655
656 if (level < BTRFS_MAX_LEVEL - 1)
657 parent_buf = path->nodes[level + 1];
658 pslot = path->slots[level + 1];
659
660 if (!parent_buf)
661 return 1;
662 parent = btrfs_buffer_node(parent_buf);
663
664 left_buf = read_node_slot(root, parent_buf, pslot - 1);
665
666 /* first, try to make some room in the middle buffer */
667 if (left_buf) {
668 u32 left_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400669 left = btrfs_buffer_node(left_buf);
670 left_nr = btrfs_header_nritems(&left->header);
Chris Mason33ade1f2007-04-20 13:48:57 -0400671 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
672 wret = 1;
673 } else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400674 ret = btrfs_cow_block(trans, root, left_buf, parent_buf,
675 pslot - 1, &left_buf);
676 if (ret)
677 wret = 1;
678 else {
679 left = btrfs_buffer_node(left_buf);
680 wret = push_node_left(trans, root,
681 left_buf, mid_buf);
682 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400683 }
Chris Masone66f7092007-04-20 13:16:02 -0400684 if (wret < 0)
685 ret = wret;
686 if (wret == 0) {
687 orig_slot += left_nr;
688 btrfs_memcpy(root, parent,
689 &parent->ptrs[pslot].key,
690 &mid->ptrs[0].key,
691 sizeof(struct btrfs_disk_key));
692 btrfs_mark_buffer_dirty(parent_buf);
693 if (btrfs_header_nritems(&left->header) > orig_slot) {
694 path->nodes[level] = left_buf;
695 path->slots[level + 1] -= 1;
696 path->slots[level] = orig_slot;
697 btrfs_block_release(root, mid_buf);
698 } else {
699 orig_slot -=
700 btrfs_header_nritems(&left->header);
701 path->slots[level] = orig_slot;
702 btrfs_block_release(root, left_buf);
703 }
704 check_node(root, path, level);
705 return 0;
706 }
707 btrfs_block_release(root, left_buf);
708 }
709 right_buf = read_node_slot(root, parent_buf, pslot + 1);
710
711 /*
712 * then try to empty the right most buffer into the middle
713 */
714 if (right_buf) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400715 u32 right_nr;
Chris Masone66f7092007-04-20 13:16:02 -0400716 right = btrfs_buffer_node(right_buf);
Chris Mason33ade1f2007-04-20 13:48:57 -0400717 right_nr = btrfs_header_nritems(&right->header);
718 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
719 wret = 1;
720 } else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400721 ret = btrfs_cow_block(trans, root, right_buf,
722 parent_buf, pslot + 1,
723 &right_buf);
724 if (ret)
725 wret = 1;
726 else {
727 right = btrfs_buffer_node(right_buf);
728 wret = balance_node_right(trans, root,
729 right_buf, mid_buf);
730 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400731 }
Chris Masone66f7092007-04-20 13:16:02 -0400732 if (wret < 0)
733 ret = wret;
734 if (wret == 0) {
735 btrfs_memcpy(root, parent,
736 &parent->ptrs[pslot + 1].key,
737 &right->ptrs[0].key,
738 sizeof(struct btrfs_disk_key));
739 btrfs_mark_buffer_dirty(parent_buf);
740 if (btrfs_header_nritems(&mid->header) <= orig_slot) {
741 path->nodes[level] = right_buf;
742 path->slots[level + 1] += 1;
743 path->slots[level] = orig_slot -
744 btrfs_header_nritems(&mid->header);
745 btrfs_block_release(root, mid_buf);
746 } else {
747 btrfs_block_release(root, right_buf);
748 }
749 check_node(root, path, level);
750 return 0;
751 }
752 btrfs_block_release(root, right_buf);
753 }
754 check_node(root, path, level);
755 return 1;
756}
757
Chris Mason74123bd2007-02-02 11:05:29 -0500758/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400759 * readahead one full node of leaves
760 */
761static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason6702ed42007-08-07 16:15:09 -0400762 int level, int slot)
Chris Mason3c69fae2007-08-07 15:52:22 -0400763{
764 struct btrfs_node *node;
765 int i;
766 u32 nritems;
767 u64 item_objectid;
768 u64 blocknr;
769 u64 search;
770 u64 cluster_start;
771 int ret;
772 int nread = 0;
773 int direction = path->reada;
774 struct radix_tree_root found;
775 unsigned long gang[8];
776 struct buffer_head *bh;
777
Chris Mason6702ed42007-08-07 16:15:09 -0400778 if (level == 0)
Chris Mason3c69fae2007-08-07 15:52:22 -0400779 return;
780
Chris Mason6702ed42007-08-07 16:15:09 -0400781 if (!path->nodes[level])
782 return;
783
784 node = btrfs_buffer_node(path->nodes[level]);
Chris Mason3c69fae2007-08-07 15:52:22 -0400785 search = btrfs_node_blockptr(node, slot);
786 bh = btrfs_find_tree_block(root, search);
787 if (bh) {
788 brelse(bh);
789 return;
790 }
791
792 init_bit_radix(&found);
793 nritems = btrfs_header_nritems(&node->header);
794 for (i = slot; i < nritems; i++) {
795 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
796 blocknr = btrfs_node_blockptr(node, i);
797 set_radix_bit(&found, blocknr);
798 }
799 if (direction > 0) {
800 cluster_start = search - 4;
801 if (cluster_start > search)
802 cluster_start = 0;
803 } else
804 cluster_start = search + 4;
805 while(1) {
806 ret = find_first_radix_bit(&found, gang, 0, ARRAY_SIZE(gang));
807 if (!ret)
808 break;
809 for (i = 0; i < ret; i++) {
810 blocknr = gang[i];
811 clear_radix_bit(&found, blocknr);
Chris Mason6702ed42007-08-07 16:15:09 -0400812 if (nread > 32)
Chris Mason3c69fae2007-08-07 15:52:22 -0400813 continue;
814 if (direction > 0 && cluster_start <= blocknr &&
815 cluster_start + 8 > blocknr) {
816 cluster_start = blocknr;
817 readahead_tree_block(root, blocknr);
818 nread++;
819 } else if (direction < 0 && cluster_start >= blocknr &&
820 blocknr + 8 > cluster_start) {
821 cluster_start = blocknr;
822 readahead_tree_block(root, blocknr);
823 nread++;
824 }
825 }
826 }
827}
828/*
Chris Mason74123bd2007-02-02 11:05:29 -0500829 * look for key in the tree. path is filled in with nodes along the way
830 * if key is found, we return zero and you can find the item in the leaf
831 * level of the path (level 0)
832 *
833 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500834 * be inserted, and 1 is returned. If there are other errors during the
835 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500836 *
837 * if ins_len > 0, nodes and leaves will be split as we walk down the
838 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
839 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500840 */
Chris Masone089f052007-03-16 16:20:31 -0400841int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
842 *root, struct btrfs_key *key, struct btrfs_path *p, int
843 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500844{
Chris Masone20d96d2007-03-22 12:13:20 -0400845 struct buffer_head *b;
846 struct buffer_head *cow_buf;
Chris Mason234b63a2007-03-13 10:46:10 -0400847 struct btrfs_node *c;
Chris Mason3c69fae2007-08-07 15:52:22 -0400848 u64 blocknr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500849 int slot;
850 int ret;
851 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -0400852 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -0400853 u8 lowest_level = 0;
854
Chris Mason6702ed42007-08-07 16:15:09 -0400855 lowest_level = p->lowest_level;
856 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400857 WARN_ON(p->nodes[0] != NULL);
858 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500859again:
860 b = root->node;
Chris Masone20d96d2007-03-22 12:13:20 -0400861 get_bh(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500862 while (b) {
Chris Masone20d96d2007-03-22 12:13:20 -0400863 c = btrfs_buffer_node(b);
864 level = btrfs_header_level(&c->header);
Chris Mason02217ed2007-03-02 16:08:05 -0500865 if (cow) {
866 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400867 wret = btrfs_cow_block(trans, root, b,
868 p->nodes[level + 1],
869 p->slots[level + 1],
Chris Masone089f052007-03-16 16:20:31 -0400870 &cow_buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400871 if (wret) {
872 btrfs_block_release(root, cow_buf);
873 return wret;
874 }
Chris Mason02217ed2007-03-02 16:08:05 -0500875 b = cow_buf;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400876 c = btrfs_buffer_node(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500877 }
878 BUG_ON(!cow && ins_len);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400879 if (level != btrfs_header_level(&c->header))
880 WARN_ON(1);
881 level = btrfs_header_level(&c->header);
Chris Masoneb60cea2007-02-02 09:18:22 -0500882 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -0400883 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500884 if (ret)
885 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500886 ret = bin_search(c, key, &slot);
Chris Mason7518a232007-03-12 12:01:18 -0400887 if (!btrfs_is_leaf(c)) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500888 if (ret && slot > 0)
889 slot -= 1;
890 p->slots[level] = slot;
Chris Masond4dbff92007-04-04 14:08:15 -0400891 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
892 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -0400893 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -0500894 BUG_ON(sret > 0);
895 if (sret)
896 return sret;
897 b = p->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -0400898 c = btrfs_buffer_node(b);
Chris Mason5c680ed2007-02-22 11:39:13 -0500899 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500900 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -0400901 int sret = balance_level(trans, root, p,
902 level);
Chris Masonbb803952007-03-01 12:04:21 -0500903 if (sret)
904 return sret;
905 b = p->nodes[level];
906 if (!b)
907 goto again;
Chris Masone20d96d2007-03-22 12:13:20 -0400908 c = btrfs_buffer_node(b);
Chris Masonbb803952007-03-01 12:04:21 -0500909 slot = p->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400910 BUG_ON(btrfs_header_nritems(&c->header) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500911 }
Chris Mason9f3a7422007-08-07 15:52:19 -0400912 /* this is only true while dropping a snapshot */
913 if (level == lowest_level)
914 break;
Chris Mason3c69fae2007-08-07 15:52:22 -0400915 blocknr = btrfs_node_blockptr(c, slot);
Chris Mason6702ed42007-08-07 16:15:09 -0400916 if (should_reada)
917 reada_for_search(root, p, level, slot);
Chris Mason1d4f8a02007-03-13 09:28:32 -0400918 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
Chris Mason3c69fae2007-08-07 15:52:22 -0400919
Chris Masonbe0e5c02007-01-26 15:51:26 -0500920 } else {
Chris Mason234b63a2007-03-13 10:46:10 -0400921 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500922 p->slots[level] = slot;
Chris Mason123abc82007-03-14 14:14:43 -0400923 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
Chris Mason0783fcf2007-03-12 20:12:07 -0400924 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -0400925 int sret = split_leaf(trans, root, key,
926 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -0500927 BUG_ON(sret > 0);
928 if (sret)
929 return sret;
930 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500931 return ret;
932 }
933 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500934 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500935}
936
Chris Mason74123bd2007-02-02 11:05:29 -0500937/*
938 * adjust the pointers going up the tree, starting at level
939 * making sure the right key of each node is points to 'key'.
940 * This is used after shifting pointers to the left, so it stops
941 * fixing up pointers when a given leaf/node is not in slot 0 of the
942 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500943 *
944 * If this fails to write a tree block, it returns -1, but continues
945 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500946 */
Chris Masone089f052007-03-16 16:20:31 -0400947static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
948 *root, struct btrfs_path *path, struct btrfs_disk_key
949 *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500950{
951 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500952 int ret = 0;
Chris Mason234b63a2007-03-13 10:46:10 -0400953 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
954 struct btrfs_node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500955 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500956 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500957 break;
Chris Masone20d96d2007-03-22 12:13:20 -0400958 t = btrfs_buffer_node(path->nodes[i]);
Chris Masond6025572007-03-30 14:27:56 -0400959 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
960 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500961 if (tslot != 0)
962 break;
963 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500964 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500965}
966
Chris Mason74123bd2007-02-02 11:05:29 -0500967/*
968 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500969 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500970 *
971 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
972 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500973 */
Chris Masone089f052007-03-16 16:20:31 -0400974static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -0400975 *root, struct buffer_head *dst_buf, struct
976 buffer_head *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500977{
Chris Masone20d96d2007-03-22 12:13:20 -0400978 struct btrfs_node *src = btrfs_buffer_node(src_buf);
979 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500980 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500981 int src_nritems;
982 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500983 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500984
Chris Mason7518a232007-03-12 12:01:18 -0400985 src_nritems = btrfs_header_nritems(&src->header);
986 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -0400987 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -0400988
Chris Masoneb60cea2007-02-02 09:18:22 -0500989 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500990 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500991 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500992
993 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500994 push_items = src_nritems;
995
Chris Masond6025572007-03-30 14:27:56 -0400996 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
997 push_items * sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -0500998 if (push_items < src_nritems) {
Chris Masond6025572007-03-30 14:27:56 -0400999 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
Chris Masone2fa7222007-03-12 16:22:34 -04001000 (src_nritems - push_items) *
Chris Mason123abc82007-03-14 14:14:43 -04001001 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001002 }
Chris Mason7518a232007-03-12 12:01:18 -04001003 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
1004 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Masond6025572007-03-30 14:27:56 -04001005 btrfs_mark_buffer_dirty(src_buf);
1006 btrfs_mark_buffer_dirty(dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001007 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001008}
1009
Chris Mason97571fd2007-02-24 13:39:08 -05001010/*
Chris Mason79f95c82007-03-01 15:16:26 -05001011 * try to push data from one node into the next node right in the
1012 * tree.
1013 *
1014 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1015 * error, and > 0 if there was no room in the right hand block.
1016 *
1017 * this will only push up to 1/2 the contents of the left node over
1018 */
Chris Masone089f052007-03-16 16:20:31 -04001019static int balance_node_right(struct btrfs_trans_handle *trans, struct
Chris Masone20d96d2007-03-22 12:13:20 -04001020 btrfs_root *root, struct buffer_head *dst_buf,
1021 struct buffer_head *src_buf)
Chris Mason79f95c82007-03-01 15:16:26 -05001022{
Chris Masone20d96d2007-03-22 12:13:20 -04001023 struct btrfs_node *src = btrfs_buffer_node(src_buf);
1024 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -05001025 int push_items = 0;
1026 int max_push;
1027 int src_nritems;
1028 int dst_nritems;
1029 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001030
Chris Mason7518a232007-03-12 12:01:18 -04001031 src_nritems = btrfs_header_nritems(&src->header);
1032 dst_nritems = btrfs_header_nritems(&dst->header);
Chris Mason123abc82007-03-14 14:14:43 -04001033 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason79f95c82007-03-01 15:16:26 -05001034 if (push_items <= 0) {
1035 return 1;
1036 }
1037
1038 max_push = src_nritems / 2 + 1;
1039 /* don't try to empty the node */
1040 if (max_push > src_nritems)
1041 return 1;
1042 if (max_push < push_items)
1043 push_items = max_push;
1044
Chris Masond6025572007-03-30 14:27:56 -04001045 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
1046 dst_nritems * sizeof(struct btrfs_key_ptr));
1047
1048 btrfs_memcpy(root, dst, dst->ptrs,
1049 src->ptrs + src_nritems - push_items,
1050 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001051
Chris Mason7518a232007-03-12 12:01:18 -04001052 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
1053 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001054
Chris Masond6025572007-03-30 14:27:56 -04001055 btrfs_mark_buffer_dirty(src_buf);
1056 btrfs_mark_buffer_dirty(dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -05001057 return ret;
1058}
1059
1060/*
Chris Mason97571fd2007-02-24 13:39:08 -05001061 * helper function to insert a new root level in the tree.
1062 * A new node is allocated, and a single item is inserted to
1063 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001064 *
1065 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001066 */
Chris Masone089f052007-03-16 16:20:31 -04001067static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
1068 *root, struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001069{
Chris Masone20d96d2007-03-22 12:13:20 -04001070 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001071 struct btrfs_node *lower;
1072 struct btrfs_node *c;
Chris Masone2fa7222007-03-12 16:22:34 -04001073 struct btrfs_disk_key *lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001074
1075 BUG_ON(path->nodes[level]);
1076 BUG_ON(path->nodes[level-1] != root->node);
1077
Chris Mason6702ed42007-08-07 16:15:09 -04001078 t = btrfs_alloc_free_block(trans, root, root->node->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001079 if (IS_ERR(t))
1080 return PTR_ERR(t);
Chris Masone20d96d2007-03-22 12:13:20 -04001081 c = btrfs_buffer_node(t);
Chris Mason123abc82007-03-14 14:14:43 -04001082 memset(c, 0, root->blocksize);
Chris Mason7518a232007-03-12 12:01:18 -04001083 btrfs_set_header_nritems(&c->header, 1);
1084 btrfs_set_header_level(&c->header, level);
Chris Mason7eccb902007-04-11 15:53:25 -04001085 btrfs_set_header_blocknr(&c->header, bh_blocknr(t));
Chris Mason7f5c1512007-03-23 15:56:19 -04001086 btrfs_set_header_generation(&c->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001087 btrfs_set_header_owner(&c->header, root->root_key.objectid);
Chris Masone20d96d2007-03-22 12:13:20 -04001088 lower = btrfs_buffer_node(path->nodes[level-1]);
Chris Mason3eb03142007-04-05 14:28:50 -04001089 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
1090 sizeof(c->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -04001091 if (btrfs_is_leaf(lower))
Chris Mason234b63a2007-03-13 10:46:10 -04001092 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001093 else
Chris Mason123abc82007-03-14 14:14:43 -04001094 lower_key = &lower->ptrs[0].key;
Chris Masond6025572007-03-30 14:27:56 -04001095 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
1096 sizeof(struct btrfs_disk_key));
Chris Mason7eccb902007-04-11 15:53:25 -04001097 btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1]));
Chris Masond5719762007-03-23 10:01:08 -04001098
Chris Masond6025572007-03-30 14:27:56 -04001099 btrfs_mark_buffer_dirty(t);
Chris Masond5719762007-03-23 10:01:08 -04001100
Chris Mason5c680ed2007-02-22 11:39:13 -05001101 /* the super has an extra ref to root->node */
Chris Mason234b63a2007-03-13 10:46:10 -04001102 btrfs_block_release(root, root->node);
Chris Mason5c680ed2007-02-22 11:39:13 -05001103 root->node = t;
Chris Masone20d96d2007-03-22 12:13:20 -04001104 get_bh(t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001105 path->nodes[level] = t;
1106 path->slots[level] = 0;
1107 return 0;
1108}
1109
Chris Mason74123bd2007-02-02 11:05:29 -05001110/*
1111 * worker function to insert a single pointer in a node.
1112 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001113 *
Chris Mason74123bd2007-02-02 11:05:29 -05001114 * slot and level indicate where you want the key to go, and
1115 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001116 *
1117 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001118 */
Chris Masone089f052007-03-16 16:20:31 -04001119static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1120 *root, struct btrfs_path *path, struct btrfs_disk_key
1121 *key, u64 blocknr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001122{
Chris Mason234b63a2007-03-13 10:46:10 -04001123 struct btrfs_node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001124 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001125
1126 BUG_ON(!path->nodes[level]);
Chris Masone20d96d2007-03-22 12:13:20 -04001127 lower = btrfs_buffer_node(path->nodes[level]);
Chris Mason7518a232007-03-12 12:01:18 -04001128 nritems = btrfs_header_nritems(&lower->header);
Chris Mason74123bd2007-02-02 11:05:29 -05001129 if (slot > nritems)
1130 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001131 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001132 BUG();
1133 if (slot != nritems) {
Chris Masond6025572007-03-30 14:27:56 -04001134 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
1135 lower->ptrs + slot,
1136 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001137 }
Chris Masond6025572007-03-30 14:27:56 -04001138 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
1139 key, sizeof(struct btrfs_disk_key));
Chris Mason1d4f8a02007-03-13 09:28:32 -04001140 btrfs_set_node_blockptr(lower, slot, blocknr);
Chris Mason7518a232007-03-12 12:01:18 -04001141 btrfs_set_header_nritems(&lower->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001142 btrfs_mark_buffer_dirty(path->nodes[level]);
Chris Mason098f59c2007-05-11 11:33:21 -04001143 check_node(root, path, level);
Chris Mason74123bd2007-02-02 11:05:29 -05001144 return 0;
1145}
1146
Chris Mason97571fd2007-02-24 13:39:08 -05001147/*
1148 * split the node at the specified level in path in two.
1149 * The path is corrected to point to the appropriate node after the split
1150 *
1151 * Before splitting this tries to make some room in the node by pushing
1152 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001153 *
1154 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001155 */
Chris Masone089f052007-03-16 16:20:31 -04001156static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1157 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001158{
Chris Masone20d96d2007-03-22 12:13:20 -04001159 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001160 struct btrfs_node *c;
Chris Masone20d96d2007-03-22 12:13:20 -04001161 struct buffer_head *split_buffer;
Chris Mason234b63a2007-03-13 10:46:10 -04001162 struct btrfs_node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001163 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001164 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001165 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001166 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001167
Chris Mason5c680ed2007-02-22 11:39:13 -05001168 t = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04001169 c = btrfs_buffer_node(t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001170 if (t == root->node) {
1171 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001172 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001173 if (ret)
1174 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001175 } else {
1176 ret = push_nodes_for_insert(trans, root, path, level);
1177 t = path->nodes[level];
1178 c = btrfs_buffer_node(t);
1179 if (!ret &&
1180 btrfs_header_nritems(&c->header) <
1181 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1182 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001183 if (ret < 0)
1184 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001185 }
Chris Masone66f7092007-04-20 13:16:02 -04001186
Chris Mason7518a232007-03-12 12:01:18 -04001187 c_nritems = btrfs_header_nritems(&c->header);
Chris Mason6702ed42007-08-07 16:15:09 -04001188 split_buffer = btrfs_alloc_free_block(trans, root, t->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001189 if (IS_ERR(split_buffer))
1190 return PTR_ERR(split_buffer);
1191
Chris Masone20d96d2007-03-22 12:13:20 -04001192 split = btrfs_buffer_node(split_buffer);
Chris Mason7518a232007-03-12 12:01:18 -04001193 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
Chris Mason9a6f11e2007-03-27 09:06:38 -04001194 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001195 btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001196 btrfs_set_header_generation(&split->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001197 btrfs_set_header_owner(&split->header, root->root_key.objectid);
Chris Mason3eb03142007-04-05 14:28:50 -04001198 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
1199 sizeof(split->header.fsid));
Chris Mason7518a232007-03-12 12:01:18 -04001200 mid = (c_nritems + 1) / 2;
Chris Masond6025572007-03-30 14:27:56 -04001201 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
1202 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
Chris Mason7518a232007-03-12 12:01:18 -04001203 btrfs_set_header_nritems(&split->header, c_nritems - mid);
1204 btrfs_set_header_nritems(&c->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001205 ret = 0;
1206
Chris Masond6025572007-03-30 14:27:56 -04001207 btrfs_mark_buffer_dirty(t);
1208 btrfs_mark_buffer_dirty(split_buffer);
Chris Masone089f052007-03-16 16:20:31 -04001209 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001210 bh_blocknr(split_buffer), path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001211 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001212 if (wret)
1213 ret = wret;
1214
Chris Mason5de08d72007-02-24 06:24:44 -05001215 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001216 path->slots[level] -= mid;
Chris Mason234b63a2007-03-13 10:46:10 -04001217 btrfs_block_release(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -05001218 path->nodes[level] = split_buffer;
1219 path->slots[level + 1] += 1;
1220 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001221 btrfs_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001222 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001223 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001224}
1225
Chris Mason74123bd2007-02-02 11:05:29 -05001226/*
1227 * how many bytes are required to store the items in a leaf. start
1228 * and nr indicate which items in the leaf to check. This totals up the
1229 * space used both by the item structs and the item data
1230 */
Chris Mason234b63a2007-03-13 10:46:10 -04001231static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001232{
1233 int data_len;
Chris Masond4dbff92007-04-04 14:08:15 -04001234 int nritems = btrfs_header_nritems(&l->header);
1235 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001236
1237 if (!nr)
1238 return 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001239 data_len = btrfs_item_end(l->items + start);
1240 data_len = data_len - btrfs_item_offset(l->items + end);
1241 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001242 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001243 return data_len;
1244}
1245
Chris Mason74123bd2007-02-02 11:05:29 -05001246/*
Chris Masond4dbff92007-04-04 14:08:15 -04001247 * The space between the end of the leaf items and
1248 * the start of the leaf data. IOW, how much room
1249 * the leaf has left for both items and data
1250 */
1251int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
1252{
1253 int nritems = btrfs_header_nritems(&leaf->header);
1254 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1255}
1256
1257/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001258 * push some data in the path leaf to the right, trying to free up at
1259 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001260 *
1261 * returns 1 if the push failed because the other node didn't have enough
1262 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001263 */
Chris Masone089f052007-03-16 16:20:31 -04001264static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1265 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -05001266{
Chris Masone20d96d2007-03-22 12:13:20 -04001267 struct buffer_head *left_buf = path->nodes[0];
1268 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04001269 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001270 struct buffer_head *right_buf;
1271 struct buffer_head *upper;
1272 struct btrfs_node *upper_node;
Chris Mason00ec4c52007-02-24 12:47:20 -05001273 int slot;
1274 int i;
1275 int free_space;
1276 int push_space = 0;
1277 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001278 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001279 u32 left_nritems;
1280 u32 right_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001281 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001282
1283 slot = path->slots[1];
1284 if (!path->nodes[1]) {
1285 return 1;
1286 }
1287 upper = path->nodes[1];
Chris Masone20d96d2007-03-22 12:13:20 -04001288 upper_node = btrfs_buffer_node(upper);
1289 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001290 return 1;
1291 }
Chris Masone20d96d2007-03-22 12:13:20 -04001292 right_buf = read_tree_block(root,
1293 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
1294 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001295 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001296 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001297 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001298 return 1;
1299 }
Chris Mason02217ed2007-03-02 16:08:05 -05001300 /* cow and double check */
Chris Mason54aa1f42007-06-22 14:16:25 -04001301 ret = btrfs_cow_block(trans, root, right_buf, upper,
1302 slot + 1, &right_buf);
1303 if (ret) {
1304 btrfs_block_release(root, right_buf);
1305 return 1;
1306 }
Chris Masone20d96d2007-03-22 12:13:20 -04001307 right = btrfs_buffer_leaf(right_buf);
Chris Mason123abc82007-03-14 14:14:43 -04001308 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001309 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001310 btrfs_block_release(root, right_buf);
Chris Mason02217ed2007-03-02 16:08:05 -05001311 return 1;
1312 }
1313
Chris Mason7518a232007-03-12 12:01:18 -04001314 left_nritems = btrfs_header_nritems(&left->header);
Chris Masona429e512007-04-18 16:15:28 -04001315 if (left_nritems == 0) {
1316 btrfs_block_release(root, right_buf);
1317 return 1;
1318 }
1319 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason00ec4c52007-02-24 12:47:20 -05001320 item = left->items + i;
1321 if (path->slots[0] == i)
1322 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001323 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1324 free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001325 break;
1326 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001327 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Mason00ec4c52007-02-24 12:47:20 -05001328 }
1329 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001330 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001331 return 1;
1332 }
Chris Masona429e512007-04-18 16:15:28 -04001333 if (push_items == left_nritems)
1334 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001335 right_nritems = btrfs_header_nritems(&right->header);
Chris Mason00ec4c52007-02-24 12:47:20 -05001336 /* push left to right */
Chris Mason0783fcf2007-03-12 20:12:07 -04001337 push_space = btrfs_item_end(left->items + left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001338 push_space -= leaf_data_end(root, left);
Chris Mason00ec4c52007-02-24 12:47:20 -05001339 /* make room in the right data area */
Chris Masond6025572007-03-30 14:27:56 -04001340 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1341 leaf_data_end(root, right) - push_space,
1342 btrfs_leaf_data(right) +
1343 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
1344 leaf_data_end(root, right));
Chris Mason00ec4c52007-02-24 12:47:20 -05001345 /* copy from the left data area */
Chris Masond6025572007-03-30 14:27:56 -04001346 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
1347 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1348 btrfs_leaf_data(left) + leaf_data_end(root, left),
1349 push_space);
1350 btrfs_memmove(root, right, right->items + push_items, right->items,
Chris Mason0783fcf2007-03-12 20:12:07 -04001351 right_nritems * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001352 /* copy the items from left to right */
Chris Masond6025572007-03-30 14:27:56 -04001353 btrfs_memcpy(root, right, right->items, left->items +
1354 left_nritems - push_items,
1355 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001356
1357 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001358 right_nritems += push_items;
1359 btrfs_set_header_nritems(&right->header, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001360 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001361 for (i = 0; i < right_nritems; i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001362 btrfs_set_item_offset(right->items + i, push_space -
1363 btrfs_item_size(right->items + i));
1364 push_space = btrfs_item_offset(right->items + i);
Chris Mason00ec4c52007-02-24 12:47:20 -05001365 }
Chris Mason7518a232007-03-12 12:01:18 -04001366 left_nritems -= push_items;
1367 btrfs_set_header_nritems(&left->header, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001368
Chris Masond6025572007-03-30 14:27:56 -04001369 btrfs_mark_buffer_dirty(left_buf);
1370 btrfs_mark_buffer_dirty(right_buf);
Chris Masona429e512007-04-18 16:15:28 -04001371
Chris Masond6025572007-03-30 14:27:56 -04001372 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
Chris Masone2fa7222007-03-12 16:22:34 -04001373 &right->items[0].key, sizeof(struct btrfs_disk_key));
Chris Masond6025572007-03-30 14:27:56 -04001374 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001375
Chris Mason00ec4c52007-02-24 12:47:20 -05001376 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001377 if (path->slots[0] >= left_nritems) {
1378 path->slots[0] -= left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001379 btrfs_block_release(root, path->nodes[0]);
Chris Mason00ec4c52007-02-24 12:47:20 -05001380 path->nodes[0] = right_buf;
1381 path->slots[1] += 1;
1382 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001383 btrfs_block_release(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -05001384 }
Chris Mason098f59c2007-05-11 11:33:21 -04001385 if (path->nodes[1])
1386 check_node(root, path, 1);
Chris Mason00ec4c52007-02-24 12:47:20 -05001387 return 0;
1388}
1389/*
Chris Mason74123bd2007-02-02 11:05:29 -05001390 * push some data in the path leaf to the left, trying to free up at
1391 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1392 */
Chris Masone089f052007-03-16 16:20:31 -04001393static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1394 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001395{
Chris Masone20d96d2007-03-22 12:13:20 -04001396 struct buffer_head *right_buf = path->nodes[0];
1397 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
1398 struct buffer_head *t;
Chris Mason234b63a2007-03-13 10:46:10 -04001399 struct btrfs_leaf *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001400 int slot;
1401 int i;
1402 int free_space;
1403 int push_space = 0;
1404 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001405 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001406 u32 old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001407 int ret = 0;
1408 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001409
1410 slot = path->slots[1];
1411 if (slot == 0) {
1412 return 1;
1413 }
1414 if (!path->nodes[1]) {
1415 return 1;
1416 }
Chris Masone20d96d2007-03-22 12:13:20 -04001417 t = read_tree_block(root,
1418 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
1419 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001420 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001421 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001422 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001423 return 1;
1424 }
Chris Mason02217ed2007-03-02 16:08:05 -05001425
1426 /* cow and double check */
Chris Mason54aa1f42007-06-22 14:16:25 -04001427 ret = btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
1428 if (ret) {
1429 /* we hit -ENOSPC, but it isn't fatal here */
1430 return 1;
1431 }
Chris Masone20d96d2007-03-22 12:13:20 -04001432 left = btrfs_buffer_leaf(t);
Chris Mason123abc82007-03-14 14:14:43 -04001433 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001434 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason234b63a2007-03-13 10:46:10 -04001435 btrfs_block_release(root, t);
Chris Mason02217ed2007-03-02 16:08:05 -05001436 return 1;
1437 }
1438
Chris Masona429e512007-04-18 16:15:28 -04001439 if (btrfs_header_nritems(&right->header) == 0) {
1440 btrfs_block_release(root, t);
1441 return 1;
1442 }
1443
1444 for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001445 item = right->items + i;
1446 if (path->slots[0] == i)
1447 push_space += data_size + sizeof(*item);
Chris Mason0783fcf2007-03-12 20:12:07 -04001448 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1449 free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001450 break;
1451 push_items++;
Chris Mason0783fcf2007-03-12 20:12:07 -04001452 push_space += btrfs_item_size(item) + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001453 }
1454 if (push_items == 0) {
Chris Mason234b63a2007-03-13 10:46:10 -04001455 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001456 return 1;
1457 }
Chris Masona429e512007-04-18 16:15:28 -04001458 if (push_items == btrfs_header_nritems(&right->header))
1459 WARN_ON(1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001460 /* push data from right to left */
Chris Masond6025572007-03-30 14:27:56 -04001461 btrfs_memcpy(root, left, left->items +
1462 btrfs_header_nritems(&left->header),
1463 right->items, push_items * sizeof(struct btrfs_item));
Chris Mason123abc82007-03-14 14:14:43 -04001464 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001465 btrfs_item_offset(right->items + push_items -1);
Chris Masond6025572007-03-30 14:27:56 -04001466 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1467 leaf_data_end(root, left) - push_space,
1468 btrfs_leaf_data(right) +
1469 btrfs_item_offset(right->items + push_items - 1),
1470 push_space);
Chris Mason7518a232007-03-12 12:01:18 -04001471 old_left_nritems = btrfs_header_nritems(&left->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001472 BUG_ON(old_left_nritems < 0);
1473
Chris Mason0783fcf2007-03-12 20:12:07 -04001474 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001475 u32 ioff = btrfs_item_offset(left->items + i);
1476 btrfs_set_item_offset(left->items + i, ioff -
1477 (BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason0783fcf2007-03-12 20:12:07 -04001478 btrfs_item_offset(left->items +
1479 old_left_nritems - 1)));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001480 }
Chris Mason7518a232007-03-12 12:01:18 -04001481 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001482
1483 /* fixup right node */
Chris Mason0783fcf2007-03-12 20:12:07 -04001484 push_space = btrfs_item_offset(right->items + push_items - 1) -
Chris Mason123abc82007-03-14 14:14:43 -04001485 leaf_data_end(root, right);
Chris Masond6025572007-03-30 14:27:56 -04001486 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1487 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1488 btrfs_leaf_data(right) +
1489 leaf_data_end(root, right), push_space);
1490 btrfs_memmove(root, right, right->items, right->items + push_items,
Chris Mason7518a232007-03-12 12:01:18 -04001491 (btrfs_header_nritems(&right->header) - push_items) *
Chris Mason0783fcf2007-03-12 20:12:07 -04001492 sizeof(struct btrfs_item));
Chris Mason7518a232007-03-12 12:01:18 -04001493 btrfs_set_header_nritems(&right->header,
1494 btrfs_header_nritems(&right->header) -
1495 push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001496 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001497
Chris Mason7518a232007-03-12 12:01:18 -04001498 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason0783fcf2007-03-12 20:12:07 -04001499 btrfs_set_item_offset(right->items + i, push_space -
1500 btrfs_item_size(right->items + i));
1501 push_space = btrfs_item_offset(right->items + i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001502 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001503
Chris Masond6025572007-03-30 14:27:56 -04001504 btrfs_mark_buffer_dirty(t);
1505 btrfs_mark_buffer_dirty(right_buf);
Chris Mason098f59c2007-05-11 11:33:21 -04001506
Chris Masone089f052007-03-16 16:20:31 -04001507 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001508 if (wret)
1509 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001510
1511 /* then fixup the leaf pointer in the path */
1512 if (path->slots[0] < push_items) {
1513 path->slots[0] += old_left_nritems;
Chris Mason234b63a2007-03-13 10:46:10 -04001514 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001515 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001516 path->slots[1] -= 1;
1517 } else {
Chris Mason234b63a2007-03-13 10:46:10 -04001518 btrfs_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001519 path->slots[0] -= push_items;
1520 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001521 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001522 if (path->nodes[1])
1523 check_node(root, path, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001524 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001525}
1526
Chris Mason74123bd2007-02-02 11:05:29 -05001527/*
1528 * split the path's leaf in two, making sure there is at least data_size
1529 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001530 *
1531 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001532 */
Chris Masone089f052007-03-16 16:20:31 -04001533static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001534 *root, struct btrfs_key *ins_key,
1535 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001536{
Chris Masone20d96d2007-03-22 12:13:20 -04001537 struct buffer_head *l_buf;
Chris Mason234b63a2007-03-13 10:46:10 -04001538 struct btrfs_leaf *l;
Chris Mason7518a232007-03-12 12:01:18 -04001539 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001540 int mid;
1541 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001542 struct btrfs_leaf *right;
Chris Masone20d96d2007-03-22 12:13:20 -04001543 struct buffer_head *right_buffer;
Chris Mason0783fcf2007-03-12 20:12:07 -04001544 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001545 int data_copy_size;
1546 int rt_data_off;
1547 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001548 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001549 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001550 int double_split = 0;
1551 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001552
Chris Mason40689472007-03-17 14:29:23 -04001553 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001554 wret = push_leaf_left(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001555 if (wret < 0)
1556 return wret;
1557 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001558 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001559 if (wret < 0)
1560 return wret;
1561 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001562 l_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001563 l = btrfs_buffer_leaf(l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001564
1565 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001566 if (btrfs_leaf_free_space(root, l) >=
1567 sizeof(struct btrfs_item) + data_size)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001568 return 0;
1569
Chris Mason5c680ed2007-02-22 11:39:13 -05001570 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001571 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001572 if (ret)
1573 return ret;
1574 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001575 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -04001576 nritems = btrfs_header_nritems(&l->header);
Chris Masoneb60cea2007-02-02 09:18:22 -05001577 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001578
Chris Mason6702ed42007-08-07 16:15:09 -04001579 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001580 if (IS_ERR(right_buffer))
1581 return PTR_ERR(right_buffer);
1582
Chris Masone20d96d2007-03-22 12:13:20 -04001583 right = btrfs_buffer_leaf(right_buffer);
Chris Mason123abc82007-03-14 14:14:43 -04001584 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001585 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Mason7f5c1512007-03-23 15:56:19 -04001586 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001587 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Mason7518a232007-03-12 12:01:18 -04001588 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001589 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1590 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001591 if (mid <= slot) {
1592 if (nritems == 1 ||
1593 leaf_space_used(l, mid, nritems - mid) + space_needed >
1594 BTRFS_LEAF_DATA_SIZE(root)) {
1595 if (slot >= nritems) {
1596 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1597 btrfs_set_header_nritems(&right->header, 0);
1598 wret = insert_ptr(trans, root, path,
1599 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001600 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001601 path->slots[1] + 1, 1);
1602 if (wret)
1603 ret = wret;
1604 btrfs_block_release(root, path->nodes[0]);
1605 path->nodes[0] = right_buffer;
1606 path->slots[0] = 0;
1607 path->slots[1] += 1;
1608 return ret;
1609 }
1610 mid = slot;
1611 double_split = 1;
1612 }
1613 } else {
1614 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1615 BTRFS_LEAF_DATA_SIZE(root)) {
1616 if (slot == 0) {
1617 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1618 btrfs_set_header_nritems(&right->header, 0);
1619 wret = insert_ptr(trans, root, path,
1620 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001621 bh_blocknr(right_buffer),
Chris Mason098f59c2007-05-11 11:33:21 -04001622 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001623 if (wret)
1624 ret = wret;
1625 btrfs_block_release(root, path->nodes[0]);
1626 path->nodes[0] = right_buffer;
1627 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04001628 if (path->slots[1] == 0) {
1629 wret = fixup_low_keys(trans, root,
1630 path, &disk_key, 1);
1631 if (wret)
1632 ret = wret;
1633 }
Chris Masond4dbff92007-04-04 14:08:15 -04001634 return ret;
1635 }
1636 mid = slot;
1637 double_split = 1;
1638 }
1639 }
1640 btrfs_set_header_nritems(&right->header, nritems - mid);
Chris Mason123abc82007-03-14 14:14:43 -04001641 data_copy_size = btrfs_item_end(l->items + mid) -
1642 leaf_data_end(root, l);
Chris Masond6025572007-03-30 14:27:56 -04001643 btrfs_memcpy(root, right, right->items, l->items + mid,
1644 (nritems - mid) * sizeof(struct btrfs_item));
1645 btrfs_memcpy(root, right,
1646 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1647 data_copy_size, btrfs_leaf_data(l) +
1648 leaf_data_end(root, l), data_copy_size);
Chris Mason123abc82007-03-14 14:14:43 -04001649 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1650 btrfs_item_end(l->items + mid);
Chris Mason74123bd2007-02-02 11:05:29 -05001651
Chris Mason0783fcf2007-03-12 20:12:07 -04001652 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001653 u32 ioff = btrfs_item_offset(right->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001654 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1655 }
Chris Mason74123bd2007-02-02 11:05:29 -05001656
Chris Mason7518a232007-03-12 12:01:18 -04001657 btrfs_set_header_nritems(&l->header, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001658 ret = 0;
Chris Masone089f052007-03-16 16:20:31 -04001659 wret = insert_ptr(trans, root, path, &right->items[0].key,
Chris Mason7eccb902007-04-11 15:53:25 -04001660 bh_blocknr(right_buffer), path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001661 if (wret)
1662 ret = wret;
Chris Masond6025572007-03-30 14:27:56 -04001663 btrfs_mark_buffer_dirty(right_buffer);
1664 btrfs_mark_buffer_dirty(l_buf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001665 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001666 if (mid <= slot) {
Chris Mason234b63a2007-03-13 10:46:10 -04001667 btrfs_block_release(root, path->nodes[0]);
Chris Masoneb60cea2007-02-02 09:18:22 -05001668 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001669 path->slots[0] -= mid;
1670 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001671 } else
Chris Mason234b63a2007-03-13 10:46:10 -04001672 btrfs_block_release(root, right_buffer);
Chris Masoneb60cea2007-02-02 09:18:22 -05001673 BUG_ON(path->slots[0] < 0);
Chris Mason098f59c2007-05-11 11:33:21 -04001674 check_node(root, path, 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001675
1676 if (!double_split)
1677 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001678 right_buffer = btrfs_alloc_free_block(trans, root, l_buf->b_blocknr, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001679 if (IS_ERR(right_buffer))
1680 return PTR_ERR(right_buffer);
1681
Chris Masond4dbff92007-04-04 14:08:15 -04001682 right = btrfs_buffer_leaf(right_buffer);
1683 memset(&right->header, 0, sizeof(right->header));
Chris Mason7eccb902007-04-11 15:53:25 -04001684 btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
Chris Masond4dbff92007-04-04 14:08:15 -04001685 btrfs_set_header_generation(&right->header, trans->transid);
Chris Mason4d775672007-04-20 20:23:12 -04001686 btrfs_set_header_owner(&right->header, root->root_key.objectid);
Chris Masond4dbff92007-04-04 14:08:15 -04001687 btrfs_set_header_level(&right->header, 0);
Chris Mason3eb03142007-04-05 14:28:50 -04001688 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1689 sizeof(right->header.fsid));
Chris Masond4dbff92007-04-04 14:08:15 -04001690 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1691 btrfs_set_header_nritems(&right->header, 0);
1692 wret = insert_ptr(trans, root, path,
1693 &disk_key,
Chris Mason7eccb902007-04-11 15:53:25 -04001694 bh_blocknr(right_buffer),
Chris Masond4dbff92007-04-04 14:08:15 -04001695 path->slots[1], 1);
1696 if (wret)
1697 ret = wret;
Chris Masona429e512007-04-18 16:15:28 -04001698 if (path->slots[1] == 0) {
1699 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1700 if (wret)
1701 ret = wret;
1702 }
Chris Masond4dbff92007-04-04 14:08:15 -04001703 btrfs_block_release(root, path->nodes[0]);
1704 path->nodes[0] = right_buffer;
1705 path->slots[0] = 0;
1706 check_node(root, path, 1);
1707 check_leaf(root, path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001708 return ret;
1709}
1710
Chris Masonb18c6682007-04-17 13:26:50 -04001711int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1712 struct btrfs_root *root,
1713 struct btrfs_path *path,
1714 u32 new_size)
1715{
1716 int ret = 0;
1717 int slot;
1718 int slot_orig;
1719 struct btrfs_leaf *leaf;
1720 struct buffer_head *leaf_buf;
1721 u32 nritems;
1722 unsigned int data_end;
1723 unsigned int old_data_start;
1724 unsigned int old_size;
1725 unsigned int size_diff;
1726 int i;
1727
1728 slot_orig = path->slots[0];
1729 leaf_buf = path->nodes[0];
1730 leaf = btrfs_buffer_leaf(leaf_buf);
1731
1732 nritems = btrfs_header_nritems(&leaf->header);
1733 data_end = leaf_data_end(root, leaf);
1734
1735 slot = path->slots[0];
1736 old_data_start = btrfs_item_offset(leaf->items + slot);
1737 old_size = btrfs_item_size(leaf->items + slot);
1738 BUG_ON(old_size <= new_size);
1739 size_diff = old_size - new_size;
1740
1741 BUG_ON(slot < 0);
1742 BUG_ON(slot >= nritems);
1743
1744 /*
1745 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1746 */
1747 /* first correct the data pointers */
1748 for (i = slot; i < nritems; i++) {
1749 u32 ioff = btrfs_item_offset(leaf->items + i);
1750 btrfs_set_item_offset(leaf->items + i,
1751 ioff + size_diff);
1752 }
1753 /* shift the data */
Chris Masonb18c6682007-04-17 13:26:50 -04001754 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1755 data_end + size_diff, btrfs_leaf_data(leaf) +
1756 data_end, old_data_start + new_size - data_end);
1757 btrfs_set_item_size(leaf->items + slot, new_size);
1758 btrfs_mark_buffer_dirty(leaf_buf);
1759
1760 ret = 0;
1761 if (btrfs_leaf_free_space(root, leaf) < 0)
1762 BUG();
1763 check_leaf(root, path, 0);
1764 return ret;
1765}
1766
Chris Mason6567e832007-04-16 09:22:45 -04001767int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1768 *root, struct btrfs_path *path, u32 data_size)
1769{
1770 int ret = 0;
1771 int slot;
1772 int slot_orig;
1773 struct btrfs_leaf *leaf;
1774 struct buffer_head *leaf_buf;
1775 u32 nritems;
1776 unsigned int data_end;
1777 unsigned int old_data;
1778 unsigned int old_size;
1779 int i;
1780
1781 slot_orig = path->slots[0];
1782 leaf_buf = path->nodes[0];
1783 leaf = btrfs_buffer_leaf(leaf_buf);
1784
1785 nritems = btrfs_header_nritems(&leaf->header);
1786 data_end = leaf_data_end(root, leaf);
1787
1788 if (btrfs_leaf_free_space(root, leaf) < data_size)
1789 BUG();
1790 slot = path->slots[0];
1791 old_data = btrfs_item_end(leaf->items + slot);
1792
1793 BUG_ON(slot < 0);
1794 BUG_ON(slot >= nritems);
1795
1796 /*
1797 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1798 */
1799 /* first correct the data pointers */
1800 for (i = slot; i < nritems; i++) {
1801 u32 ioff = btrfs_item_offset(leaf->items + i);
1802 btrfs_set_item_offset(leaf->items + i,
1803 ioff - data_size);
1804 }
1805 /* shift the data */
1806 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1807 data_end - data_size, btrfs_leaf_data(leaf) +
1808 data_end, old_data - data_end);
1809 data_end = old_data;
1810 old_size = btrfs_item_size(leaf->items + slot);
1811 btrfs_set_item_size(leaf->items + slot, old_size + data_size);
1812 btrfs_mark_buffer_dirty(leaf_buf);
1813
1814 ret = 0;
1815 if (btrfs_leaf_free_space(root, leaf) < 0)
1816 BUG();
1817 check_leaf(root, path, 0);
1818 return ret;
1819}
1820
Chris Mason74123bd2007-02-02 11:05:29 -05001821/*
1822 * Given a key and some data, insert an item into the tree.
1823 * This does all the path init required, making room in the tree if needed.
1824 */
Chris Masone089f052007-03-16 16:20:31 -04001825int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1826 *root, struct btrfs_path *path, struct btrfs_key
1827 *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001828{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001829 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001830 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001831 int slot_orig;
Chris Mason234b63a2007-03-13 10:46:10 -04001832 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001833 struct buffer_head *leaf_buf;
Chris Mason7518a232007-03-12 12:01:18 -04001834 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001835 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04001836 struct btrfs_disk_key disk_key;
1837
1838 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001839
Chris Mason74123bd2007-02-02 11:05:29 -05001840 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001841 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001842 BUG();
Chris Masone089f052007-03-16 16:20:31 -04001843 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001844 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05001845 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001846 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001847 if (ret < 0)
1848 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001849
Chris Mason62e27492007-03-15 12:56:47 -04001850 slot_orig = path->slots[0];
1851 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001852 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -05001853
Chris Mason7518a232007-03-12 12:01:18 -04001854 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason123abc82007-03-14 14:14:43 -04001855 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001856
Chris Mason123abc82007-03-14 14:14:43 -04001857 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04001858 sizeof(struct btrfs_item) + data_size) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001859 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04001860 }
Chris Mason62e27492007-03-15 12:56:47 -04001861 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001862 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001863 if (slot != nritems) {
1864 int i;
Chris Mason0783fcf2007-03-12 20:12:07 -04001865 unsigned int old_data = btrfs_item_end(leaf->items + slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001866
1867 /*
1868 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1869 */
1870 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04001871 for (i = slot; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04001872 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04001873 btrfs_set_item_offset(leaf->items + i,
1874 ioff - data_size);
1875 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001876
1877 /* shift the items */
Chris Masond6025572007-03-30 14:27:56 -04001878 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1879 leaf->items + slot,
1880 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001881
1882 /* shift the data */
Chris Masond6025572007-03-30 14:27:56 -04001883 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1884 data_end - data_size, btrfs_leaf_data(leaf) +
1885 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001886 data_end = old_data;
1887 }
Chris Mason62e27492007-03-15 12:56:47 -04001888 /* setup the item for the new data */
Chris Masond6025572007-03-30 14:27:56 -04001889 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1890 sizeof(struct btrfs_disk_key));
Chris Mason0783fcf2007-03-12 20:12:07 -04001891 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1892 btrfs_set_item_size(leaf->items + slot, data_size);
Chris Mason7518a232007-03-12 12:01:18 -04001893 btrfs_set_header_nritems(&leaf->header, nritems + 1);
Chris Masond6025572007-03-30 14:27:56 -04001894 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001895
1896 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001897 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04001898 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001899
Chris Mason123abc82007-03-14 14:14:43 -04001900 if (btrfs_leaf_free_space(root, leaf) < 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001901 BUG();
Chris Mason62e27492007-03-15 12:56:47 -04001902 check_leaf(root, path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001903out:
Chris Mason62e27492007-03-15 12:56:47 -04001904 return ret;
1905}
1906
1907/*
1908 * Given a key and some data, insert an item into the tree.
1909 * This does all the path init required, making room in the tree if needed.
1910 */
Chris Masone089f052007-03-16 16:20:31 -04001911int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1912 *root, struct btrfs_key *cpu_key, void *data, u32
1913 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04001914{
1915 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001916 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -04001917 u8 *ptr;
1918
Chris Mason2c90e5d2007-04-02 10:50:19 -04001919 path = btrfs_alloc_path();
1920 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001921 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04001922 if (!ret) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001923 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1924 path->slots[0], u8);
1925 btrfs_memcpy(root, path->nodes[0]->b_data,
Chris Masond6025572007-03-30 14:27:56 -04001926 ptr, data, data_size);
Chris Mason2c90e5d2007-04-02 10:50:19 -04001927 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -04001928 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04001929 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001930 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001931}
1932
Chris Mason74123bd2007-02-02 11:05:29 -05001933/*
Chris Mason5de08d72007-02-24 06:24:44 -05001934 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001935 *
1936 * If the delete empties a node, the node is removed from the tree,
1937 * continuing all the way the root if required. The root is converted into
1938 * a leaf if all the nodes are emptied.
1939 */
Chris Masone089f052007-03-16 16:20:31 -04001940static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1941 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001942{
Chris Mason234b63a2007-03-13 10:46:10 -04001943 struct btrfs_node *node;
Chris Masone20d96d2007-03-22 12:13:20 -04001944 struct buffer_head *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04001945 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001946 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001947 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001948
Chris Masone20d96d2007-03-22 12:13:20 -04001949 node = btrfs_buffer_node(parent);
Chris Mason7518a232007-03-12 12:01:18 -04001950 nritems = btrfs_header_nritems(&node->header);
Chris Masonbb803952007-03-01 12:04:21 -05001951 if (slot != nritems -1) {
Chris Masond6025572007-03-30 14:27:56 -04001952 btrfs_memmove(root, node, node->ptrs + slot,
1953 node->ptrs + slot + 1,
1954 sizeof(struct btrfs_key_ptr) *
1955 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05001956 }
Chris Mason7518a232007-03-12 12:01:18 -04001957 nritems--;
1958 btrfs_set_header_nritems(&node->header, nritems);
1959 if (nritems == 0 && parent == root->node) {
Chris Masone20d96d2007-03-22 12:13:20 -04001960 struct btrfs_header *header = btrfs_buffer_header(root->node);
1961 BUG_ON(btrfs_header_level(header) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05001962 /* just turn the root into a leaf and break */
Chris Masone20d96d2007-03-22 12:13:20 -04001963 btrfs_set_header_level(header, 0);
Chris Masonbb803952007-03-01 12:04:21 -05001964 } else if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04001965 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
Chris Mason123abc82007-03-14 14:14:43 -04001966 level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001967 if (wret)
1968 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001969 }
Chris Masond6025572007-03-30 14:27:56 -04001970 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001971 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001972}
1973
Chris Mason74123bd2007-02-02 11:05:29 -05001974/*
1975 * delete the item at the leaf level in path. If that empties
1976 * the leaf, remove it from the tree
1977 */
Chris Masone089f052007-03-16 16:20:31 -04001978int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1979 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001980{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001981 int slot;
Chris Mason234b63a2007-03-13 10:46:10 -04001982 struct btrfs_leaf *leaf;
Chris Masone20d96d2007-03-22 12:13:20 -04001983 struct buffer_head *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001984 int doff;
1985 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001986 int ret = 0;
1987 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001988 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001989
Chris Masoneb60cea2007-02-02 09:18:22 -05001990 leaf_buf = path->nodes[0];
Chris Masone20d96d2007-03-22 12:13:20 -04001991 leaf = btrfs_buffer_leaf(leaf_buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001992 slot = path->slots[0];
Chris Mason0783fcf2007-03-12 20:12:07 -04001993 doff = btrfs_item_offset(leaf->items + slot);
1994 dsize = btrfs_item_size(leaf->items + slot);
Chris Mason7518a232007-03-12 12:01:18 -04001995 nritems = btrfs_header_nritems(&leaf->header);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001996
Chris Mason7518a232007-03-12 12:01:18 -04001997 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001998 int i;
Chris Mason123abc82007-03-14 14:14:43 -04001999 int data_end = leaf_data_end(root, leaf);
Chris Masond6025572007-03-30 14:27:56 -04002000 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
2001 data_end + dsize,
2002 btrfs_leaf_data(leaf) + data_end,
2003 doff - data_end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002004 for (i = slot + 1; i < nritems; i++) {
Chris Mason123abc82007-03-14 14:14:43 -04002005 u32 ioff = btrfs_item_offset(leaf->items + i);
Chris Mason0783fcf2007-03-12 20:12:07 -04002006 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
2007 }
Chris Masond6025572007-03-30 14:27:56 -04002008 btrfs_memmove(root, leaf, leaf->items + slot,
2009 leaf->items + slot + 1,
2010 sizeof(struct btrfs_item) *
2011 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002012 }
Chris Mason7518a232007-03-12 12:01:18 -04002013 btrfs_set_header_nritems(&leaf->header, nritems - 1);
2014 nritems--;
Chris Mason74123bd2007-02-02 11:05:29 -05002015 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002016 if (nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05002017 if (leaf_buf == root->node) {
Chris Mason7518a232007-03-12 12:01:18 -04002018 btrfs_set_header_level(&leaf->header, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002019 } else {
Chris Masone089f052007-03-16 16:20:31 -04002020 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04002021 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002022 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002023 if (wret)
2024 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002025 wret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04002026 bh_blocknr(leaf_buf), 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002027 if (wret)
2028 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002029 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002030 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002031 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002032 if (slot == 0) {
Chris Masone089f052007-03-16 16:20:31 -04002033 wret = fixup_low_keys(trans, root, path,
2034 &leaf->items[0].key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002035 if (wret)
2036 ret = wret;
2037 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002038
Chris Mason74123bd2007-02-02 11:05:29 -05002039 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04002040 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002041 /* push_leaf_left fixes the path.
2042 * make sure the path still points to our leaf
2043 * for possible call to del_ptr below
2044 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002045 slot = path->slots[1];
Chris Masone20d96d2007-03-22 12:13:20 -04002046 get_bh(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002047 wret = push_leaf_left(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002048 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002049 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05002050 if (path->nodes[0] == leaf_buf &&
Chris Mason7518a232007-03-12 12:01:18 -04002051 btrfs_header_nritems(&leaf->header)) {
Chris Masone089f052007-03-16 16:20:31 -04002052 wret = push_leaf_right(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002053 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002054 ret = wret;
2055 }
Chris Mason7518a232007-03-12 12:01:18 -04002056 if (btrfs_header_nritems(&leaf->header) == 0) {
Chris Mason7eccb902007-04-11 15:53:25 -04002057 u64 blocknr = bh_blocknr(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002058 clean_tree_block(trans, root, leaf_buf);
Chris Masond6025572007-03-30 14:27:56 -04002059 wait_on_buffer(leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002060 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002061 if (wret)
2062 ret = wret;
Chris Mason234b63a2007-03-13 10:46:10 -04002063 btrfs_block_release(root, leaf_buf);
Chris Masone089f052007-03-16 16:20:31 -04002064 wret = btrfs_free_extent(trans, root, blocknr,
2065 1, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002066 if (wret)
2067 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002068 } else {
Chris Masond6025572007-03-30 14:27:56 -04002069 btrfs_mark_buffer_dirty(leaf_buf);
Chris Mason234b63a2007-03-13 10:46:10 -04002070 btrfs_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002071 }
Chris Masond5719762007-03-23 10:01:08 -04002072 } else {
Chris Masond6025572007-03-30 14:27:56 -04002073 btrfs_mark_buffer_dirty(leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002074 }
2075 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002076 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002077}
2078
Chris Mason97571fd2007-02-24 13:39:08 -05002079/*
2080 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002081 * returns 0 if it found something or 1 if there are no greater leaves.
2082 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002083 */
Chris Mason234b63a2007-03-13 10:46:10 -04002084int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002085{
2086 int slot;
2087 int level = 1;
2088 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -04002089 struct buffer_head *c;
2090 struct btrfs_node *c_node;
2091 struct buffer_head *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002092
Chris Mason234b63a2007-03-13 10:46:10 -04002093 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002094 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002095 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002096 slot = path->slots[level] + 1;
2097 c = path->nodes[level];
Chris Masone20d96d2007-03-22 12:13:20 -04002098 c_node = btrfs_buffer_node(c);
2099 if (slot >= btrfs_header_nritems(&c_node->header)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002100 level++;
2101 continue;
2102 }
Chris Masone20d96d2007-03-22 12:13:20 -04002103 blocknr = btrfs_node_blockptr(c_node, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002104 if (next)
Chris Mason234b63a2007-03-13 10:46:10 -04002105 btrfs_block_release(root, next);
Chris Mason6702ed42007-08-07 16:15:09 -04002106 if (path->reada)
2107 reada_for_search(root, path, level, slot);
Chris Masond97e63b2007-02-20 16:40:44 -05002108 next = read_tree_block(root, blocknr);
2109 break;
2110 }
2111 path->slots[level] = slot;
2112 while(1) {
2113 level--;
2114 c = path->nodes[level];
Chris Mason234b63a2007-03-13 10:46:10 -04002115 btrfs_block_release(root, c);
Chris Masond97e63b2007-02-20 16:40:44 -05002116 path->nodes[level] = next;
2117 path->slots[level] = 0;
2118 if (!level)
2119 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002120 if (path->reada)
2121 reada_for_search(root, path, level, slot);
Chris Mason1d4f8a02007-03-13 09:28:32 -04002122 next = read_tree_block(root,
Chris Masone20d96d2007-03-22 12:13:20 -04002123 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
Chris Masond97e63b2007-02-20 16:40:44 -05002124 }
2125 return 0;
2126}