blob: ef8809c1e69c3896655a64ee43d6713f7fc1f3ab [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 Masondc17ff82008-01-08 15:46:30 -0500224 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400225 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500226
Chris Masonccd467d2007-06-28 15:57:36 -0400227 if (trans->transaction != root->fs_info->running_transaction) {
228 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
229 root->fs_info->running_transaction->transid);
230 WARN_ON(1);
231 }
232 if (trans->transid != root->fs_info->generation) {
233 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
234 root->fs_info->generation);
235 WARN_ON(1);
236 }
Chris Masondc17ff82008-01-08 15:46:30 -0500237
238 header_trans = btrfs_header_generation(buf);
239 if (header_trans == trans->transid) {
Chris Mason02217ed2007-03-02 16:08:05 -0500240 *cow_ret = buf;
241 return 0;
242 }
Chris Mason6702ed42007-08-07 16:15:09 -0400243
Chris Masondb945352007-10-15 16:15:53 -0400244 search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400245 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400246 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400247 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400248}
249
Chris Mason6b800532007-10-15 16:17:34 -0400250static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400251{
Chris Mason6b800532007-10-15 16:17:34 -0400252 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400253 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400254 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400255 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500256 return 0;
257}
258
Chris Mason081e9572007-11-06 10:26:24 -0500259/*
260 * compare two keys in a memcmp fashion
261 */
262static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
263{
264 struct btrfs_key k1;
265
266 btrfs_disk_key_to_cpu(&k1, disk);
267
268 if (k1.objectid > k2->objectid)
269 return 1;
270 if (k1.objectid < k2->objectid)
271 return -1;
272 if (k1.type > k2->type)
273 return 1;
274 if (k1.type < k2->type)
275 return -1;
276 if (k1.offset > k2->offset)
277 return 1;
278 if (k1.offset < k2->offset)
279 return -1;
280 return 0;
281}
282
283
Chris Mason6702ed42007-08-07 16:15:09 -0400284int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400285 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400286 int start_slot, int cache_only, u64 *last_ret,
287 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400288{
Chris Mason6b800532007-10-15 16:17:34 -0400289 struct extent_buffer *cur;
290 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400291 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400292 u64 search_start = *last_ret;
293 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400294 u64 other;
295 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400296 int end_slot;
297 int i;
298 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400299 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400300 int uptodate;
301 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500302 int progress_passed = 0;
303 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400304
Chris Mason5708b952007-10-25 15:43:18 -0400305 parent_level = btrfs_header_level(parent);
306 if (cache_only && parent_level != 1)
307 return 0;
308
Chris Mason6702ed42007-08-07 16:15:09 -0400309 if (trans->transaction != root->fs_info->running_transaction) {
310 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
311 root->fs_info->running_transaction->transid);
312 WARN_ON(1);
313 }
314 if (trans->transid != root->fs_info->generation) {
315 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
316 root->fs_info->generation);
317 WARN_ON(1);
318 }
Chris Mason86479a02007-09-10 19:58:16 -0400319
Chris Mason6b800532007-10-15 16:17:34 -0400320 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400321 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400322 end_slot = parent_nritems;
323
324 if (parent_nritems == 1)
325 return 0;
326
327 for (i = start_slot; i < end_slot; i++) {
328 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400329
Chris Mason5708b952007-10-25 15:43:18 -0400330 if (!parent->map_token) {
331 map_extent_buffer(parent,
332 btrfs_node_key_ptr_offset(i),
333 sizeof(struct btrfs_key_ptr),
334 &parent->map_token, &parent->kaddr,
335 &parent->map_start, &parent->map_len,
336 KM_USER1);
337 }
Chris Mason081e9572007-11-06 10:26:24 -0500338 btrfs_node_key(parent, &disk_key, i);
339 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
340 continue;
341
342 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400343 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400344 if (last_block == 0)
345 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400346
Chris Mason6702ed42007-08-07 16:15:09 -0400347 if (i > 0) {
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 Mason5708b952007-10-25 15:43:18 -0400351 if (close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400352 other = btrfs_node_blockptr(parent, i + 1);
353 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400354 }
Chris Masone9d0b132007-08-10 14:06:19 -0400355 if (close) {
356 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400357 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400358 }
Chris Mason5708b952007-10-25 15:43:18 -0400359 if (parent->map_token) {
360 unmap_extent_buffer(parent, parent->map_token,
361 KM_USER1);
362 parent->map_token = NULL;
363 }
Chris Mason6702ed42007-08-07 16:15:09 -0400364
Chris Mason6b800532007-10-15 16:17:34 -0400365 cur = btrfs_find_tree_block(root, blocknr, blocksize);
366 if (cur)
367 uptodate = btrfs_buffer_uptodate(cur);
368 else
369 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400370 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400371 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400372 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400373 continue;
374 }
Chris Mason6b800532007-10-15 16:17:34 -0400375 if (!cur) {
376 cur = read_tree_block(root, blocknr,
377 blocksize);
378 } else if (!uptodate) {
379 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400380 }
Chris Mason6702ed42007-08-07 16:15:09 -0400381 }
Chris Masone9d0b132007-08-10 14:06:19 -0400382 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400383 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400384
Chris Mason6b800532007-10-15 16:17:34 -0400385 err = __btrfs_cow_block(trans, root, cur, parent, i,
386 &tmp, search_start,
387 min(16 * blocksize,
388 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400389 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400390 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400391 break;
Yan252c38f2007-08-29 09:11:44 -0400392 }
Chris Mason6b800532007-10-15 16:17:34 -0400393 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400394 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400395 *last_ret = search_start;
396 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400397 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400398 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400399 }
Chris Mason5708b952007-10-25 15:43:18 -0400400 if (parent->map_token) {
401 unmap_extent_buffer(parent, parent->map_token,
402 KM_USER1);
403 parent->map_token = NULL;
404 }
Chris Mason6702ed42007-08-07 16:15:09 -0400405 return err;
406}
407
Chris Mason74123bd2007-02-02 11:05:29 -0500408/*
409 * The leaf data grows from end-to-front in the node.
410 * this returns the address of the start of the last item,
411 * which is the stop of the leaf data stack
412 */
Chris Mason123abc82007-03-14 14:14:43 -0400413static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400414 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500415{
Chris Mason5f39d392007-10-15 16:14:19 -0400416 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500417 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400418 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400419 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500420}
421
Chris Mason123abc82007-03-14 14:14:43 -0400422static int check_node(struct btrfs_root *root, struct btrfs_path *path,
423 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500424{
Chris Mason5f39d392007-10-15 16:14:19 -0400425 struct extent_buffer *parent = NULL;
426 struct extent_buffer *node = path->nodes[level];
427 struct btrfs_disk_key parent_key;
428 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500429 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400430 int slot;
431 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400432 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500433
434 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400435 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400436
Chris Mason8d7be552007-05-10 11:24:42 -0400437 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400438 BUG_ON(nritems == 0);
439 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400440 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400441 btrfs_node_key(parent, &parent_key, parent_slot);
442 btrfs_node_key(node, &node_key, 0);
443 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400444 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400445 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400446 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500447 }
Chris Mason123abc82007-03-14 14:14:43 -0400448 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400449 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400450 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
451 btrfs_node_key(node, &node_key, slot);
452 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400453 }
454 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400455 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
456 btrfs_node_key(node, &node_key, slot);
457 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500458 }
459 return 0;
460}
461
Chris Mason123abc82007-03-14 14:14:43 -0400462static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
463 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500464{
Chris Mason5f39d392007-10-15 16:14:19 -0400465 struct extent_buffer *leaf = path->nodes[level];
466 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500467 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400468 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400469 struct btrfs_disk_key parent_key;
470 struct btrfs_disk_key leaf_key;
471 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400472
Chris Mason5f39d392007-10-15 16:14:19 -0400473 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500474
475 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400476 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400477
478 if (nritems == 0)
479 return 0;
480
481 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400482 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400483 btrfs_node_key(parent, &parent_key, parent_slot);
484 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400485
Chris Mason5f39d392007-10-15 16:14:19 -0400486 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400487 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400488 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400489 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500490 }
Chris Mason5f39d392007-10-15 16:14:19 -0400491#if 0
492 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
493 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
494 btrfs_item_key(leaf, &leaf_key, i);
495 if (comp_keys(&leaf_key, &cpukey) >= 0) {
496 btrfs_print_leaf(root, leaf);
497 printk("slot %d offset bad key\n", i);
498 BUG_ON(1);
499 }
500 if (btrfs_item_offset_nr(leaf, i) !=
501 btrfs_item_end_nr(leaf, i + 1)) {
502 btrfs_print_leaf(root, leaf);
503 printk("slot %d offset bad\n", i);
504 BUG_ON(1);
505 }
506 if (i == 0) {
507 if (btrfs_item_offset_nr(leaf, i) +
508 btrfs_item_size_nr(leaf, i) !=
509 BTRFS_LEAF_DATA_SIZE(root)) {
510 btrfs_print_leaf(root, leaf);
511 printk("slot %d first offset bad\n", i);
512 BUG_ON(1);
513 }
514 }
515 }
516 if (nritems > 0) {
517 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
518 btrfs_print_leaf(root, leaf);
519 printk("slot %d bad size \n", nritems - 1);
520 BUG_ON(1);
521 }
522 }
523#endif
524 if (slot != 0 && slot < nritems - 1) {
525 btrfs_item_key(leaf, &leaf_key, slot);
526 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
527 if (comp_keys(&leaf_key, &cpukey) <= 0) {
528 btrfs_print_leaf(root, leaf);
529 printk("slot %d offset bad key\n", slot);
530 BUG_ON(1);
531 }
532 if (btrfs_item_offset_nr(leaf, slot - 1) !=
533 btrfs_item_end_nr(leaf, slot)) {
534 btrfs_print_leaf(root, leaf);
535 printk("slot %d offset bad\n", slot);
536 BUG_ON(1);
537 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500538 }
Chris Mason8d7be552007-05-10 11:24:42 -0400539 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400540 btrfs_item_key(leaf, &leaf_key, slot);
541 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
542 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
543 if (btrfs_item_offset_nr(leaf, slot) !=
544 btrfs_item_end_nr(leaf, slot + 1)) {
545 btrfs_print_leaf(root, leaf);
546 printk("slot %d offset bad\n", slot);
547 BUG_ON(1);
548 }
Chris Mason8d7be552007-05-10 11:24:42 -0400549 }
Chris Mason5f39d392007-10-15 16:14:19 -0400550 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
551 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500552 return 0;
553}
554
Chris Mason98ed5172008-01-03 10:01:48 -0500555static int noinline check_block(struct btrfs_root *root,
556 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500557{
Chris Mason810191f2007-10-15 16:18:55 -0400558 return 0;
Chris Masondb945352007-10-15 16:15:53 -0400559#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400560 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400561
Chris Mason479965d2007-10-15 16:14:27 -0400562 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
563 (unsigned long)btrfs_header_fsid(buf),
564 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400565 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400566 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400567 }
Chris Masondb945352007-10-15 16:15:53 -0400568#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500569 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400570 return check_leaf(root, path, level);
571 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500572}
573
Chris Mason74123bd2007-02-02 11:05:29 -0500574/*
Chris Mason5f39d392007-10-15 16:14:19 -0400575 * search for key in the extent_buffer. The items start at offset p,
576 * and they are item_size apart. There are 'max' items in p.
577 *
Chris Mason74123bd2007-02-02 11:05:29 -0500578 * the slot in the array is returned via slot, and it points to
579 * the place where you would insert key if it is not found in
580 * the array.
581 *
582 * slot may point to max if the key is bigger than all of the keys
583 */
Chris Mason5f39d392007-10-15 16:14:19 -0400584static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
585 int item_size, struct btrfs_key *key,
586 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500587{
588 int low = 0;
589 int high = max;
590 int mid;
591 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400592 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400593 struct btrfs_disk_key unaligned;
594 unsigned long offset;
595 char *map_token = NULL;
596 char *kaddr = NULL;
597 unsigned long map_start = 0;
598 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400599 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500600
601 while(low < high) {
602 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400603 offset = p + mid * item_size;
604
605 if (!map_token || offset < map_start ||
606 (offset + sizeof(struct btrfs_disk_key)) >
607 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400608 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400609 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400610 map_token = NULL;
611 }
612 err = map_extent_buffer(eb, offset,
613 sizeof(struct btrfs_disk_key),
614 &map_token, &kaddr,
615 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400616
Chris Mason479965d2007-10-15 16:14:27 -0400617 if (!err) {
618 tmp = (struct btrfs_disk_key *)(kaddr + offset -
619 map_start);
620 } else {
621 read_extent_buffer(eb, &unaligned,
622 offset, sizeof(unaligned));
623 tmp = &unaligned;
624 }
625
Chris Mason5f39d392007-10-15 16:14:19 -0400626 } else {
627 tmp = (struct btrfs_disk_key *)(kaddr + offset -
628 map_start);
629 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500630 ret = comp_keys(tmp, key);
631
632 if (ret < 0)
633 low = mid + 1;
634 else if (ret > 0)
635 high = mid;
636 else {
637 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400638 if (map_token)
639 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500640 return 0;
641 }
642 }
643 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400644 if (map_token)
645 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500646 return 1;
647}
648
Chris Mason97571fd2007-02-24 13:39:08 -0500649/*
650 * simple bin_search frontend that does the right thing for
651 * leaves vs nodes
652 */
Chris Mason5f39d392007-10-15 16:14:19 -0400653static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
654 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500655{
Chris Mason5f39d392007-10-15 16:14:19 -0400656 if (level == 0) {
657 return generic_bin_search(eb,
658 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400659 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400660 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400661 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500662 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400663 return generic_bin_search(eb,
664 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400665 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400666 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400667 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500668 }
669 return -1;
670}
671
Chris Mason5f39d392007-10-15 16:14:19 -0400672static struct extent_buffer *read_node_slot(struct btrfs_root *root,
673 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500674{
Chris Masonbb803952007-03-01 12:04:21 -0500675 if (slot < 0)
676 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400677 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500678 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400679 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
680 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500681}
682
Chris Mason98ed5172008-01-03 10:01:48 -0500683static int balance_level(struct btrfs_trans_handle *trans,
684 struct btrfs_root *root,
685 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500686{
Chris Mason5f39d392007-10-15 16:14:19 -0400687 struct extent_buffer *right = NULL;
688 struct extent_buffer *mid;
689 struct extent_buffer *left = NULL;
690 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500691 int ret = 0;
692 int wret;
693 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500694 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400695 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500696 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500697
698 if (level == 0)
699 return 0;
700
Chris Mason5f39d392007-10-15 16:14:19 -0400701 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500702 WARN_ON(btrfs_header_generation(mid) != trans->transid);
703
Chris Mason1d4f8a02007-03-13 09:28:32 -0400704 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500705
Chris Mason234b63a2007-03-13 10:46:10 -0400706 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400707 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500708 pslot = path->slots[level + 1];
709
Chris Mason40689472007-03-17 14:29:23 -0400710 /*
711 * deal with the case where there is only one pointer in the root
712 * by promoting the node below to a root
713 */
Chris Mason5f39d392007-10-15 16:14:19 -0400714 if (!parent) {
715 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500716
Chris Mason5f39d392007-10-15 16:14:19 -0400717 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500718 return 0;
719
720 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400721 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500722 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500723 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
724 BUG_ON(ret);
725
Chris Masonbb803952007-03-01 12:04:21 -0500726 root->node = child;
727 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400728 clean_tree_block(trans, root, mid);
729 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500730 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400731 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500732 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
733 root->root_key.objectid,
734 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500735 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400736 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400737 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500738 }
Chris Mason5f39d392007-10-15 16:14:19 -0400739 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400740 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500741 return 0;
742
Chris Mason5f39d392007-10-15 16:14:19 -0400743 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400744 err_on_enospc = 1;
745
Chris Mason5f39d392007-10-15 16:14:19 -0400746 left = read_node_slot(root, parent, pslot - 1);
747 if (left) {
748 wret = btrfs_cow_block(trans, root, left,
749 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400750 if (wret) {
751 ret = wret;
752 goto enospc;
753 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400754 }
Chris Mason5f39d392007-10-15 16:14:19 -0400755 right = read_node_slot(root, parent, pslot + 1);
756 if (right) {
757 wret = btrfs_cow_block(trans, root, right,
758 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400759 if (wret) {
760 ret = wret;
761 goto enospc;
762 }
763 }
764
765 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400766 if (left) {
767 orig_slot += btrfs_header_nritems(left);
768 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500769 if (wret < 0)
770 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400771 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400772 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500773 }
Chris Mason79f95c82007-03-01 15:16:26 -0500774
775 /*
776 * then try to empty the right most buffer into the middle
777 */
Chris Mason5f39d392007-10-15 16:14:19 -0400778 if (right) {
779 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400780 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500781 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400782 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400783 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500784 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400785 u32 blocksize = right->len;
786
Chris Mason5f39d392007-10-15 16:14:19 -0400787 clean_tree_block(trans, root, right);
788 wait_on_tree_block_writeback(root, right);
789 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500790 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400791 wret = del_ptr(trans, root, path, level + 1, pslot +
792 1);
Chris Masonbb803952007-03-01 12:04:21 -0500793 if (wret)
794 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400795 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500796 blocksize,
797 btrfs_header_owner(parent),
798 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500799 if (wret)
800 ret = wret;
801 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400802 struct btrfs_disk_key right_key;
803 btrfs_node_key(right, &right_key, 0);
804 btrfs_set_node_key(parent, &right_key, pslot + 1);
805 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500806 }
807 }
Chris Mason5f39d392007-10-15 16:14:19 -0400808 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500809 /*
810 * we're not allowed to leave a node with one item in the
811 * tree during a delete. A deletion from lower in the tree
812 * could try to delete the only pointer in this node.
813 * So, pull some keys from the left.
814 * There has to be a left pointer at this point because
815 * otherwise we would have pulled some pointers from the
816 * right
817 */
Chris Mason5f39d392007-10-15 16:14:19 -0400818 BUG_ON(!left);
819 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400820 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500821 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400822 goto enospc;
823 }
Chris Mason79f95c82007-03-01 15:16:26 -0500824 BUG_ON(wret == 1);
825 }
Chris Mason5f39d392007-10-15 16:14:19 -0400826 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500827 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500828 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400829 u64 bytenr = mid->start;
830 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400831 clean_tree_block(trans, root, mid);
832 wait_on_tree_block_writeback(root, mid);
833 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500834 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400835 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500836 if (wret)
837 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500838 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
839 btrfs_header_owner(parent),
840 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500841 if (wret)
842 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500843 } else {
844 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400845 struct btrfs_disk_key mid_key;
846 btrfs_node_key(mid, &mid_key, 0);
847 btrfs_set_node_key(parent, &mid_key, pslot);
848 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500849 }
Chris Masonbb803952007-03-01 12:04:21 -0500850
Chris Mason79f95c82007-03-01 15:16:26 -0500851 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400852 if (left) {
853 if (btrfs_header_nritems(left) > orig_slot) {
854 extent_buffer_get(left);
855 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500856 path->slots[level + 1] -= 1;
857 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400858 if (mid)
859 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500860 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400861 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500862 path->slots[level] = orig_slot;
863 }
864 }
Chris Mason79f95c82007-03-01 15:16:26 -0500865 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400866 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400867 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400868 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500869 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400870enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400871 if (right)
872 free_extent_buffer(right);
873 if (left)
874 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500875 return ret;
876}
877
Chris Masone66f7092007-04-20 13:16:02 -0400878/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500879static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
880 struct btrfs_root *root,
881 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400882{
Chris Mason5f39d392007-10-15 16:14:19 -0400883 struct extent_buffer *right = NULL;
884 struct extent_buffer *mid;
885 struct extent_buffer *left = NULL;
886 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400887 int ret = 0;
888 int wret;
889 int pslot;
890 int orig_slot = path->slots[level];
891 u64 orig_ptr;
892
893 if (level == 0)
894 return 1;
895
Chris Mason5f39d392007-10-15 16:14:19 -0400896 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500897 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -0400898 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
899
900 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400901 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400902 pslot = path->slots[level + 1];
903
Chris Mason5f39d392007-10-15 16:14:19 -0400904 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400905 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400906
Chris Mason5f39d392007-10-15 16:14:19 -0400907 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400908
909 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400910 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400911 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400912 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400913 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
914 wret = 1;
915 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400916 ret = btrfs_cow_block(trans, root, left, parent,
917 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400918 if (ret)
919 wret = 1;
920 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400921 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400922 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400923 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400924 }
Chris Masone66f7092007-04-20 13:16:02 -0400925 if (wret < 0)
926 ret = wret;
927 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400928 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400929 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400930 btrfs_node_key(mid, &disk_key, 0);
931 btrfs_set_node_key(parent, &disk_key, pslot);
932 btrfs_mark_buffer_dirty(parent);
933 if (btrfs_header_nritems(left) > orig_slot) {
934 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400935 path->slots[level + 1] -= 1;
936 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400937 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400938 } else {
939 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400940 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400941 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400942 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400943 }
Chris Masone66f7092007-04-20 13:16:02 -0400944 return 0;
945 }
Chris Mason5f39d392007-10-15 16:14:19 -0400946 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400947 }
Chris Mason5f39d392007-10-15 16:14:19 -0400948 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400949
950 /*
951 * then try to empty the right most buffer into the middle
952 */
Chris Mason5f39d392007-10-15 16:14:19 -0400953 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400954 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400955 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400956 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
957 wret = 1;
958 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400959 ret = btrfs_cow_block(trans, root, right,
960 parent, pslot + 1,
961 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400962 if (ret)
963 wret = 1;
964 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400965 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400966 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400967 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400968 }
Chris Masone66f7092007-04-20 13:16:02 -0400969 if (wret < 0)
970 ret = wret;
971 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400972 struct btrfs_disk_key disk_key;
973
974 btrfs_node_key(right, &disk_key, 0);
975 btrfs_set_node_key(parent, &disk_key, pslot + 1);
976 btrfs_mark_buffer_dirty(parent);
977
978 if (btrfs_header_nritems(mid) <= orig_slot) {
979 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -0400980 path->slots[level + 1] += 1;
981 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -0400982 btrfs_header_nritems(mid);
983 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400984 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400985 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400986 }
Chris Masone66f7092007-04-20 13:16:02 -0400987 return 0;
988 }
Chris Mason5f39d392007-10-15 16:14:19 -0400989 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -0400990 }
Chris Masone66f7092007-04-20 13:16:02 -0400991 return 1;
992}
993
Chris Mason74123bd2007-02-02 11:05:29 -0500994/*
Chris Mason3c69fae2007-08-07 15:52:22 -0400995 * readahead one full node of leaves
996 */
997static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -0500998 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -0400999{
Chris Mason5f39d392007-10-15 16:14:19 -04001000 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001001 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001002 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001003 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001004 u64 lowest_read;
1005 u64 highest_read;
1006 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001007 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001008 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001009 u32 nr;
1010 u32 blocksize;
1011 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001012
Chris Masona6b6e752007-10-15 16:22:39 -04001013 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001014 return;
1015
Chris Mason6702ed42007-08-07 16:15:09 -04001016 if (!path->nodes[level])
1017 return;
1018
Chris Mason5f39d392007-10-15 16:14:19 -04001019 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -04001020 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001021 blocksize = btrfs_level_size(root, level - 1);
1022 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001023 if (eb) {
1024 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001025 return;
1026 }
1027
Chris Mason6b800532007-10-15 16:17:34 -04001028 highest_read = search;
1029 lowest_read = search;
1030
Chris Mason5f39d392007-10-15 16:14:19 -04001031 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001032 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001033 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001034 if (direction < 0) {
1035 if (nr == 0)
1036 break;
1037 nr--;
1038 } else if (direction > 0) {
1039 nr++;
1040 if (nr >= nritems)
1041 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001042 }
Chris Mason01f46652007-12-21 16:24:26 -05001043 if (path->reada < 0 && objectid) {
1044 btrfs_node_key(node, &disk_key, nr);
1045 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1046 break;
1047 }
Chris Mason6b800532007-10-15 16:17:34 -04001048 search = btrfs_node_blockptr(node, nr);
1049 if ((search >= lowest_read && search <= highest_read) ||
1050 (search < lowest_read && lowest_read - search <= 32768) ||
1051 (search > highest_read && search - highest_read <= 32768)) {
1052 readahead_tree_block(root, search, blocksize);
1053 nread += blocksize;
1054 }
1055 nscan++;
1056 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1057 break;
1058 if(nread > (1024 * 1024) || nscan > 128)
1059 break;
1060
1061 if (search < lowest_read)
1062 lowest_read = search;
1063 if (search > highest_read)
1064 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001065 }
1066}
1067/*
Chris Mason74123bd2007-02-02 11:05:29 -05001068 * look for key in the tree. path is filled in with nodes along the way
1069 * if key is found, we return zero and you can find the item in the leaf
1070 * level of the path (level 0)
1071 *
1072 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001073 * be inserted, and 1 is returned. If there are other errors during the
1074 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001075 *
1076 * if ins_len > 0, nodes and leaves will be split as we walk down the
1077 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1078 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001079 */
Chris Masone089f052007-03-16 16:20:31 -04001080int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1081 *root, struct btrfs_key *key, struct btrfs_path *p, int
1082 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001083{
Chris Mason5f39d392007-10-15 16:14:19 -04001084 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -04001085 u64 bytenr;
Chris Mason74493f72007-12-11 09:25:06 -05001086 u64 ptr_gen;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001087 int slot;
1088 int ret;
1089 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001090 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -04001091 u8 lowest_level = 0;
1092
Chris Mason6702ed42007-08-07 16:15:09 -04001093 lowest_level = p->lowest_level;
1094 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001095 WARN_ON(p->nodes[0] != NULL);
1096 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -05001097again:
1098 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001099 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001100 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001101 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001102 if (cow) {
1103 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001104 wret = btrfs_cow_block(trans, root, b,
1105 p->nodes[level + 1],
1106 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001107 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001108 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001109 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001110 return wret;
1111 }
Chris Mason02217ed2007-03-02 16:08:05 -05001112 }
1113 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001114 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001115 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001116 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001117 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001118 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001119 if (ret)
1120 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001121 ret = bin_search(b, key, level, &slot);
1122 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001123 if (ret && slot > 0)
1124 slot -= 1;
1125 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001126 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001127 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001128 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001129 BUG_ON(sret > 0);
1130 if (sret)
1131 return sret;
1132 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001133 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001134 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001135 int sret = balance_level(trans, root, p,
1136 level);
Chris Masonbb803952007-03-01 12:04:21 -05001137 if (sret)
1138 return sret;
1139 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001140 if (!b) {
1141 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001142 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001143 }
Chris Masonbb803952007-03-01 12:04:21 -05001144 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001145 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001146 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001147 /* this is only true while dropping a snapshot */
1148 if (level == lowest_level)
1149 break;
Chris Masondb945352007-10-15 16:15:53 -04001150 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason74493f72007-12-11 09:25:06 -05001151 ptr_gen = btrfs_node_ptr_generation(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001152 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001153 reada_for_search(root, p, level, slot,
1154 key->objectid);
Chris Masondb945352007-10-15 16:15:53 -04001155 b = read_tree_block(root, bytenr,
1156 btrfs_level_size(root, level - 1));
Chris Mason74493f72007-12-11 09:25:06 -05001157 if (ptr_gen != btrfs_header_generation(b)) {
1158 printk("block %llu bad gen wanted %llu "
1159 "found %llu\n",
1160 (unsigned long long)b->start,
1161 (unsigned long long)ptr_gen,
1162 (unsigned long long)btrfs_header_generation(b));
1163 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001164 } else {
1165 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001166 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001167 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001168 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001169 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001170 BUG_ON(sret > 0);
1171 if (sret)
1172 return sret;
1173 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001174 return ret;
1175 }
1176 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001177 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001178}
1179
Chris Mason74123bd2007-02-02 11:05:29 -05001180/*
1181 * adjust the pointers going up the tree, starting at level
1182 * making sure the right key of each node is points to 'key'.
1183 * This is used after shifting pointers to the left, so it stops
1184 * fixing up pointers when a given leaf/node is not in slot 0 of the
1185 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001186 *
1187 * If this fails to write a tree block, it returns -1, but continues
1188 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001189 */
Chris Mason5f39d392007-10-15 16:14:19 -04001190static int fixup_low_keys(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root, struct btrfs_path *path,
1192 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001193{
1194 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001195 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001196 struct extent_buffer *t;
1197
Chris Mason234b63a2007-03-13 10:46:10 -04001198 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001199 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001200 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001201 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001202 t = path->nodes[i];
1203 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001204 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001205 if (tslot != 0)
1206 break;
1207 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001208 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001209}
1210
Chris Mason74123bd2007-02-02 11:05:29 -05001211/*
1212 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001213 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001214 *
1215 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1216 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001217 */
Chris Mason98ed5172008-01-03 10:01:48 -05001218static int push_node_left(struct btrfs_trans_handle *trans,
1219 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason5f39d392007-10-15 16:14:19 -04001220 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001221{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001222 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001223 int src_nritems;
1224 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001225 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001226
Chris Mason5f39d392007-10-15 16:14:19 -04001227 src_nritems = btrfs_header_nritems(src);
1228 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001229 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001230 WARN_ON(btrfs_header_generation(src) != trans->transid);
1231 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001232
Chris Masoneb60cea2007-02-02 09:18:22 -05001233 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001234 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001235 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001236
1237 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001238 push_items = src_nritems;
1239
Chris Mason5f39d392007-10-15 16:14:19 -04001240 copy_extent_buffer(dst, src,
1241 btrfs_node_key_ptr_offset(dst_nritems),
1242 btrfs_node_key_ptr_offset(0),
1243 push_items * sizeof(struct btrfs_key_ptr));
1244
Chris Masonbb803952007-03-01 12:04:21 -05001245 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001246 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1247 btrfs_node_key_ptr_offset(push_items),
1248 (src_nritems - push_items) *
1249 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001250 }
Chris Mason5f39d392007-10-15 16:14:19 -04001251 btrfs_set_header_nritems(src, src_nritems - push_items);
1252 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1253 btrfs_mark_buffer_dirty(src);
1254 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001255 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001256}
1257
Chris Mason97571fd2007-02-24 13:39:08 -05001258/*
Chris Mason79f95c82007-03-01 15:16:26 -05001259 * try to push data from one node into the next node right in the
1260 * tree.
1261 *
1262 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1263 * error, and > 0 if there was no room in the right hand block.
1264 *
1265 * this will only push up to 1/2 the contents of the left node over
1266 */
Chris Mason5f39d392007-10-15 16:14:19 -04001267static int balance_node_right(struct btrfs_trans_handle *trans,
1268 struct btrfs_root *root,
1269 struct extent_buffer *dst,
1270 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001271{
Chris Mason79f95c82007-03-01 15:16:26 -05001272 int push_items = 0;
1273 int max_push;
1274 int src_nritems;
1275 int dst_nritems;
1276 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001277
Chris Mason7bb86312007-12-11 09:25:06 -05001278 WARN_ON(btrfs_header_generation(src) != trans->transid);
1279 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1280
Chris Mason5f39d392007-10-15 16:14:19 -04001281 src_nritems = btrfs_header_nritems(src);
1282 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001283 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001284 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001285 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001286
1287 max_push = src_nritems / 2 + 1;
1288 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001289 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001290 return 1;
Yan252c38f2007-08-29 09:11:44 -04001291
Chris Mason79f95c82007-03-01 15:16:26 -05001292 if (max_push < push_items)
1293 push_items = max_push;
1294
Chris Mason5f39d392007-10-15 16:14:19 -04001295 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1296 btrfs_node_key_ptr_offset(0),
1297 (dst_nritems) *
1298 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001299
Chris Mason5f39d392007-10-15 16:14:19 -04001300 copy_extent_buffer(dst, src,
1301 btrfs_node_key_ptr_offset(0),
1302 btrfs_node_key_ptr_offset(src_nritems - push_items),
1303 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001304
Chris Mason5f39d392007-10-15 16:14:19 -04001305 btrfs_set_header_nritems(src, src_nritems - push_items);
1306 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001307
Chris Mason5f39d392007-10-15 16:14:19 -04001308 btrfs_mark_buffer_dirty(src);
1309 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001310 return ret;
1311}
1312
1313/*
Chris Mason97571fd2007-02-24 13:39:08 -05001314 * helper function to insert a new root level in the tree.
1315 * A new node is allocated, and a single item is inserted to
1316 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001317 *
1318 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001319 */
Chris Mason98ed5172008-01-03 10:01:48 -05001320static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001321 struct btrfs_root *root,
1322 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001323{
Chris Mason7bb86312007-12-11 09:25:06 -05001324 u64 root_gen;
1325 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001326 struct extent_buffer *lower;
1327 struct extent_buffer *c;
1328 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001329
1330 BUG_ON(path->nodes[level]);
1331 BUG_ON(path->nodes[level-1] != root->node);
1332
Chris Mason7bb86312007-12-11 09:25:06 -05001333 if (root->ref_cows)
1334 root_gen = trans->transid;
1335 else
1336 root_gen = 0;
1337
1338 lower = path->nodes[level-1];
1339 if (level == 1)
1340 btrfs_item_key(lower, &lower_key, 0);
1341 else
1342 btrfs_node_key(lower, &lower_key, 0);
1343
1344 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1345 root->root_key.objectid,
1346 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001347 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001348 if (IS_ERR(c))
1349 return PTR_ERR(c);
1350 memset_extent_buffer(c, 0, 0, root->nodesize);
1351 btrfs_set_header_nritems(c, 1);
1352 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001353 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001354 btrfs_set_header_generation(c, trans->transid);
1355 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001356
Chris Mason5f39d392007-10-15 16:14:19 -04001357 write_extent_buffer(c, root->fs_info->fsid,
1358 (unsigned long)btrfs_header_fsid(c),
1359 BTRFS_FSID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001360 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001361 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001362 lower_gen = btrfs_header_generation(lower);
1363 WARN_ON(lower_gen == 0);
1364
1365 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001366
1367 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001368
Chris Mason5c680ed2007-02-22 11:39:13 -05001369 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001370 free_extent_buffer(root->node);
1371 root->node = c;
1372 extent_buffer_get(c);
1373 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001374 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001375
1376 if (root->ref_cows && lower_gen != trans->transid) {
1377 struct btrfs_path *back_path = btrfs_alloc_path();
1378 int ret;
1379 ret = btrfs_insert_extent_backref(trans,
1380 root->fs_info->extent_root,
1381 path, lower->start,
1382 root->root_key.objectid,
1383 trans->transid, 0, 0);
1384 BUG_ON(ret);
1385 btrfs_free_path(back_path);
1386 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001387 return 0;
1388}
1389
Chris Mason74123bd2007-02-02 11:05:29 -05001390/*
1391 * worker function to insert a single pointer in a node.
1392 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001393 *
Chris Mason74123bd2007-02-02 11:05:29 -05001394 * slot and level indicate where you want the key to go, and
1395 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001396 *
1397 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001398 */
Chris Masone089f052007-03-16 16:20:31 -04001399static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1400 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001401 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001402{
Chris Mason5f39d392007-10-15 16:14:19 -04001403 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001404 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001405
1406 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001407 lower = path->nodes[level];
1408 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001409 if (slot > nritems)
1410 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001411 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001412 BUG();
1413 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001414 memmove_extent_buffer(lower,
1415 btrfs_node_key_ptr_offset(slot + 1),
1416 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001417 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001418 }
Chris Mason5f39d392007-10-15 16:14:19 -04001419 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001420 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001421 WARN_ON(trans->transid == 0);
1422 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001423 btrfs_set_header_nritems(lower, nritems + 1);
1424 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001425 return 0;
1426}
1427
Chris Mason97571fd2007-02-24 13:39:08 -05001428/*
1429 * split the node at the specified level in path in two.
1430 * The path is corrected to point to the appropriate node after the split
1431 *
1432 * Before splitting this tries to make some room in the node by pushing
1433 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001434 *
1435 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001436 */
Chris Masone089f052007-03-16 16:20:31 -04001437static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1438 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001439{
Chris Mason7bb86312007-12-11 09:25:06 -05001440 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001441 struct extent_buffer *c;
1442 struct extent_buffer *split;
1443 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001444 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001445 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001446 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001447 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001448
Chris Mason5f39d392007-10-15 16:14:19 -04001449 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001450 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001451 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001452 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001453 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001454 if (ret)
1455 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001456 } else {
1457 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001458 c = path->nodes[level];
1459 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001460 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1461 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001462 if (ret < 0)
1463 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001464 }
Chris Masone66f7092007-04-20 13:16:02 -04001465
Chris Mason5f39d392007-10-15 16:14:19 -04001466 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001467 if (root->ref_cows)
1468 root_gen = trans->transid;
1469 else
1470 root_gen = 0;
1471
1472 btrfs_node_key(c, &disk_key, 0);
1473 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1474 root->root_key.objectid,
1475 root_gen,
1476 btrfs_disk_key_objectid(&disk_key),
1477 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001478 if (IS_ERR(split))
1479 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001480
Chris Mason5f39d392007-10-15 16:14:19 -04001481 btrfs_set_header_flags(split, btrfs_header_flags(c));
1482 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001483 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001484 btrfs_set_header_generation(split, trans->transid);
1485 btrfs_set_header_owner(split, root->root_key.objectid);
1486 write_extent_buffer(split, root->fs_info->fsid,
1487 (unsigned long)btrfs_header_fsid(split),
1488 BTRFS_FSID_SIZE);
1489
Chris Mason7518a232007-03-12 12:01:18 -04001490 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001491
1492 copy_extent_buffer(split, c,
1493 btrfs_node_key_ptr_offset(0),
1494 btrfs_node_key_ptr_offset(mid),
1495 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1496 btrfs_set_header_nritems(split, c_nritems - mid);
1497 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001498 ret = 0;
1499
Chris Mason5f39d392007-10-15 16:14:19 -04001500 btrfs_mark_buffer_dirty(c);
1501 btrfs_mark_buffer_dirty(split);
1502
1503 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001504 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001505 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001506 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001507 if (wret)
1508 ret = wret;
1509
Chris Mason5de08d72007-02-24 06:24:44 -05001510 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001511 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001512 free_extent_buffer(c);
1513 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001514 path->slots[level + 1] += 1;
1515 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001516 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001517 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001518 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001519}
1520
Chris Mason74123bd2007-02-02 11:05:29 -05001521/*
1522 * how many bytes are required to store the items in a leaf. start
1523 * and nr indicate which items in the leaf to check. This totals up the
1524 * space used both by the item structs and the item data
1525 */
Chris Mason5f39d392007-10-15 16:14:19 -04001526static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001527{
1528 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001529 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001530 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001531
1532 if (!nr)
1533 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001534 data_len = btrfs_item_end_nr(l, start);
1535 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001536 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001537 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001538 return data_len;
1539}
1540
Chris Mason74123bd2007-02-02 11:05:29 -05001541/*
Chris Masond4dbff92007-04-04 14:08:15 -04001542 * The space between the end of the leaf items and
1543 * the start of the leaf data. IOW, how much room
1544 * the leaf has left for both items and data
1545 */
Chris Mason5f39d392007-10-15 16:14:19 -04001546int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001547{
Chris Mason5f39d392007-10-15 16:14:19 -04001548 int nritems = btrfs_header_nritems(leaf);
1549 int ret;
1550 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1551 if (ret < 0) {
1552 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001553 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001554 leaf_space_used(leaf, 0, nritems), nritems);
1555 }
1556 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001557}
1558
1559/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001560 * push some data in the path leaf to the right, trying to free up at
1561 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001562 *
1563 * returns 1 if the push failed because the other node didn't have enough
1564 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001565 */
Chris Masone089f052007-03-16 16:20:31 -04001566static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001567 *root, struct btrfs_path *path, int data_size,
1568 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001569{
Chris Mason5f39d392007-10-15 16:14:19 -04001570 struct extent_buffer *left = path->nodes[0];
1571 struct extent_buffer *right;
1572 struct extent_buffer *upper;
1573 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001574 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001575 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001576 int free_space;
1577 int push_space = 0;
1578 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001579 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001580 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001581 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001582 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001583 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001584 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001585 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001586
1587 slot = path->slots[1];
1588 if (!path->nodes[1]) {
1589 return 1;
1590 }
1591 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001592 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001593 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001594
Chris Masondb945352007-10-15 16:15:53 -04001595 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1596 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001597 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001598 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001599 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001600 return 1;
1601 }
1602
Chris Mason5f39d392007-10-15 16:14:19 -04001603 /* cow and double check */
1604 ret = btrfs_cow_block(trans, root, right, upper,
1605 slot + 1, &right);
1606 if (ret) {
1607 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001608 return 1;
1609 }
Chris Mason5f39d392007-10-15 16:14:19 -04001610 free_space = btrfs_leaf_free_space(root, right);
1611 if (free_space < data_size + sizeof(struct btrfs_item)) {
1612 free_extent_buffer(right);
1613 return 1;
1614 }
1615
1616 left_nritems = btrfs_header_nritems(left);
1617 if (left_nritems == 0) {
1618 free_extent_buffer(right);
1619 return 1;
1620 }
1621
Chris Mason34a38212007-11-07 13:31:03 -05001622 if (empty)
1623 nr = 0;
1624 else
1625 nr = 1;
1626
1627 i = left_nritems - 1;
1628 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001629 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001630
Chris Mason00ec4c52007-02-24 12:47:20 -05001631 if (path->slots[0] == i)
1632 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001633
1634 if (!left->map_token) {
1635 map_extent_buffer(left, (unsigned long)item,
1636 sizeof(struct btrfs_item),
1637 &left->map_token, &left->kaddr,
1638 &left->map_start, &left->map_len,
1639 KM_USER1);
1640 }
1641
1642 this_item_size = btrfs_item_size(left, item);
1643 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001644 break;
1645 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001646 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001647 if (i == 0)
1648 break;
1649 i--;
Chris Masondb945352007-10-15 16:15:53 -04001650 }
1651 if (left->map_token) {
1652 unmap_extent_buffer(left, left->map_token, KM_USER1);
1653 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001654 }
Chris Mason5f39d392007-10-15 16:14:19 -04001655
Chris Mason00ec4c52007-02-24 12:47:20 -05001656 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001657 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001658 return 1;
1659 }
Chris Mason5f39d392007-10-15 16:14:19 -04001660
Chris Mason34a38212007-11-07 13:31:03 -05001661 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001662 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001663
Chris Mason00ec4c52007-02-24 12:47:20 -05001664 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001665 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001666
Chris Mason5f39d392007-10-15 16:14:19 -04001667 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001668 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001669
Chris Mason00ec4c52007-02-24 12:47:20 -05001670 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001671 data_end = leaf_data_end(root, right);
1672 memmove_extent_buffer(right,
1673 btrfs_leaf_data(right) + data_end - push_space,
1674 btrfs_leaf_data(right) + data_end,
1675 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1676
Chris Mason00ec4c52007-02-24 12:47:20 -05001677 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001678 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001679 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1680 btrfs_leaf_data(left) + leaf_data_end(root, left),
1681 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001682
1683 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1684 btrfs_item_nr_offset(0),
1685 right_nritems * sizeof(struct btrfs_item));
1686
Chris Mason00ec4c52007-02-24 12:47:20 -05001687 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001688 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1689 btrfs_item_nr_offset(left_nritems - push_items),
1690 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001691
1692 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001693 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001694 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001695 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001696 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001697 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001698 if (!right->map_token) {
1699 map_extent_buffer(right, (unsigned long)item,
1700 sizeof(struct btrfs_item),
1701 &right->map_token, &right->kaddr,
1702 &right->map_start, &right->map_len,
1703 KM_USER1);
1704 }
1705 push_space -= btrfs_item_size(right, item);
1706 btrfs_set_item_offset(right, item, push_space);
1707 }
1708
1709 if (right->map_token) {
1710 unmap_extent_buffer(right, right->map_token, KM_USER1);
1711 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001712 }
Chris Mason7518a232007-03-12 12:01:18 -04001713 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001714 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001715
Chris Mason34a38212007-11-07 13:31:03 -05001716 if (left_nritems)
1717 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001718 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001719
Chris Mason5f39d392007-10-15 16:14:19 -04001720 btrfs_item_key(right, &disk_key, 0);
1721 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001722 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001723
Chris Mason00ec4c52007-02-24 12:47:20 -05001724 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001725 if (path->slots[0] >= left_nritems) {
1726 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001727 free_extent_buffer(path->nodes[0]);
1728 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001729 path->slots[1] += 1;
1730 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001731 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001732 }
1733 return 0;
1734}
1735/*
Chris Mason74123bd2007-02-02 11:05:29 -05001736 * push some data in the path leaf to the left, trying to free up at
1737 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1738 */
Chris Masone089f052007-03-16 16:20:31 -04001739static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001740 *root, struct btrfs_path *path, int data_size,
1741 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001742{
Chris Mason5f39d392007-10-15 16:14:19 -04001743 struct btrfs_disk_key disk_key;
1744 struct extent_buffer *right = path->nodes[0];
1745 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001746 int slot;
1747 int i;
1748 int free_space;
1749 int push_space = 0;
1750 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001751 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001752 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001753 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001754 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001755 int ret = 0;
1756 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001757 u32 this_item_size;
1758 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001759
1760 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001761 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001762 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001763 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001764 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001765
Chris Mason3685f792007-10-19 09:23:27 -04001766 right_nritems = btrfs_header_nritems(right);
1767 if (right_nritems == 0) {
1768 return 1;
1769 }
1770
Chris Mason5f39d392007-10-15 16:14:19 -04001771 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001772 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001773 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001774 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001775 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001776 return 1;
1777 }
Chris Mason02217ed2007-03-02 16:08:05 -05001778
1779 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001780 ret = btrfs_cow_block(trans, root, left,
1781 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001782 if (ret) {
1783 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001784 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001785 return 1;
1786 }
Chris Mason3685f792007-10-19 09:23:27 -04001787
Chris Mason123abc82007-03-14 14:14:43 -04001788 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001789 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001790 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001791 return 1;
1792 }
1793
Chris Mason34a38212007-11-07 13:31:03 -05001794 if (empty)
1795 nr = right_nritems;
1796 else
1797 nr = right_nritems - 1;
1798
1799 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001800 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001801 if (!right->map_token) {
1802 map_extent_buffer(right, (unsigned long)item,
1803 sizeof(struct btrfs_item),
1804 &right->map_token, &right->kaddr,
1805 &right->map_start, &right->map_len,
1806 KM_USER1);
1807 }
1808
Chris Masonbe0e5c02007-01-26 15:51:26 -05001809 if (path->slots[0] == i)
1810 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001811
1812 this_item_size = btrfs_item_size(right, item);
1813 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001814 break;
Chris Masondb945352007-10-15 16:15:53 -04001815
Chris Masonbe0e5c02007-01-26 15:51:26 -05001816 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001817 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001818 }
Chris Masondb945352007-10-15 16:15:53 -04001819
1820 if (right->map_token) {
1821 unmap_extent_buffer(right, right->map_token, KM_USER1);
1822 right->map_token = NULL;
1823 }
1824
Chris Masonbe0e5c02007-01-26 15:51:26 -05001825 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001826 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001827 return 1;
1828 }
Chris Mason34a38212007-11-07 13:31:03 -05001829 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001830 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001831
Chris Masonbe0e5c02007-01-26 15:51:26 -05001832 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001833 copy_extent_buffer(left, right,
1834 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1835 btrfs_item_nr_offset(0),
1836 push_items * sizeof(struct btrfs_item));
1837
Chris Mason123abc82007-03-14 14:14:43 -04001838 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001839 btrfs_item_offset_nr(right, push_items -1);
1840
1841 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001842 leaf_data_end(root, left) - push_space,
1843 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001844 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001845 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001846 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001847 BUG_ON(old_left_nritems < 0);
1848
Chris Masondb945352007-10-15 16:15:53 -04001849 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001850 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001851 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001852
Chris Mason5f39d392007-10-15 16:14:19 -04001853 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001854 if (!left->map_token) {
1855 map_extent_buffer(left, (unsigned long)item,
1856 sizeof(struct btrfs_item),
1857 &left->map_token, &left->kaddr,
1858 &left->map_start, &left->map_len,
1859 KM_USER1);
1860 }
1861
Chris Mason5f39d392007-10-15 16:14:19 -04001862 ioff = btrfs_item_offset(left, item);
1863 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001864 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001865 }
Chris Mason5f39d392007-10-15 16:14:19 -04001866 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001867 if (left->map_token) {
1868 unmap_extent_buffer(left, left->map_token, KM_USER1);
1869 left->map_token = NULL;
1870 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001871
1872 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001873 if (push_items > right_nritems) {
1874 printk("push items %d nr %u\n", push_items, right_nritems);
1875 WARN_ON(1);
1876 }
Chris Mason5f39d392007-10-15 16:14:19 -04001877
Chris Mason34a38212007-11-07 13:31:03 -05001878 if (push_items < right_nritems) {
1879 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1880 leaf_data_end(root, right);
1881 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1882 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1883 btrfs_leaf_data(right) +
1884 leaf_data_end(root, right), push_space);
1885
1886 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001887 btrfs_item_nr_offset(push_items),
1888 (btrfs_header_nritems(right) - push_items) *
1889 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001890 }
Yaneef1c492007-11-26 10:58:13 -05001891 right_nritems -= push_items;
1892 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001893 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001894 for (i = 0; i < right_nritems; i++) {
1895 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001896
1897 if (!right->map_token) {
1898 map_extent_buffer(right, (unsigned long)item,
1899 sizeof(struct btrfs_item),
1900 &right->map_token, &right->kaddr,
1901 &right->map_start, &right->map_len,
1902 KM_USER1);
1903 }
1904
1905 push_space = push_space - btrfs_item_size(right, item);
1906 btrfs_set_item_offset(right, item, push_space);
1907 }
1908 if (right->map_token) {
1909 unmap_extent_buffer(right, right->map_token, KM_USER1);
1910 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001911 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001912
Chris Mason5f39d392007-10-15 16:14:19 -04001913 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001914 if (right_nritems)
1915 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001916
Chris Mason5f39d392007-10-15 16:14:19 -04001917 btrfs_item_key(right, &disk_key, 0);
1918 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001919 if (wret)
1920 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001921
1922 /* then fixup the leaf pointer in the path */
1923 if (path->slots[0] < push_items) {
1924 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001925 free_extent_buffer(path->nodes[0]);
1926 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001927 path->slots[1] -= 1;
1928 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001929 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001930 path->slots[0] -= push_items;
1931 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001932 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001933 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001934}
1935
Chris Mason74123bd2007-02-02 11:05:29 -05001936/*
1937 * split the path's leaf in two, making sure there is at least data_size
1938 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001939 *
1940 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001941 */
Chris Masone089f052007-03-16 16:20:31 -04001942static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001943 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001944 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001945{
Chris Mason7bb86312007-12-11 09:25:06 -05001946 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001947 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001948 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001949 int mid;
1950 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001951 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001952 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001953 int data_copy_size;
1954 int rt_data_off;
1955 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001956 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001957 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001958 int double_split;
1959 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001960 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001961
Chris Masoncc0c5532007-10-25 15:42:57 -04001962 if (extend)
1963 space_needed = data_size;
1964
Chris Mason7bb86312007-12-11 09:25:06 -05001965 if (root->ref_cows)
1966 root_gen = trans->transid;
1967 else
1968 root_gen = 0;
1969
Chris Mason40689472007-03-17 14:29:23 -04001970 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04001971 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05001972 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001973 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04001974 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04001975 }
1976 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05001977 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001978 if (wret < 0)
1979 return wret;
1980 }
1981 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05001982
Chris Mason3685f792007-10-19 09:23:27 -04001983 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04001984 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04001985 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04001986 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001987
Chris Mason5c680ed2007-02-22 11:39:13 -05001988 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04001989 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001990 if (ret)
1991 return ret;
1992 }
Chris Masoncc0c5532007-10-25 15:42:57 -04001993again:
1994 double_split = 0;
1995 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001996 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001997 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05001998 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04001999
Chris Mason7bb86312007-12-11 09:25:06 -05002000 btrfs_item_key(l, &disk_key, 0);
2001
2002 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2003 root->root_key.objectid,
2004 root_gen, disk_key.objectid, 0,
2005 l->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002006 if (IS_ERR(right))
2007 return PTR_ERR(right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002008
Chris Mason5f39d392007-10-15 16:14:19 -04002009 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002010 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002011 btrfs_set_header_generation(right, trans->transid);
2012 btrfs_set_header_owner(right, root->root_key.objectid);
2013 btrfs_set_header_level(right, 0);
2014 write_extent_buffer(right, root->fs_info->fsid,
2015 (unsigned long)btrfs_header_fsid(right),
2016 BTRFS_FSID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002017 if (mid <= slot) {
2018 if (nritems == 1 ||
2019 leaf_space_used(l, mid, nritems - mid) + space_needed >
2020 BTRFS_LEAF_DATA_SIZE(root)) {
2021 if (slot >= nritems) {
2022 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002023 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002024 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002025 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002026 path->slots[1] + 1, 1);
2027 if (wret)
2028 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002029 free_extent_buffer(path->nodes[0]);
2030 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002031 path->slots[0] = 0;
2032 path->slots[1] += 1;
2033 return ret;
2034 }
2035 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002036 if (mid != nritems &&
2037 leaf_space_used(l, mid, nritems - mid) +
2038 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2039 double_split = 1;
2040 }
Chris Masond4dbff92007-04-04 14:08:15 -04002041 }
2042 } else {
2043 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2044 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002045 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002046 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002047 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002048 wret = insert_ptr(trans, root, path,
2049 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002050 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002051 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002052 if (wret)
2053 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002054 free_extent_buffer(path->nodes[0]);
2055 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002056 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002057 if (path->slots[1] == 0) {
2058 wret = fixup_low_keys(trans, root,
2059 path, &disk_key, 1);
2060 if (wret)
2061 ret = wret;
2062 }
Chris Masond4dbff92007-04-04 14:08:15 -04002063 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002064 } else if (extend && slot == 0) {
2065 mid = 1;
2066 } else {
2067 mid = slot;
2068 if (mid != nritems &&
2069 leaf_space_used(l, mid, nritems - mid) +
2070 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2071 double_split = 1;
2072 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002073 }
Chris Masond4dbff92007-04-04 14:08:15 -04002074 }
2075 }
Chris Mason5f39d392007-10-15 16:14:19 -04002076 nritems = nritems - mid;
2077 btrfs_set_header_nritems(right, nritems);
2078 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2079
2080 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2081 btrfs_item_nr_offset(mid),
2082 nritems * sizeof(struct btrfs_item));
2083
2084 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002085 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2086 data_copy_size, btrfs_leaf_data(l) +
2087 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002088
Chris Mason5f39d392007-10-15 16:14:19 -04002089 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2090 btrfs_item_end_nr(l, mid);
2091
2092 for (i = 0; i < nritems; i++) {
2093 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002094 u32 ioff;
2095
2096 if (!right->map_token) {
2097 map_extent_buffer(right, (unsigned long)item,
2098 sizeof(struct btrfs_item),
2099 &right->map_token, &right->kaddr,
2100 &right->map_start, &right->map_len,
2101 KM_USER1);
2102 }
2103
2104 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002105 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002106 }
Chris Mason74123bd2007-02-02 11:05:29 -05002107
Chris Masondb945352007-10-15 16:15:53 -04002108 if (right->map_token) {
2109 unmap_extent_buffer(right, right->map_token, KM_USER1);
2110 right->map_token = NULL;
2111 }
2112
Chris Mason5f39d392007-10-15 16:14:19 -04002113 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002114 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002115 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002116 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2117 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002118 if (wret)
2119 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002120
2121 btrfs_mark_buffer_dirty(right);
2122 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002123 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002124
Chris Masonbe0e5c02007-01-26 15:51:26 -05002125 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002126 free_extent_buffer(path->nodes[0]);
2127 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002128 path->slots[0] -= mid;
2129 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002130 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002131 free_extent_buffer(right);
2132
Chris Masoneb60cea2007-02-02 09:18:22 -05002133 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002134
Chris Masoncc0c5532007-10-25 15:42:57 -04002135 if (double_split) {
2136 BUG_ON(num_doubles != 0);
2137 num_doubles++;
2138 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002139 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002140 return ret;
2141}
2142
Chris Masonb18c6682007-04-17 13:26:50 -04002143int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2144 struct btrfs_root *root,
2145 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002146 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002147{
2148 int ret = 0;
2149 int slot;
2150 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002151 struct extent_buffer *leaf;
2152 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002153 u32 nritems;
2154 unsigned int data_end;
2155 unsigned int old_data_start;
2156 unsigned int old_size;
2157 unsigned int size_diff;
2158 int i;
2159
2160 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002161 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002162 slot = path->slots[0];
2163
2164 old_size = btrfs_item_size_nr(leaf, slot);
2165 if (old_size == new_size)
2166 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002167
Chris Mason5f39d392007-10-15 16:14:19 -04002168 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002169 data_end = leaf_data_end(root, leaf);
2170
Chris Mason5f39d392007-10-15 16:14:19 -04002171 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002172
Chris Masonb18c6682007-04-17 13:26:50 -04002173 size_diff = old_size - new_size;
2174
2175 BUG_ON(slot < 0);
2176 BUG_ON(slot >= nritems);
2177
2178 /*
2179 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2180 */
2181 /* first correct the data pointers */
2182 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002183 u32 ioff;
2184 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002185
2186 if (!leaf->map_token) {
2187 map_extent_buffer(leaf, (unsigned long)item,
2188 sizeof(struct btrfs_item),
2189 &leaf->map_token, &leaf->kaddr,
2190 &leaf->map_start, &leaf->map_len,
2191 KM_USER1);
2192 }
2193
Chris Mason5f39d392007-10-15 16:14:19 -04002194 ioff = btrfs_item_offset(leaf, item);
2195 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002196 }
Chris Masondb945352007-10-15 16:15:53 -04002197
2198 if (leaf->map_token) {
2199 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2200 leaf->map_token = NULL;
2201 }
2202
Chris Masonb18c6682007-04-17 13:26:50 -04002203 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002204 if (from_end) {
2205 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2206 data_end + size_diff, btrfs_leaf_data(leaf) +
2207 data_end, old_data_start + new_size - data_end);
2208 } else {
2209 struct btrfs_disk_key disk_key;
2210 u64 offset;
2211
2212 btrfs_item_key(leaf, &disk_key, slot);
2213
2214 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2215 unsigned long ptr;
2216 struct btrfs_file_extent_item *fi;
2217
2218 fi = btrfs_item_ptr(leaf, slot,
2219 struct btrfs_file_extent_item);
2220 fi = (struct btrfs_file_extent_item *)(
2221 (unsigned long)fi - size_diff);
2222
2223 if (btrfs_file_extent_type(leaf, fi) ==
2224 BTRFS_FILE_EXTENT_INLINE) {
2225 ptr = btrfs_item_ptr_offset(leaf, slot);
2226 memmove_extent_buffer(leaf, ptr,
2227 (unsigned long)fi,
2228 offsetof(struct btrfs_file_extent_item,
2229 disk_bytenr));
2230 }
2231 }
2232
2233 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2234 data_end + size_diff, btrfs_leaf_data(leaf) +
2235 data_end, old_data_start - data_end);
2236
2237 offset = btrfs_disk_key_offset(&disk_key);
2238 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2239 btrfs_set_item_key(leaf, &disk_key, slot);
2240 if (slot == 0)
2241 fixup_low_keys(trans, root, path, &disk_key, 1);
2242 }
Chris Mason5f39d392007-10-15 16:14:19 -04002243
2244 item = btrfs_item_nr(leaf, slot);
2245 btrfs_set_item_size(leaf, item, new_size);
2246 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002247
2248 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002249 if (btrfs_leaf_free_space(root, leaf) < 0) {
2250 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002251 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002252 }
Chris Masonb18c6682007-04-17 13:26:50 -04002253 return ret;
2254}
2255
Chris Mason5f39d392007-10-15 16:14:19 -04002256int btrfs_extend_item(struct btrfs_trans_handle *trans,
2257 struct btrfs_root *root, struct btrfs_path *path,
2258 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002259{
2260 int ret = 0;
2261 int slot;
2262 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002263 struct extent_buffer *leaf;
2264 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002265 u32 nritems;
2266 unsigned int data_end;
2267 unsigned int old_data;
2268 unsigned int old_size;
2269 int i;
2270
2271 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002272 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002273
Chris Mason5f39d392007-10-15 16:14:19 -04002274 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002275 data_end = leaf_data_end(root, leaf);
2276
Chris Mason5f39d392007-10-15 16:14:19 -04002277 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2278 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002279 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002280 }
Chris Mason6567e832007-04-16 09:22:45 -04002281 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002282 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002283
2284 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002285 if (slot >= nritems) {
2286 btrfs_print_leaf(root, leaf);
2287 printk("slot %d too large, nritems %d\n", slot, nritems);
2288 BUG_ON(1);
2289 }
Chris Mason6567e832007-04-16 09:22:45 -04002290
2291 /*
2292 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2293 */
2294 /* first correct the data pointers */
2295 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002296 u32 ioff;
2297 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002298
2299 if (!leaf->map_token) {
2300 map_extent_buffer(leaf, (unsigned long)item,
2301 sizeof(struct btrfs_item),
2302 &leaf->map_token, &leaf->kaddr,
2303 &leaf->map_start, &leaf->map_len,
2304 KM_USER1);
2305 }
Chris Mason5f39d392007-10-15 16:14:19 -04002306 ioff = btrfs_item_offset(leaf, item);
2307 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002308 }
Chris Mason5f39d392007-10-15 16:14:19 -04002309
Chris Masondb945352007-10-15 16:15:53 -04002310 if (leaf->map_token) {
2311 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2312 leaf->map_token = NULL;
2313 }
2314
Chris Mason6567e832007-04-16 09:22:45 -04002315 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002316 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002317 data_end - data_size, btrfs_leaf_data(leaf) +
2318 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002319
Chris Mason6567e832007-04-16 09:22:45 -04002320 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002321 old_size = btrfs_item_size_nr(leaf, slot);
2322 item = btrfs_item_nr(leaf, slot);
2323 btrfs_set_item_size(leaf, item, old_size + data_size);
2324 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002325
2326 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002327 if (btrfs_leaf_free_space(root, leaf) < 0) {
2328 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002329 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002330 }
Chris Mason6567e832007-04-16 09:22:45 -04002331 return ret;
2332}
2333
Chris Mason74123bd2007-02-02 11:05:29 -05002334/*
2335 * Given a key and some data, insert an item into the tree.
2336 * This does all the path init required, making room in the tree if needed.
2337 */
Chris Mason9c583092008-01-29 15:15:18 -05002338int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002339 struct btrfs_root *root,
2340 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002341 struct btrfs_key *cpu_key, u32 *data_size,
2342 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002343{
Chris Mason5f39d392007-10-15 16:14:19 -04002344 struct extent_buffer *leaf;
2345 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002346 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002347 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002348 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002349 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002350 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002351 u32 total_size = 0;
2352 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002353 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002354 struct btrfs_disk_key disk_key;
2355
Chris Mason9c583092008-01-29 15:15:18 -05002356 for (i = 0; i < nr; i++) {
2357 total_data += data_size[i];
2358 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002359
Chris Mason74123bd2007-02-02 11:05:29 -05002360 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002361 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002362 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002363
Chris Mason9c583092008-01-29 15:15:18 -05002364 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2365 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002366 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002367 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002368 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002369 if (ret < 0)
2370 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002371
Chris Mason62e27492007-03-15 12:56:47 -04002372 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002373 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002374
Chris Mason5f39d392007-10-15 16:14:19 -04002375 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002376 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002377
Chris Mason123abc82007-03-14 14:14:43 -04002378 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002379 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002380 btrfs_print_leaf(root, leaf);
2381 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002382 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002383 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002384 }
Chris Mason5f39d392007-10-15 16:14:19 -04002385
Chris Mason62e27492007-03-15 12:56:47 -04002386 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002387 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002388
Chris Masonbe0e5c02007-01-26 15:51:26 -05002389 if (slot != nritems) {
2390 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002391 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002392
Chris Mason5f39d392007-10-15 16:14:19 -04002393 if (old_data < data_end) {
2394 btrfs_print_leaf(root, leaf);
2395 printk("slot %d old_data %d data_end %d\n",
2396 slot, old_data, data_end);
2397 BUG_ON(1);
2398 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002399 /*
2400 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2401 */
2402 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002403 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002404 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002405 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002406
Chris Mason5f39d392007-10-15 16:14:19 -04002407 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002408 if (!leaf->map_token) {
2409 map_extent_buffer(leaf, (unsigned long)item,
2410 sizeof(struct btrfs_item),
2411 &leaf->map_token, &leaf->kaddr,
2412 &leaf->map_start, &leaf->map_len,
2413 KM_USER1);
2414 }
2415
Chris Mason5f39d392007-10-15 16:14:19 -04002416 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002417 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002418 }
Chris Masondb945352007-10-15 16:15:53 -04002419 if (leaf->map_token) {
2420 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2421 leaf->map_token = NULL;
2422 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002423
2424 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002425 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002426 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002427 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002428
2429 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002430 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002431 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002432 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002433 data_end = old_data;
2434 }
Chris Mason5f39d392007-10-15 16:14:19 -04002435
Chris Mason62e27492007-03-15 12:56:47 -04002436 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002437 for (i = 0; i < nr; i++) {
2438 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2439 btrfs_set_item_key(leaf, &disk_key, slot + i);
2440 item = btrfs_item_nr(leaf, slot + i);
2441 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2442 data_end -= data_size[i];
2443 btrfs_set_item_size(leaf, item, data_size[i]);
2444 }
2445 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002446 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002447
2448 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002449 if (slot == 0) {
2450 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002451 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002452 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002453
Chris Mason5f39d392007-10-15 16:14:19 -04002454 if (btrfs_leaf_free_space(root, leaf) < 0) {
2455 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002456 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002457 }
Chris Mason5a01a2e2008-01-30 11:43:54 -05002458
Chris Masoned2ff2c2007-03-01 18:59:40 -05002459out:
Chris Mason62e27492007-03-15 12:56:47 -04002460 return ret;
2461}
2462
2463/*
2464 * Given a key and some data, insert an item into the tree.
2465 * This does all the path init required, making room in the tree if needed.
2466 */
Chris Masone089f052007-03-16 16:20:31 -04002467int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2468 *root, struct btrfs_key *cpu_key, void *data, u32
2469 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002470{
2471 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002472 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002473 struct extent_buffer *leaf;
2474 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002475
Chris Mason2c90e5d2007-04-02 10:50:19 -04002476 path = btrfs_alloc_path();
2477 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002478 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002479 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002480 leaf = path->nodes[0];
2481 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2482 write_extent_buffer(leaf, data, ptr, data_size);
2483 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002484 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002485 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002486 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002487}
2488
Chris Mason74123bd2007-02-02 11:05:29 -05002489/*
Chris Mason5de08d72007-02-24 06:24:44 -05002490 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002491 *
2492 * If the delete empties a node, the node is removed from the tree,
2493 * continuing all the way the root if required. The root is converted into
2494 * a leaf if all the nodes are emptied.
2495 */
Chris Masone089f052007-03-16 16:20:31 -04002496static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2497 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002498{
Chris Mason5f39d392007-10-15 16:14:19 -04002499 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002500 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002501 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002502 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002503
Chris Mason5f39d392007-10-15 16:14:19 -04002504 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002505 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002506 memmove_extent_buffer(parent,
2507 btrfs_node_key_ptr_offset(slot),
2508 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002509 sizeof(struct btrfs_key_ptr) *
2510 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002511 }
Chris Mason7518a232007-03-12 12:01:18 -04002512 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002513 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002514 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002515 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002516 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002517 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002518 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002519 struct btrfs_disk_key disk_key;
2520
2521 btrfs_node_key(parent, &disk_key, 0);
2522 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002523 if (wret)
2524 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002525 }
Chris Masond6025572007-03-30 14:27:56 -04002526 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002527 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002528}
2529
Chris Mason74123bd2007-02-02 11:05:29 -05002530/*
2531 * delete the item at the leaf level in path. If that empties
2532 * the leaf, remove it from the tree
2533 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002534int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2535 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002536{
Chris Mason5f39d392007-10-15 16:14:19 -04002537 struct extent_buffer *leaf;
2538 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002539 int last_off;
2540 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002541 int ret = 0;
2542 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002543 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002544 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002545
Chris Mason5f39d392007-10-15 16:14:19 -04002546 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002547 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2548
2549 for (i = 0; i < nr; i++)
2550 dsize += btrfs_item_size_nr(leaf, slot + i);
2551
Chris Mason5f39d392007-10-15 16:14:19 -04002552 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002553
Chris Mason85e21ba2008-01-29 15:11:36 -05002554 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002555 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002556 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002557
2558 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002559 data_end + dsize,
2560 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002561 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002562
Chris Mason85e21ba2008-01-29 15:11:36 -05002563 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002564 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002565
Chris Mason5f39d392007-10-15 16:14:19 -04002566 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002567 if (!leaf->map_token) {
2568 map_extent_buffer(leaf, (unsigned long)item,
2569 sizeof(struct btrfs_item),
2570 &leaf->map_token, &leaf->kaddr,
2571 &leaf->map_start, &leaf->map_len,
2572 KM_USER1);
2573 }
Chris Mason5f39d392007-10-15 16:14:19 -04002574 ioff = btrfs_item_offset(leaf, item);
2575 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002576 }
Chris Masondb945352007-10-15 16:15:53 -04002577
2578 if (leaf->map_token) {
2579 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2580 leaf->map_token = NULL;
2581 }
2582
Chris Mason5f39d392007-10-15 16:14:19 -04002583 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002584 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002585 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002586 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002587 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002588 btrfs_set_header_nritems(leaf, nritems - nr);
2589 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002590
Chris Mason74123bd2007-02-02 11:05:29 -05002591 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002592 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002593 if (leaf == root->node) {
2594 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002595 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002596 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002597 clean_tree_block(trans, root, leaf);
2598 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002599 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002600 if (wret)
2601 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002602 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002603 leaf->start, leaf->len,
2604 btrfs_header_owner(path->nodes[1]),
2605 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002606 if (wret)
2607 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002608 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002610 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002611 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002612 struct btrfs_disk_key disk_key;
2613
2614 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002615 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002616 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002617 if (wret)
2618 ret = wret;
2619 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002620
Chris Mason74123bd2007-02-02 11:05:29 -05002621 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002622 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002623 /* push_leaf_left fixes the path.
2624 * make sure the path still points to our leaf
2625 * for possible call to del_ptr below
2626 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002627 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002628 extent_buffer_get(leaf);
2629
Chris Mason85e21ba2008-01-29 15:11:36 -05002630 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002631 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002632 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002633
2634 if (path->nodes[0] == leaf &&
2635 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002636 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002637 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002638 ret = wret;
2639 }
Chris Mason5f39d392007-10-15 16:14:19 -04002640
2641 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002642 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002643 u64 bytenr = leaf->start;
2644 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002645
Chris Mason7bb86312007-12-11 09:25:06 -05002646 root_gen = btrfs_header_generation(
2647 path->nodes[1]);
2648
Chris Mason5f39d392007-10-15 16:14:19 -04002649 clean_tree_block(trans, root, leaf);
2650 wait_on_tree_block_writeback(root, leaf);
2651
Chris Masone089f052007-03-16 16:20:31 -04002652 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002653 if (wret)
2654 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002655
2656 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002657 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002658 blocksize,
2659 btrfs_header_owner(path->nodes[1]),
2660 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002661 if (wret)
2662 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002663 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002664 btrfs_mark_buffer_dirty(leaf);
2665 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002666 }
Chris Masond5719762007-03-23 10:01:08 -04002667 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002668 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002669 }
2670 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002671 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002672}
2673
Chris Mason97571fd2007-02-24 13:39:08 -05002674/*
Chris Mason7bb86312007-12-11 09:25:06 -05002675 * walk up the tree as far as required to find the previous leaf.
2676 * returns 0 if it found something or 1 if there are no lesser leaves.
2677 * returns < 0 on io errors.
2678 */
2679int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2680{
Chris Mason8f662a72008-01-02 10:01:11 -05002681 u64 bytenr;
Chris Mason7bb86312007-12-11 09:25:06 -05002682 int slot;
2683 int level = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002684 struct extent_buffer *c;
2685 struct extent_buffer *next = NULL;
2686
2687 while(level < BTRFS_MAX_LEVEL) {
2688 if (!path->nodes[level])
2689 return 1;
2690
2691 slot = path->slots[level];
2692 c = path->nodes[level];
2693 if (slot == 0) {
2694 level++;
2695 if (level == BTRFS_MAX_LEVEL)
2696 return 1;
2697 continue;
2698 }
2699 slot--;
2700
2701 bytenr = btrfs_node_blockptr(c, slot);
2702 if (next)
2703 free_extent_buffer(next);
2704
Chris Mason7bb86312007-12-11 09:25:06 -05002705 next = read_tree_block(root, bytenr,
2706 btrfs_level_size(root, level - 1));
2707 break;
2708 }
2709 path->slots[level] = slot;
2710 while(1) {
2711 level--;
2712 c = path->nodes[level];
2713 free_extent_buffer(c);
Chris Mason8f662a72008-01-02 10:01:11 -05002714 slot = btrfs_header_nritems(next);
2715 if (slot != 0)
2716 slot--;
Chris Mason7bb86312007-12-11 09:25:06 -05002717 path->nodes[level] = next;
Chris Mason8f662a72008-01-02 10:01:11 -05002718 path->slots[level] = slot;
Chris Mason7bb86312007-12-11 09:25:06 -05002719 if (!level)
2720 break;
Chris Mason8f662a72008-01-02 10:01:11 -05002721 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
Chris Mason7bb86312007-12-11 09:25:06 -05002722 btrfs_level_size(root, level - 1));
2723 }
2724 return 0;
2725}
2726
2727/*
Chris Mason97571fd2007-02-24 13:39:08 -05002728 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002729 * returns 0 if it found something or 1 if there are no greater leaves.
2730 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002731 */
Chris Mason234b63a2007-03-13 10:46:10 -04002732int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002733{
2734 int slot;
2735 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002736 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002737 struct extent_buffer *c;
2738 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002739
Chris Mason234b63a2007-03-13 10:46:10 -04002740 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002741 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002742 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002743
Chris Masond97e63b2007-02-20 16:40:44 -05002744 slot = path->slots[level] + 1;
2745 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002746 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002747 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002748 if (level == BTRFS_MAX_LEVEL)
2749 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002750 continue;
2751 }
Chris Mason5f39d392007-10-15 16:14:19 -04002752
Chris Masondb945352007-10-15 16:15:53 -04002753 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002754 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002755 free_extent_buffer(next);
2756
Chris Mason6702ed42007-08-07 16:15:09 -04002757 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002758 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002759
Chris Masondb945352007-10-15 16:15:53 -04002760 next = read_tree_block(root, bytenr,
2761 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002762 break;
2763 }
2764 path->slots[level] = slot;
2765 while(1) {
2766 level--;
2767 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002768 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002769 path->nodes[level] = next;
2770 path->slots[level] = 0;
2771 if (!level)
2772 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002773 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002774 reada_for_search(root, path, level, 0, 0);
Chris Masondb945352007-10-15 16:15:53 -04002775 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2776 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002777 }
2778 return 0;
2779}