blob: 6ba5394834bef19a172ba7e19776ecc4da42fa78 [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 Mason0b86a832008-03-24 15:01:56 -040073static void add_root_to_dirty_list(struct btrfs_root *root)
74{
75 if (root->track_dirty && list_empty(&root->dirty_list)) {
76 list_add(&root->dirty_list,
77 &root->fs_info->dirty_cowonly_roots);
78 }
79}
80
Chris Masonbe20aa92007-12-17 20:14:01 -050081int btrfs_copy_root(struct btrfs_trans_handle *trans,
82 struct btrfs_root *root,
83 struct extent_buffer *buf,
84 struct extent_buffer **cow_ret, u64 new_root_objectid)
85{
86 struct extent_buffer *cow;
87 u32 nritems;
88 int ret = 0;
89 int level;
90 struct btrfs_key first_key;
Chris Mason4aec2b52007-12-18 16:25:45 -050091 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -050092
Chris Mason4aec2b52007-12-18 16:25:45 -050093 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
94 if (!new_root)
95 return -ENOMEM;
96
97 memcpy(new_root, root, sizeof(*new_root));
98 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -050099
100 WARN_ON(root->ref_cows && trans->transid !=
101 root->fs_info->running_transaction->transid);
102 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
103
104 level = btrfs_header_level(buf);
105 nritems = btrfs_header_nritems(buf);
106 if (nritems) {
107 if (level == 0)
108 btrfs_item_key_to_cpu(buf, &first_key, 0);
109 else
110 btrfs_node_key_to_cpu(buf, &first_key, 0);
111 } else {
112 first_key.objectid = 0;
113 }
Chris Mason4aec2b52007-12-18 16:25:45 -0500114 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
Chris Masonbe20aa92007-12-17 20:14:01 -0500115 new_root_objectid,
116 trans->transid, first_key.objectid,
117 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500118 if (IS_ERR(cow)) {
119 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500120 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500121 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500122
123 copy_extent_buffer(cow, buf, 0, 0, cow->len);
124 btrfs_set_header_bytenr(cow, cow->start);
125 btrfs_set_header_generation(cow, trans->transid);
126 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400127 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500128
129 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Chris Mason4aec2b52007-12-18 16:25:45 -0500130 ret = btrfs_inc_ref(trans, new_root, buf);
131 kfree(new_root);
132
Chris Masonbe20aa92007-12-17 20:14:01 -0500133 if (ret)
134 return ret;
135
136 btrfs_mark_buffer_dirty(cow);
137 *cow_ret = cow;
138 return 0;
139}
140
141int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400142 struct btrfs_root *root,
143 struct extent_buffer *buf,
144 struct extent_buffer *parent, int parent_slot,
145 struct extent_buffer **cow_ret,
146 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400147{
Chris Mason7bb86312007-12-11 09:25:06 -0500148 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -0400149 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500150 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400151 int ret = 0;
152 int different_trans = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500153 int level;
154 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400155
Chris Mason7bb86312007-12-11 09:25:06 -0500156 if (root->ref_cows) {
157 root_gen = trans->transid;
158 } else {
159 root_gen = 0;
160 }
161
162 WARN_ON(root->ref_cows && trans->transid !=
163 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400164 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400165
Chris Mason7bb86312007-12-11 09:25:06 -0500166 level = btrfs_header_level(buf);
167 nritems = btrfs_header_nritems(buf);
168 if (nritems) {
169 if (level == 0)
170 btrfs_item_key_to_cpu(buf, &first_key, 0);
171 else
172 btrfs_node_key_to_cpu(buf, &first_key, 0);
173 } else {
174 first_key.objectid = 0;
175 }
176 cow = __btrfs_alloc_free_block(trans, root, buf->len,
177 root->root_key.objectid,
178 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400179 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400180 if (IS_ERR(cow))
181 return PTR_ERR(cow);
182
Chris Mason5f39d392007-10-15 16:14:19 -0400183 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400184 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400185 btrfs_set_header_generation(cow, trans->transid);
186 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400187 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400188
Chris Mason5f39d392007-10-15 16:14:19 -0400189 WARN_ON(btrfs_header_generation(buf) > trans->transid);
190 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400191 different_trans = 1;
192 ret = btrfs_inc_ref(trans, root, buf);
193 if (ret)
194 return ret;
195 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400196 clean_tree_block(trans, root, buf);
197 }
198
199 if (buf == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -0500200 root_gen = btrfs_header_generation(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400201 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400202 extent_buffer_get(cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400203 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400204 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500205 buf->len, root->root_key.objectid,
206 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400207 }
Chris Mason5f39d392007-10-15 16:14:19 -0400208 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400209 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400210 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500211 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400212 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400213 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500214 WARN_ON(trans->transid == 0);
215 btrfs_set_node_ptr_generation(parent, parent_slot,
216 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400217 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400218 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500219 btrfs_free_extent(trans, root, buf->start, buf->len,
220 btrfs_header_owner(parent), root_gen,
221 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400222 }
Chris Mason5f39d392007-10-15 16:14:19 -0400223 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400224 btrfs_mark_buffer_dirty(cow);
225 *cow_ret = cow;
226 return 0;
227}
228
Chris Mason5f39d392007-10-15 16:14:19 -0400229int btrfs_cow_block(struct btrfs_trans_handle *trans,
230 struct btrfs_root *root, struct extent_buffer *buf,
231 struct extent_buffer *parent, int parent_slot,
232 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500233{
Chris Mason6702ed42007-08-07 16:15:09 -0400234 u64 search_start;
Chris Masondc17ff82008-01-08 15:46:30 -0500235 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400236 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500237
Chris Masonccd467d2007-06-28 15:57:36 -0400238 if (trans->transaction != root->fs_info->running_transaction) {
239 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
240 root->fs_info->running_transaction->transid);
241 WARN_ON(1);
242 }
243 if (trans->transid != root->fs_info->generation) {
244 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
245 root->fs_info->generation);
246 WARN_ON(1);
247 }
Chris Masondc17ff82008-01-08 15:46:30 -0500248
249 header_trans = btrfs_header_generation(buf);
Chris Mason63b10fc2008-04-01 11:21:32 -0400250 spin_lock(&root->fs_info->hash_lock);
251 if (header_trans == trans->transid &&
252 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500253 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400254 spin_unlock(&root->fs_info->hash_lock);
Chris Mason02217ed2007-03-02 16:08:05 -0500255 return 0;
256 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400257 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400258 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400259 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400260 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400261 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400262}
263
Chris Mason6b800532007-10-15 16:17:34 -0400264static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400265{
Chris Mason6b800532007-10-15 16:17:34 -0400266 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400267 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400268 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400269 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500270 return 0;
271}
272
Chris Mason081e9572007-11-06 10:26:24 -0500273/*
274 * compare two keys in a memcmp fashion
275 */
276static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
277{
278 struct btrfs_key k1;
279
280 btrfs_disk_key_to_cpu(&k1, disk);
281
282 if (k1.objectid > k2->objectid)
283 return 1;
284 if (k1.objectid < k2->objectid)
285 return -1;
286 if (k1.type > k2->type)
287 return 1;
288 if (k1.type < k2->type)
289 return -1;
290 if (k1.offset > k2->offset)
291 return 1;
292 if (k1.offset < k2->offset)
293 return -1;
294 return 0;
295}
296
297
Chris Mason6702ed42007-08-07 16:15:09 -0400298int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400299 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400300 int start_slot, int cache_only, u64 *last_ret,
301 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400302{
Chris Mason6b800532007-10-15 16:17:34 -0400303 struct extent_buffer *cur;
304 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400305 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400306 u64 search_start = *last_ret;
307 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400308 u64 other;
309 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400310 int end_slot;
311 int i;
312 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400313 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400314 int uptodate;
315 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500316 int progress_passed = 0;
317 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400318
Chris Mason5708b952007-10-25 15:43:18 -0400319 parent_level = btrfs_header_level(parent);
320 if (cache_only && parent_level != 1)
321 return 0;
322
Chris Mason6702ed42007-08-07 16:15:09 -0400323 if (trans->transaction != root->fs_info->running_transaction) {
324 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
325 root->fs_info->running_transaction->transid);
326 WARN_ON(1);
327 }
328 if (trans->transid != root->fs_info->generation) {
329 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
330 root->fs_info->generation);
331 WARN_ON(1);
332 }
Chris Mason86479a02007-09-10 19:58:16 -0400333
Chris Mason6b800532007-10-15 16:17:34 -0400334 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400335 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400336 end_slot = parent_nritems;
337
338 if (parent_nritems == 1)
339 return 0;
340
341 for (i = start_slot; i < end_slot; i++) {
342 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400343
Chris Mason5708b952007-10-25 15:43:18 -0400344 if (!parent->map_token) {
345 map_extent_buffer(parent,
346 btrfs_node_key_ptr_offset(i),
347 sizeof(struct btrfs_key_ptr),
348 &parent->map_token, &parent->kaddr,
349 &parent->map_start, &parent->map_len,
350 KM_USER1);
351 }
Chris Mason081e9572007-11-06 10:26:24 -0500352 btrfs_node_key(parent, &disk_key, i);
353 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
354 continue;
355
356 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400357 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400358 if (last_block == 0)
359 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400360
Chris Mason6702ed42007-08-07 16:15:09 -0400361 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400362 other = btrfs_node_blockptr(parent, i - 1);
363 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400364 }
Chris Mason5708b952007-10-25 15:43:18 -0400365 if (close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400366 other = btrfs_node_blockptr(parent, i + 1);
367 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400368 }
Chris Masone9d0b132007-08-10 14:06:19 -0400369 if (close) {
370 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400371 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400372 }
Chris Mason5708b952007-10-25 15:43:18 -0400373 if (parent->map_token) {
374 unmap_extent_buffer(parent, parent->map_token,
375 KM_USER1);
376 parent->map_token = NULL;
377 }
Chris Mason6702ed42007-08-07 16:15:09 -0400378
Chris Mason6b800532007-10-15 16:17:34 -0400379 cur = btrfs_find_tree_block(root, blocknr, blocksize);
380 if (cur)
381 uptodate = btrfs_buffer_uptodate(cur);
382 else
383 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400384 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400385 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400386 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400387 continue;
388 }
Chris Mason6b800532007-10-15 16:17:34 -0400389 if (!cur) {
390 cur = read_tree_block(root, blocknr,
391 blocksize);
392 } else if (!uptodate) {
393 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400394 }
Chris Mason6702ed42007-08-07 16:15:09 -0400395 }
Chris Masone9d0b132007-08-10 14:06:19 -0400396 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400397 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400398
Chris Mason6b800532007-10-15 16:17:34 -0400399 err = __btrfs_cow_block(trans, root, cur, parent, i,
400 &tmp, search_start,
401 min(16 * blocksize,
402 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -0400403 if (err) {
Chris Mason6b800532007-10-15 16:17:34 -0400404 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400405 break;
Yan252c38f2007-08-29 09:11:44 -0400406 }
Chris Mason6b800532007-10-15 16:17:34 -0400407 search_start = tmp->start;
Chris Mason5708b952007-10-25 15:43:18 -0400408 last_block = tmp->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400409 *last_ret = search_start;
410 if (parent_level == 1)
Chris Mason6b800532007-10-15 16:17:34 -0400411 btrfs_clear_buffer_defrag(tmp);
Chris Mason6b800532007-10-15 16:17:34 -0400412 free_extent_buffer(tmp);
Chris Mason6702ed42007-08-07 16:15:09 -0400413 }
Chris Mason5708b952007-10-25 15:43:18 -0400414 if (parent->map_token) {
415 unmap_extent_buffer(parent, parent->map_token,
416 KM_USER1);
417 parent->map_token = NULL;
418 }
Chris Mason6702ed42007-08-07 16:15:09 -0400419 return err;
420}
421
Chris Mason74123bd2007-02-02 11:05:29 -0500422/*
423 * The leaf data grows from end-to-front in the node.
424 * this returns the address of the start of the last item,
425 * which is the stop of the leaf data stack
426 */
Chris Mason123abc82007-03-14 14:14:43 -0400427static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400428 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500429{
Chris Mason5f39d392007-10-15 16:14:19 -0400430 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500431 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400432 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400433 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500434}
435
Chris Mason123abc82007-03-14 14:14:43 -0400436static int check_node(struct btrfs_root *root, struct btrfs_path *path,
437 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500438{
Chris Mason5f39d392007-10-15 16:14:19 -0400439 struct extent_buffer *parent = NULL;
440 struct extent_buffer *node = path->nodes[level];
441 struct btrfs_disk_key parent_key;
442 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500443 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400444 int slot;
445 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400446 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500447
448 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400449 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400450
Chris Mason8d7be552007-05-10 11:24:42 -0400451 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400452 BUG_ON(nritems == 0);
453 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400454 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400455 btrfs_node_key(parent, &parent_key, parent_slot);
456 btrfs_node_key(node, &node_key, 0);
457 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400458 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400459 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400460 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500461 }
Chris Mason123abc82007-03-14 14:14:43 -0400462 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400463 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400464 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
465 btrfs_node_key(node, &node_key, slot);
466 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400467 }
468 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400469 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
470 btrfs_node_key(node, &node_key, slot);
471 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500472 }
473 return 0;
474}
475
Chris Mason123abc82007-03-14 14:14:43 -0400476static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
477 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500478{
Chris Mason5f39d392007-10-15 16:14:19 -0400479 struct extent_buffer *leaf = path->nodes[level];
480 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500481 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400482 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400483 struct btrfs_disk_key parent_key;
484 struct btrfs_disk_key leaf_key;
485 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400486
Chris Mason5f39d392007-10-15 16:14:19 -0400487 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500488
489 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400490 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400491
492 if (nritems == 0)
493 return 0;
494
495 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400496 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400497 btrfs_node_key(parent, &parent_key, parent_slot);
498 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400499
Chris Mason5f39d392007-10-15 16:14:19 -0400500 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400501 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400502 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400503 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500504 }
Chris Mason5f39d392007-10-15 16:14:19 -0400505#if 0
506 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
507 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
508 btrfs_item_key(leaf, &leaf_key, i);
509 if (comp_keys(&leaf_key, &cpukey) >= 0) {
510 btrfs_print_leaf(root, leaf);
511 printk("slot %d offset bad key\n", i);
512 BUG_ON(1);
513 }
514 if (btrfs_item_offset_nr(leaf, i) !=
515 btrfs_item_end_nr(leaf, i + 1)) {
516 btrfs_print_leaf(root, leaf);
517 printk("slot %d offset bad\n", i);
518 BUG_ON(1);
519 }
520 if (i == 0) {
521 if (btrfs_item_offset_nr(leaf, i) +
522 btrfs_item_size_nr(leaf, i) !=
523 BTRFS_LEAF_DATA_SIZE(root)) {
524 btrfs_print_leaf(root, leaf);
525 printk("slot %d first offset bad\n", i);
526 BUG_ON(1);
527 }
528 }
529 }
530 if (nritems > 0) {
531 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
532 btrfs_print_leaf(root, leaf);
533 printk("slot %d bad size \n", nritems - 1);
534 BUG_ON(1);
535 }
536 }
537#endif
538 if (slot != 0 && slot < nritems - 1) {
539 btrfs_item_key(leaf, &leaf_key, slot);
540 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
541 if (comp_keys(&leaf_key, &cpukey) <= 0) {
542 btrfs_print_leaf(root, leaf);
543 printk("slot %d offset bad key\n", slot);
544 BUG_ON(1);
545 }
546 if (btrfs_item_offset_nr(leaf, slot - 1) !=
547 btrfs_item_end_nr(leaf, slot)) {
548 btrfs_print_leaf(root, leaf);
549 printk("slot %d offset bad\n", slot);
550 BUG_ON(1);
551 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500552 }
Chris Mason8d7be552007-05-10 11:24:42 -0400553 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400554 btrfs_item_key(leaf, &leaf_key, slot);
555 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
556 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
557 if (btrfs_item_offset_nr(leaf, slot) !=
558 btrfs_item_end_nr(leaf, slot + 1)) {
559 btrfs_print_leaf(root, leaf);
560 printk("slot %d offset bad\n", slot);
561 BUG_ON(1);
562 }
Chris Mason8d7be552007-05-10 11:24:42 -0400563 }
Chris Mason5f39d392007-10-15 16:14:19 -0400564 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
565 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500566 return 0;
567}
568
Chris Mason98ed5172008-01-03 10:01:48 -0500569static int noinline check_block(struct btrfs_root *root,
570 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500571{
Chris Mason810191f2007-10-15 16:18:55 -0400572 return 0;
Chris Masondb945352007-10-15 16:15:53 -0400573#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400574 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400575
Chris Mason479965d2007-10-15 16:14:27 -0400576 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
577 (unsigned long)btrfs_header_fsid(buf),
578 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400579 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400580 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400581 }
Chris Masondb945352007-10-15 16:15:53 -0400582#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500583 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400584 return check_leaf(root, path, level);
585 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500586}
587
Chris Mason74123bd2007-02-02 11:05:29 -0500588/*
Chris Mason5f39d392007-10-15 16:14:19 -0400589 * search for key in the extent_buffer. The items start at offset p,
590 * and they are item_size apart. There are 'max' items in p.
591 *
Chris Mason74123bd2007-02-02 11:05:29 -0500592 * the slot in the array is returned via slot, and it points to
593 * the place where you would insert key if it is not found in
594 * the array.
595 *
596 * slot may point to max if the key is bigger than all of the keys
597 */
Chris Mason5f39d392007-10-15 16:14:19 -0400598static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
599 int item_size, struct btrfs_key *key,
600 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500601{
602 int low = 0;
603 int high = max;
604 int mid;
605 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400606 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400607 struct btrfs_disk_key unaligned;
608 unsigned long offset;
609 char *map_token = NULL;
610 char *kaddr = NULL;
611 unsigned long map_start = 0;
612 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400613 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500614
615 while(low < high) {
616 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400617 offset = p + mid * item_size;
618
619 if (!map_token || offset < map_start ||
620 (offset + sizeof(struct btrfs_disk_key)) >
621 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400622 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400623 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400624 map_token = NULL;
625 }
626 err = map_extent_buffer(eb, offset,
627 sizeof(struct btrfs_disk_key),
628 &map_token, &kaddr,
629 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400630
Chris Mason479965d2007-10-15 16:14:27 -0400631 if (!err) {
632 tmp = (struct btrfs_disk_key *)(kaddr + offset -
633 map_start);
634 } else {
635 read_extent_buffer(eb, &unaligned,
636 offset, sizeof(unaligned));
637 tmp = &unaligned;
638 }
639
Chris Mason5f39d392007-10-15 16:14:19 -0400640 } else {
641 tmp = (struct btrfs_disk_key *)(kaddr + offset -
642 map_start);
643 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500644 ret = comp_keys(tmp, key);
645
646 if (ret < 0)
647 low = mid + 1;
648 else if (ret > 0)
649 high = mid;
650 else {
651 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400652 if (map_token)
653 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500654 return 0;
655 }
656 }
657 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400658 if (map_token)
659 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500660 return 1;
661}
662
Chris Mason97571fd2007-02-24 13:39:08 -0500663/*
664 * simple bin_search frontend that does the right thing for
665 * leaves vs nodes
666 */
Chris Mason5f39d392007-10-15 16:14:19 -0400667static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
668 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500669{
Chris Mason5f39d392007-10-15 16:14:19 -0400670 if (level == 0) {
671 return generic_bin_search(eb,
672 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400673 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400674 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400675 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500676 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400677 return generic_bin_search(eb,
678 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400679 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400680 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400681 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500682 }
683 return -1;
684}
685
Chris Mason5f39d392007-10-15 16:14:19 -0400686static struct extent_buffer *read_node_slot(struct btrfs_root *root,
687 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500688{
Chris Masonbb803952007-03-01 12:04:21 -0500689 if (slot < 0)
690 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400691 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500692 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400693 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
694 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500695}
696
Chris Mason98ed5172008-01-03 10:01:48 -0500697static int balance_level(struct btrfs_trans_handle *trans,
698 struct btrfs_root *root,
699 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500700{
Chris Mason5f39d392007-10-15 16:14:19 -0400701 struct extent_buffer *right = NULL;
702 struct extent_buffer *mid;
703 struct extent_buffer *left = NULL;
704 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500705 int ret = 0;
706 int wret;
707 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500708 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400709 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500710 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500711
712 if (level == 0)
713 return 0;
714
Chris Mason5f39d392007-10-15 16:14:19 -0400715 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500716 WARN_ON(btrfs_header_generation(mid) != trans->transid);
717
Chris Mason1d4f8a02007-03-13 09:28:32 -0400718 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500719
Chris Mason234b63a2007-03-13 10:46:10 -0400720 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400721 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500722 pslot = path->slots[level + 1];
723
Chris Mason40689472007-03-17 14:29:23 -0400724 /*
725 * deal with the case where there is only one pointer in the root
726 * by promoting the node below to a root
727 */
Chris Mason5f39d392007-10-15 16:14:19 -0400728 if (!parent) {
729 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500730
Chris Mason5f39d392007-10-15 16:14:19 -0400731 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500732 return 0;
733
734 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400735 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500736 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500737 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
738 BUG_ON(ret);
739
Chris Masonbb803952007-03-01 12:04:21 -0500740 root->node = child;
Chris Mason0b86a832008-03-24 15:01:56 -0400741 add_root_to_dirty_list(root);
Chris Masonbb803952007-03-01 12:04:21 -0500742 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400743 clean_tree_block(trans, root, mid);
744 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500745 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400746 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500747 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
748 root->root_key.objectid,
749 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500750 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400751 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400752 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500753 }
Chris Mason5f39d392007-10-15 16:14:19 -0400754 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400755 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500756 return 0;
757
Chris Mason5f39d392007-10-15 16:14:19 -0400758 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400759 err_on_enospc = 1;
760
Chris Mason5f39d392007-10-15 16:14:19 -0400761 left = read_node_slot(root, parent, pslot - 1);
762 if (left) {
763 wret = btrfs_cow_block(trans, root, left,
764 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400765 if (wret) {
766 ret = wret;
767 goto enospc;
768 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400769 }
Chris Mason5f39d392007-10-15 16:14:19 -0400770 right = read_node_slot(root, parent, pslot + 1);
771 if (right) {
772 wret = btrfs_cow_block(trans, root, right,
773 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400774 if (wret) {
775 ret = wret;
776 goto enospc;
777 }
778 }
779
780 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400781 if (left) {
782 orig_slot += btrfs_header_nritems(left);
783 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500784 if (wret < 0)
785 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400786 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400787 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500788 }
Chris Mason79f95c82007-03-01 15:16:26 -0500789
790 /*
791 * then try to empty the right most buffer into the middle
792 */
Chris Mason5f39d392007-10-15 16:14:19 -0400793 if (right) {
794 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400795 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500796 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400797 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400798 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500799 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400800 u32 blocksize = right->len;
801
Chris Mason5f39d392007-10-15 16:14:19 -0400802 clean_tree_block(trans, root, right);
803 wait_on_tree_block_writeback(root, right);
804 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500805 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400806 wret = del_ptr(trans, root, path, level + 1, pslot +
807 1);
Chris Masonbb803952007-03-01 12:04:21 -0500808 if (wret)
809 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400810 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500811 blocksize,
812 btrfs_header_owner(parent),
813 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500814 if (wret)
815 ret = wret;
816 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400817 struct btrfs_disk_key right_key;
818 btrfs_node_key(right, &right_key, 0);
819 btrfs_set_node_key(parent, &right_key, pslot + 1);
820 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500821 }
822 }
Chris Mason5f39d392007-10-15 16:14:19 -0400823 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500824 /*
825 * we're not allowed to leave a node with one item in the
826 * tree during a delete. A deletion from lower in the tree
827 * could try to delete the only pointer in this node.
828 * So, pull some keys from the left.
829 * There has to be a left pointer at this point because
830 * otherwise we would have pulled some pointers from the
831 * right
832 */
Chris Mason5f39d392007-10-15 16:14:19 -0400833 BUG_ON(!left);
834 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400835 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500836 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400837 goto enospc;
838 }
Chris Mason79f95c82007-03-01 15:16:26 -0500839 BUG_ON(wret == 1);
840 }
Chris Mason5f39d392007-10-15 16:14:19 -0400841 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500842 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500843 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400844 u64 bytenr = mid->start;
845 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400846 clean_tree_block(trans, root, mid);
847 wait_on_tree_block_writeback(root, mid);
848 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500849 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400850 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500851 if (wret)
852 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500853 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
854 btrfs_header_owner(parent),
855 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500856 if (wret)
857 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500858 } else {
859 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400860 struct btrfs_disk_key mid_key;
861 btrfs_node_key(mid, &mid_key, 0);
862 btrfs_set_node_key(parent, &mid_key, pslot);
863 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500864 }
Chris Masonbb803952007-03-01 12:04:21 -0500865
Chris Mason79f95c82007-03-01 15:16:26 -0500866 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400867 if (left) {
868 if (btrfs_header_nritems(left) > orig_slot) {
869 extent_buffer_get(left);
870 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500871 path->slots[level + 1] -= 1;
872 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400873 if (mid)
874 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500875 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400876 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500877 path->slots[level] = orig_slot;
878 }
879 }
Chris Mason79f95c82007-03-01 15:16:26 -0500880 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400881 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400882 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400883 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500884 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400885enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400886 if (right)
887 free_extent_buffer(right);
888 if (left)
889 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500890 return ret;
891}
892
Chris Masone66f7092007-04-20 13:16:02 -0400893/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500894static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
895 struct btrfs_root *root,
896 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400897{
Chris Mason5f39d392007-10-15 16:14:19 -0400898 struct extent_buffer *right = NULL;
899 struct extent_buffer *mid;
900 struct extent_buffer *left = NULL;
901 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400902 int ret = 0;
903 int wret;
904 int pslot;
905 int orig_slot = path->slots[level];
906 u64 orig_ptr;
907
908 if (level == 0)
909 return 1;
910
Chris Mason5f39d392007-10-15 16:14:19 -0400911 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500912 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -0400913 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
914
915 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400916 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400917 pslot = path->slots[level + 1];
918
Chris Mason5f39d392007-10-15 16:14:19 -0400919 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400920 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400921
Chris Mason5f39d392007-10-15 16:14:19 -0400922 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400923
924 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400925 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400926 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400927 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400928 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
929 wret = 1;
930 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400931 ret = btrfs_cow_block(trans, root, left, parent,
932 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400933 if (ret)
934 wret = 1;
935 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400936 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400937 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400938 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400939 }
Chris Masone66f7092007-04-20 13:16:02 -0400940 if (wret < 0)
941 ret = wret;
942 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400943 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400944 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400945 btrfs_node_key(mid, &disk_key, 0);
946 btrfs_set_node_key(parent, &disk_key, pslot);
947 btrfs_mark_buffer_dirty(parent);
948 if (btrfs_header_nritems(left) > orig_slot) {
949 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400950 path->slots[level + 1] -= 1;
951 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400952 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400953 } else {
954 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400955 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400956 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400957 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400958 }
Chris Masone66f7092007-04-20 13:16:02 -0400959 return 0;
960 }
Chris Mason5f39d392007-10-15 16:14:19 -0400961 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400962 }
Chris Mason5f39d392007-10-15 16:14:19 -0400963 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400964
965 /*
966 * then try to empty the right most buffer into the middle
967 */
Chris Mason5f39d392007-10-15 16:14:19 -0400968 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400969 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400970 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400971 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
972 wret = 1;
973 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400974 ret = btrfs_cow_block(trans, root, right,
975 parent, pslot + 1,
976 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400977 if (ret)
978 wret = 1;
979 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400980 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400981 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400982 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400983 }
Chris Masone66f7092007-04-20 13:16:02 -0400984 if (wret < 0)
985 ret = wret;
986 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400987 struct btrfs_disk_key disk_key;
988
989 btrfs_node_key(right, &disk_key, 0);
990 btrfs_set_node_key(parent, &disk_key, pslot + 1);
991 btrfs_mark_buffer_dirty(parent);
992
993 if (btrfs_header_nritems(mid) <= orig_slot) {
994 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -0400995 path->slots[level + 1] += 1;
996 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -0400997 btrfs_header_nritems(mid);
998 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400999 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001000 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001001 }
Chris Masone66f7092007-04-20 13:16:02 -04001002 return 0;
1003 }
Chris Mason5f39d392007-10-15 16:14:19 -04001004 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001005 }
Chris Masone66f7092007-04-20 13:16:02 -04001006 return 1;
1007}
1008
Chris Mason74123bd2007-02-02 11:05:29 -05001009/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001010 * readahead one full node of leaves
1011 */
1012static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001013 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001014{
Chris Mason5f39d392007-10-15 16:14:19 -04001015 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001016 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001017 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001018 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001019 u64 lowest_read;
1020 u64 highest_read;
1021 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001022 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001023 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001024 u32 nr;
1025 u32 blocksize;
1026 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001027
Chris Masona6b6e752007-10-15 16:22:39 -04001028 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001029 return;
1030
Chris Mason6702ed42007-08-07 16:15:09 -04001031 if (!path->nodes[level])
1032 return;
1033
Chris Mason5f39d392007-10-15 16:14:19 -04001034 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -04001035 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001036 blocksize = btrfs_level_size(root, level - 1);
1037 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001038 if (eb) {
1039 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001040 return;
1041 }
1042
Chris Mason6b800532007-10-15 16:17:34 -04001043 highest_read = search;
1044 lowest_read = search;
1045
Chris Mason5f39d392007-10-15 16:14:19 -04001046 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001047 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001048 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001049 if (direction < 0) {
1050 if (nr == 0)
1051 break;
1052 nr--;
1053 } else if (direction > 0) {
1054 nr++;
1055 if (nr >= nritems)
1056 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001057 }
Chris Mason01f46652007-12-21 16:24:26 -05001058 if (path->reada < 0 && objectid) {
1059 btrfs_node_key(node, &disk_key, nr);
1060 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1061 break;
1062 }
Chris Mason6b800532007-10-15 16:17:34 -04001063 search = btrfs_node_blockptr(node, nr);
1064 if ((search >= lowest_read && search <= highest_read) ||
1065 (search < lowest_read && lowest_read - search <= 32768) ||
1066 (search > highest_read && search - highest_read <= 32768)) {
1067 readahead_tree_block(root, search, blocksize);
1068 nread += blocksize;
1069 }
1070 nscan++;
1071 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1072 break;
1073 if(nread > (1024 * 1024) || nscan > 128)
1074 break;
1075
1076 if (search < lowest_read)
1077 lowest_read = search;
1078 if (search > highest_read)
1079 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001080 }
1081}
1082/*
Chris Mason74123bd2007-02-02 11:05:29 -05001083 * look for key in the tree. path is filled in with nodes along the way
1084 * if key is found, we return zero and you can find the item in the leaf
1085 * level of the path (level 0)
1086 *
1087 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001088 * be inserted, and 1 is returned. If there are other errors during the
1089 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001090 *
1091 * if ins_len > 0, nodes and leaves will be split as we walk down the
1092 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1093 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001094 */
Chris Masone089f052007-03-16 16:20:31 -04001095int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1096 *root, struct btrfs_key *key, struct btrfs_path *p, int
1097 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001098{
Chris Mason5f39d392007-10-15 16:14:19 -04001099 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -04001100 u64 bytenr;
Chris Mason74493f72007-12-11 09:25:06 -05001101 u64 ptr_gen;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001102 int slot;
1103 int ret;
1104 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001105 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -04001106 u8 lowest_level = 0;
1107
Chris Mason6702ed42007-08-07 16:15:09 -04001108 lowest_level = p->lowest_level;
1109 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001110 WARN_ON(p->nodes[0] != NULL);
1111 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -05001112again:
1113 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001114 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001115 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001116 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001117 if (cow) {
1118 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001119 wret = btrfs_cow_block(trans, root, b,
1120 p->nodes[level + 1],
1121 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001122 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001123 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001124 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001125 return wret;
1126 }
Chris Mason02217ed2007-03-02 16:08:05 -05001127 }
1128 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001129 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001130 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001131 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001132 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001133 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001134 if (ret)
1135 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001136 ret = bin_search(b, key, level, &slot);
1137 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001138 if (ret && slot > 0)
1139 slot -= 1;
1140 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001141 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001142 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001143 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001144 BUG_ON(sret > 0);
1145 if (sret)
1146 return sret;
1147 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001148 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001149 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001150 int sret = balance_level(trans, root, p,
1151 level);
Chris Masonbb803952007-03-01 12:04:21 -05001152 if (sret)
1153 return sret;
1154 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001155 if (!b) {
1156 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001157 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001158 }
Chris Masonbb803952007-03-01 12:04:21 -05001159 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001160 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001161 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001162 /* this is only true while dropping a snapshot */
1163 if (level == lowest_level)
1164 break;
Chris Masondb945352007-10-15 16:15:53 -04001165 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason74493f72007-12-11 09:25:06 -05001166 ptr_gen = btrfs_node_ptr_generation(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001167 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001168 reada_for_search(root, p, level, slot,
1169 key->objectid);
Chris Masondb945352007-10-15 16:15:53 -04001170 b = read_tree_block(root, bytenr,
1171 btrfs_level_size(root, level - 1));
Chris Mason74493f72007-12-11 09:25:06 -05001172 if (ptr_gen != btrfs_header_generation(b)) {
1173 printk("block %llu bad gen wanted %llu "
1174 "found %llu\n",
1175 (unsigned long long)b->start,
1176 (unsigned long long)ptr_gen,
1177 (unsigned long long)btrfs_header_generation(b));
1178 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001179 } else {
1180 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001181 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001182 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001183 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001184 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001185 BUG_ON(sret > 0);
1186 if (sret)
1187 return sret;
1188 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001189 return ret;
1190 }
1191 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001192 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001193}
1194
Chris Mason74123bd2007-02-02 11:05:29 -05001195/*
1196 * adjust the pointers going up the tree, starting at level
1197 * making sure the right key of each node is points to 'key'.
1198 * This is used after shifting pointers to the left, so it stops
1199 * fixing up pointers when a given leaf/node is not in slot 0 of the
1200 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001201 *
1202 * If this fails to write a tree block, it returns -1, but continues
1203 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001204 */
Chris Mason5f39d392007-10-15 16:14:19 -04001205static int fixup_low_keys(struct btrfs_trans_handle *trans,
1206 struct btrfs_root *root, struct btrfs_path *path,
1207 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001208{
1209 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001210 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001211 struct extent_buffer *t;
1212
Chris Mason234b63a2007-03-13 10:46:10 -04001213 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001214 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001215 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001216 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001217 t = path->nodes[i];
1218 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001219 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001220 if (tslot != 0)
1221 break;
1222 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001223 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001224}
1225
Chris Mason74123bd2007-02-02 11:05:29 -05001226/*
1227 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001228 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001229 *
1230 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1231 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001232 */
Chris Mason98ed5172008-01-03 10:01:48 -05001233static int push_node_left(struct btrfs_trans_handle *trans,
1234 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason5f39d392007-10-15 16:14:19 -04001235 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001236{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001237 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001238 int src_nritems;
1239 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001240 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001241
Chris Mason5f39d392007-10-15 16:14:19 -04001242 src_nritems = btrfs_header_nritems(src);
1243 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001244 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001245 WARN_ON(btrfs_header_generation(src) != trans->transid);
1246 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001247
Chris Masoneb60cea2007-02-02 09:18:22 -05001248 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001249 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001250 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001251
1252 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001253 push_items = src_nritems;
1254
Chris Mason5f39d392007-10-15 16:14:19 -04001255 copy_extent_buffer(dst, src,
1256 btrfs_node_key_ptr_offset(dst_nritems),
1257 btrfs_node_key_ptr_offset(0),
1258 push_items * sizeof(struct btrfs_key_ptr));
1259
Chris Masonbb803952007-03-01 12:04:21 -05001260 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001261 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1262 btrfs_node_key_ptr_offset(push_items),
1263 (src_nritems - push_items) *
1264 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001265 }
Chris Mason5f39d392007-10-15 16:14:19 -04001266 btrfs_set_header_nritems(src, src_nritems - push_items);
1267 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1268 btrfs_mark_buffer_dirty(src);
1269 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001270 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001271}
1272
Chris Mason97571fd2007-02-24 13:39:08 -05001273/*
Chris Mason79f95c82007-03-01 15:16:26 -05001274 * try to push data from one node into the next node right in the
1275 * tree.
1276 *
1277 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1278 * error, and > 0 if there was no room in the right hand block.
1279 *
1280 * this will only push up to 1/2 the contents of the left node over
1281 */
Chris Mason5f39d392007-10-15 16:14:19 -04001282static int balance_node_right(struct btrfs_trans_handle *trans,
1283 struct btrfs_root *root,
1284 struct extent_buffer *dst,
1285 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001286{
Chris Mason79f95c82007-03-01 15:16:26 -05001287 int push_items = 0;
1288 int max_push;
1289 int src_nritems;
1290 int dst_nritems;
1291 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001292
Chris Mason7bb86312007-12-11 09:25:06 -05001293 WARN_ON(btrfs_header_generation(src) != trans->transid);
1294 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1295
Chris Mason5f39d392007-10-15 16:14:19 -04001296 src_nritems = btrfs_header_nritems(src);
1297 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001298 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001299 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001300 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001301
1302 max_push = src_nritems / 2 + 1;
1303 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001304 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001305 return 1;
Yan252c38f2007-08-29 09:11:44 -04001306
Chris Mason79f95c82007-03-01 15:16:26 -05001307 if (max_push < push_items)
1308 push_items = max_push;
1309
Chris Mason5f39d392007-10-15 16:14:19 -04001310 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1311 btrfs_node_key_ptr_offset(0),
1312 (dst_nritems) *
1313 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001314
Chris Mason5f39d392007-10-15 16:14:19 -04001315 copy_extent_buffer(dst, src,
1316 btrfs_node_key_ptr_offset(0),
1317 btrfs_node_key_ptr_offset(src_nritems - push_items),
1318 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001319
Chris Mason5f39d392007-10-15 16:14:19 -04001320 btrfs_set_header_nritems(src, src_nritems - push_items);
1321 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001322
Chris Mason5f39d392007-10-15 16:14:19 -04001323 btrfs_mark_buffer_dirty(src);
1324 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001325 return ret;
1326}
1327
1328/*
Chris Mason97571fd2007-02-24 13:39:08 -05001329 * helper function to insert a new root level in the tree.
1330 * A new node is allocated, and a single item is inserted to
1331 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001332 *
1333 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001334 */
Chris Mason98ed5172008-01-03 10:01:48 -05001335static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001336 struct btrfs_root *root,
1337 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001338{
Chris Mason7bb86312007-12-11 09:25:06 -05001339 u64 root_gen;
1340 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001341 struct extent_buffer *lower;
1342 struct extent_buffer *c;
1343 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001344
1345 BUG_ON(path->nodes[level]);
1346 BUG_ON(path->nodes[level-1] != root->node);
1347
Chris Mason7bb86312007-12-11 09:25:06 -05001348 if (root->ref_cows)
1349 root_gen = trans->transid;
1350 else
1351 root_gen = 0;
1352
1353 lower = path->nodes[level-1];
1354 if (level == 1)
1355 btrfs_item_key(lower, &lower_key, 0);
1356 else
1357 btrfs_node_key(lower, &lower_key, 0);
1358
1359 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1360 root->root_key.objectid,
1361 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001362 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001363 if (IS_ERR(c))
1364 return PTR_ERR(c);
1365 memset_extent_buffer(c, 0, 0, root->nodesize);
1366 btrfs_set_header_nritems(c, 1);
1367 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001368 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001369 btrfs_set_header_generation(c, trans->transid);
1370 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001371
Chris Mason5f39d392007-10-15 16:14:19 -04001372 write_extent_buffer(c, root->fs_info->fsid,
1373 (unsigned long)btrfs_header_fsid(c),
1374 BTRFS_FSID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001375 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001376 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001377 lower_gen = btrfs_header_generation(lower);
1378 WARN_ON(lower_gen == 0);
1379
1380 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001381
1382 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001383
Chris Mason5c680ed2007-02-22 11:39:13 -05001384 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001385 free_extent_buffer(root->node);
1386 root->node = c;
Chris Mason0b86a832008-03-24 15:01:56 -04001387 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001388 extent_buffer_get(c);
1389 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001390 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001391
1392 if (root->ref_cows && lower_gen != trans->transid) {
1393 struct btrfs_path *back_path = btrfs_alloc_path();
1394 int ret;
1395 ret = btrfs_insert_extent_backref(trans,
1396 root->fs_info->extent_root,
1397 path, lower->start,
1398 root->root_key.objectid,
1399 trans->transid, 0, 0);
1400 BUG_ON(ret);
1401 btrfs_free_path(back_path);
1402 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001403 return 0;
1404}
1405
Chris Mason74123bd2007-02-02 11:05:29 -05001406/*
1407 * worker function to insert a single pointer in a node.
1408 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001409 *
Chris Mason74123bd2007-02-02 11:05:29 -05001410 * slot and level indicate where you want the key to go, and
1411 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001412 *
1413 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001414 */
Chris Masone089f052007-03-16 16:20:31 -04001415static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1416 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001417 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001418{
Chris Mason5f39d392007-10-15 16:14:19 -04001419 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001420 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001421
1422 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001423 lower = path->nodes[level];
1424 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001425 if (slot > nritems)
1426 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001427 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001428 BUG();
1429 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001430 memmove_extent_buffer(lower,
1431 btrfs_node_key_ptr_offset(slot + 1),
1432 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001433 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001434 }
Chris Mason5f39d392007-10-15 16:14:19 -04001435 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001436 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001437 WARN_ON(trans->transid == 0);
1438 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001439 btrfs_set_header_nritems(lower, nritems + 1);
1440 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001441 return 0;
1442}
1443
Chris Mason97571fd2007-02-24 13:39:08 -05001444/*
1445 * split the node at the specified level in path in two.
1446 * The path is corrected to point to the appropriate node after the split
1447 *
1448 * Before splitting this tries to make some room in the node by pushing
1449 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001450 *
1451 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001452 */
Chris Masone089f052007-03-16 16:20:31 -04001453static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1454 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001455{
Chris Mason7bb86312007-12-11 09:25:06 -05001456 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001457 struct extent_buffer *c;
1458 struct extent_buffer *split;
1459 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001460 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001461 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001462 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001463 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001464
Chris Mason5f39d392007-10-15 16:14:19 -04001465 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001466 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001467 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001468 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001469 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001470 if (ret)
1471 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001472 } else {
1473 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001474 c = path->nodes[level];
1475 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001476 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1477 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001478 if (ret < 0)
1479 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001480 }
Chris Masone66f7092007-04-20 13:16:02 -04001481
Chris Mason5f39d392007-10-15 16:14:19 -04001482 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001483 if (root->ref_cows)
1484 root_gen = trans->transid;
1485 else
1486 root_gen = 0;
1487
1488 btrfs_node_key(c, &disk_key, 0);
1489 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1490 root->root_key.objectid,
1491 root_gen,
1492 btrfs_disk_key_objectid(&disk_key),
1493 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001494 if (IS_ERR(split))
1495 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001496
Chris Mason5f39d392007-10-15 16:14:19 -04001497 btrfs_set_header_flags(split, btrfs_header_flags(c));
1498 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001499 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001500 btrfs_set_header_generation(split, trans->transid);
1501 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001502 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001503 write_extent_buffer(split, root->fs_info->fsid,
1504 (unsigned long)btrfs_header_fsid(split),
1505 BTRFS_FSID_SIZE);
1506
Chris Mason7518a232007-03-12 12:01:18 -04001507 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001508
1509 copy_extent_buffer(split, c,
1510 btrfs_node_key_ptr_offset(0),
1511 btrfs_node_key_ptr_offset(mid),
1512 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1513 btrfs_set_header_nritems(split, c_nritems - mid);
1514 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001515 ret = 0;
1516
Chris Mason5f39d392007-10-15 16:14:19 -04001517 btrfs_mark_buffer_dirty(c);
1518 btrfs_mark_buffer_dirty(split);
1519
1520 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001521 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001522 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001523 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001524 if (wret)
1525 ret = wret;
1526
Chris Mason5de08d72007-02-24 06:24:44 -05001527 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001528 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001529 free_extent_buffer(c);
1530 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001531 path->slots[level + 1] += 1;
1532 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001533 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001534 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001535 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001536}
1537
Chris Mason74123bd2007-02-02 11:05:29 -05001538/*
1539 * how many bytes are required to store the items in a leaf. start
1540 * and nr indicate which items in the leaf to check. This totals up the
1541 * space used both by the item structs and the item data
1542 */
Chris Mason5f39d392007-10-15 16:14:19 -04001543static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001544{
1545 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001546 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001547 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001548
1549 if (!nr)
1550 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001551 data_len = btrfs_item_end_nr(l, start);
1552 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001553 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001554 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001555 return data_len;
1556}
1557
Chris Mason74123bd2007-02-02 11:05:29 -05001558/*
Chris Masond4dbff92007-04-04 14:08:15 -04001559 * The space between the end of the leaf items and
1560 * the start of the leaf data. IOW, how much room
1561 * the leaf has left for both items and data
1562 */
Chris Mason5f39d392007-10-15 16:14:19 -04001563int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001564{
Chris Mason5f39d392007-10-15 16:14:19 -04001565 int nritems = btrfs_header_nritems(leaf);
1566 int ret;
1567 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1568 if (ret < 0) {
1569 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001570 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001571 leaf_space_used(leaf, 0, nritems), nritems);
1572 }
1573 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001574}
1575
1576/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001577 * push some data in the path leaf to the right, trying to free up at
1578 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001579 *
1580 * returns 1 if the push failed because the other node didn't have enough
1581 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001582 */
Chris Masone089f052007-03-16 16:20:31 -04001583static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001584 *root, struct btrfs_path *path, int data_size,
1585 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001586{
Chris Mason5f39d392007-10-15 16:14:19 -04001587 struct extent_buffer *left = path->nodes[0];
1588 struct extent_buffer *right;
1589 struct extent_buffer *upper;
1590 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001591 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001592 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001593 int free_space;
1594 int push_space = 0;
1595 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001596 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001597 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001598 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001599 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001600 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001601 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001602 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001603
1604 slot = path->slots[1];
1605 if (!path->nodes[1]) {
1606 return 1;
1607 }
1608 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001609 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001610 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001611
Chris Masondb945352007-10-15 16:15:53 -04001612 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1613 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001614 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001615 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001616 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001617 return 1;
1618 }
1619
Chris Mason5f39d392007-10-15 16:14:19 -04001620 /* cow and double check */
1621 ret = btrfs_cow_block(trans, root, right, upper,
1622 slot + 1, &right);
1623 if (ret) {
1624 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001625 return 1;
1626 }
Chris Mason5f39d392007-10-15 16:14:19 -04001627 free_space = btrfs_leaf_free_space(root, right);
1628 if (free_space < data_size + sizeof(struct btrfs_item)) {
1629 free_extent_buffer(right);
1630 return 1;
1631 }
1632
1633 left_nritems = btrfs_header_nritems(left);
1634 if (left_nritems == 0) {
1635 free_extent_buffer(right);
1636 return 1;
1637 }
1638
Chris Mason34a38212007-11-07 13:31:03 -05001639 if (empty)
1640 nr = 0;
1641 else
1642 nr = 1;
1643
1644 i = left_nritems - 1;
1645 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001646 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001647
Chris Mason00ec4c52007-02-24 12:47:20 -05001648 if (path->slots[0] == i)
1649 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001650
1651 if (!left->map_token) {
1652 map_extent_buffer(left, (unsigned long)item,
1653 sizeof(struct btrfs_item),
1654 &left->map_token, &left->kaddr,
1655 &left->map_start, &left->map_len,
1656 KM_USER1);
1657 }
1658
1659 this_item_size = btrfs_item_size(left, item);
1660 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001661 break;
1662 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001663 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001664 if (i == 0)
1665 break;
1666 i--;
Chris Masondb945352007-10-15 16:15:53 -04001667 }
1668 if (left->map_token) {
1669 unmap_extent_buffer(left, left->map_token, KM_USER1);
1670 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001671 }
Chris Mason5f39d392007-10-15 16:14:19 -04001672
Chris Mason00ec4c52007-02-24 12:47:20 -05001673 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001674 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001675 return 1;
1676 }
Chris Mason5f39d392007-10-15 16:14:19 -04001677
Chris Mason34a38212007-11-07 13:31:03 -05001678 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001679 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001680
Chris Mason00ec4c52007-02-24 12:47:20 -05001681 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001682 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001683
Chris Mason5f39d392007-10-15 16:14:19 -04001684 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001685 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001686
Chris Mason00ec4c52007-02-24 12:47:20 -05001687 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001688 data_end = leaf_data_end(root, right);
1689 memmove_extent_buffer(right,
1690 btrfs_leaf_data(right) + data_end - push_space,
1691 btrfs_leaf_data(right) + data_end,
1692 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1693
Chris Mason00ec4c52007-02-24 12:47:20 -05001694 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001695 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001696 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1697 btrfs_leaf_data(left) + leaf_data_end(root, left),
1698 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001699
1700 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1701 btrfs_item_nr_offset(0),
1702 right_nritems * sizeof(struct btrfs_item));
1703
Chris Mason00ec4c52007-02-24 12:47:20 -05001704 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001705 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1706 btrfs_item_nr_offset(left_nritems - push_items),
1707 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001708
1709 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001710 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001711 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001712 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001713 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001714 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001715 if (!right->map_token) {
1716 map_extent_buffer(right, (unsigned long)item,
1717 sizeof(struct btrfs_item),
1718 &right->map_token, &right->kaddr,
1719 &right->map_start, &right->map_len,
1720 KM_USER1);
1721 }
1722 push_space -= btrfs_item_size(right, item);
1723 btrfs_set_item_offset(right, item, push_space);
1724 }
1725
1726 if (right->map_token) {
1727 unmap_extent_buffer(right, right->map_token, KM_USER1);
1728 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001729 }
Chris Mason7518a232007-03-12 12:01:18 -04001730 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001731 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001732
Chris Mason34a38212007-11-07 13:31:03 -05001733 if (left_nritems)
1734 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001735 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001736
Chris Mason5f39d392007-10-15 16:14:19 -04001737 btrfs_item_key(right, &disk_key, 0);
1738 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001739 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001740
Chris Mason00ec4c52007-02-24 12:47:20 -05001741 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001742 if (path->slots[0] >= left_nritems) {
1743 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001744 free_extent_buffer(path->nodes[0]);
1745 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001746 path->slots[1] += 1;
1747 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001748 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001749 }
1750 return 0;
1751}
1752/*
Chris Mason74123bd2007-02-02 11:05:29 -05001753 * push some data in the path leaf to the left, trying to free up at
1754 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1755 */
Chris Masone089f052007-03-16 16:20:31 -04001756static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001757 *root, struct btrfs_path *path, int data_size,
1758 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001759{
Chris Mason5f39d392007-10-15 16:14:19 -04001760 struct btrfs_disk_key disk_key;
1761 struct extent_buffer *right = path->nodes[0];
1762 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001763 int slot;
1764 int i;
1765 int free_space;
1766 int push_space = 0;
1767 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001768 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001769 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001770 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001771 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001772 int ret = 0;
1773 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001774 u32 this_item_size;
1775 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001776
1777 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001778 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001779 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001780 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001781 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001782
Chris Mason3685f792007-10-19 09:23:27 -04001783 right_nritems = btrfs_header_nritems(right);
1784 if (right_nritems == 0) {
1785 return 1;
1786 }
1787
Chris Mason5f39d392007-10-15 16:14:19 -04001788 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001789 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001790 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001791 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001792 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001793 return 1;
1794 }
Chris Mason02217ed2007-03-02 16:08:05 -05001795
1796 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001797 ret = btrfs_cow_block(trans, root, left,
1798 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001799 if (ret) {
1800 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001801 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001802 return 1;
1803 }
Chris Mason3685f792007-10-19 09:23:27 -04001804
Chris Mason123abc82007-03-14 14:14:43 -04001805 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001806 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001807 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001808 return 1;
1809 }
1810
Chris Mason34a38212007-11-07 13:31:03 -05001811 if (empty)
1812 nr = right_nritems;
1813 else
1814 nr = right_nritems - 1;
1815
1816 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001817 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001818 if (!right->map_token) {
1819 map_extent_buffer(right, (unsigned long)item,
1820 sizeof(struct btrfs_item),
1821 &right->map_token, &right->kaddr,
1822 &right->map_start, &right->map_len,
1823 KM_USER1);
1824 }
1825
Chris Masonbe0e5c02007-01-26 15:51:26 -05001826 if (path->slots[0] == i)
1827 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001828
1829 this_item_size = btrfs_item_size(right, item);
1830 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001831 break;
Chris Masondb945352007-10-15 16:15:53 -04001832
Chris Masonbe0e5c02007-01-26 15:51:26 -05001833 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001834 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001835 }
Chris Masondb945352007-10-15 16:15:53 -04001836
1837 if (right->map_token) {
1838 unmap_extent_buffer(right, right->map_token, KM_USER1);
1839 right->map_token = NULL;
1840 }
1841
Chris Masonbe0e5c02007-01-26 15:51:26 -05001842 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001843 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001844 return 1;
1845 }
Chris Mason34a38212007-11-07 13:31:03 -05001846 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001847 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001848
Chris Masonbe0e5c02007-01-26 15:51:26 -05001849 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001850 copy_extent_buffer(left, right,
1851 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1852 btrfs_item_nr_offset(0),
1853 push_items * sizeof(struct btrfs_item));
1854
Chris Mason123abc82007-03-14 14:14:43 -04001855 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001856 btrfs_item_offset_nr(right, push_items -1);
1857
1858 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001859 leaf_data_end(root, left) - push_space,
1860 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001861 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001862 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001863 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001864 BUG_ON(old_left_nritems < 0);
1865
Chris Masondb945352007-10-15 16:15:53 -04001866 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001867 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001868 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001869
Chris Mason5f39d392007-10-15 16:14:19 -04001870 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001871 if (!left->map_token) {
1872 map_extent_buffer(left, (unsigned long)item,
1873 sizeof(struct btrfs_item),
1874 &left->map_token, &left->kaddr,
1875 &left->map_start, &left->map_len,
1876 KM_USER1);
1877 }
1878
Chris Mason5f39d392007-10-15 16:14:19 -04001879 ioff = btrfs_item_offset(left, item);
1880 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001881 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001882 }
Chris Mason5f39d392007-10-15 16:14:19 -04001883 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001884 if (left->map_token) {
1885 unmap_extent_buffer(left, left->map_token, KM_USER1);
1886 left->map_token = NULL;
1887 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001888
1889 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001890 if (push_items > right_nritems) {
1891 printk("push items %d nr %u\n", push_items, right_nritems);
1892 WARN_ON(1);
1893 }
Chris Mason5f39d392007-10-15 16:14:19 -04001894
Chris Mason34a38212007-11-07 13:31:03 -05001895 if (push_items < right_nritems) {
1896 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1897 leaf_data_end(root, right);
1898 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1899 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1900 btrfs_leaf_data(right) +
1901 leaf_data_end(root, right), push_space);
1902
1903 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001904 btrfs_item_nr_offset(push_items),
1905 (btrfs_header_nritems(right) - push_items) *
1906 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001907 }
Yaneef1c492007-11-26 10:58:13 -05001908 right_nritems -= push_items;
1909 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001910 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001911 for (i = 0; i < right_nritems; i++) {
1912 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001913
1914 if (!right->map_token) {
1915 map_extent_buffer(right, (unsigned long)item,
1916 sizeof(struct btrfs_item),
1917 &right->map_token, &right->kaddr,
1918 &right->map_start, &right->map_len,
1919 KM_USER1);
1920 }
1921
1922 push_space = push_space - btrfs_item_size(right, item);
1923 btrfs_set_item_offset(right, item, push_space);
1924 }
1925 if (right->map_token) {
1926 unmap_extent_buffer(right, right->map_token, KM_USER1);
1927 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001928 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001929
Chris Mason5f39d392007-10-15 16:14:19 -04001930 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001931 if (right_nritems)
1932 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001933
Chris Mason5f39d392007-10-15 16:14:19 -04001934 btrfs_item_key(right, &disk_key, 0);
1935 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001936 if (wret)
1937 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938
1939 /* then fixup the leaf pointer in the path */
1940 if (path->slots[0] < push_items) {
1941 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001942 free_extent_buffer(path->nodes[0]);
1943 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001944 path->slots[1] -= 1;
1945 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001946 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001947 path->slots[0] -= push_items;
1948 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001949 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001950 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001951}
1952
Chris Mason74123bd2007-02-02 11:05:29 -05001953/*
1954 * split the path's leaf in two, making sure there is at least data_size
1955 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001956 *
1957 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001958 */
Chris Masone089f052007-03-16 16:20:31 -04001959static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001960 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001961 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001962{
Chris Mason7bb86312007-12-11 09:25:06 -05001963 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001964 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001965 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001966 int mid;
1967 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001968 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001969 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001970 int data_copy_size;
1971 int rt_data_off;
1972 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001973 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001974 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001975 int double_split;
1976 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001977 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001978
Chris Masoncc0c5532007-10-25 15:42:57 -04001979 if (extend)
1980 space_needed = data_size;
1981
Chris Mason7bb86312007-12-11 09:25:06 -05001982 if (root->ref_cows)
1983 root_gen = trans->transid;
1984 else
1985 root_gen = 0;
1986
Chris Mason40689472007-03-17 14:29:23 -04001987 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04001988 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05001989 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001990 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04001991 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04001992 }
1993 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05001994 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04001995 if (wret < 0)
1996 return wret;
1997 }
1998 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05001999
Chris Mason3685f792007-10-19 09:23:27 -04002000 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002001 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002002 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002003 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002004
Chris Mason5c680ed2007-02-22 11:39:13 -05002005 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002006 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002007 if (ret)
2008 return ret;
2009 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002010again:
2011 double_split = 0;
2012 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002013 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002014 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002015 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002016
Chris Mason7bb86312007-12-11 09:25:06 -05002017 btrfs_item_key(l, &disk_key, 0);
2018
2019 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2020 root->root_key.objectid,
2021 root_gen, disk_key.objectid, 0,
2022 l->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002023 if (IS_ERR(right))
2024 return PTR_ERR(right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002025
Chris Mason5f39d392007-10-15 16:14:19 -04002026 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002027 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002028 btrfs_set_header_generation(right, trans->transid);
2029 btrfs_set_header_owner(right, root->root_key.objectid);
2030 btrfs_set_header_level(right, 0);
2031 write_extent_buffer(right, root->fs_info->fsid,
2032 (unsigned long)btrfs_header_fsid(right),
2033 BTRFS_FSID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002034 if (mid <= slot) {
2035 if (nritems == 1 ||
2036 leaf_space_used(l, mid, nritems - mid) + space_needed >
2037 BTRFS_LEAF_DATA_SIZE(root)) {
2038 if (slot >= nritems) {
2039 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002040 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002041 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002042 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002043 path->slots[1] + 1, 1);
2044 if (wret)
2045 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002046 free_extent_buffer(path->nodes[0]);
2047 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002048 path->slots[0] = 0;
2049 path->slots[1] += 1;
2050 return ret;
2051 }
2052 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002053 if (mid != nritems &&
2054 leaf_space_used(l, mid, nritems - mid) +
2055 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2056 double_split = 1;
2057 }
Chris Masond4dbff92007-04-04 14:08:15 -04002058 }
2059 } else {
2060 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2061 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002062 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002063 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002064 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002065 wret = insert_ptr(trans, root, path,
2066 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002067 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002068 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002069 if (wret)
2070 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002071 free_extent_buffer(path->nodes[0]);
2072 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002073 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002074 if (path->slots[1] == 0) {
2075 wret = fixup_low_keys(trans, root,
2076 path, &disk_key, 1);
2077 if (wret)
2078 ret = wret;
2079 }
Chris Masond4dbff92007-04-04 14:08:15 -04002080 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002081 } else if (extend && slot == 0) {
2082 mid = 1;
2083 } else {
2084 mid = slot;
2085 if (mid != nritems &&
2086 leaf_space_used(l, mid, nritems - mid) +
2087 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2088 double_split = 1;
2089 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002090 }
Chris Masond4dbff92007-04-04 14:08:15 -04002091 }
2092 }
Chris Mason5f39d392007-10-15 16:14:19 -04002093 nritems = nritems - mid;
2094 btrfs_set_header_nritems(right, nritems);
2095 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2096
2097 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2098 btrfs_item_nr_offset(mid),
2099 nritems * sizeof(struct btrfs_item));
2100
2101 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002102 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2103 data_copy_size, btrfs_leaf_data(l) +
2104 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002105
Chris Mason5f39d392007-10-15 16:14:19 -04002106 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2107 btrfs_item_end_nr(l, mid);
2108
2109 for (i = 0; i < nritems; i++) {
2110 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002111 u32 ioff;
2112
2113 if (!right->map_token) {
2114 map_extent_buffer(right, (unsigned long)item,
2115 sizeof(struct btrfs_item),
2116 &right->map_token, &right->kaddr,
2117 &right->map_start, &right->map_len,
2118 KM_USER1);
2119 }
2120
2121 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002122 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002123 }
Chris Mason74123bd2007-02-02 11:05:29 -05002124
Chris Masondb945352007-10-15 16:15:53 -04002125 if (right->map_token) {
2126 unmap_extent_buffer(right, right->map_token, KM_USER1);
2127 right->map_token = NULL;
2128 }
2129
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002131 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002132 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002133 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2134 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002135 if (wret)
2136 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002137
2138 btrfs_mark_buffer_dirty(right);
2139 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002140 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002141
Chris Masonbe0e5c02007-01-26 15:51:26 -05002142 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002143 free_extent_buffer(path->nodes[0]);
2144 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002145 path->slots[0] -= mid;
2146 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002147 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002148 free_extent_buffer(right);
2149
Chris Masoneb60cea2007-02-02 09:18:22 -05002150 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002151
Chris Masoncc0c5532007-10-25 15:42:57 -04002152 if (double_split) {
2153 BUG_ON(num_doubles != 0);
2154 num_doubles++;
2155 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002156 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002157 return ret;
2158}
2159
Chris Masonb18c6682007-04-17 13:26:50 -04002160int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2161 struct btrfs_root *root,
2162 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002163 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002164{
2165 int ret = 0;
2166 int slot;
2167 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002168 struct extent_buffer *leaf;
2169 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002170 u32 nritems;
2171 unsigned int data_end;
2172 unsigned int old_data_start;
2173 unsigned int old_size;
2174 unsigned int size_diff;
2175 int i;
2176
2177 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002178 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002179 slot = path->slots[0];
2180
2181 old_size = btrfs_item_size_nr(leaf, slot);
2182 if (old_size == new_size)
2183 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002184
Chris Mason5f39d392007-10-15 16:14:19 -04002185 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002186 data_end = leaf_data_end(root, leaf);
2187
Chris Mason5f39d392007-10-15 16:14:19 -04002188 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002189
Chris Masonb18c6682007-04-17 13:26:50 -04002190 size_diff = old_size - new_size;
2191
2192 BUG_ON(slot < 0);
2193 BUG_ON(slot >= nritems);
2194
2195 /*
2196 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2197 */
2198 /* first correct the data pointers */
2199 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002200 u32 ioff;
2201 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002202
2203 if (!leaf->map_token) {
2204 map_extent_buffer(leaf, (unsigned long)item,
2205 sizeof(struct btrfs_item),
2206 &leaf->map_token, &leaf->kaddr,
2207 &leaf->map_start, &leaf->map_len,
2208 KM_USER1);
2209 }
2210
Chris Mason5f39d392007-10-15 16:14:19 -04002211 ioff = btrfs_item_offset(leaf, item);
2212 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002213 }
Chris Masondb945352007-10-15 16:15:53 -04002214
2215 if (leaf->map_token) {
2216 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2217 leaf->map_token = NULL;
2218 }
2219
Chris Masonb18c6682007-04-17 13:26:50 -04002220 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002221 if (from_end) {
2222 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2223 data_end + size_diff, btrfs_leaf_data(leaf) +
2224 data_end, old_data_start + new_size - data_end);
2225 } else {
2226 struct btrfs_disk_key disk_key;
2227 u64 offset;
2228
2229 btrfs_item_key(leaf, &disk_key, slot);
2230
2231 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2232 unsigned long ptr;
2233 struct btrfs_file_extent_item *fi;
2234
2235 fi = btrfs_item_ptr(leaf, slot,
2236 struct btrfs_file_extent_item);
2237 fi = (struct btrfs_file_extent_item *)(
2238 (unsigned long)fi - size_diff);
2239
2240 if (btrfs_file_extent_type(leaf, fi) ==
2241 BTRFS_FILE_EXTENT_INLINE) {
2242 ptr = btrfs_item_ptr_offset(leaf, slot);
2243 memmove_extent_buffer(leaf, ptr,
2244 (unsigned long)fi,
2245 offsetof(struct btrfs_file_extent_item,
2246 disk_bytenr));
2247 }
2248 }
2249
2250 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2251 data_end + size_diff, btrfs_leaf_data(leaf) +
2252 data_end, old_data_start - data_end);
2253
2254 offset = btrfs_disk_key_offset(&disk_key);
2255 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2256 btrfs_set_item_key(leaf, &disk_key, slot);
2257 if (slot == 0)
2258 fixup_low_keys(trans, root, path, &disk_key, 1);
2259 }
Chris Mason5f39d392007-10-15 16:14:19 -04002260
2261 item = btrfs_item_nr(leaf, slot);
2262 btrfs_set_item_size(leaf, item, new_size);
2263 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002264
2265 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002266 if (btrfs_leaf_free_space(root, leaf) < 0) {
2267 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002268 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002269 }
Chris Masonb18c6682007-04-17 13:26:50 -04002270 return ret;
2271}
2272
Chris Mason5f39d392007-10-15 16:14:19 -04002273int btrfs_extend_item(struct btrfs_trans_handle *trans,
2274 struct btrfs_root *root, struct btrfs_path *path,
2275 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002276{
2277 int ret = 0;
2278 int slot;
2279 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002280 struct extent_buffer *leaf;
2281 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002282 u32 nritems;
2283 unsigned int data_end;
2284 unsigned int old_data;
2285 unsigned int old_size;
2286 int i;
2287
2288 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002289 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002290
Chris Mason5f39d392007-10-15 16:14:19 -04002291 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002292 data_end = leaf_data_end(root, leaf);
2293
Chris Mason5f39d392007-10-15 16:14:19 -04002294 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2295 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002296 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002297 }
Chris Mason6567e832007-04-16 09:22:45 -04002298 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002299 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002300
2301 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002302 if (slot >= nritems) {
2303 btrfs_print_leaf(root, leaf);
2304 printk("slot %d too large, nritems %d\n", slot, nritems);
2305 BUG_ON(1);
2306 }
Chris Mason6567e832007-04-16 09:22:45 -04002307
2308 /*
2309 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2310 */
2311 /* first correct the data pointers */
2312 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002313 u32 ioff;
2314 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002315
2316 if (!leaf->map_token) {
2317 map_extent_buffer(leaf, (unsigned long)item,
2318 sizeof(struct btrfs_item),
2319 &leaf->map_token, &leaf->kaddr,
2320 &leaf->map_start, &leaf->map_len,
2321 KM_USER1);
2322 }
Chris Mason5f39d392007-10-15 16:14:19 -04002323 ioff = btrfs_item_offset(leaf, item);
2324 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002325 }
Chris Mason5f39d392007-10-15 16:14:19 -04002326
Chris Masondb945352007-10-15 16:15:53 -04002327 if (leaf->map_token) {
2328 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2329 leaf->map_token = NULL;
2330 }
2331
Chris Mason6567e832007-04-16 09:22:45 -04002332 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002333 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002334 data_end - data_size, btrfs_leaf_data(leaf) +
2335 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002336
Chris Mason6567e832007-04-16 09:22:45 -04002337 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002338 old_size = btrfs_item_size_nr(leaf, slot);
2339 item = btrfs_item_nr(leaf, slot);
2340 btrfs_set_item_size(leaf, item, old_size + data_size);
2341 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002342
2343 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002344 if (btrfs_leaf_free_space(root, leaf) < 0) {
2345 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002346 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002347 }
Chris Mason6567e832007-04-16 09:22:45 -04002348 return ret;
2349}
2350
Chris Mason74123bd2007-02-02 11:05:29 -05002351/*
2352 * Given a key and some data, insert an item into the tree.
2353 * This does all the path init required, making room in the tree if needed.
2354 */
Chris Mason9c583092008-01-29 15:15:18 -05002355int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002356 struct btrfs_root *root,
2357 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002358 struct btrfs_key *cpu_key, u32 *data_size,
2359 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002360{
Chris Mason5f39d392007-10-15 16:14:19 -04002361 struct extent_buffer *leaf;
2362 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002363 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002364 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002365 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002366 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002367 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002368 u32 total_size = 0;
2369 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002370 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002371 struct btrfs_disk_key disk_key;
2372
Chris Mason9c583092008-01-29 15:15:18 -05002373 for (i = 0; i < nr; i++) {
2374 total_data += data_size[i];
2375 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002376
Chris Mason74123bd2007-02-02 11:05:29 -05002377 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002378 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002379 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002380
Chris Mason9c583092008-01-29 15:15:18 -05002381 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2382 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002383 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002384 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002385 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002386 if (ret < 0)
2387 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002388
Chris Mason62e27492007-03-15 12:56:47 -04002389 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002390 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002391
Chris Mason5f39d392007-10-15 16:14:19 -04002392 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002393 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002394
Chris Mason123abc82007-03-14 14:14:43 -04002395 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002396 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002397 btrfs_print_leaf(root, leaf);
2398 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002399 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002400 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002401 }
Chris Mason5f39d392007-10-15 16:14:19 -04002402
Chris Mason62e27492007-03-15 12:56:47 -04002403 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002404 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002405
Chris Masonbe0e5c02007-01-26 15:51:26 -05002406 if (slot != nritems) {
2407 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002408 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002409
Chris Mason5f39d392007-10-15 16:14:19 -04002410 if (old_data < data_end) {
2411 btrfs_print_leaf(root, leaf);
2412 printk("slot %d old_data %d data_end %d\n",
2413 slot, old_data, data_end);
2414 BUG_ON(1);
2415 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002416 /*
2417 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2418 */
2419 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002420 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002421 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002422 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002423
Chris Mason5f39d392007-10-15 16:14:19 -04002424 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002425 if (!leaf->map_token) {
2426 map_extent_buffer(leaf, (unsigned long)item,
2427 sizeof(struct btrfs_item),
2428 &leaf->map_token, &leaf->kaddr,
2429 &leaf->map_start, &leaf->map_len,
2430 KM_USER1);
2431 }
2432
Chris Mason5f39d392007-10-15 16:14:19 -04002433 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002434 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002435 }
Chris Masondb945352007-10-15 16:15:53 -04002436 if (leaf->map_token) {
2437 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2438 leaf->map_token = NULL;
2439 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002440
2441 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002442 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002443 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002444 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002445
2446 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002447 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002448 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002449 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002450 data_end = old_data;
2451 }
Chris Mason5f39d392007-10-15 16:14:19 -04002452
Chris Mason62e27492007-03-15 12:56:47 -04002453 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002454 for (i = 0; i < nr; i++) {
2455 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2456 btrfs_set_item_key(leaf, &disk_key, slot + i);
2457 item = btrfs_item_nr(leaf, slot + i);
2458 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2459 data_end -= data_size[i];
2460 btrfs_set_item_size(leaf, item, data_size[i]);
2461 }
2462 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002463 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002464
2465 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002466 if (slot == 0) {
2467 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002468 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002469 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002470
Chris Mason5f39d392007-10-15 16:14:19 -04002471 if (btrfs_leaf_free_space(root, leaf) < 0) {
2472 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002473 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002474 }
Chris Mason5a01a2e2008-01-30 11:43:54 -05002475
Chris Masoned2ff2c2007-03-01 18:59:40 -05002476out:
Chris Mason62e27492007-03-15 12:56:47 -04002477 return ret;
2478}
2479
2480/*
2481 * Given a key and some data, insert an item into the tree.
2482 * This does all the path init required, making room in the tree if needed.
2483 */
Chris Masone089f052007-03-16 16:20:31 -04002484int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2485 *root, struct btrfs_key *cpu_key, void *data, u32
2486 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002487{
2488 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002489 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002490 struct extent_buffer *leaf;
2491 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002492
Chris Mason2c90e5d2007-04-02 10:50:19 -04002493 path = btrfs_alloc_path();
2494 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002495 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002496 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002497 leaf = path->nodes[0];
2498 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2499 write_extent_buffer(leaf, data, ptr, data_size);
2500 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002501 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002502 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002503 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002504}
2505
Chris Mason74123bd2007-02-02 11:05:29 -05002506/*
Chris Mason5de08d72007-02-24 06:24:44 -05002507 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002508 *
2509 * If the delete empties a node, the node is removed from the tree,
2510 * continuing all the way the root if required. The root is converted into
2511 * a leaf if all the nodes are emptied.
2512 */
Chris Masone089f052007-03-16 16:20:31 -04002513static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2514 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002515{
Chris Mason5f39d392007-10-15 16:14:19 -04002516 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002517 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002518 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002519 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002520
Chris Mason5f39d392007-10-15 16:14:19 -04002521 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002522 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002523 memmove_extent_buffer(parent,
2524 btrfs_node_key_ptr_offset(slot),
2525 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002526 sizeof(struct btrfs_key_ptr) *
2527 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002528 }
Chris Mason7518a232007-03-12 12:01:18 -04002529 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002530 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002531 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002532 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002533 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002534 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002535 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002536 struct btrfs_disk_key disk_key;
2537
2538 btrfs_node_key(parent, &disk_key, 0);
2539 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002540 if (wret)
2541 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002542 }
Chris Masond6025572007-03-30 14:27:56 -04002543 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002544 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002545}
2546
Chris Mason74123bd2007-02-02 11:05:29 -05002547/*
2548 * delete the item at the leaf level in path. If that empties
2549 * the leaf, remove it from the tree
2550 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002551int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2552 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002553{
Chris Mason5f39d392007-10-15 16:14:19 -04002554 struct extent_buffer *leaf;
2555 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002556 int last_off;
2557 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002558 int ret = 0;
2559 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002560 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002561 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002562
Chris Mason5f39d392007-10-15 16:14:19 -04002563 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002564 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2565
2566 for (i = 0; i < nr; i++)
2567 dsize += btrfs_item_size_nr(leaf, slot + i);
2568
Chris Mason5f39d392007-10-15 16:14:19 -04002569 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570
Chris Mason85e21ba2008-01-29 15:11:36 -05002571 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002572 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002573 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002574
2575 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002576 data_end + dsize,
2577 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002578 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002579
Chris Mason85e21ba2008-01-29 15:11:36 -05002580 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002581 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002582
Chris Mason5f39d392007-10-15 16:14:19 -04002583 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002584 if (!leaf->map_token) {
2585 map_extent_buffer(leaf, (unsigned long)item,
2586 sizeof(struct btrfs_item),
2587 &leaf->map_token, &leaf->kaddr,
2588 &leaf->map_start, &leaf->map_len,
2589 KM_USER1);
2590 }
Chris Mason5f39d392007-10-15 16:14:19 -04002591 ioff = btrfs_item_offset(leaf, item);
2592 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002593 }
Chris Masondb945352007-10-15 16:15:53 -04002594
2595 if (leaf->map_token) {
2596 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2597 leaf->map_token = NULL;
2598 }
2599
Chris Mason5f39d392007-10-15 16:14:19 -04002600 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002601 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002602 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002603 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002604 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002605 btrfs_set_header_nritems(leaf, nritems - nr);
2606 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002607
Chris Mason74123bd2007-02-02 11:05:29 -05002608 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002609 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002610 if (leaf == root->node) {
2611 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002612 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002613 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002614 clean_tree_block(trans, root, leaf);
2615 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002616 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002617 if (wret)
2618 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002619 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002620 leaf->start, leaf->len,
2621 btrfs_header_owner(path->nodes[1]),
2622 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002623 if (wret)
2624 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002625 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002626 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002627 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002628 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002629 struct btrfs_disk_key disk_key;
2630
2631 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002632 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002633 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002634 if (wret)
2635 ret = wret;
2636 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002637
Chris Mason74123bd2007-02-02 11:05:29 -05002638 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002639 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002640 /* push_leaf_left fixes the path.
2641 * make sure the path still points to our leaf
2642 * for possible call to del_ptr below
2643 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002644 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002645 extent_buffer_get(leaf);
2646
Chris Mason85e21ba2008-01-29 15:11:36 -05002647 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002648 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002649 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002650
2651 if (path->nodes[0] == leaf &&
2652 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002653 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002654 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002655 ret = wret;
2656 }
Chris Mason5f39d392007-10-15 16:14:19 -04002657
2658 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002659 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002660 u64 bytenr = leaf->start;
2661 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002662
Chris Mason7bb86312007-12-11 09:25:06 -05002663 root_gen = btrfs_header_generation(
2664 path->nodes[1]);
2665
Chris Mason5f39d392007-10-15 16:14:19 -04002666 clean_tree_block(trans, root, leaf);
2667 wait_on_tree_block_writeback(root, leaf);
2668
Chris Masone089f052007-03-16 16:20:31 -04002669 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002670 if (wret)
2671 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002672
2673 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002674 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002675 blocksize,
2676 btrfs_header_owner(path->nodes[1]),
2677 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002678 if (wret)
2679 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002680 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002681 btrfs_mark_buffer_dirty(leaf);
2682 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002683 }
Chris Masond5719762007-03-23 10:01:08 -04002684 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002685 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002686 }
2687 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002688 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689}
2690
Chris Mason97571fd2007-02-24 13:39:08 -05002691/*
Chris Mason7bb86312007-12-11 09:25:06 -05002692 * walk up the tree as far as required to find the previous leaf.
2693 * returns 0 if it found something or 1 if there are no lesser leaves.
2694 * returns < 0 on io errors.
2695 */
2696int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2697{
Chris Mason8f662a72008-01-02 10:01:11 -05002698 u64 bytenr;
Chris Mason7bb86312007-12-11 09:25:06 -05002699 int slot;
2700 int level = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002701 struct extent_buffer *c;
2702 struct extent_buffer *next = NULL;
2703
2704 while(level < BTRFS_MAX_LEVEL) {
2705 if (!path->nodes[level])
2706 return 1;
2707
2708 slot = path->slots[level];
2709 c = path->nodes[level];
2710 if (slot == 0) {
2711 level++;
2712 if (level == BTRFS_MAX_LEVEL)
2713 return 1;
2714 continue;
2715 }
2716 slot--;
2717
2718 bytenr = btrfs_node_blockptr(c, slot);
2719 if (next)
2720 free_extent_buffer(next);
2721
Chris Mason7bb86312007-12-11 09:25:06 -05002722 next = read_tree_block(root, bytenr,
2723 btrfs_level_size(root, level - 1));
2724 break;
2725 }
2726 path->slots[level] = slot;
2727 while(1) {
2728 level--;
2729 c = path->nodes[level];
2730 free_extent_buffer(c);
Chris Mason8f662a72008-01-02 10:01:11 -05002731 slot = btrfs_header_nritems(next);
2732 if (slot != 0)
2733 slot--;
Chris Mason7bb86312007-12-11 09:25:06 -05002734 path->nodes[level] = next;
Chris Mason8f662a72008-01-02 10:01:11 -05002735 path->slots[level] = slot;
Chris Mason7bb86312007-12-11 09:25:06 -05002736 if (!level)
2737 break;
Chris Mason8f662a72008-01-02 10:01:11 -05002738 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
Chris Mason7bb86312007-12-11 09:25:06 -05002739 btrfs_level_size(root, level - 1));
2740 }
2741 return 0;
2742}
2743
2744/*
Chris Mason97571fd2007-02-24 13:39:08 -05002745 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002746 * returns 0 if it found something or 1 if there are no greater leaves.
2747 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002748 */
Chris Mason234b63a2007-03-13 10:46:10 -04002749int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002750{
2751 int slot;
2752 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002753 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002754 struct extent_buffer *c;
2755 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002756
Chris Mason234b63a2007-03-13 10:46:10 -04002757 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002758 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002759 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002760
Chris Masond97e63b2007-02-20 16:40:44 -05002761 slot = path->slots[level] + 1;
2762 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002763 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002764 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002765 if (level == BTRFS_MAX_LEVEL)
2766 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002767 continue;
2768 }
Chris Mason5f39d392007-10-15 16:14:19 -04002769
Chris Masondb945352007-10-15 16:15:53 -04002770 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002771 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002772 free_extent_buffer(next);
2773
Chris Mason6702ed42007-08-07 16:15:09 -04002774 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002775 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002776
Chris Masondb945352007-10-15 16:15:53 -04002777 next = read_tree_block(root, bytenr,
2778 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002779 break;
2780 }
2781 path->slots[level] = slot;
2782 while(1) {
2783 level--;
2784 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002785 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002786 path->nodes[level] = next;
2787 path->slots[level] = 0;
2788 if (!level)
2789 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002790 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002791 reada_for_search(root, path, level, 0, 0);
Chris Masondb945352007-10-15 16:15:53 -04002792 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2793 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002794 }
2795 return 0;
2796}
Chris Mason0b86a832008-03-24 15:01:56 -04002797
2798int btrfs_previous_item(struct btrfs_root *root,
2799 struct btrfs_path *path, u64 min_objectid,
2800 int type)
2801{
2802 struct btrfs_key found_key;
2803 struct extent_buffer *leaf;
2804 int ret;
2805
2806 while(1) {
2807 if (path->slots[0] == 0) {
2808 ret = btrfs_prev_leaf(root, path);
2809 if (ret != 0)
2810 return ret;
2811 } else {
2812 path->slots[0]--;
2813 }
2814 leaf = path->nodes[0];
2815 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2816 if (found_key.type == type)
2817 return 0;
2818 }
2819 return 1;
2820}
2821