blob: 35c57074a376b2523e57df92fcc9f1425a3c68a3 [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 Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050020#include "ctree.h"
21#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040022#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040023#include "print-tree.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050024
Chris Masone089f052007-03-16 16:20:31 -040025static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_path *path, int level);
27static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040028 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040029 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040030static int push_node_left(struct btrfs_trans_handle *trans,
31 struct btrfs_root *root, struct extent_buffer *dst,
32 struct extent_buffer *src);
33static int balance_node_right(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root,
35 struct extent_buffer *dst_buf,
36 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040037static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
38 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050039
Chris Masondf24a2b2007-04-04 09:36:31 -040040inline void btrfs_init_path(struct btrfs_path *p)
41{
42 memset(p, 0, sizeof(*p));
43}
44
Chris Mason2c90e5d2007-04-02 10:50:19 -040045struct btrfs_path *btrfs_alloc_path(void)
46{
Chris Masondf24a2b2007-04-04 09:36:31 -040047 struct btrfs_path *path;
48 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040049 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040050 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040051 path->reada = 1;
52 }
Chris Masondf24a2b2007-04-04 09:36:31 -040053 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040054}
55
56void btrfs_free_path(struct btrfs_path *p)
57{
Chris Masondf24a2b2007-04-04 09:36:31 -040058 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040059 kmem_cache_free(btrfs_path_cachep, p);
60}
61
Chris Mason234b63a2007-03-13 10:46:10 -040062void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050063{
64 int i;
Chris Mason234b63a2007-03-13 10:46:10 -040065 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -050066 if (!p->nodes[i])
67 break;
Chris Mason5f39d392007-10-15 16:14:19 -040068 free_extent_buffer(p->nodes[i]);
Chris Masoneb60cea2007-02-02 09:18:22 -050069 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050070 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050071}
72
Chris Masonbe20aa92007-12-17 20:14:01 -050073int btrfs_copy_root(struct btrfs_trans_handle *trans,
74 struct btrfs_root *root,
75 struct extent_buffer *buf,
76 struct extent_buffer **cow_ret, u64 new_root_objectid)
77{
78 struct extent_buffer *cow;
79 u32 nritems;
80 int ret = 0;
81 int level;
82 struct btrfs_key first_key;
Chris Mason4aec2b52007-12-18 16:25:45 -050083 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -050084
Chris Mason4aec2b52007-12-18 16:25:45 -050085 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
86 if (!new_root)
87 return -ENOMEM;
88
89 memcpy(new_root, root, sizeof(*new_root));
90 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -050091
92 WARN_ON(root->ref_cows && trans->transid !=
93 root->fs_info->running_transaction->transid);
94 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
95
96 level = btrfs_header_level(buf);
97 nritems = btrfs_header_nritems(buf);
98 if (nritems) {
99 if (level == 0)
100 btrfs_item_key_to_cpu(buf, &first_key, 0);
101 else
102 btrfs_node_key_to_cpu(buf, &first_key, 0);
103 } else {
104 first_key.objectid = 0;
105 }
Chris Mason4aec2b52007-12-18 16:25:45 -0500106 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
Chris Masonbe20aa92007-12-17 20:14:01 -0500107 new_root_objectid,
108 trans->transid, first_key.objectid,
109 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500110 if (IS_ERR(cow)) {
111 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500112 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500113 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500114
115 copy_extent_buffer(cow, buf, 0, 0, cow->len);
116 btrfs_set_header_bytenr(cow, cow->start);
117 btrfs_set_header_generation(cow, trans->transid);
118 btrfs_set_header_owner(cow, new_root_objectid);
119
120 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Chris Mason4aec2b52007-12-18 16:25:45 -0500121 ret = btrfs_inc_ref(trans, new_root, buf);
122 kfree(new_root);
123
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 if (ret)
125 return ret;
126
127 btrfs_mark_buffer_dirty(cow);
128 *cow_ret = cow;
129 return 0;
130}
131
132int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400133 struct btrfs_root *root,
134 struct extent_buffer *buf,
135 struct extent_buffer *parent, int parent_slot,
136 struct extent_buffer **cow_ret,
137 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400138{
Chris Mason7bb86312007-12-11 09:25:06 -0500139 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400140 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500141 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400142 int ret = 0;
143 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500144 int level;
145 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400146
Chris Mason7bb86312007-12-11 09:25:06 -0500147 if (root->ref_cows) {
148 root_gen = trans->transid;
149 } else {
150 root_gen = 0;
151 }
152
153 WARN_ON(root->ref_cows && trans->transid !=
154 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400155 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400156
Chris Mason7bb86312007-12-11 09:25:06 -0500157 level = btrfs_header_level(buf);
158 nritems = btrfs_header_nritems(buf);
159 if (nritems) {
160 if (level == 0)
161 btrfs_item_key_to_cpu(buf, &first_key, 0);
162 else
163 btrfs_node_key_to_cpu(buf, &first_key, 0);
164 } else {
165 first_key.objectid = 0;
166 }
167 cow = __btrfs_alloc_free_block(trans, root, buf->len,
168 root->root_key.objectid,
169 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400170 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400171 if (IS_ERR(cow))
172 return PTR_ERR(cow);
173
Chris Mason5f39d392007-10-15 16:14:19 -0400174 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400175 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400176 btrfs_set_header_generation(cow, trans->transid);
177 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400178
Chris Mason5f39d392007-10-15 16:14:19 -0400179 WARN_ON(btrfs_header_generation(buf) > trans->transid);
180 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400181 different_trans = 1;
182 ret = btrfs_inc_ref(trans, root, buf);
183 if (ret)
184 return ret;
185 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400186 clean_tree_block(trans, root, buf);
187 }
188
189 if (buf == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -0500190 root_gen = btrfs_header_generation(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400191 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400192 extent_buffer_get(cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400193 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400194 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500195 buf->len, root->root_key.objectid,
196 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400197 }
Chris Mason5f39d392007-10-15 16:14:19 -0400198 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400199 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500200 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400201 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400202 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500203 WARN_ON(trans->transid == 0);
204 btrfs_set_node_ptr_generation(parent, parent_slot,
205 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400206 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400207 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500208 btrfs_free_extent(trans, root, buf->start, buf->len,
209 btrfs_header_owner(parent), root_gen,
210 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400211 }
Chris Mason5f39d392007-10-15 16:14:19 -0400212 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400213 btrfs_mark_buffer_dirty(cow);
214 *cow_ret = cow;
215 return 0;
216}
217
Chris Mason5f39d392007-10-15 16:14:19 -0400218int btrfs_cow_block(struct btrfs_trans_handle *trans,
219 struct btrfs_root *root, struct extent_buffer *buf,
220 struct extent_buffer *parent, int parent_slot,
221 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500222{
Chris Mason6702ed42007-08-07 16:15:09 -0400223 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400224 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400225 if (trans->transaction != root->fs_info->running_transaction) {
226 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
227 root->fs_info->running_transaction->transid);
228 WARN_ON(1);
229 }
230 if (trans->transid != root->fs_info->generation) {
231 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
232 root->fs_info->generation);
233 WARN_ON(1);
234 }
Chris Mason5f39d392007-10-15 16:14:19 -0400235 if (btrfs_header_generation(buf) == trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -0500236 *cow_ret = buf;
237 return 0;
238 }
Chris Mason6702ed42007-08-07 16:15:09 -0400239
Chris Masondb945352007-10-15 16:15:53 -0400240 search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400241 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400242 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400243 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400244}
245
Chris Mason6b800532007-10-15 16:17:34 -0400246static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400247{
Chris Mason6b800532007-10-15 16:17:34 -0400248 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400249 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400250 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400251 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500252 return 0;
253}
254
Chris Mason081e9572007-11-06 10:26:24 -0500255/*
256 * compare two keys in a memcmp fashion
257 */
258static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
259{
260 struct btrfs_key k1;
261
262 btrfs_disk_key_to_cpu(&k1, disk);
263
264 if (k1.objectid > k2->objectid)
265 return 1;
266 if (k1.objectid < k2->objectid)
267 return -1;
268 if (k1.type > k2->type)
269 return 1;
270 if (k1.type < k2->type)
271 return -1;
272 if (k1.offset > k2->offset)
273 return 1;
274 if (k1.offset < k2->offset)
275 return -1;
276 return 0;
277}
278
279
Chris Mason6702ed42007-08-07 16:15:09 -0400280int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400281 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400282 int start_slot, int cache_only, u64 *last_ret,
283 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400284{
Chris Mason6b800532007-10-15 16:17:34 -0400285 struct extent_buffer *cur;
286 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400287 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400288 u64 search_start = *last_ret;
289 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400290 u64 other;
291 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400292 int end_slot;
293 int i;
294 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400295 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400296 int uptodate;
297 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500298 int progress_passed = 0;
299 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400300
Chris Mason5708b952007-10-25 15:43:18 -0400301 parent_level = btrfs_header_level(parent);
302 if (cache_only && parent_level != 1)
303 return 0;
304
Chris Mason6702ed42007-08-07 16:15:09 -0400305 if (trans->transaction != root->fs_info->running_transaction) {
306 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
307 root->fs_info->running_transaction->transid);
308 WARN_ON(1);
309 }
310 if (trans->transid != root->fs_info->generation) {
311 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
312 root->fs_info->generation);
313 WARN_ON(1);
314 }
Chris Mason86479a02007-09-10 19:58:16 -0400315
Chris Mason6b800532007-10-15 16:17:34 -0400316 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400317 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400318 end_slot = parent_nritems;
319
320 if (parent_nritems == 1)
321 return 0;
322
323 for (i = start_slot; i < end_slot; i++) {
324 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400325
Chris Mason5708b952007-10-25 15:43:18 -0400326 if (!parent->map_token) {
327 map_extent_buffer(parent,
328 btrfs_node_key_ptr_offset(i),
329 sizeof(struct btrfs_key_ptr),
330 &parent->map_token, &parent->kaddr,
331 &parent->map_start, &parent->map_len,
332 KM_USER1);
333 }
Chris Mason081e9572007-11-06 10:26:24 -0500334 btrfs_node_key(parent, &disk_key, i);
335 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
336 continue;
337
338 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400339 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400340 if (last_block == 0)
341 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400342
Chris Mason6702ed42007-08-07 16:15:09 -0400343 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400344 other = btrfs_node_blockptr(parent, i - 1);
345 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400346 }
Chris Mason5708b952007-10-25 15:43:18 -0400347 if (close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400348 other = btrfs_node_blockptr(parent, i + 1);
349 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400350 }
Chris Masone9d0b132007-08-10 14:06:19 -0400351 if (close) {
352 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400353 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400354 }
Chris Mason5708b952007-10-25 15:43:18 -0400355 if (parent->map_token) {
356 unmap_extent_buffer(parent, parent->map_token,
357 KM_USER1);
358 parent->map_token = NULL;
359 }
Chris Mason6702ed42007-08-07 16:15:09 -0400360
Chris Mason6b800532007-10-15 16:17:34 -0400361 cur = btrfs_find_tree_block(root, blocknr, blocksize);
362 if (cur)
363 uptodate = btrfs_buffer_uptodate(cur);
364 else
365 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400366 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400367 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400368 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400369 continue;
370 }
Chris Mason6b800532007-10-15 16:17:34 -0400371 if (!cur) {
372 cur = read_tree_block(root, blocknr,
373 blocksize);
374 } else if (!uptodate) {
375 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400376 }
Chris Mason6702ed42007-08-07 16:15:09 -0400377 }
Chris Masone9d0b132007-08-10 14:06:19 -0400378 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400379 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400380
Chris Mason6b800532007-10-15 16:17:34 -0400381 err = __btrfs_cow_block(trans, root, cur, parent, i,
382 &tmp, search_start,
383 min(16 * blocksize,
384 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400385 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400386 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400387 break;
Yan252c38f2007-08-29 09:11:44 -0400388 }
Chris Mason6b800532007-10-15 16:17:34 -0400389 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400390 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400391 *last_ret = search_start;
392 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400393 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400394 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400395 }
Chris Mason5708b952007-10-25 15:43:18 -0400396 if (parent->map_token) {
397 unmap_extent_buffer(parent, parent->map_token,
398 KM_USER1);
399 parent->map_token = NULL;
400 }
Chris Mason6702ed42007-08-07 16:15:09 -0400401 return err;
402}
403
Chris Mason74123bd2007-02-02 11:05:29 -0500404/*
405 * The leaf data grows from end-to-front in the node.
406 * this returns the address of the start of the last item,
407 * which is the stop of the leaf data stack
408 */
Chris Mason123abc82007-03-14 14:14:43 -0400409static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400410 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500411{
Chris Mason5f39d392007-10-15 16:14:19 -0400412 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500413 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400414 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400415 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500416}
417
Chris Mason123abc82007-03-14 14:14:43 -0400418static int check_node(struct btrfs_root *root, struct btrfs_path *path,
419 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500420{
Chris Mason5f39d392007-10-15 16:14:19 -0400421 struct extent_buffer *parent = NULL;
422 struct extent_buffer *node = path->nodes[level];
423 struct btrfs_disk_key parent_key;
424 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500425 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400426 int slot;
427 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400428 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500429
430 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400431 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400432
Chris Mason8d7be552007-05-10 11:24:42 -0400433 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400434 BUG_ON(nritems == 0);
435 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400436 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400437 btrfs_node_key(parent, &parent_key, parent_slot);
438 btrfs_node_key(node, &node_key, 0);
439 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400440 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400441 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400442 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500443 }
Chris Mason123abc82007-03-14 14:14:43 -0400444 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400445 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400446 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
447 btrfs_node_key(node, &node_key, slot);
448 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400449 }
450 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400451 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
452 btrfs_node_key(node, &node_key, slot);
453 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500454 }
455 return 0;
456}
457
Chris Mason123abc82007-03-14 14:14:43 -0400458static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
459 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500460{
Chris Mason5f39d392007-10-15 16:14:19 -0400461 struct extent_buffer *leaf = path->nodes[level];
462 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500463 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400464 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400465 struct btrfs_disk_key parent_key;
466 struct btrfs_disk_key leaf_key;
467 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400468
Chris Mason5f39d392007-10-15 16:14:19 -0400469 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500470
471 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400472 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400473
474 if (nritems == 0)
475 return 0;
476
477 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400478 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400479 btrfs_node_key(parent, &parent_key, parent_slot);
480 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400481
Chris Mason5f39d392007-10-15 16:14:19 -0400482 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400483 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400484 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400485 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500486 }
Chris Mason5f39d392007-10-15 16:14:19 -0400487#if 0
488 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
489 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
490 btrfs_item_key(leaf, &leaf_key, i);
491 if (comp_keys(&leaf_key, &cpukey) >= 0) {
492 btrfs_print_leaf(root, leaf);
493 printk("slot %d offset bad key\n", i);
494 BUG_ON(1);
495 }
496 if (btrfs_item_offset_nr(leaf, i) !=
497 btrfs_item_end_nr(leaf, i + 1)) {
498 btrfs_print_leaf(root, leaf);
499 printk("slot %d offset bad\n", i);
500 BUG_ON(1);
501 }
502 if (i == 0) {
503 if (btrfs_item_offset_nr(leaf, i) +
504 btrfs_item_size_nr(leaf, i) !=
505 BTRFS_LEAF_DATA_SIZE(root)) {
506 btrfs_print_leaf(root, leaf);
507 printk("slot %d first offset bad\n", i);
508 BUG_ON(1);
509 }
510 }
511 }
512 if (nritems > 0) {
513 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
514 btrfs_print_leaf(root, leaf);
515 printk("slot %d bad size \n", nritems - 1);
516 BUG_ON(1);
517 }
518 }
519#endif
520 if (slot != 0 && slot < nritems - 1) {
521 btrfs_item_key(leaf, &leaf_key, slot);
522 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
523 if (comp_keys(&leaf_key, &cpukey) <= 0) {
524 btrfs_print_leaf(root, leaf);
525 printk("slot %d offset bad key\n", slot);
526 BUG_ON(1);
527 }
528 if (btrfs_item_offset_nr(leaf, slot - 1) !=
529 btrfs_item_end_nr(leaf, slot)) {
530 btrfs_print_leaf(root, leaf);
531 printk("slot %d offset bad\n", slot);
532 BUG_ON(1);
533 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500534 }
Chris Mason8d7be552007-05-10 11:24:42 -0400535 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400536 btrfs_item_key(leaf, &leaf_key, slot);
537 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
538 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
539 if (btrfs_item_offset_nr(leaf, slot) !=
540 btrfs_item_end_nr(leaf, slot + 1)) {
541 btrfs_print_leaf(root, leaf);
542 printk("slot %d offset bad\n", slot);
543 BUG_ON(1);
544 }
Chris Mason8d7be552007-05-10 11:24:42 -0400545 }
Chris Mason5f39d392007-10-15 16:14:19 -0400546 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
547 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500548 return 0;
549}
550
Chris Mason98ed5172008-01-03 10:01:48 -0500551static int noinline check_block(struct btrfs_root *root,
552 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500553{
Chris Mason810191f2007-10-15 16:18:55 -0400554 return 0;
Chris Masondb945352007-10-15 16:15:53 -0400555#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400556 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400557
Chris Mason479965d2007-10-15 16:14:27 -0400558 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
559 (unsigned long)btrfs_header_fsid(buf),
560 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400561 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400562 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400563 }
Chris Masondb945352007-10-15 16:15:53 -0400564#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500565 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400566 return check_leaf(root, path, level);
567 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500568}
569
Chris Mason74123bd2007-02-02 11:05:29 -0500570/*
Chris Mason5f39d392007-10-15 16:14:19 -0400571 * search for key in the extent_buffer. The items start at offset p,
572 * and they are item_size apart. There are 'max' items in p.
573 *
Chris Mason74123bd2007-02-02 11:05:29 -0500574 * the slot in the array is returned via slot, and it points to
575 * the place where you would insert key if it is not found in
576 * the array.
577 *
578 * slot may point to max if the key is bigger than all of the keys
579 */
Chris Mason5f39d392007-10-15 16:14:19 -0400580static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
581 int item_size, struct btrfs_key *key,
582 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500583{
584 int low = 0;
585 int high = max;
586 int mid;
587 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400588 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400589 struct btrfs_disk_key unaligned;
590 unsigned long offset;
591 char *map_token = NULL;
592 char *kaddr = NULL;
593 unsigned long map_start = 0;
594 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400595 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500596
597 while(low < high) {
598 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400599 offset = p + mid * item_size;
600
601 if (!map_token || offset < map_start ||
602 (offset + sizeof(struct btrfs_disk_key)) >
603 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400604 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400605 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400606 map_token = NULL;
607 }
608 err = map_extent_buffer(eb, offset,
609 sizeof(struct btrfs_disk_key),
610 &map_token, &kaddr,
611 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400612
Chris Mason479965d2007-10-15 16:14:27 -0400613 if (!err) {
614 tmp = (struct btrfs_disk_key *)(kaddr + offset -
615 map_start);
616 } else {
617 read_extent_buffer(eb, &unaligned,
618 offset, sizeof(unaligned));
619 tmp = &unaligned;
620 }
621
Chris Mason5f39d392007-10-15 16:14:19 -0400622 } else {
623 tmp = (struct btrfs_disk_key *)(kaddr + offset -
624 map_start);
625 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500626 ret = comp_keys(tmp, key);
627
628 if (ret < 0)
629 low = mid + 1;
630 else if (ret > 0)
631 high = mid;
632 else {
633 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400634 if (map_token)
635 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500636 return 0;
637 }
638 }
639 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400640 if (map_token)
641 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500642 return 1;
643}
644
Chris Mason97571fd2007-02-24 13:39:08 -0500645/*
646 * simple bin_search frontend that does the right thing for
647 * leaves vs nodes
648 */
Chris Mason5f39d392007-10-15 16:14:19 -0400649static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
650 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500651{
Chris Mason5f39d392007-10-15 16:14:19 -0400652 if (level == 0) {
653 return generic_bin_search(eb,
654 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400655 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400656 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400657 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500658 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400659 return generic_bin_search(eb,
660 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400661 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400662 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400663 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500664 }
665 return -1;
666}
667
Chris Mason5f39d392007-10-15 16:14:19 -0400668static struct extent_buffer *read_node_slot(struct btrfs_root *root,
669 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500670{
Chris Masonbb803952007-03-01 12:04:21 -0500671 if (slot < 0)
672 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400673 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500674 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400675 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
676 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500677}
678
Chris Mason98ed5172008-01-03 10:01:48 -0500679static int balance_level(struct btrfs_trans_handle *trans,
680 struct btrfs_root *root,
681 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500682{
Chris Mason5f39d392007-10-15 16:14:19 -0400683 struct extent_buffer *right = NULL;
684 struct extent_buffer *mid;
685 struct extent_buffer *left = NULL;
686 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500687 int ret = 0;
688 int wret;
689 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500690 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400691 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500692 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500693
694 if (level == 0)
695 return 0;
696
Chris Mason5f39d392007-10-15 16:14:19 -0400697 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500698 WARN_ON(btrfs_header_generation(mid) != trans->transid);
699
Chris Mason1d4f8a02007-03-13 09:28:32 -0400700 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500701
Chris Mason234b63a2007-03-13 10:46:10 -0400702 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400703 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500704 pslot = path->slots[level + 1];
705
Chris Mason40689472007-03-17 14:29:23 -0400706 /*
707 * deal with the case where there is only one pointer in the root
708 * by promoting the node below to a root
709 */
Chris Mason5f39d392007-10-15 16:14:19 -0400710 if (!parent) {
711 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500712
Chris Mason5f39d392007-10-15 16:14:19 -0400713 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500714 return 0;
715
716 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400717 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500718 BUG_ON(!child);
719 root->node = child;
720 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400721 clean_tree_block(trans, root, mid);
722 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500723 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400724 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500725 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
726 root->root_key.objectid,
727 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500728 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400729 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400730 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500731 }
Chris Mason5f39d392007-10-15 16:14:19 -0400732 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400733 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500734 return 0;
735
Chris Mason5f39d392007-10-15 16:14:19 -0400736 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400737 err_on_enospc = 1;
738
Chris Mason5f39d392007-10-15 16:14:19 -0400739 left = read_node_slot(root, parent, pslot - 1);
740 if (left) {
741 wret = btrfs_cow_block(trans, root, left,
742 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400743 if (wret) {
744 ret = wret;
745 goto enospc;
746 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400747 }
Chris Mason5f39d392007-10-15 16:14:19 -0400748 right = read_node_slot(root, parent, pslot + 1);
749 if (right) {
750 wret = btrfs_cow_block(trans, root, right,
751 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400752 if (wret) {
753 ret = wret;
754 goto enospc;
755 }
756 }
757
758 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400759 if (left) {
760 orig_slot += btrfs_header_nritems(left);
761 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500762 if (wret < 0)
763 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400764 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400765 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500766 }
Chris Mason79f95c82007-03-01 15:16:26 -0500767
768 /*
769 * then try to empty the right most buffer into the middle
770 */
Chris Mason5f39d392007-10-15 16:14:19 -0400771 if (right) {
772 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400773 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500774 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400775 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400776 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500777 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400778 u32 blocksize = right->len;
779
Chris Mason5f39d392007-10-15 16:14:19 -0400780 clean_tree_block(trans, root, right);
781 wait_on_tree_block_writeback(root, right);
782 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500783 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400784 wret = del_ptr(trans, root, path, level + 1, pslot +
785 1);
Chris Masonbb803952007-03-01 12:04:21 -0500786 if (wret)
787 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400788 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500789 blocksize,
790 btrfs_header_owner(parent),
791 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500792 if (wret)
793 ret = wret;
794 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400795 struct btrfs_disk_key right_key;
796 btrfs_node_key(right, &right_key, 0);
797 btrfs_set_node_key(parent, &right_key, pslot + 1);
798 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500799 }
800 }
Chris Mason5f39d392007-10-15 16:14:19 -0400801 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500802 /*
803 * we're not allowed to leave a node with one item in the
804 * tree during a delete. A deletion from lower in the tree
805 * could try to delete the only pointer in this node.
806 * So, pull some keys from the left.
807 * There has to be a left pointer at this point because
808 * otherwise we would have pulled some pointers from the
809 * right
810 */
Chris Mason5f39d392007-10-15 16:14:19 -0400811 BUG_ON(!left);
812 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400813 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500814 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400815 goto enospc;
816 }
Chris Mason79f95c82007-03-01 15:16:26 -0500817 BUG_ON(wret == 1);
818 }
Chris Mason5f39d392007-10-15 16:14:19 -0400819 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500820 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500821 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400822 u64 bytenr = mid->start;
823 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400824 clean_tree_block(trans, root, mid);
825 wait_on_tree_block_writeback(root, mid);
826 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500827 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400828 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500829 if (wret)
830 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500831 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
832 btrfs_header_owner(parent),
833 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500834 if (wret)
835 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500836 } else {
837 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400838 struct btrfs_disk_key mid_key;
839 btrfs_node_key(mid, &mid_key, 0);
840 btrfs_set_node_key(parent, &mid_key, pslot);
841 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500842 }
Chris Masonbb803952007-03-01 12:04:21 -0500843
Chris Mason79f95c82007-03-01 15:16:26 -0500844 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400845 if (left) {
846 if (btrfs_header_nritems(left) > orig_slot) {
847 extent_buffer_get(left);
848 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500849 path->slots[level + 1] -= 1;
850 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400851 if (mid)
852 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500853 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400854 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500855 path->slots[level] = orig_slot;
856 }
857 }
Chris Mason79f95c82007-03-01 15:16:26 -0500858 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400859 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400860 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400861 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500862 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400863enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400864 if (right)
865 free_extent_buffer(right);
866 if (left)
867 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500868 return ret;
869}
870
Chris Masone66f7092007-04-20 13:16:02 -0400871/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500872static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
873 struct btrfs_root *root,
874 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400875{
Chris Mason5f39d392007-10-15 16:14:19 -0400876 struct extent_buffer *right = NULL;
877 struct extent_buffer *mid;
878 struct extent_buffer *left = NULL;
879 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400880 int ret = 0;
881 int wret;
882 int pslot;
883 int orig_slot = path->slots[level];
884 u64 orig_ptr;
885
886 if (level == 0)
887 return 1;
888
Chris Mason5f39d392007-10-15 16:14:19 -0400889 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500890 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -0400891 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
892
893 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400894 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400895 pslot = path->slots[level + 1];
896
Chris Mason5f39d392007-10-15 16:14:19 -0400897 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400898 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400899
Chris Mason5f39d392007-10-15 16:14:19 -0400900 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400901
902 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400903 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400904 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400905 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400906 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
907 wret = 1;
908 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400909 ret = btrfs_cow_block(trans, root, left, parent,
910 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400911 if (ret)
912 wret = 1;
913 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400914 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400915 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400916 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400917 }
Chris Masone66f7092007-04-20 13:16:02 -0400918 if (wret < 0)
919 ret = wret;
920 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400921 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400922 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400923 btrfs_node_key(mid, &disk_key, 0);
924 btrfs_set_node_key(parent, &disk_key, pslot);
925 btrfs_mark_buffer_dirty(parent);
926 if (btrfs_header_nritems(left) > orig_slot) {
927 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400928 path->slots[level + 1] -= 1;
929 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400930 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400931 } else {
932 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400933 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400934 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400935 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400936 }
Chris Masone66f7092007-04-20 13:16:02 -0400937 return 0;
938 }
Chris Mason5f39d392007-10-15 16:14:19 -0400939 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400940 }
Chris Mason5f39d392007-10-15 16:14:19 -0400941 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400942
943 /*
944 * then try to empty the right most buffer into the middle
945 */
Chris Mason5f39d392007-10-15 16:14:19 -0400946 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400947 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400948 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400949 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
950 wret = 1;
951 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400952 ret = btrfs_cow_block(trans, root, right,
953 parent, pslot + 1,
954 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400955 if (ret)
956 wret = 1;
957 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400958 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400959 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400960 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400961 }
Chris Masone66f7092007-04-20 13:16:02 -0400962 if (wret < 0)
963 ret = wret;
964 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400965 struct btrfs_disk_key disk_key;
966
967 btrfs_node_key(right, &disk_key, 0);
968 btrfs_set_node_key(parent, &disk_key, pslot + 1);
969 btrfs_mark_buffer_dirty(parent);
970
971 if (btrfs_header_nritems(mid) <= orig_slot) {
972 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -0400973 path->slots[level + 1] += 1;
974 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -0400975 btrfs_header_nritems(mid);
976 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400977 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400978 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400979 }
Chris Masone66f7092007-04-20 13:16:02 -0400980 return 0;
981 }
Chris Mason5f39d392007-10-15 16:14:19 -0400982 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400983 }
Chris Masone66f7092007-04-20 13:16:02 -0400984 return 1;
985}
986
Chris Mason74123bd2007-02-02 11:05:29 -0500987/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400988 * readahead one full node of leaves
989 */
990static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -0500991 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -0400992{
Chris Mason5f39d392007-10-15 16:14:19 -0400993 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -0500994 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -0400995 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -0400996 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -0400997 u64 lowest_read;
998 u64 highest_read;
999 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001000 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001001 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001002 u32 nr;
1003 u32 blocksize;
1004 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001005
Chris Masona6b6e752007-10-15 16:22:39 -04001006 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001007 return;
1008
Chris Mason6702ed42007-08-07 16:15:09 -04001009 if (!path->nodes[level])
1010 return;
1011
Chris Mason5f39d392007-10-15 16:14:19 -04001012 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -04001013 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001014 blocksize = btrfs_level_size(root, level - 1);
1015 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001016 if (eb) {
1017 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001018 return;
1019 }
1020
Chris Mason6b800532007-10-15 16:17:34 -04001021 highest_read = search;
1022 lowest_read = search;
1023
Chris Mason5f39d392007-10-15 16:14:19 -04001024 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001025 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001026 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001027 if (direction < 0) {
1028 if (nr == 0)
1029 break;
1030 nr--;
1031 } else if (direction > 0) {
1032 nr++;
1033 if (nr >= nritems)
1034 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001035 }
Chris Mason01f46652007-12-21 16:24:26 -05001036 if (path->reada < 0 && objectid) {
1037 btrfs_node_key(node, &disk_key, nr);
1038 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1039 break;
1040 }
Chris Mason6b800532007-10-15 16:17:34 -04001041 search = btrfs_node_blockptr(node, nr);
1042 if ((search >= lowest_read && search <= highest_read) ||
1043 (search < lowest_read && lowest_read - search <= 32768) ||
1044 (search > highest_read && search - highest_read <= 32768)) {
1045 readahead_tree_block(root, search, blocksize);
1046 nread += blocksize;
1047 }
1048 nscan++;
1049 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1050 break;
1051 if(nread > (1024 * 1024) || nscan > 128)
1052 break;
1053
1054 if (search < lowest_read)
1055 lowest_read = search;
1056 if (search > highest_read)
1057 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001058 }
1059}
1060/*
Chris Mason74123bd2007-02-02 11:05:29 -05001061 * look for key in the tree. path is filled in with nodes along the way
1062 * if key is found, we return zero and you can find the item in the leaf
1063 * level of the path (level 0)
1064 *
1065 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001066 * be inserted, and 1 is returned. If there are other errors during the
1067 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001068 *
1069 * if ins_len > 0, nodes and leaves will be split as we walk down the
1070 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1071 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001072 */
Chris Masone089f052007-03-16 16:20:31 -04001073int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1074 *root, struct btrfs_key *key, struct btrfs_path *p, int
1075 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001076{
Chris Mason5f39d392007-10-15 16:14:19 -04001077 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -04001078 u64 bytenr;
Chris Mason74493f72007-12-11 09:25:06 -05001079 u64 ptr_gen;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001080 int slot;
1081 int ret;
1082 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001083 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -04001084 u8 lowest_level = 0;
1085
Chris Mason6702ed42007-08-07 16:15:09 -04001086 lowest_level = p->lowest_level;
1087 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001088 WARN_ON(p->nodes[0] != NULL);
1089 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -05001090again:
1091 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001092 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001093 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001094 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001095 if (cow) {
1096 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001097 wret = btrfs_cow_block(trans, root, b,
1098 p->nodes[level + 1],
1099 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001100 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001101 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001102 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001103 return wret;
1104 }
Chris Mason02217ed2007-03-02 16:08:05 -05001105 }
1106 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001107 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001108 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001109 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001110 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001111 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001112 if (ret)
1113 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001114 ret = bin_search(b, key, level, &slot);
1115 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001116 if (ret && slot > 0)
1117 slot -= 1;
1118 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001119 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001120 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001121 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001122 BUG_ON(sret > 0);
1123 if (sret)
1124 return sret;
1125 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001126 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001127 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001128 int sret = balance_level(trans, root, p,
1129 level);
Chris Masonbb803952007-03-01 12:04:21 -05001130 if (sret)
1131 return sret;
1132 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001133 if (!b) {
1134 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001135 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001136 }
Chris Masonbb803952007-03-01 12:04:21 -05001137 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001138 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001139 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001140 /* this is only true while dropping a snapshot */
1141 if (level == lowest_level)
1142 break;
Chris Masondb945352007-10-15 16:15:53 -04001143 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason74493f72007-12-11 09:25:06 -05001144 ptr_gen = btrfs_node_ptr_generation(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001145 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001146 reada_for_search(root, p, level, slot,
1147 key->objectid);
Chris Masondb945352007-10-15 16:15:53 -04001148 b = read_tree_block(root, bytenr,
1149 btrfs_level_size(root, level - 1));
Chris Mason74493f72007-12-11 09:25:06 -05001150 if (ptr_gen != btrfs_header_generation(b)) {
1151 printk("block %llu bad gen wanted %llu "
1152 "found %llu\n",
1153 (unsigned long long)b->start,
1154 (unsigned long long)ptr_gen,
1155 (unsigned long long)btrfs_header_generation(b));
1156 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001157 } else {
1158 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001159 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001160 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001161 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001162 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001163 BUG_ON(sret > 0);
1164 if (sret)
1165 return sret;
1166 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001167 return ret;
1168 }
1169 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001170 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001171}
1172
Chris Mason74123bd2007-02-02 11:05:29 -05001173/*
1174 * adjust the pointers going up the tree, starting at level
1175 * making sure the right key of each node is points to 'key'.
1176 * This is used after shifting pointers to the left, so it stops
1177 * fixing up pointers when a given leaf/node is not in slot 0 of the
1178 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001179 *
1180 * If this fails to write a tree block, it returns -1, but continues
1181 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001182 */
Chris Mason5f39d392007-10-15 16:14:19 -04001183static int fixup_low_keys(struct btrfs_trans_handle *trans,
1184 struct btrfs_root *root, struct btrfs_path *path,
1185 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001186{
1187 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001188 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001189 struct extent_buffer *t;
1190
Chris Mason234b63a2007-03-13 10:46:10 -04001191 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001192 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001193 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001194 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001195 t = path->nodes[i];
1196 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001197 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001198 if (tslot != 0)
1199 break;
1200 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001201 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001202}
1203
Chris Mason74123bd2007-02-02 11:05:29 -05001204/*
1205 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001206 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001207 *
1208 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1209 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001210 */
Chris Mason98ed5172008-01-03 10:01:48 -05001211static int push_node_left(struct btrfs_trans_handle *trans,
1212 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason5f39d392007-10-15 16:14:19 -04001213 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001214{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001215 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001216 int src_nritems;
1217 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001218 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001219
Chris Mason5f39d392007-10-15 16:14:19 -04001220 src_nritems = btrfs_header_nritems(src);
1221 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001222 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001223 WARN_ON(btrfs_header_generation(src) != trans->transid);
1224 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001225
Chris Masoneb60cea2007-02-02 09:18:22 -05001226 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001227 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001228 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001229
1230 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001231 push_items = src_nritems;
1232
Chris Mason5f39d392007-10-15 16:14:19 -04001233 copy_extent_buffer(dst, src,
1234 btrfs_node_key_ptr_offset(dst_nritems),
1235 btrfs_node_key_ptr_offset(0),
1236 push_items * sizeof(struct btrfs_key_ptr));
1237
Chris Masonbb803952007-03-01 12:04:21 -05001238 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001239 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1240 btrfs_node_key_ptr_offset(push_items),
1241 (src_nritems - push_items) *
1242 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001243 }
Chris Mason5f39d392007-10-15 16:14:19 -04001244 btrfs_set_header_nritems(src, src_nritems - push_items);
1245 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1246 btrfs_mark_buffer_dirty(src);
1247 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001248 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001249}
1250
Chris Mason97571fd2007-02-24 13:39:08 -05001251/*
Chris Mason79f95c82007-03-01 15:16:26 -05001252 * try to push data from one node into the next node right in the
1253 * tree.
1254 *
1255 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1256 * error, and > 0 if there was no room in the right hand block.
1257 *
1258 * this will only push up to 1/2 the contents of the left node over
1259 */
Chris Mason5f39d392007-10-15 16:14:19 -04001260static int balance_node_right(struct btrfs_trans_handle *trans,
1261 struct btrfs_root *root,
1262 struct extent_buffer *dst,
1263 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001264{
Chris Mason79f95c82007-03-01 15:16:26 -05001265 int push_items = 0;
1266 int max_push;
1267 int src_nritems;
1268 int dst_nritems;
1269 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001270
Chris Mason7bb86312007-12-11 09:25:06 -05001271 WARN_ON(btrfs_header_generation(src) != trans->transid);
1272 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1273
Chris Mason5f39d392007-10-15 16:14:19 -04001274 src_nritems = btrfs_header_nritems(src);
1275 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001276 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001277 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001278 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001279
1280 max_push = src_nritems / 2 + 1;
1281 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001282 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001283 return 1;
Yan252c38f2007-08-29 09:11:44 -04001284
Chris Mason79f95c82007-03-01 15:16:26 -05001285 if (max_push < push_items)
1286 push_items = max_push;
1287
Chris Mason5f39d392007-10-15 16:14:19 -04001288 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1289 btrfs_node_key_ptr_offset(0),
1290 (dst_nritems) *
1291 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001292
Chris Mason5f39d392007-10-15 16:14:19 -04001293 copy_extent_buffer(dst, src,
1294 btrfs_node_key_ptr_offset(0),
1295 btrfs_node_key_ptr_offset(src_nritems - push_items),
1296 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001297
Chris Mason5f39d392007-10-15 16:14:19 -04001298 btrfs_set_header_nritems(src, src_nritems - push_items);
1299 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001300
Chris Mason5f39d392007-10-15 16:14:19 -04001301 btrfs_mark_buffer_dirty(src);
1302 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001303 return ret;
1304}
1305
1306/*
Chris Mason97571fd2007-02-24 13:39:08 -05001307 * helper function to insert a new root level in the tree.
1308 * A new node is allocated, and a single item is inserted to
1309 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001310 *
1311 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001312 */
Chris Mason98ed5172008-01-03 10:01:48 -05001313static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001314 struct btrfs_root *root,
1315 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001316{
Chris Mason7bb86312007-12-11 09:25:06 -05001317 u64 root_gen;
1318 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001319 struct extent_buffer *lower;
1320 struct extent_buffer *c;
1321 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001322
1323 BUG_ON(path->nodes[level]);
1324 BUG_ON(path->nodes[level-1] != root->node);
1325
Chris Mason7bb86312007-12-11 09:25:06 -05001326 if (root->ref_cows)
1327 root_gen = trans->transid;
1328 else
1329 root_gen = 0;
1330
1331 lower = path->nodes[level-1];
1332 if (level == 1)
1333 btrfs_item_key(lower, &lower_key, 0);
1334 else
1335 btrfs_node_key(lower, &lower_key, 0);
1336
1337 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1338 root->root_key.objectid,
1339 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001340 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001341 if (IS_ERR(c))
1342 return PTR_ERR(c);
1343 memset_extent_buffer(c, 0, 0, root->nodesize);
1344 btrfs_set_header_nritems(c, 1);
1345 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001346 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001347 btrfs_set_header_generation(c, trans->transid);
1348 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001349
Chris Mason5f39d392007-10-15 16:14:19 -04001350 write_extent_buffer(c, root->fs_info->fsid,
1351 (unsigned long)btrfs_header_fsid(c),
1352 BTRFS_FSID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001353 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001354 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001355 lower_gen = btrfs_header_generation(lower);
1356 WARN_ON(lower_gen == 0);
1357
1358 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001359
1360 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001361
Chris Mason5c680ed2007-02-22 11:39:13 -05001362 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001363 free_extent_buffer(root->node);
1364 root->node = c;
1365 extent_buffer_get(c);
1366 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001367 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001368
1369 if (root->ref_cows && lower_gen != trans->transid) {
1370 struct btrfs_path *back_path = btrfs_alloc_path();
1371 int ret;
1372 ret = btrfs_insert_extent_backref(trans,
1373 root->fs_info->extent_root,
1374 path, lower->start,
1375 root->root_key.objectid,
1376 trans->transid, 0, 0);
1377 BUG_ON(ret);
1378 btrfs_free_path(back_path);
1379 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001380 return 0;
1381}
1382
Chris Mason74123bd2007-02-02 11:05:29 -05001383/*
1384 * worker function to insert a single pointer in a node.
1385 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001386 *
Chris Mason74123bd2007-02-02 11:05:29 -05001387 * slot and level indicate where you want the key to go, and
1388 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001389 *
1390 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001391 */
Chris Masone089f052007-03-16 16:20:31 -04001392static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1393 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001394 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001395{
Chris Mason5f39d392007-10-15 16:14:19 -04001396 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001397 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001398
1399 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001400 lower = path->nodes[level];
1401 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001402 if (slot > nritems)
1403 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001404 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001405 BUG();
1406 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001407 memmove_extent_buffer(lower,
1408 btrfs_node_key_ptr_offset(slot + 1),
1409 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001410 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001411 }
Chris Mason5f39d392007-10-15 16:14:19 -04001412 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001413 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001414 WARN_ON(trans->transid == 0);
1415 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001416 btrfs_set_header_nritems(lower, nritems + 1);
1417 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001418 return 0;
1419}
1420
Chris Mason97571fd2007-02-24 13:39:08 -05001421/*
1422 * split the node at the specified level in path in two.
1423 * The path is corrected to point to the appropriate node after the split
1424 *
1425 * Before splitting this tries to make some room in the node by pushing
1426 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001427 *
1428 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001429 */
Chris Masone089f052007-03-16 16:20:31 -04001430static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1431 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001432{
Chris Mason7bb86312007-12-11 09:25:06 -05001433 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001434 struct extent_buffer *c;
1435 struct extent_buffer *split;
1436 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001437 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001438 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001439 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001440 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001441
Chris Mason5f39d392007-10-15 16:14:19 -04001442 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001443 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001444 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001445 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001446 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001447 if (ret)
1448 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001449 } else {
1450 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001451 c = path->nodes[level];
1452 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001453 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1454 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001455 if (ret < 0)
1456 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001457 }
Chris Masone66f7092007-04-20 13:16:02 -04001458
Chris Mason5f39d392007-10-15 16:14:19 -04001459 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001460 if (root->ref_cows)
1461 root_gen = trans->transid;
1462 else
1463 root_gen = 0;
1464
1465 btrfs_node_key(c, &disk_key, 0);
1466 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1467 root->root_key.objectid,
1468 root_gen,
1469 btrfs_disk_key_objectid(&disk_key),
1470 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001471 if (IS_ERR(split))
1472 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001473
Chris Mason5f39d392007-10-15 16:14:19 -04001474 btrfs_set_header_flags(split, btrfs_header_flags(c));
1475 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001476 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001477 btrfs_set_header_generation(split, trans->transid);
1478 btrfs_set_header_owner(split, root->root_key.objectid);
1479 write_extent_buffer(split, root->fs_info->fsid,
1480 (unsigned long)btrfs_header_fsid(split),
1481 BTRFS_FSID_SIZE);
1482
Chris Mason7518a232007-03-12 12:01:18 -04001483 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001484
1485 copy_extent_buffer(split, c,
1486 btrfs_node_key_ptr_offset(0),
1487 btrfs_node_key_ptr_offset(mid),
1488 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1489 btrfs_set_header_nritems(split, c_nritems - mid);
1490 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001491 ret = 0;
1492
Chris Mason5f39d392007-10-15 16:14:19 -04001493 btrfs_mark_buffer_dirty(c);
1494 btrfs_mark_buffer_dirty(split);
1495
1496 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001497 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001498 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001499 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001500 if (wret)
1501 ret = wret;
1502
Chris Mason5de08d72007-02-24 06:24:44 -05001503 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001504 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001505 free_extent_buffer(c);
1506 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001507 path->slots[level + 1] += 1;
1508 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001509 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001510 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001511 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001512}
1513
Chris Mason74123bd2007-02-02 11:05:29 -05001514/*
1515 * how many bytes are required to store the items in a leaf. start
1516 * and nr indicate which items in the leaf to check. This totals up the
1517 * space used both by the item structs and the item data
1518 */
Chris Mason5f39d392007-10-15 16:14:19 -04001519static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001520{
1521 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001522 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001523 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001524
1525 if (!nr)
1526 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001527 data_len = btrfs_item_end_nr(l, start);
1528 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001529 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001530 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001531 return data_len;
1532}
1533
Chris Mason74123bd2007-02-02 11:05:29 -05001534/*
Chris Masond4dbff92007-04-04 14:08:15 -04001535 * The space between the end of the leaf items and
1536 * the start of the leaf data. IOW, how much room
1537 * the leaf has left for both items and data
1538 */
Chris Mason5f39d392007-10-15 16:14:19 -04001539int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001540{
Chris Mason5f39d392007-10-15 16:14:19 -04001541 int nritems = btrfs_header_nritems(leaf);
1542 int ret;
1543 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1544 if (ret < 0) {
1545 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001546 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001547 leaf_space_used(leaf, 0, nritems), nritems);
1548 }
1549 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001550}
1551
1552/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001553 * push some data in the path leaf to the right, trying to free up at
1554 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001555 *
1556 * returns 1 if the push failed because the other node didn't have enough
1557 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001558 */
Chris Masone089f052007-03-16 16:20:31 -04001559static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001560 *root, struct btrfs_path *path, int data_size,
1561 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001562{
Chris Mason5f39d392007-10-15 16:14:19 -04001563 struct extent_buffer *left = path->nodes[0];
1564 struct extent_buffer *right;
1565 struct extent_buffer *upper;
1566 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001567 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001568 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001569 int free_space;
1570 int push_space = 0;
1571 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001572 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001573 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001574 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001575 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001576 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001577 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001578 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001579
1580 slot = path->slots[1];
1581 if (!path->nodes[1]) {
1582 return 1;
1583 }
1584 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001585 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001586 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001587
Chris Masondb945352007-10-15 16:15:53 -04001588 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1589 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001590 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001591 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001592 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001593 return 1;
1594 }
1595
Chris Mason5f39d392007-10-15 16:14:19 -04001596 /* cow and double check */
1597 ret = btrfs_cow_block(trans, root, right, upper,
1598 slot + 1, &right);
1599 if (ret) {
1600 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001601 return 1;
1602 }
Chris Mason5f39d392007-10-15 16:14:19 -04001603 free_space = btrfs_leaf_free_space(root, right);
1604 if (free_space < data_size + sizeof(struct btrfs_item)) {
1605 free_extent_buffer(right);
1606 return 1;
1607 }
1608
1609 left_nritems = btrfs_header_nritems(left);
1610 if (left_nritems == 0) {
1611 free_extent_buffer(right);
1612 return 1;
1613 }
1614
Chris Mason34a38212007-11-07 13:31:03 -05001615 if (empty)
1616 nr = 0;
1617 else
1618 nr = 1;
1619
1620 i = left_nritems - 1;
1621 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001622 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001623
Chris Mason00ec4c52007-02-24 12:47:20 -05001624 if (path->slots[0] == i)
1625 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001626
1627 if (!left->map_token) {
1628 map_extent_buffer(left, (unsigned long)item,
1629 sizeof(struct btrfs_item),
1630 &left->map_token, &left->kaddr,
1631 &left->map_start, &left->map_len,
1632 KM_USER1);
1633 }
1634
1635 this_item_size = btrfs_item_size(left, item);
1636 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001637 break;
1638 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001639 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001640 if (i == 0)
1641 break;
1642 i--;
Chris Masondb945352007-10-15 16:15:53 -04001643 }
1644 if (left->map_token) {
1645 unmap_extent_buffer(left, left->map_token, KM_USER1);
1646 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001647 }
Chris Mason5f39d392007-10-15 16:14:19 -04001648
Chris Mason00ec4c52007-02-24 12:47:20 -05001649 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001650 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001651 return 1;
1652 }
Chris Mason5f39d392007-10-15 16:14:19 -04001653
Chris Mason34a38212007-11-07 13:31:03 -05001654 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001655 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001656
Chris Mason00ec4c52007-02-24 12:47:20 -05001657 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001658 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001659
Chris Mason5f39d392007-10-15 16:14:19 -04001660 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001661 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001662
Chris Mason00ec4c52007-02-24 12:47:20 -05001663 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001664 data_end = leaf_data_end(root, right);
1665 memmove_extent_buffer(right,
1666 btrfs_leaf_data(right) + data_end - push_space,
1667 btrfs_leaf_data(right) + data_end,
1668 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1669
Chris Mason00ec4c52007-02-24 12:47:20 -05001670 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001671 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001672 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1673 btrfs_leaf_data(left) + leaf_data_end(root, left),
1674 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001675
1676 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1677 btrfs_item_nr_offset(0),
1678 right_nritems * sizeof(struct btrfs_item));
1679
Chris Mason00ec4c52007-02-24 12:47:20 -05001680 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001681 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1682 btrfs_item_nr_offset(left_nritems - push_items),
1683 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001684
1685 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001686 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001687 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001688 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001689 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001690 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001691 if (!right->map_token) {
1692 map_extent_buffer(right, (unsigned long)item,
1693 sizeof(struct btrfs_item),
1694 &right->map_token, &right->kaddr,
1695 &right->map_start, &right->map_len,
1696 KM_USER1);
1697 }
1698 push_space -= btrfs_item_size(right, item);
1699 btrfs_set_item_offset(right, item, push_space);
1700 }
1701
1702 if (right->map_token) {
1703 unmap_extent_buffer(right, right->map_token, KM_USER1);
1704 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001705 }
Chris Mason7518a232007-03-12 12:01:18 -04001706 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001707 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001708
Chris Mason34a38212007-11-07 13:31:03 -05001709 if (left_nritems)
1710 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001711 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001712
Chris Mason5f39d392007-10-15 16:14:19 -04001713 btrfs_item_key(right, &disk_key, 0);
1714 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001715 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001716
Chris Mason00ec4c52007-02-24 12:47:20 -05001717 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001718 if (path->slots[0] >= left_nritems) {
1719 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001720 free_extent_buffer(path->nodes[0]);
1721 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001722 path->slots[1] += 1;
1723 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001724 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001725 }
1726 return 0;
1727}
1728/*
Chris Mason74123bd2007-02-02 11:05:29 -05001729 * push some data in the path leaf to the left, trying to free up at
1730 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1731 */
Chris Masone089f052007-03-16 16:20:31 -04001732static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001733 *root, struct btrfs_path *path, int data_size,
1734 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001735{
Chris Mason5f39d392007-10-15 16:14:19 -04001736 struct btrfs_disk_key disk_key;
1737 struct extent_buffer *right = path->nodes[0];
1738 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001739 int slot;
1740 int i;
1741 int free_space;
1742 int push_space = 0;
1743 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001744 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001745 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001746 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001747 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001748 int ret = 0;
1749 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001750 u32 this_item_size;
1751 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001752
1753 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001754 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001755 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001756 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001757 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001758
Chris Mason3685f792007-10-19 09:23:27 -04001759 right_nritems = btrfs_header_nritems(right);
1760 if (right_nritems == 0) {
1761 return 1;
1762 }
1763
Chris Mason5f39d392007-10-15 16:14:19 -04001764 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001765 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001766 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001767 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001768 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769 return 1;
1770 }
Chris Mason02217ed2007-03-02 16:08:05 -05001771
1772 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001773 ret = btrfs_cow_block(trans, root, left,
1774 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001775 if (ret) {
1776 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001777 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001778 return 1;
1779 }
Chris Mason3685f792007-10-19 09:23:27 -04001780
Chris Mason123abc82007-03-14 14:14:43 -04001781 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001782 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001783 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001784 return 1;
1785 }
1786
Chris Mason34a38212007-11-07 13:31:03 -05001787 if (empty)
1788 nr = right_nritems;
1789 else
1790 nr = right_nritems - 1;
1791
1792 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001793 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001794 if (!right->map_token) {
1795 map_extent_buffer(right, (unsigned long)item,
1796 sizeof(struct btrfs_item),
1797 &right->map_token, &right->kaddr,
1798 &right->map_start, &right->map_len,
1799 KM_USER1);
1800 }
1801
Chris Masonbe0e5c02007-01-26 15:51:26 -05001802 if (path->slots[0] == i)
1803 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001804
1805 this_item_size = btrfs_item_size(right, item);
1806 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001807 break;
Chris Masondb945352007-10-15 16:15:53 -04001808
Chris Masonbe0e5c02007-01-26 15:51:26 -05001809 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001810 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001811 }
Chris Masondb945352007-10-15 16:15:53 -04001812
1813 if (right->map_token) {
1814 unmap_extent_buffer(right, right->map_token, KM_USER1);
1815 right->map_token = NULL;
1816 }
1817
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001819 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001820 return 1;
1821 }
Chris Mason34a38212007-11-07 13:31:03 -05001822 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001823 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001824
Chris Masonbe0e5c02007-01-26 15:51:26 -05001825 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001826 copy_extent_buffer(left, right,
1827 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1828 btrfs_item_nr_offset(0),
1829 push_items * sizeof(struct btrfs_item));
1830
Chris Mason123abc82007-03-14 14:14:43 -04001831 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001832 btrfs_item_offset_nr(right, push_items -1);
1833
1834 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001835 leaf_data_end(root, left) - push_space,
1836 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001837 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001838 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001839 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001840 BUG_ON(old_left_nritems < 0);
1841
Chris Masondb945352007-10-15 16:15:53 -04001842 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001843 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001844 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001845
Chris Mason5f39d392007-10-15 16:14:19 -04001846 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001847 if (!left->map_token) {
1848 map_extent_buffer(left, (unsigned long)item,
1849 sizeof(struct btrfs_item),
1850 &left->map_token, &left->kaddr,
1851 &left->map_start, &left->map_len,
1852 KM_USER1);
1853 }
1854
Chris Mason5f39d392007-10-15 16:14:19 -04001855 ioff = btrfs_item_offset(left, item);
1856 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001857 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001858 }
Chris Mason5f39d392007-10-15 16:14:19 -04001859 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001860 if (left->map_token) {
1861 unmap_extent_buffer(left, left->map_token, KM_USER1);
1862 left->map_token = NULL;
1863 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001864
1865 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001866 if (push_items > right_nritems) {
1867 printk("push items %d nr %u\n", push_items, right_nritems);
1868 WARN_ON(1);
1869 }
Chris Mason5f39d392007-10-15 16:14:19 -04001870
Chris Mason34a38212007-11-07 13:31:03 -05001871 if (push_items < right_nritems) {
1872 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1873 leaf_data_end(root, right);
1874 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1875 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1876 btrfs_leaf_data(right) +
1877 leaf_data_end(root, right), push_space);
1878
1879 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001880 btrfs_item_nr_offset(push_items),
1881 (btrfs_header_nritems(right) - push_items) *
1882 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001883 }
Yaneef1c492007-11-26 10:58:13 -05001884 right_nritems -= push_items;
1885 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001886 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001887 for (i = 0; i < right_nritems; i++) {
1888 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001889
1890 if (!right->map_token) {
1891 map_extent_buffer(right, (unsigned long)item,
1892 sizeof(struct btrfs_item),
1893 &right->map_token, &right->kaddr,
1894 &right->map_start, &right->map_len,
1895 KM_USER1);
1896 }
1897
1898 push_space = push_space - btrfs_item_size(right, item);
1899 btrfs_set_item_offset(right, item, push_space);
1900 }
1901 if (right->map_token) {
1902 unmap_extent_buffer(right, right->map_token, KM_USER1);
1903 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001904 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001905
Chris Mason5f39d392007-10-15 16:14:19 -04001906 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001907 if (right_nritems)
1908 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001909
Chris Mason5f39d392007-10-15 16:14:19 -04001910 btrfs_item_key(right, &disk_key, 0);
1911 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001912 if (wret)
1913 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001914
1915 /* then fixup the leaf pointer in the path */
1916 if (path->slots[0] < push_items) {
1917 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001918 free_extent_buffer(path->nodes[0]);
1919 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001920 path->slots[1] -= 1;
1921 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001922 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001923 path->slots[0] -= push_items;
1924 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001925 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001926 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001927}
1928
Chris Mason74123bd2007-02-02 11:05:29 -05001929/*
1930 * split the path's leaf in two, making sure there is at least data_size
1931 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001932 *
1933 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001934 */
Chris Masone089f052007-03-16 16:20:31 -04001935static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001936 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001937 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938{
Chris Mason7bb86312007-12-11 09:25:06 -05001939 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001940 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001941 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001942 int mid;
1943 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001944 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001945 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001946 int data_copy_size;
1947 int rt_data_off;
1948 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001949 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001950 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001951 int double_split;
1952 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001953 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001954
Chris Masoncc0c5532007-10-25 15:42:57 -04001955 if (extend)
1956 space_needed = data_size;
1957
Chris Mason7bb86312007-12-11 09:25:06 -05001958 if (root->ref_cows)
1959 root_gen = trans->transid;
1960 else
1961 root_gen = 0;
1962
Chris Mason40689472007-03-17 14:29:23 -04001963 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04001964 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05001965 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001966 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04001967 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04001968 }
1969 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05001970 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001971 if (wret < 0)
1972 return wret;
1973 }
1974 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05001975
Chris Mason3685f792007-10-19 09:23:27 -04001976 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04001977 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04001978 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04001979 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001980
Chris Mason5c680ed2007-02-22 11:39:13 -05001981 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001982 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001983 if (ret)
1984 return ret;
1985 }
Chris Masoncc0c5532007-10-25 15:42:57 -04001986again:
1987 double_split = 0;
1988 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001989 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001990 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001991 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001992
Chris Mason7bb86312007-12-11 09:25:06 -05001993 btrfs_item_key(l, &disk_key, 0);
1994
1995 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
1996 root->root_key.objectid,
1997 root_gen, disk_key.objectid, 0,
1998 l->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001999 if (IS_ERR(right))
2000 return PTR_ERR(right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002001
Chris Mason5f39d392007-10-15 16:14:19 -04002002 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002003 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002004 btrfs_set_header_generation(right, trans->transid);
2005 btrfs_set_header_owner(right, root->root_key.objectid);
2006 btrfs_set_header_level(right, 0);
2007 write_extent_buffer(right, root->fs_info->fsid,
2008 (unsigned long)btrfs_header_fsid(right),
2009 BTRFS_FSID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002010 if (mid <= slot) {
2011 if (nritems == 1 ||
2012 leaf_space_used(l, mid, nritems - mid) + space_needed >
2013 BTRFS_LEAF_DATA_SIZE(root)) {
2014 if (slot >= nritems) {
2015 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002016 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002017 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002018 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002019 path->slots[1] + 1, 1);
2020 if (wret)
2021 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002022 free_extent_buffer(path->nodes[0]);
2023 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002024 path->slots[0] = 0;
2025 path->slots[1] += 1;
2026 return ret;
2027 }
2028 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002029 if (mid != nritems &&
2030 leaf_space_used(l, mid, nritems - mid) +
2031 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2032 double_split = 1;
2033 }
Chris Masond4dbff92007-04-04 14:08:15 -04002034 }
2035 } else {
2036 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2037 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002038 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002039 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002040 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002041 wret = insert_ptr(trans, root, path,
2042 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002043 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002044 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002045 if (wret)
2046 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002047 free_extent_buffer(path->nodes[0]);
2048 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002049 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002050 if (path->slots[1] == 0) {
2051 wret = fixup_low_keys(trans, root,
2052 path, &disk_key, 1);
2053 if (wret)
2054 ret = wret;
2055 }
Chris Masond4dbff92007-04-04 14:08:15 -04002056 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002057 } else if (extend && slot == 0) {
2058 mid = 1;
2059 } else {
2060 mid = slot;
2061 if (mid != nritems &&
2062 leaf_space_used(l, mid, nritems - mid) +
2063 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2064 double_split = 1;
2065 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002066 }
Chris Masond4dbff92007-04-04 14:08:15 -04002067 }
2068 }
Chris Mason5f39d392007-10-15 16:14:19 -04002069 nritems = nritems - mid;
2070 btrfs_set_header_nritems(right, nritems);
2071 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2072
2073 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2074 btrfs_item_nr_offset(mid),
2075 nritems * sizeof(struct btrfs_item));
2076
2077 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002078 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2079 data_copy_size, btrfs_leaf_data(l) +
2080 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002081
Chris Mason5f39d392007-10-15 16:14:19 -04002082 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2083 btrfs_item_end_nr(l, mid);
2084
2085 for (i = 0; i < nritems; i++) {
2086 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002087 u32 ioff;
2088
2089 if (!right->map_token) {
2090 map_extent_buffer(right, (unsigned long)item,
2091 sizeof(struct btrfs_item),
2092 &right->map_token, &right->kaddr,
2093 &right->map_start, &right->map_len,
2094 KM_USER1);
2095 }
2096
2097 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002098 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002099 }
Chris Mason74123bd2007-02-02 11:05:29 -05002100
Chris Masondb945352007-10-15 16:15:53 -04002101 if (right->map_token) {
2102 unmap_extent_buffer(right, right->map_token, KM_USER1);
2103 right->map_token = NULL;
2104 }
2105
Chris Mason5f39d392007-10-15 16:14:19 -04002106 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002107 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002108 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002109 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2110 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002111 if (wret)
2112 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002113
2114 btrfs_mark_buffer_dirty(right);
2115 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002116 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002117
Chris Masonbe0e5c02007-01-26 15:51:26 -05002118 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002119 free_extent_buffer(path->nodes[0]);
2120 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002121 path->slots[0] -= mid;
2122 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002123 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002124 free_extent_buffer(right);
2125
Chris Masoneb60cea2007-02-02 09:18:22 -05002126 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002127
Chris Masoncc0c5532007-10-25 15:42:57 -04002128 if (double_split) {
2129 BUG_ON(num_doubles != 0);
2130 num_doubles++;
2131 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002132 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002133 return ret;
2134}
2135
Chris Masonb18c6682007-04-17 13:26:50 -04002136int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2137 struct btrfs_root *root,
2138 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002139 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002140{
2141 int ret = 0;
2142 int slot;
2143 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002144 struct extent_buffer *leaf;
2145 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002146 u32 nritems;
2147 unsigned int data_end;
2148 unsigned int old_data_start;
2149 unsigned int old_size;
2150 unsigned int size_diff;
2151 int i;
2152
2153 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002154 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002155 slot = path->slots[0];
2156
2157 old_size = btrfs_item_size_nr(leaf, slot);
2158 if (old_size == new_size)
2159 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002160
Chris Mason5f39d392007-10-15 16:14:19 -04002161 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002162 data_end = leaf_data_end(root, leaf);
2163
Chris Mason5f39d392007-10-15 16:14:19 -04002164 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002165
Chris Masonb18c6682007-04-17 13:26:50 -04002166 size_diff = old_size - new_size;
2167
2168 BUG_ON(slot < 0);
2169 BUG_ON(slot >= nritems);
2170
2171 /*
2172 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2173 */
2174 /* first correct the data pointers */
2175 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002176 u32 ioff;
2177 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002178
2179 if (!leaf->map_token) {
2180 map_extent_buffer(leaf, (unsigned long)item,
2181 sizeof(struct btrfs_item),
2182 &leaf->map_token, &leaf->kaddr,
2183 &leaf->map_start, &leaf->map_len,
2184 KM_USER1);
2185 }
2186
Chris Mason5f39d392007-10-15 16:14:19 -04002187 ioff = btrfs_item_offset(leaf, item);
2188 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002189 }
Chris Masondb945352007-10-15 16:15:53 -04002190
2191 if (leaf->map_token) {
2192 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2193 leaf->map_token = NULL;
2194 }
2195
Chris Masonb18c6682007-04-17 13:26:50 -04002196 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002197 if (from_end) {
2198 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2199 data_end + size_diff, btrfs_leaf_data(leaf) +
2200 data_end, old_data_start + new_size - data_end);
2201 } else {
2202 struct btrfs_disk_key disk_key;
2203 u64 offset;
2204
2205 btrfs_item_key(leaf, &disk_key, slot);
2206
2207 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2208 unsigned long ptr;
2209 struct btrfs_file_extent_item *fi;
2210
2211 fi = btrfs_item_ptr(leaf, slot,
2212 struct btrfs_file_extent_item);
2213 fi = (struct btrfs_file_extent_item *)(
2214 (unsigned long)fi - size_diff);
2215
2216 if (btrfs_file_extent_type(leaf, fi) ==
2217 BTRFS_FILE_EXTENT_INLINE) {
2218 ptr = btrfs_item_ptr_offset(leaf, slot);
2219 memmove_extent_buffer(leaf, ptr,
2220 (unsigned long)fi,
2221 offsetof(struct btrfs_file_extent_item,
2222 disk_bytenr));
2223 }
2224 }
2225
2226 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2227 data_end + size_diff, btrfs_leaf_data(leaf) +
2228 data_end, old_data_start - data_end);
2229
2230 offset = btrfs_disk_key_offset(&disk_key);
2231 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2232 btrfs_set_item_key(leaf, &disk_key, slot);
2233 if (slot == 0)
2234 fixup_low_keys(trans, root, path, &disk_key, 1);
2235 }
Chris Mason5f39d392007-10-15 16:14:19 -04002236
2237 item = btrfs_item_nr(leaf, slot);
2238 btrfs_set_item_size(leaf, item, new_size);
2239 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002240
2241 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002242 if (btrfs_leaf_free_space(root, leaf) < 0) {
2243 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002244 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002245 }
Chris Masonb18c6682007-04-17 13:26:50 -04002246 return ret;
2247}
2248
Chris Mason5f39d392007-10-15 16:14:19 -04002249int btrfs_extend_item(struct btrfs_trans_handle *trans,
2250 struct btrfs_root *root, struct btrfs_path *path,
2251 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002252{
2253 int ret = 0;
2254 int slot;
2255 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002256 struct extent_buffer *leaf;
2257 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002258 u32 nritems;
2259 unsigned int data_end;
2260 unsigned int old_data;
2261 unsigned int old_size;
2262 int i;
2263
2264 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002265 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002266
Chris Mason5f39d392007-10-15 16:14:19 -04002267 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002268 data_end = leaf_data_end(root, leaf);
2269
Chris Mason5f39d392007-10-15 16:14:19 -04002270 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2271 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002272 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002273 }
Chris Mason6567e832007-04-16 09:22:45 -04002274 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002275 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002276
2277 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002278 if (slot >= nritems) {
2279 btrfs_print_leaf(root, leaf);
2280 printk("slot %d too large, nritems %d\n", slot, nritems);
2281 BUG_ON(1);
2282 }
Chris Mason6567e832007-04-16 09:22:45 -04002283
2284 /*
2285 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2286 */
2287 /* first correct the data pointers */
2288 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002289 u32 ioff;
2290 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002291
2292 if (!leaf->map_token) {
2293 map_extent_buffer(leaf, (unsigned long)item,
2294 sizeof(struct btrfs_item),
2295 &leaf->map_token, &leaf->kaddr,
2296 &leaf->map_start, &leaf->map_len,
2297 KM_USER1);
2298 }
Chris Mason5f39d392007-10-15 16:14:19 -04002299 ioff = btrfs_item_offset(leaf, item);
2300 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002301 }
Chris Mason5f39d392007-10-15 16:14:19 -04002302
Chris Masondb945352007-10-15 16:15:53 -04002303 if (leaf->map_token) {
2304 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2305 leaf->map_token = NULL;
2306 }
2307
Chris Mason6567e832007-04-16 09:22:45 -04002308 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002309 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002310 data_end - data_size, btrfs_leaf_data(leaf) +
2311 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002312
Chris Mason6567e832007-04-16 09:22:45 -04002313 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002314 old_size = btrfs_item_size_nr(leaf, slot);
2315 item = btrfs_item_nr(leaf, slot);
2316 btrfs_set_item_size(leaf, item, old_size + data_size);
2317 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002318
2319 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002320 if (btrfs_leaf_free_space(root, leaf) < 0) {
2321 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002322 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002323 }
Chris Mason6567e832007-04-16 09:22:45 -04002324 return ret;
2325}
2326
Chris Mason74123bd2007-02-02 11:05:29 -05002327/*
2328 * Given a key and some data, insert an item into the tree.
2329 * This does all the path init required, making room in the tree if needed.
2330 */
Chris Mason5f39d392007-10-15 16:14:19 -04002331int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2332 struct btrfs_root *root,
2333 struct btrfs_path *path,
2334 struct btrfs_key *cpu_key, u32 data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002335{
Chris Mason5f39d392007-10-15 16:14:19 -04002336 struct extent_buffer *leaf;
2337 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002338 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002339 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002340 int slot_orig;
Chris Mason7518a232007-03-12 12:01:18 -04002341 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002342 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002343 struct btrfs_disk_key disk_key;
2344
2345 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002346
Chris Mason74123bd2007-02-02 11:05:29 -05002347 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002348 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002349 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002350
Chris Masone089f052007-03-16 16:20:31 -04002351 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002352 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002353 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002354 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002355 if (ret < 0)
2356 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002357
Chris Mason62e27492007-03-15 12:56:47 -04002358 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002359 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002360
Chris Mason5f39d392007-10-15 16:14:19 -04002361 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002362 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002363
Chris Mason123abc82007-03-14 14:14:43 -04002364 if (btrfs_leaf_free_space(root, leaf) <
Chris Masond4dbff92007-04-04 14:08:15 -04002365 sizeof(struct btrfs_item) + data_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002366 btrfs_print_leaf(root, leaf);
2367 printk("not enough freespace need %u have %d\n",
2368 data_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002369 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002370 }
Chris Mason5f39d392007-10-15 16:14:19 -04002371
Chris Mason62e27492007-03-15 12:56:47 -04002372 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002373 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002374
Chris Masonbe0e5c02007-01-26 15:51:26 -05002375 if (slot != nritems) {
2376 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002377 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002378
Chris Mason5f39d392007-10-15 16:14:19 -04002379 if (old_data < data_end) {
2380 btrfs_print_leaf(root, leaf);
2381 printk("slot %d old_data %d data_end %d\n",
2382 slot, old_data, data_end);
2383 BUG_ON(1);
2384 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002385 /*
2386 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2387 */
2388 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002389 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002390 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002391 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002392
Chris Mason5f39d392007-10-15 16:14:19 -04002393 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002394 if (!leaf->map_token) {
2395 map_extent_buffer(leaf, (unsigned long)item,
2396 sizeof(struct btrfs_item),
2397 &leaf->map_token, &leaf->kaddr,
2398 &leaf->map_start, &leaf->map_len,
2399 KM_USER1);
2400 }
2401
Chris Mason5f39d392007-10-15 16:14:19 -04002402 ioff = btrfs_item_offset(leaf, item);
2403 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason0783fcf2007-03-12 20:12:07 -04002404 }
Chris Masondb945352007-10-15 16:15:53 -04002405 if (leaf->map_token) {
2406 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2407 leaf->map_token = NULL;
2408 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002409
2410 /* shift the items */
Chris Mason5f39d392007-10-15 16:14:19 -04002411 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2412 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002413 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002414
2415 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002416 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002417 data_end - data_size, btrfs_leaf_data(leaf) +
2418 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002419 data_end = old_data;
2420 }
Chris Mason5f39d392007-10-15 16:14:19 -04002421
Chris Mason62e27492007-03-15 12:56:47 -04002422 /* setup the item for the new data */
Chris Mason5f39d392007-10-15 16:14:19 -04002423 btrfs_set_item_key(leaf, &disk_key, slot);
2424 item = btrfs_item_nr(leaf, slot);
2425 btrfs_set_item_offset(leaf, item, data_end - data_size);
2426 btrfs_set_item_size(leaf, item, data_size);
2427 btrfs_set_header_nritems(leaf, nritems + 1);
2428 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002429
2430 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05002431 if (slot == 0)
Chris Masone089f052007-03-16 16:20:31 -04002432 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002433
Chris Mason5f39d392007-10-15 16:14:19 -04002434 if (btrfs_leaf_free_space(root, leaf) < 0) {
2435 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002436 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002437 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002438out:
Chris Mason62e27492007-03-15 12:56:47 -04002439 return ret;
2440}
2441
2442/*
2443 * Given a key and some data, insert an item into the tree.
2444 * This does all the path init required, making room in the tree if needed.
2445 */
Chris Masone089f052007-03-16 16:20:31 -04002446int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2447 *root, struct btrfs_key *cpu_key, void *data, u32
2448 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002449{
2450 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002451 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002452 struct extent_buffer *leaf;
2453 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002454
Chris Mason2c90e5d2007-04-02 10:50:19 -04002455 path = btrfs_alloc_path();
2456 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002457 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002458 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002459 leaf = path->nodes[0];
2460 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2461 write_extent_buffer(leaf, data, ptr, data_size);
2462 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002463 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002464 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002465 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002466}
2467
Chris Mason74123bd2007-02-02 11:05:29 -05002468/*
Chris Mason5de08d72007-02-24 06:24:44 -05002469 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002470 *
2471 * If the delete empties a node, the node is removed from the tree,
2472 * continuing all the way the root if required. The root is converted into
2473 * a leaf if all the nodes are emptied.
2474 */
Chris Masone089f052007-03-16 16:20:31 -04002475static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2476 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002477{
Chris Mason5f39d392007-10-15 16:14:19 -04002478 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002479 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002480 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002481 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002482
Chris Mason5f39d392007-10-15 16:14:19 -04002483 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002484 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002485 memmove_extent_buffer(parent,
2486 btrfs_node_key_ptr_offset(slot),
2487 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002488 sizeof(struct btrfs_key_ptr) *
2489 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002490 }
Chris Mason7518a232007-03-12 12:01:18 -04002491 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002492 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002493 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002494 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002495 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002496 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002497 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002498 struct btrfs_disk_key disk_key;
2499
2500 btrfs_node_key(parent, &disk_key, 0);
2501 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002502 if (wret)
2503 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002504 }
Chris Masond6025572007-03-30 14:27:56 -04002505 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002506 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002507}
2508
Chris Mason74123bd2007-02-02 11:05:29 -05002509/*
2510 * delete the item at the leaf level in path. If that empties
2511 * the leaf, remove it from the tree
2512 */
Chris Masone089f052007-03-16 16:20:31 -04002513int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2514 struct btrfs_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002516 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002517 struct extent_buffer *leaf;
2518 struct btrfs_item *item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002519 int doff;
2520 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002521 int ret = 0;
2522 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002523 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002524
Chris Mason5f39d392007-10-15 16:14:19 -04002525 leaf = path->nodes[0];
Chris Mason4920c9a2007-01-26 16:38:42 -05002526 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002527 doff = btrfs_item_offset_nr(leaf, slot);
2528 dsize = btrfs_item_size_nr(leaf, slot);
2529 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002530
Chris Mason7518a232007-03-12 12:01:18 -04002531 if (slot != nritems - 1) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002532 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002533 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002534
2535 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002536 data_end + dsize,
2537 btrfs_leaf_data(leaf) + data_end,
2538 doff - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002539
Chris Mason0783fcf2007-03-12 20:12:07 -04002540 for (i = slot + 1; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002541 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002542
Chris Mason5f39d392007-10-15 16:14:19 -04002543 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002544 if (!leaf->map_token) {
2545 map_extent_buffer(leaf, (unsigned long)item,
2546 sizeof(struct btrfs_item),
2547 &leaf->map_token, &leaf->kaddr,
2548 &leaf->map_start, &leaf->map_len,
2549 KM_USER1);
2550 }
Chris Mason5f39d392007-10-15 16:14:19 -04002551 ioff = btrfs_item_offset(leaf, item);
2552 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002553 }
Chris Masondb945352007-10-15 16:15:53 -04002554
2555 if (leaf->map_token) {
2556 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2557 leaf->map_token = NULL;
2558 }
2559
Chris Mason5f39d392007-10-15 16:14:19 -04002560 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2561 btrfs_item_nr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002562 sizeof(struct btrfs_item) *
2563 (nritems - slot - 1));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002564 }
Chris Mason5f39d392007-10-15 16:14:19 -04002565 btrfs_set_header_nritems(leaf, nritems - 1);
Chris Mason7518a232007-03-12 12:01:18 -04002566 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002567
Chris Mason74123bd2007-02-02 11:05:29 -05002568 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002569 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002570 if (leaf == root->node) {
2571 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002572 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002573 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002574 clean_tree_block(trans, root, leaf);
2575 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002576 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002577 if (wret)
2578 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002579 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002580 leaf->start, leaf->len,
2581 btrfs_header_owner(path->nodes[1]),
2582 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002583 if (wret)
2584 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002585 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002586 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002587 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002588 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002589 struct btrfs_disk_key disk_key;
2590
2591 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002592 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002593 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002594 if (wret)
2595 ret = wret;
2596 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002597
Chris Mason74123bd2007-02-02 11:05:29 -05002598 /* delete the leaf if it is mostly empty */
Chris Mason7936ca32007-10-19 09:22:41 -04002599 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 /* push_leaf_left fixes the path.
2601 * make sure the path still points to our leaf
2602 * for possible call to del_ptr below
2603 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002604 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002605 extent_buffer_get(leaf);
2606
Chris Mason34a38212007-11-07 13:31:03 -05002607 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002608 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002609 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002610
2611 if (path->nodes[0] == leaf &&
2612 btrfs_header_nritems(leaf)) {
Chris Mason34a38212007-11-07 13:31:03 -05002613 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002614 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002615 ret = wret;
2616 }
Chris Mason5f39d392007-10-15 16:14:19 -04002617
2618 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002619 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002620 u64 bytenr = leaf->start;
2621 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002622
Chris Mason7bb86312007-12-11 09:25:06 -05002623 root_gen = btrfs_header_generation(
2624 path->nodes[1]);
2625
Chris Mason5f39d392007-10-15 16:14:19 -04002626 clean_tree_block(trans, root, leaf);
2627 wait_on_tree_block_writeback(root, leaf);
2628
Chris Masone089f052007-03-16 16:20:31 -04002629 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002630 if (wret)
2631 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002632
2633 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002634 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002635 blocksize,
2636 btrfs_header_owner(path->nodes[1]),
2637 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002638 if (wret)
2639 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002640 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002641 btrfs_mark_buffer_dirty(leaf);
2642 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002643 }
Chris Masond5719762007-03-23 10:01:08 -04002644 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002645 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002646 }
2647 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002648 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002649}
2650
Chris Mason97571fd2007-02-24 13:39:08 -05002651/*
Chris Mason7bb86312007-12-11 09:25:06 -05002652 * walk up the tree as far as required to find the previous leaf.
2653 * returns 0 if it found something or 1 if there are no lesser leaves.
2654 * returns < 0 on io errors.
2655 */
2656int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2657{
Chris Mason8f662a72008-01-02 10:01:11 -05002658 u64 bytenr;
Chris Mason7bb86312007-12-11 09:25:06 -05002659 int slot;
2660 int level = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002661 struct extent_buffer *c;
2662 struct extent_buffer *next = NULL;
2663
2664 while(level < BTRFS_MAX_LEVEL) {
2665 if (!path->nodes[level])
2666 return 1;
2667
2668 slot = path->slots[level];
2669 c = path->nodes[level];
2670 if (slot == 0) {
2671 level++;
2672 if (level == BTRFS_MAX_LEVEL)
2673 return 1;
2674 continue;
2675 }
2676 slot--;
2677
2678 bytenr = btrfs_node_blockptr(c, slot);
2679 if (next)
2680 free_extent_buffer(next);
2681
Chris Mason7bb86312007-12-11 09:25:06 -05002682 next = read_tree_block(root, bytenr,
2683 btrfs_level_size(root, level - 1));
2684 break;
2685 }
2686 path->slots[level] = slot;
2687 while(1) {
2688 level--;
2689 c = path->nodes[level];
2690 free_extent_buffer(c);
Chris Mason8f662a72008-01-02 10:01:11 -05002691 slot = btrfs_header_nritems(next);
2692 if (slot != 0)
2693 slot--;
Chris Mason7bb86312007-12-11 09:25:06 -05002694 path->nodes[level] = next;
Chris Mason8f662a72008-01-02 10:01:11 -05002695 path->slots[level] = slot;
Chris Mason7bb86312007-12-11 09:25:06 -05002696 if (!level)
2697 break;
Chris Mason8f662a72008-01-02 10:01:11 -05002698 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
Chris Mason7bb86312007-12-11 09:25:06 -05002699 btrfs_level_size(root, level - 1));
2700 }
2701 return 0;
2702}
2703
2704/*
Chris Mason97571fd2007-02-24 13:39:08 -05002705 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002706 * returns 0 if it found something or 1 if there are no greater leaves.
2707 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002708 */
Chris Mason234b63a2007-03-13 10:46:10 -04002709int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002710{
2711 int slot;
2712 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002713 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002714 struct extent_buffer *c;
2715 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002716
Chris Mason234b63a2007-03-13 10:46:10 -04002717 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002718 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002719 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002720
Chris Masond97e63b2007-02-20 16:40:44 -05002721 slot = path->slots[level] + 1;
2722 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002723 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002724 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002725 if (level == BTRFS_MAX_LEVEL)
2726 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002727 continue;
2728 }
Chris Mason5f39d392007-10-15 16:14:19 -04002729
Chris Masondb945352007-10-15 16:15:53 -04002730 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002731 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002732 free_extent_buffer(next);
2733
Chris Mason6702ed42007-08-07 16:15:09 -04002734 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002735 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002736
Chris Masondb945352007-10-15 16:15:53 -04002737 next = read_tree_block(root, bytenr,
2738 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002739 break;
2740 }
2741 path->slots[level] = slot;
2742 while(1) {
2743 level--;
2744 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002745 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002746 path->nodes[level] = next;
2747 path->slots[level] = 0;
2748 if (!level)
2749 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002750 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002751 reada_for_search(root, path, level, 0, 0);
Chris Masondb945352007-10-15 16:15:53 -04002752 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2753 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002754 }
2755 return 0;
2756}