blob: 984f4745440eb1b231be859afd606689e7bdcbae [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 Mason5f39d392007-10-15 16:14:19 -040022#include "print-tree.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050023
Chris Masone089f052007-03-16 16:20:31 -040024static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
25 *root, struct btrfs_path *path, int level);
26static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040027 *root, struct btrfs_key *ins_key,
28 struct btrfs_path *path, int data_size);
Chris Mason5f39d392007-10-15 16:14:19 -040029static int push_node_left(struct btrfs_trans_handle *trans,
30 struct btrfs_root *root, struct extent_buffer *dst,
31 struct extent_buffer *src);
32static int balance_node_right(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root,
34 struct extent_buffer *dst_buf,
35 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040036static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
37 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050038
Chris Masondf24a2b2007-04-04 09:36:31 -040039inline void btrfs_init_path(struct btrfs_path *p)
40{
41 memset(p, 0, sizeof(*p));
42}
43
Chris Mason2c90e5d2007-04-02 10:50:19 -040044struct btrfs_path *btrfs_alloc_path(void)
45{
Chris Masondf24a2b2007-04-04 09:36:31 -040046 struct btrfs_path *path;
47 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040048 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040049 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040050 path->reada = 1;
51 }
Chris Masondf24a2b2007-04-04 09:36:31 -040052 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040053}
54
55void btrfs_free_path(struct btrfs_path *p)
56{
Chris Masondf24a2b2007-04-04 09:36:31 -040057 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040058 kmem_cache_free(btrfs_path_cachep, p);
59}
60
Chris Mason234b63a2007-03-13 10:46:10 -040061void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050062{
63 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040064 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050065 if (!p->nodes[i])
66 break;
Chris Mason5f39d392007-10-15 16:14:19 -040067 free_extent_buffer(p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050068 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050069 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050070}
71
Chris Mason5f39d392007-10-15 16:14:19 -040072static int __btrfs_cow_block(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 struct extent_buffer *buf,
75 struct extent_buffer *parent, int parent_slot,
76 struct extent_buffer **cow_ret,
77 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -040078{
Chris Mason5f39d392007-10-15 16:14:19 -040079 struct extent_buffer *cow;
Chris Mason6702ed42007-08-07 16:15:09 -040080 int ret = 0;
81 int different_trans = 0;
82
83 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -040084
Chris Masondb945352007-10-15 16:15:53 -040085 cow = btrfs_alloc_free_block(trans, root, buf->len,
86 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -040087 if (IS_ERR(cow))
88 return PTR_ERR(cow);
89
Chris Mason5f39d392007-10-15 16:14:19 -040090 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -040091 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -040092 btrfs_set_header_generation(cow, trans->transid);
93 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -040094
Chris Mason5f39d392007-10-15 16:14:19 -040095 WARN_ON(btrfs_header_generation(buf) > trans->transid);
96 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -040097 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;
Chris Mason5f39d392007-10-15 16:14:19 -0400107 extent_buffer_get(cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400108 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400109 btrfs_free_extent(trans, root, buf->start,
110 buf->len, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400111 }
Chris Mason5f39d392007-10-15 16:14:19 -0400112 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400113 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400114 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400115 cow->start);
Chris Mason6702ed42007-08-07 16:15:09 -0400116 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400117 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Masondb945352007-10-15 16:15:53 -0400118 btrfs_free_extent(trans, root, buf->start, buf->len, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400119 }
Chris Mason5f39d392007-10-15 16:14:19 -0400120 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400121 btrfs_mark_buffer_dirty(cow);
122 *cow_ret = cow;
123 return 0;
124}
125
Chris Mason5f39d392007-10-15 16:14:19 -0400126int btrfs_cow_block(struct btrfs_trans_handle *trans,
127 struct btrfs_root *root, struct extent_buffer *buf,
128 struct extent_buffer *parent, int parent_slot,
129 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500130{
Chris Mason6702ed42007-08-07 16:15:09 -0400131 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400132 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400133 if (trans->transaction != root->fs_info->running_transaction) {
134 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
135 root->fs_info->running_transaction->transid);
136 WARN_ON(1);
137 }
138 if (trans->transid != root->fs_info->generation) {
139 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
140 root->fs_info->generation);
141 WARN_ON(1);
142 }
Chris Mason5f39d392007-10-15 16:14:19 -0400143 if (btrfs_header_generation(buf) == 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
Chris Masondb945352007-10-15 16:15:53 -0400148 search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400149 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400150 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400151 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400152}
153
Chris Mason6b800532007-10-15 16:17:34 -0400154static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400155{
Chris Mason6b800532007-10-15 16:17:34 -0400156 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400157 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400158 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400159 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500160 return 0;
161}
162
Chris Mason6b800532007-10-15 16:17:34 -0400163static int should_defrag_leaf(struct extent_buffer *leaf)
Chris Mason2cc58cf2007-08-27 16:49:44 -0400164{
Chris Mason6b800532007-10-15 16:17:34 -0400165 struct btrfs_key key;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400166 u32 nritems;
167
Chris Mason6b800532007-10-15 16:17:34 -0400168 if (btrfs_buffer_defrag(leaf))
Chris Mason2cc58cf2007-08-27 16:49:44 -0400169 return 1;
170
Chris Mason6b800532007-10-15 16:17:34 -0400171 nritems = btrfs_header_nritems(leaf);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400172 if (nritems == 0)
173 return 0;
174
Chris Mason6b800532007-10-15 16:17:34 -0400175 btrfs_item_key_to_cpu(leaf, &key, 0);
176 if (key.type == BTRFS_DIR_ITEM_KEY)
Chris Mason2cc58cf2007-08-27 16:49:44 -0400177 return 1;
178
Chris Mason6b800532007-10-15 16:17:34 -0400179
180 btrfs_item_key_to_cpu(leaf, &key, nritems - 1);
181 if (key.type == BTRFS_DIR_ITEM_KEY)
Chris Mason2cc58cf2007-08-27 16:49:44 -0400182 return 1;
183 if (nritems > 4) {
Chris Mason6b800532007-10-15 16:17:34 -0400184 btrfs_item_key_to_cpu(leaf, &key, nritems / 2);
185 if (key.type == BTRFS_DIR_ITEM_KEY)
Chris Mason2cc58cf2007-08-27 16:49:44 -0400186 return 1;
187 }
188 return 0;
189}
190
Chris Mason6702ed42007-08-07 16:15:09 -0400191int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400192 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masone9d0b132007-08-10 14:06:19 -0400193 int cache_only, u64 *last_ret)
Chris Mason6702ed42007-08-07 16:15:09 -0400194{
Chris Mason6b800532007-10-15 16:17:34 -0400195 struct extent_buffer *cur;
196 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400197 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400198 u64 search_start = *last_ret;
199 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400200 u64 other;
201 u32 parent_nritems;
202 int start_slot;
203 int end_slot;
204 int i;
205 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400206 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400207 int uptodate;
208 u32 blocksize;
Chris Mason6702ed42007-08-07 16:15:09 -0400209
210 if (trans->transaction != root->fs_info->running_transaction) {
211 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
212 root->fs_info->running_transaction->transid);
213 WARN_ON(1);
214 }
215 if (trans->transid != root->fs_info->generation) {
216 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
217 root->fs_info->generation);
218 WARN_ON(1);
219 }
Chris Mason0f1ebbd2007-10-15 16:19:46 -0400220 parent_level = btrfs_header_level(parent);
Chris Mason86479a02007-09-10 19:58:16 -0400221
Chris Mason6b800532007-10-15 16:17:34 -0400222 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400223 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400224
225 start_slot = 0;
226 end_slot = parent_nritems;
227
228 if (parent_nritems == 1)
229 return 0;
230
231 for (i = start_slot; i < end_slot; i++) {
232 int close = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400233 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400234 if (last_block == 0)
235 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400236 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400237 other = btrfs_node_blockptr(parent, i - 1);
238 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400239 }
240 if (close && i < end_slot - 1) {
Chris Mason6b800532007-10-15 16:17:34 -0400241 other = btrfs_node_blockptr(parent, i + 1);
242 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400243 }
Chris Masone9d0b132007-08-10 14:06:19 -0400244 if (close) {
245 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400246 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400247 }
Chris Mason6702ed42007-08-07 16:15:09 -0400248
Chris Mason6b800532007-10-15 16:17:34 -0400249 cur = btrfs_find_tree_block(root, blocknr, blocksize);
250 if (cur)
251 uptodate = btrfs_buffer_uptodate(cur);
252 else
253 uptodate = 0;
254 if (!cur || !uptodate ||
255 (parent_level != 1 && !btrfs_buffer_defrag(cur)) ||
256 (parent_level == 1 && !should_defrag_leaf(cur))) {
Chris Mason6702ed42007-08-07 16:15:09 -0400257 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400258 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400259 continue;
260 }
Chris Mason6b800532007-10-15 16:17:34 -0400261 if (!cur) {
262 cur = read_tree_block(root, blocknr,
263 blocksize);
264 } else if (!uptodate) {
265 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400266 }
Chris Mason6702ed42007-08-07 16:15:09 -0400267 }
Chris Masone9d0b132007-08-10 14:06:19 -0400268 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400269 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400270
Chris Mason6b800532007-10-15 16:17:34 -0400271 err = __btrfs_cow_block(trans, root, cur, parent, i,
272 &tmp, search_start,
273 min(16 * blocksize,
274 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400275 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400276 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400277 break;
Yan252c38f2007-08-29 09:11:44 -0400278 }
Chris Mason6b800532007-10-15 16:17:34 -0400279 search_start = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400280 *last_ret = search_start;
281 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400282 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400283 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400284 }
285 return err;
286}
287
Chris Mason74123bd2007-02-02 11:05:29 -0500288/*
289 * The leaf data grows from end-to-front in the node.
290 * this returns the address of the start of the last item,
291 * which is the stop of the leaf data stack
292 */
Chris Mason123abc82007-03-14 14:14:43 -0400293static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400294 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500295{
Chris Mason5f39d392007-10-15 16:14:19 -0400296 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500297 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400298 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400299 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500300}
301
Chris Mason74123bd2007-02-02 11:05:29 -0500302/*
Chris Mason74123bd2007-02-02 11:05:29 -0500303 * compare two keys in a memcmp fashion
304 */
Chris Mason9aca1d52007-03-13 11:09:37 -0400305static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500306{
Chris Masone2fa7222007-03-12 16:22:34 -0400307 struct btrfs_key k1;
308
309 btrfs_disk_key_to_cpu(&k1, disk);
310
311 if (k1.objectid > k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500312 return 1;
Chris Masone2fa7222007-03-12 16:22:34 -0400313 if (k1.objectid < k2->objectid)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500314 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -0400315 if (k1.type > k2->type)
Chris Masonf254e522007-03-29 15:15:27 -0400316 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400317 if (k1.type < k2->type)
Chris Masonf254e522007-03-29 15:15:27 -0400318 return -1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400319 if (k1.offset > k2->offset)
320 return 1;
321 if (k1.offset < k2->offset)
322 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500323 return 0;
324}
Chris Mason74123bd2007-02-02 11:05:29 -0500325
Chris Mason123abc82007-03-14 14:14:43 -0400326static int check_node(struct btrfs_root *root, struct btrfs_path *path,
327 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500328{
Chris Mason5f39d392007-10-15 16:14:19 -0400329 struct extent_buffer *parent = NULL;
330 struct extent_buffer *node = path->nodes[level];
331 struct btrfs_disk_key parent_key;
332 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500333 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400334 int slot;
335 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400336 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500337
338 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400339 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400340
Chris Mason8d7be552007-05-10 11:24:42 -0400341 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400342 BUG_ON(nritems == 0);
343 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400344 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400345 btrfs_node_key(parent, &parent_key, parent_slot);
346 btrfs_node_key(node, &node_key, 0);
347 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400348 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400349 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400350 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500351 }
Chris Mason123abc82007-03-14 14:14:43 -0400352 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400353 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400354 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
355 btrfs_node_key(node, &node_key, slot);
356 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400357 }
358 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400359 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
360 btrfs_node_key(node, &node_key, slot);
361 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500362 }
363 return 0;
364}
365
Chris Mason123abc82007-03-14 14:14:43 -0400366static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
367 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500368{
Chris Mason5f39d392007-10-15 16:14:19 -0400369 struct extent_buffer *leaf = path->nodes[level];
370 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500371 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400372 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400373 struct btrfs_disk_key parent_key;
374 struct btrfs_disk_key leaf_key;
375 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400376
Chris Mason5f39d392007-10-15 16:14:19 -0400377 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500378
379 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400380 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400381
382 if (nritems == 0)
383 return 0;
384
385 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400386 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400387 btrfs_node_key(parent, &parent_key, parent_slot);
388 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400389
Chris Mason5f39d392007-10-15 16:14:19 -0400390 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400391 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400392 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400393 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500394 }
Chris Mason5f39d392007-10-15 16:14:19 -0400395#if 0
396 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
397 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
398 btrfs_item_key(leaf, &leaf_key, i);
399 if (comp_keys(&leaf_key, &cpukey) >= 0) {
400 btrfs_print_leaf(root, leaf);
401 printk("slot %d offset bad key\n", i);
402 BUG_ON(1);
403 }
404 if (btrfs_item_offset_nr(leaf, i) !=
405 btrfs_item_end_nr(leaf, i + 1)) {
406 btrfs_print_leaf(root, leaf);
407 printk("slot %d offset bad\n", i);
408 BUG_ON(1);
409 }
410 if (i == 0) {
411 if (btrfs_item_offset_nr(leaf, i) +
412 btrfs_item_size_nr(leaf, i) !=
413 BTRFS_LEAF_DATA_SIZE(root)) {
414 btrfs_print_leaf(root, leaf);
415 printk("slot %d first offset bad\n", i);
416 BUG_ON(1);
417 }
418 }
419 }
420 if (nritems > 0) {
421 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
422 btrfs_print_leaf(root, leaf);
423 printk("slot %d bad size \n", nritems - 1);
424 BUG_ON(1);
425 }
426 }
427#endif
428 if (slot != 0 && slot < nritems - 1) {
429 btrfs_item_key(leaf, &leaf_key, slot);
430 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
431 if (comp_keys(&leaf_key, &cpukey) <= 0) {
432 btrfs_print_leaf(root, leaf);
433 printk("slot %d offset bad key\n", slot);
434 BUG_ON(1);
435 }
436 if (btrfs_item_offset_nr(leaf, slot - 1) !=
437 btrfs_item_end_nr(leaf, slot)) {
438 btrfs_print_leaf(root, leaf);
439 printk("slot %d offset bad\n", slot);
440 BUG_ON(1);
441 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500442 }
Chris Mason8d7be552007-05-10 11:24:42 -0400443 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400444 btrfs_item_key(leaf, &leaf_key, slot);
445 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
446 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
447 if (btrfs_item_offset_nr(leaf, slot) !=
448 btrfs_item_end_nr(leaf, slot + 1)) {
449 btrfs_print_leaf(root, leaf);
450 printk("slot %d offset bad\n", slot);
451 BUG_ON(1);
452 }
Chris Mason8d7be552007-05-10 11:24:42 -0400453 }
Chris Mason5f39d392007-10-15 16:14:19 -0400454 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
455 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500456 return 0;
457}
458
Chris Mason123abc82007-03-14 14:14:43 -0400459static int check_block(struct btrfs_root *root, struct btrfs_path *path,
460 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500461{
Chris Mason810191f2007-10-15 16:18:55 -0400462 return 0;
Chris Masondb945352007-10-15 16:15:53 -0400463#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400464 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400465
Chris Mason479965d2007-10-15 16:14:27 -0400466 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
467 (unsigned long)btrfs_header_fsid(buf),
468 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400469 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400470 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400471 }
Chris Masondb945352007-10-15 16:15:53 -0400472#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500473 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400474 return check_leaf(root, path, level);
475 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500476}
477
Chris Mason74123bd2007-02-02 11:05:29 -0500478/*
Chris Mason5f39d392007-10-15 16:14:19 -0400479 * search for key in the extent_buffer. The items start at offset p,
480 * and they are item_size apart. There are 'max' items in p.
481 *
Chris Mason74123bd2007-02-02 11:05:29 -0500482 * the slot in the array is returned via slot, and it points to
483 * the place where you would insert key if it is not found in
484 * the array.
485 *
486 * slot may point to max if the key is bigger than all of the keys
487 */
Chris Mason5f39d392007-10-15 16:14:19 -0400488static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
489 int item_size, struct btrfs_key *key,
490 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500491{
492 int low = 0;
493 int high = max;
494 int mid;
495 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400496 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400497 struct btrfs_disk_key unaligned;
498 unsigned long offset;
499 char *map_token = NULL;
500 char *kaddr = NULL;
501 unsigned long map_start = 0;
502 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400503 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500504
505 while(low < high) {
506 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400507 offset = p + mid * item_size;
508
509 if (!map_token || offset < map_start ||
510 (offset + sizeof(struct btrfs_disk_key)) >
511 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400512 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400513 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400514 map_token = NULL;
515 }
516 err = map_extent_buffer(eb, offset,
517 sizeof(struct btrfs_disk_key),
518 &map_token, &kaddr,
519 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400520
Chris Mason479965d2007-10-15 16:14:27 -0400521 if (!err) {
522 tmp = (struct btrfs_disk_key *)(kaddr + offset -
523 map_start);
524 } else {
525 read_extent_buffer(eb, &unaligned,
526 offset, sizeof(unaligned));
527 tmp = &unaligned;
528 }
529
Chris Mason5f39d392007-10-15 16:14:19 -0400530 } else {
531 tmp = (struct btrfs_disk_key *)(kaddr + offset -
532 map_start);
533 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500534 ret = comp_keys(tmp, key);
535
536 if (ret < 0)
537 low = mid + 1;
538 else if (ret > 0)
539 high = mid;
540 else {
541 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400542 if (map_token)
543 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500544 return 0;
545 }
546 }
547 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400548 if (map_token)
549 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500550 return 1;
551}
552
Chris Mason97571fd2007-02-24 13:39:08 -0500553/*
554 * simple bin_search frontend that does the right thing for
555 * leaves vs nodes
556 */
Chris Mason5f39d392007-10-15 16:14:19 -0400557static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
558 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500559{
Chris Mason5f39d392007-10-15 16:14:19 -0400560 if (level == 0) {
561 return generic_bin_search(eb,
562 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400563 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400564 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400565 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500566 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400567 return generic_bin_search(eb,
568 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400569 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400570 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400571 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500572 }
573 return -1;
574}
575
Chris Mason5f39d392007-10-15 16:14:19 -0400576static struct extent_buffer *read_node_slot(struct btrfs_root *root,
577 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500578{
Chris Masonbb803952007-03-01 12:04:21 -0500579 if (slot < 0)
580 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400581 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500582 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400583 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
584 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500585}
586
Chris Masone089f052007-03-16 16:20:31 -0400587static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
588 *root, struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500589{
Chris Mason5f39d392007-10-15 16:14:19 -0400590 struct extent_buffer *right = NULL;
591 struct extent_buffer *mid;
592 struct extent_buffer *left = NULL;
593 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500594 int ret = 0;
595 int wret;
596 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500597 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400598 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500599 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500600
601 if (level == 0)
602 return 0;
603
Chris Mason5f39d392007-10-15 16:14:19 -0400604 mid = path->nodes[level];
Chris Mason1d4f8a02007-03-13 09:28:32 -0400605 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500606
Chris Mason234b63a2007-03-13 10:46:10 -0400607 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400608 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500609 pslot = path->slots[level + 1];
610
Chris Mason40689472007-03-17 14:29:23 -0400611 /*
612 * deal with the case where there is only one pointer in the root
613 * by promoting the node below to a root
614 */
Chris Mason5f39d392007-10-15 16:14:19 -0400615 if (!parent) {
616 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500617
Chris Mason5f39d392007-10-15 16:14:19 -0400618 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500619 return 0;
620
621 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400622 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500623 BUG_ON(!child);
624 root->node = child;
625 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400626 clean_tree_block(trans, root, mid);
627 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500628 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400629 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400630 ret = btrfs_free_extent(trans, root, mid->start, mid->len, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500631 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400632 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400633 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500634 }
Chris Mason5f39d392007-10-15 16:14:19 -0400635 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400636 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500637 return 0;
638
Chris Mason5f39d392007-10-15 16:14:19 -0400639 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400640 err_on_enospc = 1;
641
Chris Mason5f39d392007-10-15 16:14:19 -0400642 left = read_node_slot(root, parent, pslot - 1);
643 if (left) {
644 wret = btrfs_cow_block(trans, root, left,
645 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400646 if (wret) {
647 ret = wret;
648 goto enospc;
649 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400650 }
Chris Mason5f39d392007-10-15 16:14:19 -0400651 right = read_node_slot(root, parent, pslot + 1);
652 if (right) {
653 wret = btrfs_cow_block(trans, root, right,
654 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400655 if (wret) {
656 ret = wret;
657 goto enospc;
658 }
659 }
660
661 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400662 if (left) {
663 orig_slot += btrfs_header_nritems(left);
664 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500665 if (wret < 0)
666 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400667 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400668 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500669 }
Chris Mason79f95c82007-03-01 15:16:26 -0500670
671 /*
672 * then try to empty the right most buffer into the middle
673 */
Chris Mason5f39d392007-10-15 16:14:19 -0400674 if (right) {
675 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400676 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500677 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400678 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400679 u64 bytenr = right->start;
680 u32 blocksize = right->len;
681
Chris Mason5f39d392007-10-15 16:14:19 -0400682 clean_tree_block(trans, root, right);
683 wait_on_tree_block_writeback(root, right);
684 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500685 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400686 wret = del_ptr(trans, root, path, level + 1, pslot +
687 1);
Chris Masonbb803952007-03-01 12:04:21 -0500688 if (wret)
689 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400690 wret = btrfs_free_extent(trans, root, bytenr,
691 blocksize, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500692 if (wret)
693 ret = wret;
694 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400695 struct btrfs_disk_key right_key;
696 btrfs_node_key(right, &right_key, 0);
697 btrfs_set_node_key(parent, &right_key, pslot + 1);
698 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500699 }
700 }
Chris Mason5f39d392007-10-15 16:14:19 -0400701 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500702 /*
703 * we're not allowed to leave a node with one item in the
704 * tree during a delete. A deletion from lower in the tree
705 * could try to delete the only pointer in this node.
706 * So, pull some keys from the left.
707 * There has to be a left pointer at this point because
708 * otherwise we would have pulled some pointers from the
709 * right
710 */
Chris Mason5f39d392007-10-15 16:14:19 -0400711 BUG_ON(!left);
712 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400713 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500714 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400715 goto enospc;
716 }
Chris Mason79f95c82007-03-01 15:16:26 -0500717 BUG_ON(wret == 1);
718 }
Chris Mason5f39d392007-10-15 16:14:19 -0400719 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500720 /* we've managed to empty the middle node, drop it */
Chris Masondb945352007-10-15 16:15:53 -0400721 u64 bytenr = mid->start;
722 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400723 clean_tree_block(trans, root, mid);
724 wait_on_tree_block_writeback(root, mid);
725 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500726 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400727 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500728 if (wret)
729 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400730 wret = btrfs_free_extent(trans, root, bytenr, blocksize, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500731 if (wret)
732 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500733 } else {
734 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400735 struct btrfs_disk_key mid_key;
736 btrfs_node_key(mid, &mid_key, 0);
737 btrfs_set_node_key(parent, &mid_key, pslot);
738 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500739 }
Chris Masonbb803952007-03-01 12:04:21 -0500740
Chris Mason79f95c82007-03-01 15:16:26 -0500741 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400742 if (left) {
743 if (btrfs_header_nritems(left) > orig_slot) {
744 extent_buffer_get(left);
745 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500746 path->slots[level + 1] -= 1;
747 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400748 if (mid)
749 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500750 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400751 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500752 path->slots[level] = orig_slot;
753 }
754 }
Chris Mason79f95c82007-03-01 15:16:26 -0500755 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400756 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400757 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400758 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500759 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400760enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400761 if (right)
762 free_extent_buffer(right);
763 if (left)
764 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500765 return ret;
766}
767
Chris Masone66f7092007-04-20 13:16:02 -0400768/* returns zero if the push worked, non-zero otherwise */
769static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
770 struct btrfs_root *root,
771 struct btrfs_path *path, int level)
772{
Chris Mason5f39d392007-10-15 16:14:19 -0400773 struct extent_buffer *right = NULL;
774 struct extent_buffer *mid;
775 struct extent_buffer *left = NULL;
776 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400777 int ret = 0;
778 int wret;
779 int pslot;
780 int orig_slot = path->slots[level];
781 u64 orig_ptr;
782
783 if (level == 0)
784 return 1;
785
Chris Mason5f39d392007-10-15 16:14:19 -0400786 mid = path->nodes[level];
Chris Masone66f7092007-04-20 13:16:02 -0400787 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
788
789 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400790 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400791 pslot = path->slots[level + 1];
792
Chris Mason5f39d392007-10-15 16:14:19 -0400793 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400794 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400795
Chris Mason5f39d392007-10-15 16:14:19 -0400796 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400797
798 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400799 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400800 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400801 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400802 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
803 wret = 1;
804 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400805 ret = btrfs_cow_block(trans, root, left, parent,
806 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400807 if (ret)
808 wret = 1;
809 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400810 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400811 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400812 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400813 }
Chris Masone66f7092007-04-20 13:16:02 -0400814 if (wret < 0)
815 ret = wret;
816 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400817 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400818 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400819 btrfs_node_key(mid, &disk_key, 0);
820 btrfs_set_node_key(parent, &disk_key, pslot);
821 btrfs_mark_buffer_dirty(parent);
822 if (btrfs_header_nritems(left) > orig_slot) {
823 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400824 path->slots[level + 1] -= 1;
825 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400826 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400827 } else {
828 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400829 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400830 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400831 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400832 }
Chris Masone66f7092007-04-20 13:16:02 -0400833 return 0;
834 }
Chris Mason5f39d392007-10-15 16:14:19 -0400835 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400836 }
Chris Mason5f39d392007-10-15 16:14:19 -0400837 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400838
839 /*
840 * then try to empty the right most buffer into the middle
841 */
Chris Mason5f39d392007-10-15 16:14:19 -0400842 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400843 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400844 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400845 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
846 wret = 1;
847 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400848 ret = btrfs_cow_block(trans, root, right,
849 parent, pslot + 1,
850 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400851 if (ret)
852 wret = 1;
853 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400854 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400855 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400856 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400857 }
Chris Masone66f7092007-04-20 13:16:02 -0400858 if (wret < 0)
859 ret = wret;
860 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400861 struct btrfs_disk_key disk_key;
862
863 btrfs_node_key(right, &disk_key, 0);
864 btrfs_set_node_key(parent, &disk_key, pslot + 1);
865 btrfs_mark_buffer_dirty(parent);
866
867 if (btrfs_header_nritems(mid) <= orig_slot) {
868 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -0400869 path->slots[level + 1] += 1;
870 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -0400871 btrfs_header_nritems(mid);
872 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400873 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400874 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400875 }
Chris Masone66f7092007-04-20 13:16:02 -0400876 return 0;
877 }
Chris Mason5f39d392007-10-15 16:14:19 -0400878 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400879 }
Chris Masone66f7092007-04-20 13:16:02 -0400880 return 1;
881}
882
Chris Mason74123bd2007-02-02 11:05:29 -0500883/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400884 * readahead one full node of leaves
885 */
886static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason6702ed42007-08-07 16:15:09 -0400887 int level, int slot)
Chris Mason3c69fae2007-08-07 15:52:22 -0400888{
Chris Mason5f39d392007-10-15 16:14:19 -0400889 struct extent_buffer *node;
Chris Mason3c69fae2007-08-07 15:52:22 -0400890 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -0400891 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -0400892 u64 lowest_read;
893 u64 highest_read;
894 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -0400895 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -0400896 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -0400897 u32 nr;
898 u32 blocksize;
899 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -0400900
Chris Mason6702ed42007-08-07 16:15:09 -0400901 if (level == 0)
Chris Mason3c69fae2007-08-07 15:52:22 -0400902 return;
903
Chris Mason6702ed42007-08-07 16:15:09 -0400904 if (!path->nodes[level])
905 return;
906
Chris Mason5f39d392007-10-15 16:14:19 -0400907 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -0400908 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -0400909 blocksize = btrfs_level_size(root, level - 1);
910 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -0400911 if (eb) {
912 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -0400913 return;
914 }
915
Chris Mason6b800532007-10-15 16:17:34 -0400916 highest_read = search;
917 lowest_read = search;
918
Chris Mason5f39d392007-10-15 16:14:19 -0400919 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -0400920 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -0400921 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -0400922 if (direction < 0) {
923 if (nr == 0)
924 break;
925 nr--;
926 } else if (direction > 0) {
927 nr++;
928 if (nr >= nritems)
929 break;
Chris Mason3c69fae2007-08-07 15:52:22 -0400930 }
Chris Mason6b800532007-10-15 16:17:34 -0400931 search = btrfs_node_blockptr(node, nr);
932 if ((search >= lowest_read && search <= highest_read) ||
933 (search < lowest_read && lowest_read - search <= 32768) ||
934 (search > highest_read && search - highest_read <= 32768)) {
935 readahead_tree_block(root, search, blocksize);
936 nread += blocksize;
937 }
938 nscan++;
939 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
940 break;
941 if(nread > (1024 * 1024) || nscan > 128)
942 break;
943
944 if (search < lowest_read)
945 lowest_read = search;
946 if (search > highest_read)
947 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -0400948 }
949}
950/*
Chris Mason74123bd2007-02-02 11:05:29 -0500951 * look for key in the tree. path is filled in with nodes along the way
952 * if key is found, we return zero and you can find the item in the leaf
953 * level of the path (level 0)
954 *
955 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500956 * be inserted, and 1 is returned. If there are other errors during the
957 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500958 *
959 * if ins_len > 0, nodes and leaves will be split as we walk down the
960 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
961 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500962 */
Chris Masone089f052007-03-16 16:20:31 -0400963int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
964 *root, struct btrfs_key *key, struct btrfs_path *p, int
965 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500966{
Chris Mason5f39d392007-10-15 16:14:19 -0400967 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -0400968 u64 bytenr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500969 int slot;
970 int ret;
971 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -0400972 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -0400973 u8 lowest_level = 0;
974
Chris Mason6702ed42007-08-07 16:15:09 -0400975 lowest_level = p->lowest_level;
976 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400977 WARN_ON(p->nodes[0] != NULL);
978 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -0500979again:
980 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -0400981 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500982 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -0400983 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -0500984 if (cow) {
985 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -0400986 wret = btrfs_cow_block(trans, root, b,
987 p->nodes[level + 1],
988 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -0400989 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -0400990 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400991 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -0400992 return wret;
993 }
Chris Mason02217ed2007-03-02 16:08:05 -0500994 }
995 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -0400996 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -0400997 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -0400998 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -0500999 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001000 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001001 if (ret)
1002 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001003 ret = bin_search(b, key, level, &slot);
1004 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001005 if (ret && slot > 0)
1006 slot -= 1;
1007 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001008 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001009 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001010 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001011 BUG_ON(sret > 0);
1012 if (sret)
1013 return sret;
1014 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001015 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001016 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001017 int sret = balance_level(trans, root, p,
1018 level);
Chris Masonbb803952007-03-01 12:04:21 -05001019 if (sret)
1020 return sret;
1021 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001022 if (!b) {
1023 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001024 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001025 }
Chris Masonbb803952007-03-01 12:04:21 -05001026 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001027 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001028 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001029 /* this is only true while dropping a snapshot */
1030 if (level == lowest_level)
1031 break;
Chris Masondb945352007-10-15 16:15:53 -04001032 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001033 if (should_reada)
1034 reada_for_search(root, p, level, slot);
Chris Masondb945352007-10-15 16:15:53 -04001035 b = read_tree_block(root, bytenr,
1036 btrfs_level_size(root, level - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001037 } else {
1038 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001039 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001040 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001041 int sret = split_leaf(trans, root, key,
1042 p, ins_len);
Chris Mason5c680ed2007-02-22 11:39:13 -05001043 BUG_ON(sret > 0);
1044 if (sret)
1045 return sret;
1046 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001047 return ret;
1048 }
1049 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001050 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001051}
1052
Chris Mason74123bd2007-02-02 11:05:29 -05001053/*
1054 * adjust the pointers going up the tree, starting at level
1055 * making sure the right key of each node is points to 'key'.
1056 * This is used after shifting pointers to the left, so it stops
1057 * fixing up pointers when a given leaf/node is not in slot 0 of the
1058 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001059 *
1060 * If this fails to write a tree block, it returns -1, but continues
1061 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001062 */
Chris Mason5f39d392007-10-15 16:14:19 -04001063static int fixup_low_keys(struct btrfs_trans_handle *trans,
1064 struct btrfs_root *root, struct btrfs_path *path,
1065 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001066{
1067 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001068 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001069 struct extent_buffer *t;
1070
Chris Mason234b63a2007-03-13 10:46:10 -04001071 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001072 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001073 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001074 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001075 t = path->nodes[i];
1076 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001077 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001078 if (tslot != 0)
1079 break;
1080 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001081 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001082}
1083
Chris Mason74123bd2007-02-02 11:05:29 -05001084/*
1085 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001086 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001087 *
1088 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1089 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001090 */
Chris Masone089f052007-03-16 16:20:31 -04001091static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason5f39d392007-10-15 16:14:19 -04001092 *root, struct extent_buffer *dst,
1093 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001094{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001095 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001096 int src_nritems;
1097 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001098 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001099
Chris Mason5f39d392007-10-15 16:14:19 -04001100 src_nritems = btrfs_header_nritems(src);
1101 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001102 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason54aa1f42007-06-22 14:16:25 -04001103
Chris Masoneb60cea2007-02-02 09:18:22 -05001104 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001105 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001106 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001107
1108 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001109 push_items = src_nritems;
1110
Chris Mason5f39d392007-10-15 16:14:19 -04001111 copy_extent_buffer(dst, src,
1112 btrfs_node_key_ptr_offset(dst_nritems),
1113 btrfs_node_key_ptr_offset(0),
1114 push_items * sizeof(struct btrfs_key_ptr));
1115
Chris Masonbb803952007-03-01 12:04:21 -05001116 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001117 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1118 btrfs_node_key_ptr_offset(push_items),
1119 (src_nritems - push_items) *
1120 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001121 }
Chris Mason5f39d392007-10-15 16:14:19 -04001122 btrfs_set_header_nritems(src, src_nritems - push_items);
1123 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1124 btrfs_mark_buffer_dirty(src);
1125 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001126 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001127}
1128
Chris Mason97571fd2007-02-24 13:39:08 -05001129/*
Chris Mason79f95c82007-03-01 15:16:26 -05001130 * try to push data from one node into the next node right in the
1131 * tree.
1132 *
1133 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1134 * error, and > 0 if there was no room in the right hand block.
1135 *
1136 * this will only push up to 1/2 the contents of the left node over
1137 */
Chris Mason5f39d392007-10-15 16:14:19 -04001138static int balance_node_right(struct btrfs_trans_handle *trans,
1139 struct btrfs_root *root,
1140 struct extent_buffer *dst,
1141 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001142{
Chris Mason79f95c82007-03-01 15:16:26 -05001143 int push_items = 0;
1144 int max_push;
1145 int src_nritems;
1146 int dst_nritems;
1147 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001148
Chris Mason5f39d392007-10-15 16:14:19 -04001149 src_nritems = btrfs_header_nritems(src);
1150 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001151 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001152 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001153 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001154
1155 max_push = src_nritems / 2 + 1;
1156 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001157 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001158 return 1;
Yan252c38f2007-08-29 09:11:44 -04001159
Chris Mason79f95c82007-03-01 15:16:26 -05001160 if (max_push < push_items)
1161 push_items = max_push;
1162
Chris Mason5f39d392007-10-15 16:14:19 -04001163 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1164 btrfs_node_key_ptr_offset(0),
1165 (dst_nritems) *
1166 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001167
Chris Mason5f39d392007-10-15 16:14:19 -04001168 copy_extent_buffer(dst, src,
1169 btrfs_node_key_ptr_offset(0),
1170 btrfs_node_key_ptr_offset(src_nritems - push_items),
1171 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001172
Chris Mason5f39d392007-10-15 16:14:19 -04001173 btrfs_set_header_nritems(src, src_nritems - push_items);
1174 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001175
Chris Mason5f39d392007-10-15 16:14:19 -04001176 btrfs_mark_buffer_dirty(src);
1177 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001178 return ret;
1179}
1180
1181/*
Chris Mason97571fd2007-02-24 13:39:08 -05001182 * helper function to insert a new root level in the tree.
1183 * A new node is allocated, and a single item is inserted to
1184 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001185 *
1186 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001187 */
Chris Mason5f39d392007-10-15 16:14:19 -04001188static int insert_new_root(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root,
1190 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001191{
Chris Mason5f39d392007-10-15 16:14:19 -04001192 struct extent_buffer *lower;
1193 struct extent_buffer *c;
1194 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001195
1196 BUG_ON(path->nodes[level]);
1197 BUG_ON(path->nodes[level-1] != root->node);
1198
Chris Masondb945352007-10-15 16:15:53 -04001199 c = btrfs_alloc_free_block(trans, root, root->nodesize,
1200 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001201 if (IS_ERR(c))
1202 return PTR_ERR(c);
1203 memset_extent_buffer(c, 0, 0, root->nodesize);
1204 btrfs_set_header_nritems(c, 1);
1205 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001206 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001207 btrfs_set_header_generation(c, trans->transid);
1208 btrfs_set_header_owner(c, root->root_key.objectid);
1209 lower = path->nodes[level-1];
Chris Masond5719762007-03-23 10:01:08 -04001210
Chris Mason5f39d392007-10-15 16:14:19 -04001211 write_extent_buffer(c, root->fs_info->fsid,
1212 (unsigned long)btrfs_header_fsid(c),
1213 BTRFS_FSID_SIZE);
1214 if (level == 1)
1215 btrfs_item_key(lower, &lower_key, 0);
1216 else
1217 btrfs_node_key(lower, &lower_key, 0);
1218 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001219 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001220
1221 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001222
Chris Mason5c680ed2007-02-22 11:39:13 -05001223 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001224 free_extent_buffer(root->node);
1225 root->node = c;
1226 extent_buffer_get(c);
1227 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001228 path->slots[level] = 0;
1229 return 0;
1230}
1231
Chris Mason74123bd2007-02-02 11:05:29 -05001232/*
1233 * worker function to insert a single pointer in a node.
1234 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001235 *
Chris Mason74123bd2007-02-02 11:05:29 -05001236 * slot and level indicate where you want the key to go, and
1237 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001238 *
1239 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001240 */
Chris Masone089f052007-03-16 16:20:31 -04001241static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1242 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001243 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001244{
Chris Mason5f39d392007-10-15 16:14:19 -04001245 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001246 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001247
1248 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001249 lower = path->nodes[level];
1250 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001251 if (slot > nritems)
1252 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001253 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001254 BUG();
1255 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001256 memmove_extent_buffer(lower,
1257 btrfs_node_key_ptr_offset(slot + 1),
1258 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001259 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001260 }
Chris Mason5f39d392007-10-15 16:14:19 -04001261 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001262 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason5f39d392007-10-15 16:14:19 -04001263 btrfs_set_header_nritems(lower, nritems + 1);
1264 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001265 return 0;
1266}
1267
Chris Mason97571fd2007-02-24 13:39:08 -05001268/*
1269 * split the node at the specified level in path in two.
1270 * The path is corrected to point to the appropriate node after the split
1271 *
1272 * Before splitting this tries to make some room in the node by pushing
1273 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001274 *
1275 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001276 */
Chris Masone089f052007-03-16 16:20:31 -04001277static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1278 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001279{
Chris Mason5f39d392007-10-15 16:14:19 -04001280 struct extent_buffer *c;
1281 struct extent_buffer *split;
1282 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001283 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001284 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001285 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001286 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001287
Chris Mason5f39d392007-10-15 16:14:19 -04001288 c = path->nodes[level];
1289 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001290 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001291 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001292 if (ret)
1293 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001294 } else {
1295 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001296 c = path->nodes[level];
1297 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001298 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1299 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001300 if (ret < 0)
1301 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001302 }
Chris Masone66f7092007-04-20 13:16:02 -04001303
Chris Mason5f39d392007-10-15 16:14:19 -04001304 c_nritems = btrfs_header_nritems(c);
Chris Masondb945352007-10-15 16:15:53 -04001305 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1306 c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001307 if (IS_ERR(split))
1308 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001309
Chris Mason5f39d392007-10-15 16:14:19 -04001310 btrfs_set_header_flags(split, btrfs_header_flags(c));
1311 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001312 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001313 btrfs_set_header_generation(split, trans->transid);
1314 btrfs_set_header_owner(split, root->root_key.objectid);
1315 write_extent_buffer(split, root->fs_info->fsid,
1316 (unsigned long)btrfs_header_fsid(split),
1317 BTRFS_FSID_SIZE);
1318
Chris Mason7518a232007-03-12 12:01:18 -04001319 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001320
1321 copy_extent_buffer(split, c,
1322 btrfs_node_key_ptr_offset(0),
1323 btrfs_node_key_ptr_offset(mid),
1324 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1325 btrfs_set_header_nritems(split, c_nritems - mid);
1326 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001327 ret = 0;
1328
Chris Mason5f39d392007-10-15 16:14:19 -04001329 btrfs_mark_buffer_dirty(c);
1330 btrfs_mark_buffer_dirty(split);
1331
1332 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001333 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001334 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001335 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001336 if (wret)
1337 ret = wret;
1338
Chris Mason5de08d72007-02-24 06:24:44 -05001339 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001340 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001341 free_extent_buffer(c);
1342 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001343 path->slots[level + 1] += 1;
1344 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001345 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001346 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001347 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001348}
1349
Chris Mason74123bd2007-02-02 11:05:29 -05001350/*
1351 * how many bytes are required to store the items in a leaf. start
1352 * and nr indicate which items in the leaf to check. This totals up the
1353 * space used both by the item structs and the item data
1354 */
Chris Mason5f39d392007-10-15 16:14:19 -04001355static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001356{
1357 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001358 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001359 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001360
1361 if (!nr)
1362 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001363 data_len = btrfs_item_end_nr(l, start);
1364 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001365 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001366 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001367 return data_len;
1368}
1369
Chris Mason74123bd2007-02-02 11:05:29 -05001370/*
Chris Masond4dbff92007-04-04 14:08:15 -04001371 * The space between the end of the leaf items and
1372 * the start of the leaf data. IOW, how much room
1373 * the leaf has left for both items and data
1374 */
Chris Mason5f39d392007-10-15 16:14:19 -04001375int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001376{
Chris Mason5f39d392007-10-15 16:14:19 -04001377 int nritems = btrfs_header_nritems(leaf);
1378 int ret;
1379 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1380 if (ret < 0) {
1381 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1382 ret, BTRFS_LEAF_DATA_SIZE(root),
1383 leaf_space_used(leaf, 0, nritems), nritems);
1384 }
1385 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001386}
1387
1388/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001389 * push some data in the path leaf to the right, trying to free up at
1390 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001391 *
1392 * returns 1 if the push failed because the other node didn't have enough
1393 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001394 */
Chris Masone089f052007-03-16 16:20:31 -04001395static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1396 *root, struct btrfs_path *path, int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -05001397{
Chris Mason5f39d392007-10-15 16:14:19 -04001398 struct extent_buffer *left = path->nodes[0];
1399 struct extent_buffer *right;
1400 struct extent_buffer *upper;
1401 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001402 int slot;
1403 int i;
1404 int free_space;
1405 int push_space = 0;
1406 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001407 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001408 u32 left_nritems;
1409 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001410 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001411 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001412 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001413
1414 slot = path->slots[1];
1415 if (!path->nodes[1]) {
1416 return 1;
1417 }
1418 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001419 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001420 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001421
Chris Masondb945352007-10-15 16:15:53 -04001422 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1423 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001424 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001425 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001426 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001427 return 1;
1428 }
1429
Chris Mason5f39d392007-10-15 16:14:19 -04001430 /* cow and double check */
1431 ret = btrfs_cow_block(trans, root, right, upper,
1432 slot + 1, &right);
1433 if (ret) {
1434 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001435 return 1;
1436 }
Chris Mason5f39d392007-10-15 16:14:19 -04001437 free_space = btrfs_leaf_free_space(root, right);
1438 if (free_space < data_size + sizeof(struct btrfs_item)) {
1439 free_extent_buffer(right);
1440 return 1;
1441 }
1442
1443 left_nritems = btrfs_header_nritems(left);
1444 if (left_nritems == 0) {
1445 free_extent_buffer(right);
1446 return 1;
1447 }
1448
Chris Masona429e512007-04-18 16:15:28 -04001449 for (i = left_nritems - 1; i >= 1; i--) {
Chris Mason5f39d392007-10-15 16:14:19 -04001450 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001451
Chris Mason00ec4c52007-02-24 12:47:20 -05001452 if (path->slots[0] == i)
1453 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001454
1455 if (!left->map_token) {
1456 map_extent_buffer(left, (unsigned long)item,
1457 sizeof(struct btrfs_item),
1458 &left->map_token, &left->kaddr,
1459 &left->map_start, &left->map_len,
1460 KM_USER1);
1461 }
1462
1463 this_item_size = btrfs_item_size(left, item);
1464 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001465 break;
1466 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001467 push_space += this_item_size + sizeof(*item);
1468 }
1469 if (left->map_token) {
1470 unmap_extent_buffer(left, left->map_token, KM_USER1);
1471 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001472 }
Chris Mason5f39d392007-10-15 16:14:19 -04001473
Chris Mason00ec4c52007-02-24 12:47:20 -05001474 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001475 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001476 return 1;
1477 }
Chris Mason5f39d392007-10-15 16:14:19 -04001478
Chris Masona429e512007-04-18 16:15:28 -04001479 if (push_items == left_nritems)
1480 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001481
Chris Mason00ec4c52007-02-24 12:47:20 -05001482 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001483 right_nritems = btrfs_header_nritems(right);
1484 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001485 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001486
Chris Mason00ec4c52007-02-24 12:47:20 -05001487 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001488 data_end = leaf_data_end(root, right);
1489 memmove_extent_buffer(right,
1490 btrfs_leaf_data(right) + data_end - push_space,
1491 btrfs_leaf_data(right) + data_end,
1492 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1493
Chris Mason00ec4c52007-02-24 12:47:20 -05001494 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001495 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001496 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1497 btrfs_leaf_data(left) + leaf_data_end(root, left),
1498 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001499
1500 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1501 btrfs_item_nr_offset(0),
1502 right_nritems * sizeof(struct btrfs_item));
1503
Chris Mason00ec4c52007-02-24 12:47:20 -05001504 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001505 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1506 btrfs_item_nr_offset(left_nritems - push_items),
1507 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001508
1509 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001510 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001511 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001512 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masondb945352007-10-15 16:15:53 -04001513
Chris Mason7518a232007-03-12 12:01:18 -04001514 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001515 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001516 if (!right->map_token) {
1517 map_extent_buffer(right, (unsigned long)item,
1518 sizeof(struct btrfs_item),
1519 &right->map_token, &right->kaddr,
1520 &right->map_start, &right->map_len,
1521 KM_USER1);
1522 }
1523 push_space -= btrfs_item_size(right, item);
1524 btrfs_set_item_offset(right, item, push_space);
1525 }
1526
1527 if (right->map_token) {
1528 unmap_extent_buffer(right, right->map_token, KM_USER1);
1529 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001530 }
Chris Mason7518a232007-03-12 12:01:18 -04001531 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001532 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001533
Chris Mason5f39d392007-10-15 16:14:19 -04001534 btrfs_mark_buffer_dirty(left);
1535 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001536
Chris Mason5f39d392007-10-15 16:14:19 -04001537 btrfs_item_key(right, &disk_key, 0);
1538 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001539 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001540
Chris Mason00ec4c52007-02-24 12:47:20 -05001541 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001542 if (path->slots[0] >= left_nritems) {
1543 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001544 free_extent_buffer(path->nodes[0]);
1545 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001546 path->slots[1] += 1;
1547 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001548 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001549 }
1550 return 0;
1551}
1552/*
Chris Mason74123bd2007-02-02 11:05:29 -05001553 * push some data in the path leaf to the left, trying to free up at
1554 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1555 */
Chris Masone089f052007-03-16 16:20:31 -04001556static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1557 *root, struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001558{
Chris Mason5f39d392007-10-15 16:14:19 -04001559 struct btrfs_disk_key disk_key;
1560 struct extent_buffer *right = path->nodes[0];
1561 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001562 int slot;
1563 int i;
1564 int free_space;
1565 int push_space = 0;
1566 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001567 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001568 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001569 u32 right_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001570 int ret = 0;
1571 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001572 u32 this_item_size;
1573 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001574
1575 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001576 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001577 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001578 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001579 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001580
1581 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001582 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001583 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001584 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001585 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001586 return 1;
1587 }
Chris Mason02217ed2007-03-02 16:08:05 -05001588
1589 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001590 ret = btrfs_cow_block(trans, root, left,
1591 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001592 if (ret) {
1593 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001594 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001595 return 1;
1596 }
Chris Mason123abc82007-03-14 14:14:43 -04001597 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001598 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001599 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001600 return 1;
1601 }
1602
Chris Mason5f39d392007-10-15 16:14:19 -04001603 right_nritems = btrfs_header_nritems(right);
1604 if (right_nritems == 0) {
1605 free_extent_buffer(left);
Chris Masona429e512007-04-18 16:15:28 -04001606 return 1;
1607 }
1608
Chris Mason5f39d392007-10-15 16:14:19 -04001609 for (i = 0; i < right_nritems - 1; i++) {
1610 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001611 if (!right->map_token) {
1612 map_extent_buffer(right, (unsigned long)item,
1613 sizeof(struct btrfs_item),
1614 &right->map_token, &right->kaddr,
1615 &right->map_start, &right->map_len,
1616 KM_USER1);
1617 }
1618
Chris Masonbe0e5c02007-01-26 15:51:26 -05001619 if (path->slots[0] == i)
1620 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001621
1622 this_item_size = btrfs_item_size(right, item);
1623 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001624 break;
Chris Masondb945352007-10-15 16:15:53 -04001625
Chris Masonbe0e5c02007-01-26 15:51:26 -05001626 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001627 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001628 }
Chris Masondb945352007-10-15 16:15:53 -04001629
1630 if (right->map_token) {
1631 unmap_extent_buffer(right, right->map_token, KM_USER1);
1632 right->map_token = NULL;
1633 }
1634
Chris Masonbe0e5c02007-01-26 15:51:26 -05001635 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001636 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001637 return 1;
1638 }
Chris Mason5f39d392007-10-15 16:14:19 -04001639 if (push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001640 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001641
Chris Masonbe0e5c02007-01-26 15:51:26 -05001642 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001643 copy_extent_buffer(left, right,
1644 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1645 btrfs_item_nr_offset(0),
1646 push_items * sizeof(struct btrfs_item));
1647
Chris Mason123abc82007-03-14 14:14:43 -04001648 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001649 btrfs_item_offset_nr(right, push_items -1);
1650
1651 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001652 leaf_data_end(root, left) - push_space,
1653 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001654 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001655 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001656 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001657 BUG_ON(old_left_nritems < 0);
1658
Chris Masondb945352007-10-15 16:15:53 -04001659 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001660 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001661 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001662
Chris Mason5f39d392007-10-15 16:14:19 -04001663 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001664 if (!left->map_token) {
1665 map_extent_buffer(left, (unsigned long)item,
1666 sizeof(struct btrfs_item),
1667 &left->map_token, &left->kaddr,
1668 &left->map_start, &left->map_len,
1669 KM_USER1);
1670 }
1671
Chris Mason5f39d392007-10-15 16:14:19 -04001672 ioff = btrfs_item_offset(left, item);
1673 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001674 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001675 }
Chris Mason5f39d392007-10-15 16:14:19 -04001676 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001677 if (left->map_token) {
1678 unmap_extent_buffer(left, left->map_token, KM_USER1);
1679 left->map_token = NULL;
1680 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001681
1682 /* fixup right node */
Chris Mason5f39d392007-10-15 16:14:19 -04001683 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1684 leaf_data_end(root, right);
1685 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1686 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1687 btrfs_leaf_data(right) +
1688 leaf_data_end(root, right), push_space);
1689
1690 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1691 btrfs_item_nr_offset(push_items),
1692 (btrfs_header_nritems(right) - push_items) *
1693 sizeof(struct btrfs_item));
1694
1695 right_nritems = btrfs_header_nritems(right) - push_items;
1696 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001697 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Masoneb60cea2007-02-02 09:18:22 -05001698
Chris Mason5f39d392007-10-15 16:14:19 -04001699 for (i = 0; i < right_nritems; i++) {
1700 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001701
1702 if (!right->map_token) {
1703 map_extent_buffer(right, (unsigned long)item,
1704 sizeof(struct btrfs_item),
1705 &right->map_token, &right->kaddr,
1706 &right->map_start, &right->map_len,
1707 KM_USER1);
1708 }
1709
1710 push_space = push_space - btrfs_item_size(right, item);
1711 btrfs_set_item_offset(right, item, push_space);
1712 }
1713 if (right->map_token) {
1714 unmap_extent_buffer(right, right->map_token, KM_USER1);
1715 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001716 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001717
Chris Mason5f39d392007-10-15 16:14:19 -04001718 btrfs_mark_buffer_dirty(left);
1719 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001720
Chris Mason5f39d392007-10-15 16:14:19 -04001721 btrfs_item_key(right, &disk_key, 0);
1722 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001723 if (wret)
1724 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001725
1726 /* then fixup the leaf pointer in the path */
1727 if (path->slots[0] < push_items) {
1728 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001729 free_extent_buffer(path->nodes[0]);
1730 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001731 path->slots[1] -= 1;
1732 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001733 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001734 path->slots[0] -= push_items;
1735 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001736 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001737 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001738}
1739
Chris Mason74123bd2007-02-02 11:05:29 -05001740/*
1741 * split the path's leaf in two, making sure there is at least data_size
1742 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001743 *
1744 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001745 */
Chris Masone089f052007-03-16 16:20:31 -04001746static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001747 *root, struct btrfs_key *ins_key,
1748 struct btrfs_path *path, int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001749{
Chris Mason5f39d392007-10-15 16:14:19 -04001750 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001751 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001752 int mid;
1753 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001754 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001755 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001756 int data_copy_size;
1757 int rt_data_off;
1758 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001759 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001760 int wret;
Chris Masond4dbff92007-04-04 14:08:15 -04001761 int double_split = 0;
1762 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001763
Chris Mason40689472007-03-17 14:29:23 -04001764 /* first try to make some room by pushing left and right */
Chris Masone089f052007-03-16 16:20:31 -04001765 wret = push_leaf_left(trans, root, path, data_size);
Chris Mason3326d1b2007-10-15 16:18:25 -04001766 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04001767 return wret;
Chris Mason3326d1b2007-10-15 16:18:25 -04001768 }
Chris Masoneaee50e2007-03-13 11:17:52 -04001769 if (wret) {
Chris Masone089f052007-03-16 16:20:31 -04001770 wret = push_leaf_right(trans, root, path, data_size);
Chris Masoneaee50e2007-03-13 11:17:52 -04001771 if (wret < 0)
1772 return wret;
1773 }
Chris Mason5f39d392007-10-15 16:14:19 -04001774 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05001775
1776 /* did the pushes work? */
Chris Mason123abc82007-03-14 14:14:43 -04001777 if (btrfs_leaf_free_space(root, l) >=
Chris Mason3326d1b2007-10-15 16:18:25 -04001778 sizeof(struct btrfs_item) + data_size) {
Chris Masonaa5d6be2007-02-28 16:35:06 -05001779 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04001780 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001781
Chris Mason5c680ed2007-02-22 11:39:13 -05001782 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001783 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001784 if (ret)
1785 return ret;
1786 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001787 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001788 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001789 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001790
Chris Masondb945352007-10-15 16:15:53 -04001791 right = btrfs_alloc_free_block(trans, root, root->leafsize,
1792 l->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001793 if (IS_ERR(right))
1794 return PTR_ERR(right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001795
Chris Mason5f39d392007-10-15 16:14:19 -04001796 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04001797 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001798 btrfs_set_header_generation(right, trans->transid);
1799 btrfs_set_header_owner(right, root->root_key.objectid);
1800 btrfs_set_header_level(right, 0);
1801 write_extent_buffer(right, root->fs_info->fsid,
1802 (unsigned long)btrfs_header_fsid(right),
1803 BTRFS_FSID_SIZE);
1804
Chris Masond4dbff92007-04-04 14:08:15 -04001805 if (mid <= slot) {
1806 if (nritems == 1 ||
1807 leaf_space_used(l, mid, nritems - mid) + space_needed >
1808 BTRFS_LEAF_DATA_SIZE(root)) {
1809 if (slot >= nritems) {
1810 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001811 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001812 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04001813 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04001814 path->slots[1] + 1, 1);
1815 if (wret)
1816 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001817 free_extent_buffer(path->nodes[0]);
1818 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001819 path->slots[0] = 0;
1820 path->slots[1] += 1;
1821 return ret;
1822 }
1823 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04001824 if (mid != nritems &&
1825 leaf_space_used(l, mid, nritems - mid) +
1826 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1827 double_split = 1;
1828 }
Chris Masond4dbff92007-04-04 14:08:15 -04001829 }
1830 } else {
1831 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1832 BTRFS_LEAF_DATA_SIZE(root)) {
1833 if (slot == 0) {
1834 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001835 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001836 wret = insert_ptr(trans, root, path,
1837 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04001838 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04001839 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04001840 if (wret)
1841 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001842 free_extent_buffer(path->nodes[0]);
1843 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001844 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04001845 if (path->slots[1] == 0) {
1846 wret = fixup_low_keys(trans, root,
1847 path, &disk_key, 1);
1848 if (wret)
1849 ret = wret;
1850 }
Chris Masond4dbff92007-04-04 14:08:15 -04001851 return ret;
1852 }
1853 mid = slot;
1854 double_split = 1;
1855 }
1856 }
Chris Mason5f39d392007-10-15 16:14:19 -04001857 nritems = nritems - mid;
1858 btrfs_set_header_nritems(right, nritems);
1859 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
1860
1861 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1862 btrfs_item_nr_offset(mid),
1863 nritems * sizeof(struct btrfs_item));
1864
1865 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04001866 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1867 data_copy_size, btrfs_leaf_data(l) +
1868 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05001869
Chris Mason5f39d392007-10-15 16:14:19 -04001870 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1871 btrfs_item_end_nr(l, mid);
1872
1873 for (i = 0; i < nritems; i++) {
1874 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001875 u32 ioff;
1876
1877 if (!right->map_token) {
1878 map_extent_buffer(right, (unsigned long)item,
1879 sizeof(struct btrfs_item),
1880 &right->map_token, &right->kaddr,
1881 &right->map_start, &right->map_len,
1882 KM_USER1);
1883 }
1884
1885 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04001886 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04001887 }
Chris Mason74123bd2007-02-02 11:05:29 -05001888
Chris Masondb945352007-10-15 16:15:53 -04001889 if (right->map_token) {
1890 unmap_extent_buffer(right, right->map_token, KM_USER1);
1891 right->map_token = NULL;
1892 }
1893
Chris Mason5f39d392007-10-15 16:14:19 -04001894 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001895 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001896 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001897 wret = insert_ptr(trans, root, path, &disk_key, right->start,
1898 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001899 if (wret)
1900 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001901
1902 btrfs_mark_buffer_dirty(right);
1903 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001904 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04001905
Chris Masonbe0e5c02007-01-26 15:51:26 -05001906 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04001907 free_extent_buffer(path->nodes[0]);
1908 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001909 path->slots[0] -= mid;
1910 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001911 } else
Chris Mason5f39d392007-10-15 16:14:19 -04001912 free_extent_buffer(right);
1913
Chris Masoneb60cea2007-02-02 09:18:22 -05001914 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001915
Chris Mason3326d1b2007-10-15 16:18:25 -04001916 if (!double_split) {
Chris Masond4dbff92007-04-04 14:08:15 -04001917 return ret;
Chris Mason3326d1b2007-10-15 16:18:25 -04001918 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001919
Chris Masondb945352007-10-15 16:15:53 -04001920 right = btrfs_alloc_free_block(trans, root, root->leafsize,
1921 l->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001922 if (IS_ERR(right))
1923 return PTR_ERR(right);
1924
1925 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04001926 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001927 btrfs_set_header_generation(right, trans->transid);
1928 btrfs_set_header_owner(right, root->root_key.objectid);
1929 btrfs_set_header_level(right, 0);
1930 write_extent_buffer(right, root->fs_info->fsid,
1931 (unsigned long)btrfs_header_fsid(right),
1932 BTRFS_FSID_SIZE);
1933
Chris Masond4dbff92007-04-04 14:08:15 -04001934 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04001935 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04001936 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04001937 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04001938 path->slots[1], 1);
1939 if (wret)
1940 ret = wret;
Chris Masona429e512007-04-18 16:15:28 -04001941 if (path->slots[1] == 0) {
1942 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1943 if (wret)
1944 ret = wret;
1945 }
Chris Mason5f39d392007-10-15 16:14:19 -04001946 free_extent_buffer(path->nodes[0]);
1947 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04001948 path->slots[0] = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001949 return ret;
1950}
1951
Chris Masonb18c6682007-04-17 13:26:50 -04001952int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct btrfs_path *path,
1955 u32 new_size)
1956{
1957 int ret = 0;
1958 int slot;
1959 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04001960 struct extent_buffer *leaf;
1961 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04001962 u32 nritems;
1963 unsigned int data_end;
1964 unsigned int old_data_start;
1965 unsigned int old_size;
1966 unsigned int size_diff;
1967 int i;
1968
1969 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001970 leaf = path->nodes[0];
Chris Masonb18c6682007-04-17 13:26:50 -04001971
Chris Mason5f39d392007-10-15 16:14:19 -04001972 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04001973 data_end = leaf_data_end(root, leaf);
1974
1975 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001976 old_data_start = btrfs_item_offset_nr(leaf, slot);
1977 old_size = btrfs_item_size_nr(leaf, slot);
Chris Masonb18c6682007-04-17 13:26:50 -04001978 BUG_ON(old_size <= new_size);
1979 size_diff = old_size - new_size;
1980
1981 BUG_ON(slot < 0);
1982 BUG_ON(slot >= nritems);
1983
1984 /*
1985 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1986 */
1987 /* first correct the data pointers */
1988 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001989 u32 ioff;
1990 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04001991
1992 if (!leaf->map_token) {
1993 map_extent_buffer(leaf, (unsigned long)item,
1994 sizeof(struct btrfs_item),
1995 &leaf->map_token, &leaf->kaddr,
1996 &leaf->map_start, &leaf->map_len,
1997 KM_USER1);
1998 }
1999
Chris Mason5f39d392007-10-15 16:14:19 -04002000 ioff = btrfs_item_offset(leaf, item);
2001 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002002 }
Chris Masondb945352007-10-15 16:15:53 -04002003
2004 if (leaf->map_token) {
2005 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2006 leaf->map_token = NULL;
2007 }
2008
Chris Masonb18c6682007-04-17 13:26:50 -04002009 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002010 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masonb18c6682007-04-17 13:26:50 -04002011 data_end + size_diff, btrfs_leaf_data(leaf) +
2012 data_end, old_data_start + new_size - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002013
2014 item = btrfs_item_nr(leaf, slot);
2015 btrfs_set_item_size(leaf, item, new_size);
2016 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002017
2018 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002019 if (btrfs_leaf_free_space(root, leaf) < 0) {
2020 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002021 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002022 }
Chris Masonb18c6682007-04-17 13:26:50 -04002023 return ret;
2024}
2025
Chris Mason5f39d392007-10-15 16:14:19 -04002026int btrfs_extend_item(struct btrfs_trans_handle *trans,
2027 struct btrfs_root *root, struct btrfs_path *path,
2028 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002029{
2030 int ret = 0;
2031 int slot;
2032 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002033 struct extent_buffer *leaf;
2034 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002035 u32 nritems;
2036 unsigned int data_end;
2037 unsigned int old_data;
2038 unsigned int old_size;
2039 int i;
2040
2041 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002042 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002043
Chris Mason5f39d392007-10-15 16:14:19 -04002044 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002045 data_end = leaf_data_end(root, leaf);
2046
Chris Mason5f39d392007-10-15 16:14:19 -04002047 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2048 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002049 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002050 }
Chris Mason6567e832007-04-16 09:22:45 -04002051 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002052 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002053
2054 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002055 if (slot >= nritems) {
2056 btrfs_print_leaf(root, leaf);
2057 printk("slot %d too large, nritems %d\n", slot, nritems);
2058 BUG_ON(1);
2059 }
Chris Mason6567e832007-04-16 09:22:45 -04002060
2061 /*
2062 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2063 */
2064 /* first correct the data pointers */
2065 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002066 u32 ioff;
2067 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002068
2069 if (!leaf->map_token) {
2070 map_extent_buffer(leaf, (unsigned long)item,
2071 sizeof(struct btrfs_item),
2072 &leaf->map_token, &leaf->kaddr,
2073 &leaf->map_start, &leaf->map_len,
2074 KM_USER1);
2075 }
Chris Mason5f39d392007-10-15 16:14:19 -04002076 ioff = btrfs_item_offset(leaf, item);
2077 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002078 }
Chris Mason5f39d392007-10-15 16:14:19 -04002079
Chris Masondb945352007-10-15 16:15:53 -04002080 if (leaf->map_token) {
2081 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2082 leaf->map_token = NULL;
2083 }
2084
Chris Mason6567e832007-04-16 09:22:45 -04002085 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002086 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002087 data_end - data_size, btrfs_leaf_data(leaf) +
2088 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002089
Chris Mason6567e832007-04-16 09:22:45 -04002090 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002091 old_size = btrfs_item_size_nr(leaf, slot);
2092 item = btrfs_item_nr(leaf, slot);
2093 btrfs_set_item_size(leaf, item, old_size + data_size);
2094 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002095
2096 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002097 if (btrfs_leaf_free_space(root, leaf) < 0) {
2098 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002099 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002100 }
Chris Mason6567e832007-04-16 09:22:45 -04002101 return ret;
2102}
2103
Chris Mason74123bd2007-02-02 11:05:29 -05002104/*
2105 * Given a key and some data, insert an item into the tree.
2106 * This does all the path init required, making room in the tree if needed.
2107 */
Chris Mason5f39d392007-10-15 16:14:19 -04002108int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2109 struct btrfs_root *root,
2110 struct btrfs_path *path,
2111 struct btrfs_key *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002112{
Chris Mason5f39d392007-10-15 16:14:19 -04002113 struct extent_buffer *leaf;
2114 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002115 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002116 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002117 int slot_orig;
Chris Mason7518a232007-03-12 12:01:18 -04002118 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002119 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002120 struct btrfs_disk_key disk_key;
2121
2122 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002123
Chris Mason74123bd2007-02-02 11:05:29 -05002124 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002125 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002126 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002127
Chris Masone089f052007-03-16 16:20:31 -04002128 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002129 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002130 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002131 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002132 if (ret < 0)
2133 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002134
Chris Mason62e27492007-03-15 12:56:47 -04002135 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002136 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002137
Chris Mason5f39d392007-10-15 16:14:19 -04002138 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002139 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002140
Chris Mason123abc82007-03-14 14:14:43 -04002141 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04002142 sizeof(struct btrfs_item) + data_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002143 btrfs_print_leaf(root, leaf);
2144 printk("not enough freespace need %u have %d\n",
2145 data_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002146 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002147 }
Chris Mason5f39d392007-10-15 16:14:19 -04002148
Chris Mason62e27492007-03-15 12:56:47 -04002149 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002150 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002151
Chris Masonbe0e5c02007-01-26 15:51:26 -05002152 if (slot != nritems) {
2153 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002154 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002155
Chris Mason5f39d392007-10-15 16:14:19 -04002156 if (old_data < data_end) {
2157 btrfs_print_leaf(root, leaf);
2158 printk("slot %d old_data %d data_end %d\n",
2159 slot, old_data, data_end);
2160 BUG_ON(1);
2161 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002162 /*
2163 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2164 */
2165 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002166 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002167 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002168 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002169
Chris Mason5f39d392007-10-15 16:14:19 -04002170 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002171 if (!leaf->map_token) {
2172 map_extent_buffer(leaf, (unsigned long)item,
2173 sizeof(struct btrfs_item),
2174 &leaf->map_token, &leaf->kaddr,
2175 &leaf->map_start, &leaf->map_len,
2176 KM_USER1);
2177 }
2178
Chris Mason5f39d392007-10-15 16:14:19 -04002179 ioff = btrfs_item_offset(leaf, item);
2180 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason0783fcf2007-03-12 20:12:07 -04002181 }
Chris Masondb945352007-10-15 16:15:53 -04002182 if (leaf->map_token) {
2183 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2184 leaf->map_token = NULL;
2185 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002186
2187 /* shift the items */
Chris Mason5f39d392007-10-15 16:14:19 -04002188 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2189 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002190 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002191
2192 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002193 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002194 data_end - data_size, btrfs_leaf_data(leaf) +
2195 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002196 data_end = old_data;
2197 }
Chris Mason5f39d392007-10-15 16:14:19 -04002198
Chris Mason62e27492007-03-15 12:56:47 -04002199 /* setup the item for the new data */
Chris Mason5f39d392007-10-15 16:14:19 -04002200 btrfs_set_item_key(leaf, &disk_key, slot);
2201 item = btrfs_item_nr(leaf, slot);
2202 btrfs_set_item_offset(leaf, item, data_end - data_size);
2203 btrfs_set_item_size(leaf, item, data_size);
2204 btrfs_set_header_nritems(leaf, nritems + 1);
2205 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002206
2207 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05002208 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04002209 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002210
Chris Mason5f39d392007-10-15 16:14:19 -04002211 if (btrfs_leaf_free_space(root, leaf) < 0) {
2212 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002213 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002214 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002215out:
Chris Mason62e27492007-03-15 12:56:47 -04002216 return ret;
2217}
2218
2219/*
2220 * Given a key and some data, insert an item into the tree.
2221 * This does all the path init required, making room in the tree if needed.
2222 */
Chris Masone089f052007-03-16 16:20:31 -04002223int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2224 *root, struct btrfs_key *cpu_key, void *data, u32
2225 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002226{
2227 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002228 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002229 struct extent_buffer *leaf;
2230 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002231
Chris Mason2c90e5d2007-04-02 10:50:19 -04002232 path = btrfs_alloc_path();
2233 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002234 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002235 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002236 leaf = path->nodes[0];
2237 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2238 write_extent_buffer(leaf, data, ptr, data_size);
2239 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002240 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002241 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002242 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002243}
2244
Chris Mason74123bd2007-02-02 11:05:29 -05002245/*
Chris Mason5de08d72007-02-24 06:24:44 -05002246 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002247 *
2248 * If the delete empties a node, the node is removed from the tree,
2249 * continuing all the way the root if required. The root is converted into
2250 * a leaf if all the nodes are emptied.
2251 */
Chris Masone089f052007-03-16 16:20:31 -04002252static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2253 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002254{
Chris Mason5f39d392007-10-15 16:14:19 -04002255 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002256 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002257 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002258 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002259
Chris Mason5f39d392007-10-15 16:14:19 -04002260 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002261 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002262 memmove_extent_buffer(parent,
2263 btrfs_node_key_ptr_offset(slot),
2264 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002265 sizeof(struct btrfs_key_ptr) *
2266 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002267 }
Chris Mason7518a232007-03-12 12:01:18 -04002268 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002269 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002270 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002271 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002272 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002273 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002274 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002275 struct btrfs_disk_key disk_key;
2276
2277 btrfs_node_key(parent, &disk_key, 0);
2278 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002279 if (wret)
2280 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002281 }
Chris Masond6025572007-03-30 14:27:56 -04002282 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002283 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002284}
2285
Chris Mason74123bd2007-02-02 11:05:29 -05002286/*
2287 * delete the item at the leaf level in path. If that empties
2288 * the leaf, remove it from the tree
2289 */
Chris Masone089f052007-03-16 16:20:31 -04002290int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2291 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002292{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002293 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002294 struct extent_buffer *leaf;
2295 struct btrfs_item *item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002296 int doff;
2297 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002298 int ret = 0;
2299 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002300 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002301
Chris Mason5f39d392007-10-15 16:14:19 -04002302 leaf = path->nodes[0];
Chris Mason4920c9a2007-01-26 16:38:42 -05002303 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002304 doff = btrfs_item_offset_nr(leaf, slot);
2305 dsize = btrfs_item_size_nr(leaf, slot);
2306 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002307
Chris Mason7518a232007-03-12 12:01:18 -04002308 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002309 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002310 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002311
2312 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002313 data_end + dsize,
2314 btrfs_leaf_data(leaf) + data_end,
2315 doff - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002316
Chris Mason0783fcf2007-03-12 20:12:07 -04002317 for (i = slot + 1; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002318 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002319
Chris Mason5f39d392007-10-15 16:14:19 -04002320 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002321 if (!leaf->map_token) {
2322 map_extent_buffer(leaf, (unsigned long)item,
2323 sizeof(struct btrfs_item),
2324 &leaf->map_token, &leaf->kaddr,
2325 &leaf->map_start, &leaf->map_len,
2326 KM_USER1);
2327 }
Chris Mason5f39d392007-10-15 16:14:19 -04002328 ioff = btrfs_item_offset(leaf, item);
2329 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002330 }
Chris Masondb945352007-10-15 16:15:53 -04002331
2332 if (leaf->map_token) {
2333 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2334 leaf->map_token = NULL;
2335 }
2336
Chris Mason5f39d392007-10-15 16:14:19 -04002337 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2338 btrfs_item_nr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002339 sizeof(struct btrfs_item) *
2340 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002341 }
Chris Mason5f39d392007-10-15 16:14:19 -04002342 btrfs_set_header_nritems(leaf, nritems - 1);
Chris Mason7518a232007-03-12 12:01:18 -04002343 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002344
Chris Mason74123bd2007-02-02 11:05:29 -05002345 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002346 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002347 if (leaf == root->node) {
2348 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002349 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002350 clean_tree_block(trans, root, leaf);
2351 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002352 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002353 if (wret)
2354 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002355 wret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002356 leaf->start, leaf->len, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002357 if (wret)
2358 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002359 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002360 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002361 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002362 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002363 struct btrfs_disk_key disk_key;
2364
2365 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002366 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002367 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002368 if (wret)
2369 ret = wret;
2370 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002371
Chris Mason74123bd2007-02-02 11:05:29 -05002372 /* delete the leaf if it is mostly empty */
Chris Mason123abc82007-03-14 14:14:43 -04002373 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002374 /* push_leaf_left fixes the path.
2375 * make sure the path still points to our leaf
2376 * for possible call to del_ptr below
2377 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002378 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002379 extent_buffer_get(leaf);
2380
Chris Masone089f052007-03-16 16:20:31 -04002381 wret = push_leaf_left(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002382 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002383 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002384
2385 if (path->nodes[0] == leaf &&
2386 btrfs_header_nritems(leaf)) {
Chris Masone089f052007-03-16 16:20:31 -04002387 wret = push_leaf_right(trans, root, path, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002388 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002389 ret = wret;
2390 }
Chris Mason5f39d392007-10-15 16:14:19 -04002391
2392 if (btrfs_header_nritems(leaf) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002393 u64 bytenr = leaf->start;
2394 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002395
2396 clean_tree_block(trans, root, leaf);
2397 wait_on_tree_block_writeback(root, leaf);
2398
Chris Masone089f052007-03-16 16:20:31 -04002399 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002400 if (wret)
2401 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002402
2403 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002404 wret = btrfs_free_extent(trans, root, bytenr,
2405 blocksize, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002406 if (wret)
2407 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002408 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002409 btrfs_mark_buffer_dirty(leaf);
2410 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002411 }
Chris Masond5719762007-03-23 10:01:08 -04002412 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002413 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002414 }
2415 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002416 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002417}
2418
Chris Mason97571fd2007-02-24 13:39:08 -05002419/*
2420 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002421 * returns 0 if it found something or 1 if there are no greater leaves.
2422 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002423 */
Chris Mason234b63a2007-03-13 10:46:10 -04002424int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002425{
2426 int slot;
2427 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002428 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002429 struct extent_buffer *c;
2430 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002431
Chris Mason234b63a2007-03-13 10:46:10 -04002432 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002433 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002434 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002435
Chris Masond97e63b2007-02-20 16:40:44 -05002436 slot = path->slots[level] + 1;
2437 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002438 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002439 level++;
2440 continue;
2441 }
Chris Mason5f39d392007-10-15 16:14:19 -04002442
Chris Masondb945352007-10-15 16:15:53 -04002443 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002444 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002445 free_extent_buffer(next);
2446
Chris Mason6702ed42007-08-07 16:15:09 -04002447 if (path->reada)
2448 reada_for_search(root, path, level, slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002449
Chris Masondb945352007-10-15 16:15:53 -04002450 next = read_tree_block(root, bytenr,
2451 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002452 break;
2453 }
2454 path->slots[level] = slot;
2455 while(1) {
2456 level--;
2457 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002458 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002459 path->nodes[level] = next;
2460 path->slots[level] = 0;
2461 if (!level)
2462 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002463 if (path->reada)
Yan32020612007-08-27 15:17:54 -04002464 reada_for_search(root, path, level, 0);
Chris Masondb945352007-10-15 16:15:53 -04002465 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2466 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002467 }
2468 return 0;
2469}