blob: 618e526c9046ce48ae7ef5c70d2b9db4a500f38c [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 }
Chris Mason7bb86312007-12-11 09:25:06 -0500161 WARN_ON(root->ref_cows && trans->transid !=
162 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400163 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400164
Chris Mason7bb86312007-12-11 09:25:06 -0500165 level = btrfs_header_level(buf);
166 nritems = btrfs_header_nritems(buf);
167 if (nritems) {
168 if (level == 0)
169 btrfs_item_key_to_cpu(buf, &first_key, 0);
170 else
171 btrfs_node_key_to_cpu(buf, &first_key, 0);
172 } else {
173 first_key.objectid = 0;
174 }
175 cow = __btrfs_alloc_free_block(trans, root, buf->len,
176 root->root_key.objectid,
177 root_gen, first_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -0400178 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400179 if (IS_ERR(cow))
180 return PTR_ERR(cow);
181
Chris Mason5f39d392007-10-15 16:14:19 -0400182 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400183 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400184 btrfs_set_header_generation(cow, trans->transid);
185 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400187
Chris Mason5f39d392007-10-15 16:14:19 -0400188 WARN_ON(btrfs_header_generation(buf) > trans->transid);
189 if (btrfs_header_generation(buf) != trans->transid) {
Chris Mason6702ed42007-08-07 16:15:09 -0400190 different_trans = 1;
191 ret = btrfs_inc_ref(trans, root, buf);
192 if (ret)
193 return ret;
194 } else {
Chris Mason6702ed42007-08-07 16:15:09 -0400195 clean_tree_block(trans, root, buf);
196 }
197
198 if (buf == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -0500199 root_gen = btrfs_header_generation(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400200 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400201 extent_buffer_get(cow);
Chris Mason6702ed42007-08-07 16:15:09 -0400202 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400203 btrfs_free_extent(trans, root, buf->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500204 buf->len, root->root_key.objectid,
205 root_gen, 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400206 }
Chris Mason5f39d392007-10-15 16:14:19 -0400207 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400208 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400209 } else {
Chris Mason7bb86312007-12-11 09:25:06 -0500210 root_gen = btrfs_header_generation(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400211 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400212 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500213 WARN_ON(trans->transid == 0);
214 btrfs_set_node_ptr_generation(parent, parent_slot,
215 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400216 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400217 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500218 btrfs_free_extent(trans, root, buf->start, buf->len,
219 btrfs_header_owner(parent), root_gen,
220 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400221 }
Chris Mason5f39d392007-10-15 16:14:19 -0400222 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400223 btrfs_mark_buffer_dirty(cow);
224 *cow_ret = cow;
225 return 0;
226}
227
Chris Mason5f39d392007-10-15 16:14:19 -0400228int btrfs_cow_block(struct btrfs_trans_handle *trans,
229 struct btrfs_root *root, struct extent_buffer *buf,
230 struct extent_buffer *parent, int parent_slot,
231 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500232{
Chris Mason6702ed42007-08-07 16:15:09 -0400233 u64 search_start;
Chris Masondc17ff82008-01-08 15:46:30 -0500234 u64 header_trans;
Chris Masonf510cfe2007-10-15 16:14:48 -0400235 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500236
Chris Masonccd467d2007-06-28 15:57:36 -0400237 if (trans->transaction != root->fs_info->running_transaction) {
238 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
239 root->fs_info->running_transaction->transid);
240 WARN_ON(1);
241 }
242 if (trans->transid != root->fs_info->generation) {
243 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
244 root->fs_info->generation);
245 WARN_ON(1);
246 }
Chris Masondc17ff82008-01-08 15:46:30 -0500247
248 header_trans = btrfs_header_generation(buf);
Chris Mason63b10fc2008-04-01 11:21:32 -0400249 spin_lock(&root->fs_info->hash_lock);
250 if (header_trans == trans->transid &&
251 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500252 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400253 spin_unlock(&root->fs_info->hash_lock);
Chris Mason02217ed2007-03-02 16:08:05 -0500254 return 0;
255 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400256 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400257 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400258 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason6702ed42007-08-07 16:15:09 -0400259 parent_slot, cow_ret, search_start, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -0400260 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400261}
262
Chris Mason6b800532007-10-15 16:17:34 -0400263static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400264{
Chris Mason6b800532007-10-15 16:17:34 -0400265 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400266 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400267 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400268 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500269 return 0;
270}
271
Chris Mason081e9572007-11-06 10:26:24 -0500272/*
273 * compare two keys in a memcmp fashion
274 */
275static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
276{
277 struct btrfs_key k1;
278
279 btrfs_disk_key_to_cpu(&k1, disk);
280
281 if (k1.objectid > k2->objectid)
282 return 1;
283 if (k1.objectid < k2->objectid)
284 return -1;
285 if (k1.type > k2->type)
286 return 1;
287 if (k1.type < k2->type)
288 return -1;
289 if (k1.offset > k2->offset)
290 return 1;
291 if (k1.offset < k2->offset)
292 return -1;
293 return 0;
294}
295
296
Chris Mason6702ed42007-08-07 16:15:09 -0400297int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400298 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400299 int start_slot, int cache_only, u64 *last_ret,
300 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400301{
Chris Mason6b800532007-10-15 16:17:34 -0400302 struct extent_buffer *cur;
303 struct extent_buffer *tmp;
Chris Mason6702ed42007-08-07 16:15:09 -0400304 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400305 u64 search_start = *last_ret;
306 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400307 u64 other;
308 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400309 int end_slot;
310 int i;
311 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400312 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400313 int uptodate;
314 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500315 int progress_passed = 0;
316 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400317
Chris Mason5708b952007-10-25 15:43:18 -0400318 parent_level = btrfs_header_level(parent);
319 if (cache_only && parent_level != 1)
320 return 0;
321
Chris Mason6702ed42007-08-07 16:15:09 -0400322 if (trans->transaction != root->fs_info->running_transaction) {
323 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
324 root->fs_info->running_transaction->transid);
325 WARN_ON(1);
326 }
327 if (trans->transid != root->fs_info->generation) {
328 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
329 root->fs_info->generation);
330 WARN_ON(1);
331 }
Chris Mason86479a02007-09-10 19:58:16 -0400332
Chris Mason6b800532007-10-15 16:17:34 -0400333 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400334 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400335 end_slot = parent_nritems;
336
337 if (parent_nritems == 1)
338 return 0;
339
340 for (i = start_slot; i < end_slot; i++) {
341 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400342
Chris Mason5708b952007-10-25 15:43:18 -0400343 if (!parent->map_token) {
344 map_extent_buffer(parent,
345 btrfs_node_key_ptr_offset(i),
346 sizeof(struct btrfs_key_ptr),
347 &parent->map_token, &parent->kaddr,
348 &parent->map_start, &parent->map_len,
349 KM_USER1);
350 }
Chris Mason081e9572007-11-06 10:26:24 -0500351 btrfs_node_key(parent, &disk_key, i);
352 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
353 continue;
354
355 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400356 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400357 if (last_block == 0)
358 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400359
Chris Mason6702ed42007-08-07 16:15:09 -0400360 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400361 other = btrfs_node_blockptr(parent, i - 1);
362 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400363 }
Chris Mason5708b952007-10-25 15:43:18 -0400364 if (close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400365 other = btrfs_node_blockptr(parent, i + 1);
366 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400367 }
Chris Masone9d0b132007-08-10 14:06:19 -0400368 if (close) {
369 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400370 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400371 }
Chris Mason5708b952007-10-25 15:43:18 -0400372 if (parent->map_token) {
373 unmap_extent_buffer(parent, parent->map_token,
374 KM_USER1);
375 parent->map_token = NULL;
376 }
Chris Mason6702ed42007-08-07 16:15:09 -0400377
Chris Mason6b800532007-10-15 16:17:34 -0400378 cur = btrfs_find_tree_block(root, blocknr, blocksize);
379 if (cur)
380 uptodate = btrfs_buffer_uptodate(cur);
381 else
382 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400383 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400384 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400385 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400386 continue;
387 }
Chris Mason6b800532007-10-15 16:17:34 -0400388 if (!cur) {
389 cur = read_tree_block(root, blocknr,
390 blocksize);
391 } else if (!uptodate) {
392 btrfs_read_buffer(cur);
Chris Masonf2183bd2007-08-10 14:42:37 -0400393 }
Chris Mason6702ed42007-08-07 16:15:09 -0400394 }
Chris Masone9d0b132007-08-10 14:06:19 -0400395 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400396 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400397
Chris Mason0999df52008-04-01 13:48:14 -0400398 btrfs_verify_block_csum(root, cur);
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 Masonf1885912008-04-09 16:28:12 -0400572 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400573 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400574 if (btrfs_header_level(path->nodes[level]) != level)
575 printk("warning: bad level %Lu wanted %d found %d\n",
576 path->nodes[level]->start, level,
577 btrfs_header_level(path->nodes[level]));
578 found_start = btrfs_header_bytenr(path->nodes[level]);
579 if (found_start != path->nodes[level]->start) {
580 printk("warning: bad bytentr %Lu found %Lu\n",
581 path->nodes[level]->start, found_start);
582 }
Chris Masondb945352007-10-15 16:15:53 -0400583#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400584 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400585
Chris Mason479965d2007-10-15 16:14:27 -0400586 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
587 (unsigned long)btrfs_header_fsid(buf),
588 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400589 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400590 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400591 }
Chris Masondb945352007-10-15 16:15:53 -0400592#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500593 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400594 return check_leaf(root, path, level);
595 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500596}
597
Chris Mason74123bd2007-02-02 11:05:29 -0500598/*
Chris Mason5f39d392007-10-15 16:14:19 -0400599 * search for key in the extent_buffer. The items start at offset p,
600 * and they are item_size apart. There are 'max' items in p.
601 *
Chris Mason74123bd2007-02-02 11:05:29 -0500602 * the slot in the array is returned via slot, and it points to
603 * the place where you would insert key if it is not found in
604 * the array.
605 *
606 * slot may point to max if the key is bigger than all of the keys
607 */
Chris Mason5f39d392007-10-15 16:14:19 -0400608static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
609 int item_size, struct btrfs_key *key,
610 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500611{
612 int low = 0;
613 int high = max;
614 int mid;
615 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400616 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400617 struct btrfs_disk_key unaligned;
618 unsigned long offset;
619 char *map_token = NULL;
620 char *kaddr = NULL;
621 unsigned long map_start = 0;
622 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400623 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500624
625 while(low < high) {
626 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400627 offset = p + mid * item_size;
628
629 if (!map_token || offset < map_start ||
630 (offset + sizeof(struct btrfs_disk_key)) >
631 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400632 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400633 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400634 map_token = NULL;
635 }
636 err = map_extent_buffer(eb, offset,
637 sizeof(struct btrfs_disk_key),
638 &map_token, &kaddr,
639 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400640
Chris Mason479965d2007-10-15 16:14:27 -0400641 if (!err) {
642 tmp = (struct btrfs_disk_key *)(kaddr + offset -
643 map_start);
644 } else {
645 read_extent_buffer(eb, &unaligned,
646 offset, sizeof(unaligned));
647 tmp = &unaligned;
648 }
649
Chris Mason5f39d392007-10-15 16:14:19 -0400650 } else {
651 tmp = (struct btrfs_disk_key *)(kaddr + offset -
652 map_start);
653 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500654 ret = comp_keys(tmp, key);
655
656 if (ret < 0)
657 low = mid + 1;
658 else if (ret > 0)
659 high = mid;
660 else {
661 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400662 if (map_token)
663 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500664 return 0;
665 }
666 }
667 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400668 if (map_token)
669 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500670 return 1;
671}
672
Chris Mason97571fd2007-02-24 13:39:08 -0500673/*
674 * simple bin_search frontend that does the right thing for
675 * leaves vs nodes
676 */
Chris Mason5f39d392007-10-15 16:14:19 -0400677static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
678 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500679{
Chris Mason5f39d392007-10-15 16:14:19 -0400680 if (level == 0) {
681 return generic_bin_search(eb,
682 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400683 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400684 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400685 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500686 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400687 return generic_bin_search(eb,
688 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400689 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400690 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400691 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500692 }
693 return -1;
694}
695
Chris Mason5f39d392007-10-15 16:14:19 -0400696static struct extent_buffer *read_node_slot(struct btrfs_root *root,
697 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500698{
Chris Masonbb803952007-03-01 12:04:21 -0500699 if (slot < 0)
700 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400701 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500702 return NULL;
Chris Masondb945352007-10-15 16:15:53 -0400703 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
704 btrfs_level_size(root, btrfs_header_level(parent) - 1));
Chris Masonbb803952007-03-01 12:04:21 -0500705}
706
Chris Mason98ed5172008-01-03 10:01:48 -0500707static int balance_level(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root,
709 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500710{
Chris Mason5f39d392007-10-15 16:14:19 -0400711 struct extent_buffer *right = NULL;
712 struct extent_buffer *mid;
713 struct extent_buffer *left = NULL;
714 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500715 int ret = 0;
716 int wret;
717 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500718 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400719 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500720 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500721
722 if (level == 0)
723 return 0;
724
Chris Mason5f39d392007-10-15 16:14:19 -0400725 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500726 WARN_ON(btrfs_header_generation(mid) != trans->transid);
727
Chris Mason1d4f8a02007-03-13 09:28:32 -0400728 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500729
Chris Mason234b63a2007-03-13 10:46:10 -0400730 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400731 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500732 pslot = path->slots[level + 1];
733
Chris Mason40689472007-03-17 14:29:23 -0400734 /*
735 * deal with the case where there is only one pointer in the root
736 * by promoting the node below to a root
737 */
Chris Mason5f39d392007-10-15 16:14:19 -0400738 if (!parent) {
739 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500740
Chris Mason5f39d392007-10-15 16:14:19 -0400741 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500742 return 0;
743
744 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400745 child = read_node_slot(root, mid, 0);
Chris Masonbb803952007-03-01 12:04:21 -0500746 BUG_ON(!child);
Yan2f375ab2008-02-01 14:58:07 -0500747 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
748 BUG_ON(ret);
749
Chris Masonbb803952007-03-01 12:04:21 -0500750 root->node = child;
Chris Mason0b86a832008-03-24 15:01:56 -0400751 add_root_to_dirty_list(root);
Chris Masonbb803952007-03-01 12:04:21 -0500752 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400753 clean_tree_block(trans, root, mid);
754 wait_on_tree_block_writeback(root, mid);
Chris Masonbb803952007-03-01 12:04:21 -0500755 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400756 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500757 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
758 root->root_key.objectid,
759 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500760 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400761 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400762 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500763 }
Chris Mason5f39d392007-10-15 16:14:19 -0400764 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400765 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500766 return 0;
767
Chris Mason5f39d392007-10-15 16:14:19 -0400768 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400769 err_on_enospc = 1;
770
Chris Mason5f39d392007-10-15 16:14:19 -0400771 left = read_node_slot(root, parent, pslot - 1);
772 if (left) {
773 wret = btrfs_cow_block(trans, root, left,
774 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400775 if (wret) {
776 ret = wret;
777 goto enospc;
778 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400779 }
Chris Mason5f39d392007-10-15 16:14:19 -0400780 right = read_node_slot(root, parent, pslot + 1);
781 if (right) {
782 wret = btrfs_cow_block(trans, root, right,
783 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400784 if (wret) {
785 ret = wret;
786 goto enospc;
787 }
788 }
789
790 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400791 if (left) {
792 orig_slot += btrfs_header_nritems(left);
793 wret = push_node_left(trans, root, left, mid);
Chris Mason79f95c82007-03-01 15:16:26 -0500794 if (wret < 0)
795 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400796 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400797 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500798 }
Chris Mason79f95c82007-03-01 15:16:26 -0500799
800 /*
801 * then try to empty the right most buffer into the middle
802 */
Chris Mason5f39d392007-10-15 16:14:19 -0400803 if (right) {
804 wret = push_node_left(trans, root, mid, right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400805 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500806 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400807 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400808 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500809 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400810 u32 blocksize = right->len;
811
Chris Mason5f39d392007-10-15 16:14:19 -0400812 clean_tree_block(trans, root, right);
813 wait_on_tree_block_writeback(root, right);
814 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500815 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400816 wret = del_ptr(trans, root, path, level + 1, pslot +
817 1);
Chris Masonbb803952007-03-01 12:04:21 -0500818 if (wret)
819 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400820 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500821 blocksize,
822 btrfs_header_owner(parent),
823 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500824 if (wret)
825 ret = wret;
826 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400827 struct btrfs_disk_key right_key;
828 btrfs_node_key(right, &right_key, 0);
829 btrfs_set_node_key(parent, &right_key, pslot + 1);
830 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500831 }
832 }
Chris Mason5f39d392007-10-15 16:14:19 -0400833 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500834 /*
835 * we're not allowed to leave a node with one item in the
836 * tree during a delete. A deletion from lower in the tree
837 * could try to delete the only pointer in this node.
838 * So, pull some keys from the left.
839 * There has to be a left pointer at this point because
840 * otherwise we would have pulled some pointers from the
841 * right
842 */
Chris Mason5f39d392007-10-15 16:14:19 -0400843 BUG_ON(!left);
844 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400845 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500846 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400847 goto enospc;
848 }
Chris Mason79f95c82007-03-01 15:16:26 -0500849 BUG_ON(wret == 1);
850 }
Chris Mason5f39d392007-10-15 16:14:19 -0400851 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500852 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500853 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400854 u64 bytenr = mid->start;
855 u32 blocksize = mid->len;
Chris Mason5f39d392007-10-15 16:14:19 -0400856 clean_tree_block(trans, root, mid);
857 wait_on_tree_block_writeback(root, mid);
858 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500859 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400860 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500861 if (wret)
862 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500863 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
864 btrfs_header_owner(parent),
865 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500866 if (wret)
867 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500868 } else {
869 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400870 struct btrfs_disk_key mid_key;
871 btrfs_node_key(mid, &mid_key, 0);
872 btrfs_set_node_key(parent, &mid_key, pslot);
873 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500874 }
Chris Masonbb803952007-03-01 12:04:21 -0500875
Chris Mason79f95c82007-03-01 15:16:26 -0500876 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400877 if (left) {
878 if (btrfs_header_nritems(left) > orig_slot) {
879 extent_buffer_get(left);
880 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500881 path->slots[level + 1] -= 1;
882 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400883 if (mid)
884 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500885 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400886 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -0500887 path->slots[level] = orig_slot;
888 }
889 }
Chris Mason79f95c82007-03-01 15:16:26 -0500890 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -0400891 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -0400892 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -0400893 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -0500894 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -0400895enospc:
Chris Mason5f39d392007-10-15 16:14:19 -0400896 if (right)
897 free_extent_buffer(right);
898 if (left)
899 free_extent_buffer(left);
Chris Masonbb803952007-03-01 12:04:21 -0500900 return ret;
901}
902
Chris Masone66f7092007-04-20 13:16:02 -0400903/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -0500904static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
905 struct btrfs_root *root,
906 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -0400907{
Chris Mason5f39d392007-10-15 16:14:19 -0400908 struct extent_buffer *right = NULL;
909 struct extent_buffer *mid;
910 struct extent_buffer *left = NULL;
911 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -0400912 int ret = 0;
913 int wret;
914 int pslot;
915 int orig_slot = path->slots[level];
916 u64 orig_ptr;
917
918 if (level == 0)
919 return 1;
920
Chris Mason5f39d392007-10-15 16:14:19 -0400921 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -0500922 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -0400923 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
924
925 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400926 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -0400927 pslot = path->slots[level + 1];
928
Chris Mason5f39d392007-10-15 16:14:19 -0400929 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -0400930 return 1;
Chris Masone66f7092007-04-20 13:16:02 -0400931
Chris Mason5f39d392007-10-15 16:14:19 -0400932 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -0400933
934 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400935 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -0400936 u32 left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400937 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -0400938 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
939 wret = 1;
940 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400941 ret = btrfs_cow_block(trans, root, left, parent,
942 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400943 if (ret)
944 wret = 1;
945 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400946 wret = push_node_left(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400947 left, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400948 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400949 }
Chris Masone66f7092007-04-20 13:16:02 -0400950 if (wret < 0)
951 ret = wret;
952 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400953 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -0400954 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400955 btrfs_node_key(mid, &disk_key, 0);
956 btrfs_set_node_key(parent, &disk_key, pslot);
957 btrfs_mark_buffer_dirty(parent);
958 if (btrfs_header_nritems(left) > orig_slot) {
959 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -0400960 path->slots[level + 1] -= 1;
961 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400962 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -0400963 } else {
964 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -0400965 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -0400966 path->slots[level] = orig_slot;
Chris Mason5f39d392007-10-15 16:14:19 -0400967 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400968 }
Chris Masone66f7092007-04-20 13:16:02 -0400969 return 0;
970 }
Chris Mason5f39d392007-10-15 16:14:19 -0400971 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -0400972 }
Chris Mason5f39d392007-10-15 16:14:19 -0400973 right= read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -0400974
975 /*
976 * then try to empty the right most buffer into the middle
977 */
Chris Mason5f39d392007-10-15 16:14:19 -0400978 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -0400979 u32 right_nr;
Chris Mason5f39d392007-10-15 16:14:19 -0400980 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -0400981 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
982 wret = 1;
983 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400984 ret = btrfs_cow_block(trans, root, right,
985 parent, pslot + 1,
986 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 if (ret)
988 wret = 1;
989 else {
Chris Mason54aa1f42007-06-22 14:16:25 -0400990 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -0400991 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400992 }
Chris Mason33ade1f2007-04-20 13:48:57 -0400993 }
Chris Masone66f7092007-04-20 13:16:02 -0400994 if (wret < 0)
995 ret = wret;
996 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400997 struct btrfs_disk_key disk_key;
998
999 btrfs_node_key(right, &disk_key, 0);
1000 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1001 btrfs_mark_buffer_dirty(parent);
1002
1003 if (btrfs_header_nritems(mid) <= orig_slot) {
1004 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001005 path->slots[level + 1] += 1;
1006 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001007 btrfs_header_nritems(mid);
1008 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001009 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001010 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001011 }
Chris Masone66f7092007-04-20 13:16:02 -04001012 return 0;
1013 }
Chris Mason5f39d392007-10-15 16:14:19 -04001014 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001015 }
Chris Masone66f7092007-04-20 13:16:02 -04001016 return 1;
1017}
1018
Chris Mason74123bd2007-02-02 11:05:29 -05001019/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001020 * readahead one full node of leaves
1021 */
1022static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason01f46652007-12-21 16:24:26 -05001023 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001024{
Chris Mason5f39d392007-10-15 16:14:19 -04001025 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001026 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001027 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001028 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001029 u64 lowest_read;
1030 u64 highest_read;
1031 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001032 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001033 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001034 u32 nr;
1035 u32 blocksize;
1036 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001037
Chris Masona6b6e752007-10-15 16:22:39 -04001038 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001039 return;
1040
Chris Mason6702ed42007-08-07 16:15:09 -04001041 if (!path->nodes[level])
1042 return;
1043
Chris Mason5f39d392007-10-15 16:14:19 -04001044 node = path->nodes[level];
Chris Mason3c69fae2007-08-07 15:52:22 -04001045 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001046 blocksize = btrfs_level_size(root, level - 1);
1047 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001048 if (eb) {
1049 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001050 return;
1051 }
1052
Chris Mason6b800532007-10-15 16:17:34 -04001053 highest_read = search;
1054 lowest_read = search;
1055
Chris Mason5f39d392007-10-15 16:14:19 -04001056 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001057 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001058 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001059 if (direction < 0) {
1060 if (nr == 0)
1061 break;
1062 nr--;
1063 } else if (direction > 0) {
1064 nr++;
1065 if (nr >= nritems)
1066 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001067 }
Chris Mason01f46652007-12-21 16:24:26 -05001068 if (path->reada < 0 && objectid) {
1069 btrfs_node_key(node, &disk_key, nr);
1070 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1071 break;
1072 }
Chris Mason6b800532007-10-15 16:17:34 -04001073 search = btrfs_node_blockptr(node, nr);
1074 if ((search >= lowest_read && search <= highest_read) ||
1075 (search < lowest_read && lowest_read - search <= 32768) ||
1076 (search > highest_read && search - highest_read <= 32768)) {
1077 readahead_tree_block(root, search, blocksize);
1078 nread += blocksize;
1079 }
1080 nscan++;
1081 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1082 break;
1083 if(nread > (1024 * 1024) || nscan > 128)
1084 break;
1085
1086 if (search < lowest_read)
1087 lowest_read = search;
1088 if (search > highest_read)
1089 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001090 }
1091}
1092/*
Chris Mason74123bd2007-02-02 11:05:29 -05001093 * look for key in the tree. path is filled in with nodes along the way
1094 * if key is found, we return zero and you can find the item in the leaf
1095 * level of the path (level 0)
1096 *
1097 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001098 * be inserted, and 1 is returned. If there are other errors during the
1099 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001100 *
1101 * if ins_len > 0, nodes and leaves will be split as we walk down the
1102 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1103 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001104 */
Chris Masone089f052007-03-16 16:20:31 -04001105int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1106 *root, struct btrfs_key *key, struct btrfs_path *p, int
1107 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001108{
Chris Mason5f39d392007-10-15 16:14:19 -04001109 struct extent_buffer *b;
Chris Masondb945352007-10-15 16:15:53 -04001110 u64 bytenr;
Chris Mason74493f72007-12-11 09:25:06 -05001111 u64 ptr_gen;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001112 int slot;
1113 int ret;
1114 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001115 int should_reada = p->reada;
Chris Mason9f3a7422007-08-07 15:52:19 -04001116 u8 lowest_level = 0;
1117
Chris Mason6702ed42007-08-07 16:15:09 -04001118 lowest_level = p->lowest_level;
1119 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001120 WARN_ON(p->nodes[0] != NULL);
1121 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
Chris Masonbb803952007-03-01 12:04:21 -05001122again:
1123 b = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001124 extent_buffer_get(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001125 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001126 level = btrfs_header_level(b);
Chris Mason02217ed2007-03-02 16:08:05 -05001127 if (cow) {
1128 int wret;
Chris Masone20d96d2007-03-22 12:13:20 -04001129 wret = btrfs_cow_block(trans, root, b,
1130 p->nodes[level + 1],
1131 p->slots[level + 1],
Yan252c38f2007-08-29 09:11:44 -04001132 &b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001133 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001134 free_extent_buffer(b);
Chris Mason54aa1f42007-06-22 14:16:25 -04001135 return wret;
1136 }
Chris Mason02217ed2007-03-02 16:08:05 -05001137 }
1138 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001139 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001140 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001141 level = btrfs_header_level(b);
Chris Masoneb60cea2007-02-02 09:18:22 -05001142 p->nodes[level] = b;
Chris Mason123abc82007-03-14 14:14:43 -04001143 ret = check_block(root, p, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001144 if (ret)
1145 return -1;
Chris Mason5f39d392007-10-15 16:14:19 -04001146 ret = bin_search(b, key, level, &slot);
1147 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001148 if (ret && slot > 0)
1149 slot -= 1;
1150 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001151 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Masond4dbff92007-04-04 14:08:15 -04001152 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
Chris Masone089f052007-03-16 16:20:31 -04001153 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001154 BUG_ON(sret > 0);
1155 if (sret)
1156 return sret;
1157 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001158 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001159 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001160 int sret = balance_level(trans, root, p,
1161 level);
Chris Masonbb803952007-03-01 12:04:21 -05001162 if (sret)
1163 return sret;
1164 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001165 if (!b) {
1166 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001167 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001168 }
Chris Masonbb803952007-03-01 12:04:21 -05001169 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001170 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001171 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001172 /* this is only true while dropping a snapshot */
1173 if (level == lowest_level)
1174 break;
Chris Masondb945352007-10-15 16:15:53 -04001175 bytenr = btrfs_node_blockptr(b, slot);
Chris Mason74493f72007-12-11 09:25:06 -05001176 ptr_gen = btrfs_node_ptr_generation(b, slot);
Chris Mason6702ed42007-08-07 16:15:09 -04001177 if (should_reada)
Chris Mason01f46652007-12-21 16:24:26 -05001178 reada_for_search(root, p, level, slot,
1179 key->objectid);
Chris Masondb945352007-10-15 16:15:53 -04001180 b = read_tree_block(root, bytenr,
1181 btrfs_level_size(root, level - 1));
Chris Mason74493f72007-12-11 09:25:06 -05001182 if (ptr_gen != btrfs_header_generation(b)) {
1183 printk("block %llu bad gen wanted %llu "
1184 "found %llu\n",
1185 (unsigned long long)b->start,
1186 (unsigned long long)ptr_gen,
1187 (unsigned long long)btrfs_header_generation(b));
1188 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001189 } else {
1190 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001191 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001192 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001193 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001194 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001195 BUG_ON(sret > 0);
1196 if (sret)
1197 return sret;
1198 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001199 return ret;
1200 }
1201 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001202 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001203}
1204
Chris Mason74123bd2007-02-02 11:05:29 -05001205/*
1206 * adjust the pointers going up the tree, starting at level
1207 * making sure the right key of each node is points to 'key'.
1208 * This is used after shifting pointers to the left, so it stops
1209 * fixing up pointers when a given leaf/node is not in slot 0 of the
1210 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001211 *
1212 * If this fails to write a tree block, it returns -1, but continues
1213 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001214 */
Chris Mason5f39d392007-10-15 16:14:19 -04001215static int fixup_low_keys(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root, struct btrfs_path *path,
1217 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001218{
1219 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001220 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001221 struct extent_buffer *t;
1222
Chris Mason234b63a2007-03-13 10:46:10 -04001223 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001224 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001225 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001226 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001227 t = path->nodes[i];
1228 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001229 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001230 if (tslot != 0)
1231 break;
1232 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001233 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001234}
1235
Chris Mason74123bd2007-02-02 11:05:29 -05001236/*
1237 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001238 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001239 *
1240 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1241 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001242 */
Chris Mason98ed5172008-01-03 10:01:48 -05001243static int push_node_left(struct btrfs_trans_handle *trans,
1244 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason5f39d392007-10-15 16:14:19 -04001245 struct extent_buffer *src)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001246{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001247 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001248 int src_nritems;
1249 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001250 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001251
Chris Mason5f39d392007-10-15 16:14:19 -04001252 src_nritems = btrfs_header_nritems(src);
1253 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001254 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001255 WARN_ON(btrfs_header_generation(src) != trans->transid);
1256 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001257
Chris Masoneb60cea2007-02-02 09:18:22 -05001258 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001259 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001260 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001261
1262 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -05001263 push_items = src_nritems;
1264
Chris Mason5f39d392007-10-15 16:14:19 -04001265 copy_extent_buffer(dst, src,
1266 btrfs_node_key_ptr_offset(dst_nritems),
1267 btrfs_node_key_ptr_offset(0),
1268 push_items * sizeof(struct btrfs_key_ptr));
1269
Chris Masonbb803952007-03-01 12:04:21 -05001270 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001271 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1272 btrfs_node_key_ptr_offset(push_items),
1273 (src_nritems - push_items) *
1274 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001275 }
Chris Mason5f39d392007-10-15 16:14:19 -04001276 btrfs_set_header_nritems(src, src_nritems - push_items);
1277 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1278 btrfs_mark_buffer_dirty(src);
1279 btrfs_mark_buffer_dirty(dst);
Chris Masonbb803952007-03-01 12:04:21 -05001280 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001281}
1282
Chris Mason97571fd2007-02-24 13:39:08 -05001283/*
Chris Mason79f95c82007-03-01 15:16:26 -05001284 * try to push data from one node into the next node right in the
1285 * tree.
1286 *
1287 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1288 * error, and > 0 if there was no room in the right hand block.
1289 *
1290 * this will only push up to 1/2 the contents of the left node over
1291 */
Chris Mason5f39d392007-10-15 16:14:19 -04001292static int balance_node_right(struct btrfs_trans_handle *trans,
1293 struct btrfs_root *root,
1294 struct extent_buffer *dst,
1295 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001296{
Chris Mason79f95c82007-03-01 15:16:26 -05001297 int push_items = 0;
1298 int max_push;
1299 int src_nritems;
1300 int dst_nritems;
1301 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001302
Chris Mason7bb86312007-12-11 09:25:06 -05001303 WARN_ON(btrfs_header_generation(src) != trans->transid);
1304 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1305
Chris Mason5f39d392007-10-15 16:14:19 -04001306 src_nritems = btrfs_header_nritems(src);
1307 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001308 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001309 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05001310 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05001311
1312 max_push = src_nritems / 2 + 1;
1313 /* don't try to empty the node */
Yan252c38f2007-08-29 09:11:44 -04001314 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05001315 return 1;
Yan252c38f2007-08-29 09:11:44 -04001316
Chris Mason79f95c82007-03-01 15:16:26 -05001317 if (max_push < push_items)
1318 push_items = max_push;
1319
Chris Mason5f39d392007-10-15 16:14:19 -04001320 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1321 btrfs_node_key_ptr_offset(0),
1322 (dst_nritems) *
1323 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001324
Chris Mason5f39d392007-10-15 16:14:19 -04001325 copy_extent_buffer(dst, src,
1326 btrfs_node_key_ptr_offset(0),
1327 btrfs_node_key_ptr_offset(src_nritems - push_items),
1328 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001329
Chris Mason5f39d392007-10-15 16:14:19 -04001330 btrfs_set_header_nritems(src, src_nritems - push_items);
1331 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001332
Chris Mason5f39d392007-10-15 16:14:19 -04001333 btrfs_mark_buffer_dirty(src);
1334 btrfs_mark_buffer_dirty(dst);
Chris Mason79f95c82007-03-01 15:16:26 -05001335 return ret;
1336}
1337
1338/*
Chris Mason97571fd2007-02-24 13:39:08 -05001339 * helper function to insert a new root level in the tree.
1340 * A new node is allocated, and a single item is inserted to
1341 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001342 *
1343 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001344 */
Chris Mason98ed5172008-01-03 10:01:48 -05001345static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001346 struct btrfs_root *root,
1347 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001348{
Chris Mason7bb86312007-12-11 09:25:06 -05001349 u64 root_gen;
1350 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001351 struct extent_buffer *lower;
1352 struct extent_buffer *c;
1353 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05001354
1355 BUG_ON(path->nodes[level]);
1356 BUG_ON(path->nodes[level-1] != root->node);
1357
Chris Mason7bb86312007-12-11 09:25:06 -05001358 if (root->ref_cows)
1359 root_gen = trans->transid;
1360 else
1361 root_gen = 0;
1362
1363 lower = path->nodes[level-1];
1364 if (level == 1)
1365 btrfs_item_key(lower, &lower_key, 0);
1366 else
1367 btrfs_node_key(lower, &lower_key, 0);
1368
1369 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1370 root->root_key.objectid,
1371 root_gen, lower_key.objectid, level,
Chris Masondb945352007-10-15 16:15:53 -04001372 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001373 if (IS_ERR(c))
1374 return PTR_ERR(c);
1375 memset_extent_buffer(c, 0, 0, root->nodesize);
1376 btrfs_set_header_nritems(c, 1);
1377 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001378 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001379 btrfs_set_header_generation(c, trans->transid);
1380 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001381
Chris Mason5f39d392007-10-15 16:14:19 -04001382 write_extent_buffer(c, root->fs_info->fsid,
1383 (unsigned long)btrfs_header_fsid(c),
1384 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001385
1386 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1387 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1388 BTRFS_UUID_SIZE);
1389
Chris Mason5f39d392007-10-15 16:14:19 -04001390 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001391 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001392 lower_gen = btrfs_header_generation(lower);
1393 WARN_ON(lower_gen == 0);
1394
1395 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001396
1397 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001398
Chris Mason5c680ed2007-02-22 11:39:13 -05001399 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001400 free_extent_buffer(root->node);
1401 root->node = c;
Chris Mason0b86a832008-03-24 15:01:56 -04001402 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001403 extent_buffer_get(c);
1404 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001405 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001406
1407 if (root->ref_cows && lower_gen != trans->transid) {
1408 struct btrfs_path *back_path = btrfs_alloc_path();
1409 int ret;
1410 ret = btrfs_insert_extent_backref(trans,
1411 root->fs_info->extent_root,
1412 path, lower->start,
1413 root->root_key.objectid,
1414 trans->transid, 0, 0);
1415 BUG_ON(ret);
1416 btrfs_free_path(back_path);
1417 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001418 return 0;
1419}
1420
Chris Mason74123bd2007-02-02 11:05:29 -05001421/*
1422 * worker function to insert a single pointer in a node.
1423 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001424 *
Chris Mason74123bd2007-02-02 11:05:29 -05001425 * slot and level indicate where you want the key to go, and
1426 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001427 *
1428 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001429 */
Chris Masone089f052007-03-16 16:20:31 -04001430static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1431 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001432 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001433{
Chris Mason5f39d392007-10-15 16:14:19 -04001434 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001435 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001436
1437 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001438 lower = path->nodes[level];
1439 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001440 if (slot > nritems)
1441 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001442 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001443 BUG();
1444 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001445 memmove_extent_buffer(lower,
1446 btrfs_node_key_ptr_offset(slot + 1),
1447 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001448 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001449 }
Chris Mason5f39d392007-10-15 16:14:19 -04001450 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001451 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001452 WARN_ON(trans->transid == 0);
1453 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001454 btrfs_set_header_nritems(lower, nritems + 1);
1455 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001456 return 0;
1457}
1458
Chris Mason97571fd2007-02-24 13:39:08 -05001459/*
1460 * split the node at the specified level in path in two.
1461 * The path is corrected to point to the appropriate node after the split
1462 *
1463 * Before splitting this tries to make some room in the node by pushing
1464 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001465 *
1466 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001467 */
Chris Masone089f052007-03-16 16:20:31 -04001468static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1469 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001470{
Chris Mason7bb86312007-12-11 09:25:06 -05001471 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001472 struct extent_buffer *c;
1473 struct extent_buffer *split;
1474 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001475 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001476 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001477 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001478 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001479
Chris Mason5f39d392007-10-15 16:14:19 -04001480 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001481 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001482 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001483 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001484 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001485 if (ret)
1486 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001487 } else {
1488 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001489 c = path->nodes[level];
1490 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001491 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1492 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001493 if (ret < 0)
1494 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001495 }
Chris Masone66f7092007-04-20 13:16:02 -04001496
Chris Mason5f39d392007-10-15 16:14:19 -04001497 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001498 if (root->ref_cows)
1499 root_gen = trans->transid;
1500 else
1501 root_gen = 0;
1502
1503 btrfs_node_key(c, &disk_key, 0);
1504 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1505 root->root_key.objectid,
1506 root_gen,
1507 btrfs_disk_key_objectid(&disk_key),
1508 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001509 if (IS_ERR(split))
1510 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001511
Chris Mason5f39d392007-10-15 16:14:19 -04001512 btrfs_set_header_flags(split, btrfs_header_flags(c));
1513 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001514 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001515 btrfs_set_header_generation(split, trans->transid);
1516 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001517 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001518 write_extent_buffer(split, root->fs_info->fsid,
1519 (unsigned long)btrfs_header_fsid(split),
1520 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001521 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1522 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1523 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001524
Chris Mason7518a232007-03-12 12:01:18 -04001525 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001526
1527 copy_extent_buffer(split, c,
1528 btrfs_node_key_ptr_offset(0),
1529 btrfs_node_key_ptr_offset(mid),
1530 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1531 btrfs_set_header_nritems(split, c_nritems - mid);
1532 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001533 ret = 0;
1534
Chris Mason5f39d392007-10-15 16:14:19 -04001535 btrfs_mark_buffer_dirty(c);
1536 btrfs_mark_buffer_dirty(split);
1537
1538 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001539 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001540 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001541 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001542 if (wret)
1543 ret = wret;
1544
Chris Mason5de08d72007-02-24 06:24:44 -05001545 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001546 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001547 free_extent_buffer(c);
1548 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001549 path->slots[level + 1] += 1;
1550 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001551 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001552 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001553 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001554}
1555
Chris Mason74123bd2007-02-02 11:05:29 -05001556/*
1557 * how many bytes are required to store the items in a leaf. start
1558 * and nr indicate which items in the leaf to check. This totals up the
1559 * space used both by the item structs and the item data
1560 */
Chris Mason5f39d392007-10-15 16:14:19 -04001561static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001562{
1563 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001564 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001565 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001566
1567 if (!nr)
1568 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001569 data_len = btrfs_item_end_nr(l, start);
1570 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001571 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001572 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001573 return data_len;
1574}
1575
Chris Mason74123bd2007-02-02 11:05:29 -05001576/*
Chris Masond4dbff92007-04-04 14:08:15 -04001577 * The space between the end of the leaf items and
1578 * the start of the leaf data. IOW, how much room
1579 * the leaf has left for both items and data
1580 */
Chris Mason5f39d392007-10-15 16:14:19 -04001581int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001582{
Chris Mason5f39d392007-10-15 16:14:19 -04001583 int nritems = btrfs_header_nritems(leaf);
1584 int ret;
1585 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1586 if (ret < 0) {
1587 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001588 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001589 leaf_space_used(leaf, 0, nritems), nritems);
1590 }
1591 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001592}
1593
1594/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001595 * push some data in the path leaf to the right, trying to free up at
1596 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001597 *
1598 * returns 1 if the push failed because the other node didn't have enough
1599 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001600 */
Chris Masone089f052007-03-16 16:20:31 -04001601static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001602 *root, struct btrfs_path *path, int data_size,
1603 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001604{
Chris Mason5f39d392007-10-15 16:14:19 -04001605 struct extent_buffer *left = path->nodes[0];
1606 struct extent_buffer *right;
1607 struct extent_buffer *upper;
1608 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001609 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001610 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001611 int free_space;
1612 int push_space = 0;
1613 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001614 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001615 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001616 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001617 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001618 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001619 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001620 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001621
1622 slot = path->slots[1];
1623 if (!path->nodes[1]) {
1624 return 1;
1625 }
1626 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001627 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001628 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001629
Chris Masondb945352007-10-15 16:15:53 -04001630 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1631 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001632 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001633 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001634 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001635 return 1;
1636 }
1637
Chris Mason5f39d392007-10-15 16:14:19 -04001638 /* cow and double check */
1639 ret = btrfs_cow_block(trans, root, right, upper,
1640 slot + 1, &right);
1641 if (ret) {
1642 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001643 return 1;
1644 }
Chris Mason5f39d392007-10-15 16:14:19 -04001645 free_space = btrfs_leaf_free_space(root, right);
1646 if (free_space < data_size + sizeof(struct btrfs_item)) {
1647 free_extent_buffer(right);
1648 return 1;
1649 }
1650
1651 left_nritems = btrfs_header_nritems(left);
1652 if (left_nritems == 0) {
1653 free_extent_buffer(right);
1654 return 1;
1655 }
1656
Chris Mason34a38212007-11-07 13:31:03 -05001657 if (empty)
1658 nr = 0;
1659 else
1660 nr = 1;
1661
1662 i = left_nritems - 1;
1663 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001664 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001665
Chris Mason00ec4c52007-02-24 12:47:20 -05001666 if (path->slots[0] == i)
1667 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001668
1669 if (!left->map_token) {
1670 map_extent_buffer(left, (unsigned long)item,
1671 sizeof(struct btrfs_item),
1672 &left->map_token, &left->kaddr,
1673 &left->map_start, &left->map_len,
1674 KM_USER1);
1675 }
1676
1677 this_item_size = btrfs_item_size(left, item);
1678 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001679 break;
1680 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001681 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001682 if (i == 0)
1683 break;
1684 i--;
Chris Masondb945352007-10-15 16:15:53 -04001685 }
1686 if (left->map_token) {
1687 unmap_extent_buffer(left, left->map_token, KM_USER1);
1688 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001689 }
Chris Mason5f39d392007-10-15 16:14:19 -04001690
Chris Mason00ec4c52007-02-24 12:47:20 -05001691 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001692 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001693 return 1;
1694 }
Chris Mason5f39d392007-10-15 16:14:19 -04001695
Chris Mason34a38212007-11-07 13:31:03 -05001696 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001697 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001698
Chris Mason00ec4c52007-02-24 12:47:20 -05001699 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001700 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001701
Chris Mason5f39d392007-10-15 16:14:19 -04001702 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001703 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001704
Chris Mason00ec4c52007-02-24 12:47:20 -05001705 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001706 data_end = leaf_data_end(root, right);
1707 memmove_extent_buffer(right,
1708 btrfs_leaf_data(right) + data_end - push_space,
1709 btrfs_leaf_data(right) + data_end,
1710 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1711
Chris Mason00ec4c52007-02-24 12:47:20 -05001712 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001713 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001714 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1715 btrfs_leaf_data(left) + leaf_data_end(root, left),
1716 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001717
1718 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1719 btrfs_item_nr_offset(0),
1720 right_nritems * sizeof(struct btrfs_item));
1721
Chris Mason00ec4c52007-02-24 12:47:20 -05001722 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001723 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1724 btrfs_item_nr_offset(left_nritems - push_items),
1725 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001726
1727 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001728 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001729 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001730 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001731 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001732 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001733 if (!right->map_token) {
1734 map_extent_buffer(right, (unsigned long)item,
1735 sizeof(struct btrfs_item),
1736 &right->map_token, &right->kaddr,
1737 &right->map_start, &right->map_len,
1738 KM_USER1);
1739 }
1740 push_space -= btrfs_item_size(right, item);
1741 btrfs_set_item_offset(right, item, push_space);
1742 }
1743
1744 if (right->map_token) {
1745 unmap_extent_buffer(right, right->map_token, KM_USER1);
1746 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001747 }
Chris Mason7518a232007-03-12 12:01:18 -04001748 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001749 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001750
Chris Mason34a38212007-11-07 13:31:03 -05001751 if (left_nritems)
1752 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001753 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001754
Chris Mason5f39d392007-10-15 16:14:19 -04001755 btrfs_item_key(right, &disk_key, 0);
1756 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001757 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001758
Chris Mason00ec4c52007-02-24 12:47:20 -05001759 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001760 if (path->slots[0] >= left_nritems) {
1761 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001762 free_extent_buffer(path->nodes[0]);
1763 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001764 path->slots[1] += 1;
1765 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001766 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001767 }
1768 return 0;
1769}
1770/*
Chris Mason74123bd2007-02-02 11:05:29 -05001771 * push some data in the path leaf to the left, trying to free up at
1772 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1773 */
Chris Masone089f052007-03-16 16:20:31 -04001774static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001775 *root, struct btrfs_path *path, int data_size,
1776 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001777{
Chris Mason5f39d392007-10-15 16:14:19 -04001778 struct btrfs_disk_key disk_key;
1779 struct extent_buffer *right = path->nodes[0];
1780 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001781 int slot;
1782 int i;
1783 int free_space;
1784 int push_space = 0;
1785 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001786 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001787 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001788 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001789 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001790 int ret = 0;
1791 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001792 u32 this_item_size;
1793 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001794
1795 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001796 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001797 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001798 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001799 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001800
Chris Mason3685f792007-10-19 09:23:27 -04001801 right_nritems = btrfs_header_nritems(right);
1802 if (right_nritems == 0) {
1803 return 1;
1804 }
1805
Chris Mason5f39d392007-10-15 16:14:19 -04001806 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001807 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001808 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001809 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001810 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001811 return 1;
1812 }
Chris Mason02217ed2007-03-02 16:08:05 -05001813
1814 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001815 ret = btrfs_cow_block(trans, root, left,
1816 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001817 if (ret) {
1818 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001819 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001820 return 1;
1821 }
Chris Mason3685f792007-10-19 09:23:27 -04001822
Chris Mason123abc82007-03-14 14:14:43 -04001823 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001824 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001825 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001826 return 1;
1827 }
1828
Chris Mason34a38212007-11-07 13:31:03 -05001829 if (empty)
1830 nr = right_nritems;
1831 else
1832 nr = right_nritems - 1;
1833
1834 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001835 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001836 if (!right->map_token) {
1837 map_extent_buffer(right, (unsigned long)item,
1838 sizeof(struct btrfs_item),
1839 &right->map_token, &right->kaddr,
1840 &right->map_start, &right->map_len,
1841 KM_USER1);
1842 }
1843
Chris Masonbe0e5c02007-01-26 15:51:26 -05001844 if (path->slots[0] == i)
1845 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001846
1847 this_item_size = btrfs_item_size(right, item);
1848 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001849 break;
Chris Masondb945352007-10-15 16:15:53 -04001850
Chris Masonbe0e5c02007-01-26 15:51:26 -05001851 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001852 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001853 }
Chris Masondb945352007-10-15 16:15:53 -04001854
1855 if (right->map_token) {
1856 unmap_extent_buffer(right, right->map_token, KM_USER1);
1857 right->map_token = NULL;
1858 }
1859
Chris Masonbe0e5c02007-01-26 15:51:26 -05001860 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001861 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001862 return 1;
1863 }
Chris Mason34a38212007-11-07 13:31:03 -05001864 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001865 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001866
Chris Masonbe0e5c02007-01-26 15:51:26 -05001867 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001868 copy_extent_buffer(left, right,
1869 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1870 btrfs_item_nr_offset(0),
1871 push_items * sizeof(struct btrfs_item));
1872
Chris Mason123abc82007-03-14 14:14:43 -04001873 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001874 btrfs_item_offset_nr(right, push_items -1);
1875
1876 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001877 leaf_data_end(root, left) - push_space,
1878 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001879 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001880 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001881 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001882 BUG_ON(old_left_nritems < 0);
1883
Chris Masondb945352007-10-15 16:15:53 -04001884 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001885 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001886 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001887
Chris Mason5f39d392007-10-15 16:14:19 -04001888 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001889 if (!left->map_token) {
1890 map_extent_buffer(left, (unsigned long)item,
1891 sizeof(struct btrfs_item),
1892 &left->map_token, &left->kaddr,
1893 &left->map_start, &left->map_len,
1894 KM_USER1);
1895 }
1896
Chris Mason5f39d392007-10-15 16:14:19 -04001897 ioff = btrfs_item_offset(left, item);
1898 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001899 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001900 }
Chris Mason5f39d392007-10-15 16:14:19 -04001901 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001902 if (left->map_token) {
1903 unmap_extent_buffer(left, left->map_token, KM_USER1);
1904 left->map_token = NULL;
1905 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001906
1907 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001908 if (push_items > right_nritems) {
1909 printk("push items %d nr %u\n", push_items, right_nritems);
1910 WARN_ON(1);
1911 }
Chris Mason5f39d392007-10-15 16:14:19 -04001912
Chris Mason34a38212007-11-07 13:31:03 -05001913 if (push_items < right_nritems) {
1914 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1915 leaf_data_end(root, right);
1916 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1917 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1918 btrfs_leaf_data(right) +
1919 leaf_data_end(root, right), push_space);
1920
1921 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001922 btrfs_item_nr_offset(push_items),
1923 (btrfs_header_nritems(right) - push_items) *
1924 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001925 }
Yaneef1c492007-11-26 10:58:13 -05001926 right_nritems -= push_items;
1927 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001928 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001929 for (i = 0; i < right_nritems; i++) {
1930 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001931
1932 if (!right->map_token) {
1933 map_extent_buffer(right, (unsigned long)item,
1934 sizeof(struct btrfs_item),
1935 &right->map_token, &right->kaddr,
1936 &right->map_start, &right->map_len,
1937 KM_USER1);
1938 }
1939
1940 push_space = push_space - btrfs_item_size(right, item);
1941 btrfs_set_item_offset(right, item, push_space);
1942 }
1943 if (right->map_token) {
1944 unmap_extent_buffer(right, right->map_token, KM_USER1);
1945 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001946 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001947
Chris Mason5f39d392007-10-15 16:14:19 -04001948 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001949 if (right_nritems)
1950 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001951
Chris Mason5f39d392007-10-15 16:14:19 -04001952 btrfs_item_key(right, &disk_key, 0);
1953 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001954 if (wret)
1955 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001956
1957 /* then fixup the leaf pointer in the path */
1958 if (path->slots[0] < push_items) {
1959 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001960 free_extent_buffer(path->nodes[0]);
1961 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001962 path->slots[1] -= 1;
1963 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001964 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001965 path->slots[0] -= push_items;
1966 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001967 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001968 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001969}
1970
Chris Mason74123bd2007-02-02 11:05:29 -05001971/*
1972 * split the path's leaf in two, making sure there is at least data_size
1973 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001974 *
1975 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001976 */
Chris Masone089f052007-03-16 16:20:31 -04001977static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001978 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001979 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001980{
Chris Mason7bb86312007-12-11 09:25:06 -05001981 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001982 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001983 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001984 int mid;
1985 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001986 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001987 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001988 int data_copy_size;
1989 int rt_data_off;
1990 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001991 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001992 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001993 int double_split;
1994 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001995 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001996
Chris Masoncc0c5532007-10-25 15:42:57 -04001997 if (extend)
1998 space_needed = data_size;
1999
Chris Mason7bb86312007-12-11 09:25:06 -05002000 if (root->ref_cows)
2001 root_gen = trans->transid;
2002 else
2003 root_gen = 0;
2004
Chris Mason40689472007-03-17 14:29:23 -04002005 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002006 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002007 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002008 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002009 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002010 }
2011 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002012 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002013 if (wret < 0)
2014 return wret;
2015 }
2016 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002017
Chris Mason3685f792007-10-19 09:23:27 -04002018 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002019 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002020 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002021 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002022
Chris Mason5c680ed2007-02-22 11:39:13 -05002023 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002024 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002025 if (ret)
2026 return ret;
2027 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002028again:
2029 double_split = 0;
2030 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002031 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002032 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002033 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002034
Chris Mason7bb86312007-12-11 09:25:06 -05002035 btrfs_item_key(l, &disk_key, 0);
2036
2037 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2038 root->root_key.objectid,
2039 root_gen, disk_key.objectid, 0,
2040 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002041 if (IS_ERR(right)) {
2042 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002043 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002044 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002045
Chris Mason5f39d392007-10-15 16:14:19 -04002046 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002047 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002048 btrfs_set_header_generation(right, trans->transid);
2049 btrfs_set_header_owner(right, root->root_key.objectid);
2050 btrfs_set_header_level(right, 0);
2051 write_extent_buffer(right, root->fs_info->fsid,
2052 (unsigned long)btrfs_header_fsid(right),
2053 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002054
2055 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2056 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2057 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002058 if (mid <= slot) {
2059 if (nritems == 1 ||
2060 leaf_space_used(l, mid, nritems - mid) + space_needed >
2061 BTRFS_LEAF_DATA_SIZE(root)) {
2062 if (slot >= nritems) {
2063 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,
Chris Masondb945352007-10-15 16:15:53 -04002066 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002067 path->slots[1] + 1, 1);
2068 if (wret)
2069 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002070 free_extent_buffer(path->nodes[0]);
2071 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002072 path->slots[0] = 0;
2073 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002074 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002075 return ret;
2076 }
2077 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002078 if (mid != nritems &&
2079 leaf_space_used(l, mid, nritems - mid) +
2080 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2081 double_split = 1;
2082 }
Chris Masond4dbff92007-04-04 14:08:15 -04002083 }
2084 } else {
2085 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2086 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002087 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002088 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002089 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002090 wret = insert_ptr(trans, root, path,
2091 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002092 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002093 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002094 if (wret)
2095 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002096 free_extent_buffer(path->nodes[0]);
2097 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002098 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002099 if (path->slots[1] == 0) {
2100 wret = fixup_low_keys(trans, root,
2101 path, &disk_key, 1);
2102 if (wret)
2103 ret = wret;
2104 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002105 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002106 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002107 } else if (extend && slot == 0) {
2108 mid = 1;
2109 } else {
2110 mid = slot;
2111 if (mid != nritems &&
2112 leaf_space_used(l, mid, nritems - mid) +
2113 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2114 double_split = 1;
2115 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002116 }
Chris Masond4dbff92007-04-04 14:08:15 -04002117 }
2118 }
Chris Mason5f39d392007-10-15 16:14:19 -04002119 nritems = nritems - mid;
2120 btrfs_set_header_nritems(right, nritems);
2121 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2122
2123 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2124 btrfs_item_nr_offset(mid),
2125 nritems * sizeof(struct btrfs_item));
2126
2127 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002128 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2129 data_copy_size, btrfs_leaf_data(l) +
2130 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002131
Chris Mason5f39d392007-10-15 16:14:19 -04002132 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2133 btrfs_item_end_nr(l, mid);
2134
2135 for (i = 0; i < nritems; i++) {
2136 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002137 u32 ioff;
2138
2139 if (!right->map_token) {
2140 map_extent_buffer(right, (unsigned long)item,
2141 sizeof(struct btrfs_item),
2142 &right->map_token, &right->kaddr,
2143 &right->map_start, &right->map_len,
2144 KM_USER1);
2145 }
2146
2147 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002148 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002149 }
Chris Mason74123bd2007-02-02 11:05:29 -05002150
Chris Masondb945352007-10-15 16:15:53 -04002151 if (right->map_token) {
2152 unmap_extent_buffer(right, right->map_token, KM_USER1);
2153 right->map_token = NULL;
2154 }
2155
Chris Mason5f39d392007-10-15 16:14:19 -04002156 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002157 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002158 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002159 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2160 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002161 if (wret)
2162 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002163
2164 btrfs_mark_buffer_dirty(right);
2165 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002166 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002167
Chris Masonbe0e5c02007-01-26 15:51:26 -05002168 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002169 free_extent_buffer(path->nodes[0]);
2170 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002171 path->slots[0] -= mid;
2172 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002173 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002174 free_extent_buffer(right);
2175
Chris Masoneb60cea2007-02-02 09:18:22 -05002176 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002177
Chris Masoncc0c5532007-10-25 15:42:57 -04002178 if (double_split) {
2179 BUG_ON(num_doubles != 0);
2180 num_doubles++;
2181 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002182 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002183 return ret;
2184}
2185
Chris Masonb18c6682007-04-17 13:26:50 -04002186int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2187 struct btrfs_root *root,
2188 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002189 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002190{
2191 int ret = 0;
2192 int slot;
2193 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002194 struct extent_buffer *leaf;
2195 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002196 u32 nritems;
2197 unsigned int data_end;
2198 unsigned int old_data_start;
2199 unsigned int old_size;
2200 unsigned int size_diff;
2201 int i;
2202
2203 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002204 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002205 slot = path->slots[0];
2206
2207 old_size = btrfs_item_size_nr(leaf, slot);
2208 if (old_size == new_size)
2209 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002210
Chris Mason5f39d392007-10-15 16:14:19 -04002211 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002212 data_end = leaf_data_end(root, leaf);
2213
Chris Mason5f39d392007-10-15 16:14:19 -04002214 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002215
Chris Masonb18c6682007-04-17 13:26:50 -04002216 size_diff = old_size - new_size;
2217
2218 BUG_ON(slot < 0);
2219 BUG_ON(slot >= nritems);
2220
2221 /*
2222 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2223 */
2224 /* first correct the data pointers */
2225 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002226 u32 ioff;
2227 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002228
2229 if (!leaf->map_token) {
2230 map_extent_buffer(leaf, (unsigned long)item,
2231 sizeof(struct btrfs_item),
2232 &leaf->map_token, &leaf->kaddr,
2233 &leaf->map_start, &leaf->map_len,
2234 KM_USER1);
2235 }
2236
Chris Mason5f39d392007-10-15 16:14:19 -04002237 ioff = btrfs_item_offset(leaf, item);
2238 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002239 }
Chris Masondb945352007-10-15 16:15:53 -04002240
2241 if (leaf->map_token) {
2242 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2243 leaf->map_token = NULL;
2244 }
2245
Chris Masonb18c6682007-04-17 13:26:50 -04002246 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002247 if (from_end) {
2248 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2249 data_end + size_diff, btrfs_leaf_data(leaf) +
2250 data_end, old_data_start + new_size - data_end);
2251 } else {
2252 struct btrfs_disk_key disk_key;
2253 u64 offset;
2254
2255 btrfs_item_key(leaf, &disk_key, slot);
2256
2257 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2258 unsigned long ptr;
2259 struct btrfs_file_extent_item *fi;
2260
2261 fi = btrfs_item_ptr(leaf, slot,
2262 struct btrfs_file_extent_item);
2263 fi = (struct btrfs_file_extent_item *)(
2264 (unsigned long)fi - size_diff);
2265
2266 if (btrfs_file_extent_type(leaf, fi) ==
2267 BTRFS_FILE_EXTENT_INLINE) {
2268 ptr = btrfs_item_ptr_offset(leaf, slot);
2269 memmove_extent_buffer(leaf, ptr,
2270 (unsigned long)fi,
2271 offsetof(struct btrfs_file_extent_item,
2272 disk_bytenr));
2273 }
2274 }
2275
2276 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2277 data_end + size_diff, btrfs_leaf_data(leaf) +
2278 data_end, old_data_start - data_end);
2279
2280 offset = btrfs_disk_key_offset(&disk_key);
2281 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2282 btrfs_set_item_key(leaf, &disk_key, slot);
2283 if (slot == 0)
2284 fixup_low_keys(trans, root, path, &disk_key, 1);
2285 }
Chris Mason5f39d392007-10-15 16:14:19 -04002286
2287 item = btrfs_item_nr(leaf, slot);
2288 btrfs_set_item_size(leaf, item, new_size);
2289 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002290
2291 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002292 if (btrfs_leaf_free_space(root, leaf) < 0) {
2293 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002294 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002295 }
Chris Masonb18c6682007-04-17 13:26:50 -04002296 return ret;
2297}
2298
Chris Mason5f39d392007-10-15 16:14:19 -04002299int btrfs_extend_item(struct btrfs_trans_handle *trans,
2300 struct btrfs_root *root, struct btrfs_path *path,
2301 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002302{
2303 int ret = 0;
2304 int slot;
2305 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002306 struct extent_buffer *leaf;
2307 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002308 u32 nritems;
2309 unsigned int data_end;
2310 unsigned int old_data;
2311 unsigned int old_size;
2312 int i;
2313
2314 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002315 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002316
Chris Mason5f39d392007-10-15 16:14:19 -04002317 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002318 data_end = leaf_data_end(root, leaf);
2319
Chris Mason5f39d392007-10-15 16:14:19 -04002320 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2321 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002322 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002323 }
Chris Mason6567e832007-04-16 09:22:45 -04002324 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002325 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002326
2327 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002328 if (slot >= nritems) {
2329 btrfs_print_leaf(root, leaf);
2330 printk("slot %d too large, nritems %d\n", slot, nritems);
2331 BUG_ON(1);
2332 }
Chris Mason6567e832007-04-16 09:22:45 -04002333
2334 /*
2335 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2336 */
2337 /* first correct the data pointers */
2338 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002339 u32 ioff;
2340 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002341
2342 if (!leaf->map_token) {
2343 map_extent_buffer(leaf, (unsigned long)item,
2344 sizeof(struct btrfs_item),
2345 &leaf->map_token, &leaf->kaddr,
2346 &leaf->map_start, &leaf->map_len,
2347 KM_USER1);
2348 }
Chris Mason5f39d392007-10-15 16:14:19 -04002349 ioff = btrfs_item_offset(leaf, item);
2350 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002351 }
Chris Mason5f39d392007-10-15 16:14:19 -04002352
Chris Masondb945352007-10-15 16:15:53 -04002353 if (leaf->map_token) {
2354 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2355 leaf->map_token = NULL;
2356 }
2357
Chris Mason6567e832007-04-16 09:22:45 -04002358 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002359 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002360 data_end - data_size, btrfs_leaf_data(leaf) +
2361 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002362
Chris Mason6567e832007-04-16 09:22:45 -04002363 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002364 old_size = btrfs_item_size_nr(leaf, slot);
2365 item = btrfs_item_nr(leaf, slot);
2366 btrfs_set_item_size(leaf, item, old_size + data_size);
2367 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002368
2369 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002370 if (btrfs_leaf_free_space(root, leaf) < 0) {
2371 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002372 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002373 }
Chris Mason6567e832007-04-16 09:22:45 -04002374 return ret;
2375}
2376
Chris Mason74123bd2007-02-02 11:05:29 -05002377/*
2378 * Given a key and some data, insert an item into the tree.
2379 * This does all the path init required, making room in the tree if needed.
2380 */
Chris Mason9c583092008-01-29 15:15:18 -05002381int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002382 struct btrfs_root *root,
2383 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002384 struct btrfs_key *cpu_key, u32 *data_size,
2385 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002386{
Chris Mason5f39d392007-10-15 16:14:19 -04002387 struct extent_buffer *leaf;
2388 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002389 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002390 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002391 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002392 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002393 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002394 u32 total_size = 0;
2395 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002396 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002397 struct btrfs_disk_key disk_key;
2398
Chris Mason9c583092008-01-29 15:15:18 -05002399 for (i = 0; i < nr; i++) {
2400 total_data += data_size[i];
2401 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002402
Chris Mason74123bd2007-02-02 11:05:29 -05002403 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002404 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002405 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002406
Chris Mason9c583092008-01-29 15:15:18 -05002407 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2408 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002409 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002410 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002411 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002412 if (ret < 0)
2413 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002414
Chris Mason62e27492007-03-15 12:56:47 -04002415 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002416 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002417
Chris Mason5f39d392007-10-15 16:14:19 -04002418 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002419 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002420
Chris Mason123abc82007-03-14 14:14:43 -04002421 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002422 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002423 btrfs_print_leaf(root, leaf);
2424 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002425 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002426 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002427 }
Chris Mason5f39d392007-10-15 16:14:19 -04002428
Chris Mason62e27492007-03-15 12:56:47 -04002429 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002430 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002431
Chris Masonbe0e5c02007-01-26 15:51:26 -05002432 if (slot != nritems) {
2433 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002434 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002435
Chris Mason5f39d392007-10-15 16:14:19 -04002436 if (old_data < data_end) {
2437 btrfs_print_leaf(root, leaf);
2438 printk("slot %d old_data %d data_end %d\n",
2439 slot, old_data, data_end);
2440 BUG_ON(1);
2441 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002442 /*
2443 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2444 */
2445 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002446 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002447 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002448 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002449
Chris Mason5f39d392007-10-15 16:14:19 -04002450 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002451 if (!leaf->map_token) {
2452 map_extent_buffer(leaf, (unsigned long)item,
2453 sizeof(struct btrfs_item),
2454 &leaf->map_token, &leaf->kaddr,
2455 &leaf->map_start, &leaf->map_len,
2456 KM_USER1);
2457 }
2458
Chris Mason5f39d392007-10-15 16:14:19 -04002459 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002460 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002461 }
Chris Masondb945352007-10-15 16:15:53 -04002462 if (leaf->map_token) {
2463 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2464 leaf->map_token = NULL;
2465 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002466
2467 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002468 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002469 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002470 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002471
2472 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002473 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002474 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002475 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002476 data_end = old_data;
2477 }
Chris Mason5f39d392007-10-15 16:14:19 -04002478
Chris Mason62e27492007-03-15 12:56:47 -04002479 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002480 for (i = 0; i < nr; i++) {
2481 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2482 btrfs_set_item_key(leaf, &disk_key, slot + i);
2483 item = btrfs_item_nr(leaf, slot + i);
2484 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2485 data_end -= data_size[i];
2486 btrfs_set_item_size(leaf, item, data_size[i]);
2487 }
2488 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002489 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002490
2491 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002492 if (slot == 0) {
2493 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002494 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002495 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002496
Chris Mason5f39d392007-10-15 16:14:19 -04002497 if (btrfs_leaf_free_space(root, leaf) < 0) {
2498 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002499 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002500 }
Chris Mason5a01a2e2008-01-30 11:43:54 -05002501
Chris Masoned2ff2c2007-03-01 18:59:40 -05002502out:
Chris Mason62e27492007-03-15 12:56:47 -04002503 return ret;
2504}
2505
2506/*
2507 * Given a key and some data, insert an item into the tree.
2508 * This does all the path init required, making room in the tree if needed.
2509 */
Chris Masone089f052007-03-16 16:20:31 -04002510int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2511 *root, struct btrfs_key *cpu_key, void *data, u32
2512 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002513{
2514 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002515 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002516 struct extent_buffer *leaf;
2517 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002518
Chris Mason2c90e5d2007-04-02 10:50:19 -04002519 path = btrfs_alloc_path();
2520 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002521 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002522 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002523 leaf = path->nodes[0];
2524 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2525 write_extent_buffer(leaf, data, ptr, data_size);
2526 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002527 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002528 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002529 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002530}
2531
Chris Mason74123bd2007-02-02 11:05:29 -05002532/*
Chris Mason5de08d72007-02-24 06:24:44 -05002533 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002534 *
2535 * If the delete empties a node, the node is removed from the tree,
2536 * continuing all the way the root if required. The root is converted into
2537 * a leaf if all the nodes are emptied.
2538 */
Chris Masone089f052007-03-16 16:20:31 -04002539static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2540 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002541{
Chris Mason5f39d392007-10-15 16:14:19 -04002542 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002543 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002544 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002545 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002546
Chris Mason5f39d392007-10-15 16:14:19 -04002547 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002548 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002549 memmove_extent_buffer(parent,
2550 btrfs_node_key_ptr_offset(slot),
2551 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002552 sizeof(struct btrfs_key_ptr) *
2553 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002554 }
Chris Mason7518a232007-03-12 12:01:18 -04002555 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002556 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002557 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002558 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002559 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002560 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002561 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002562 struct btrfs_disk_key disk_key;
2563
2564 btrfs_node_key(parent, &disk_key, 0);
2565 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002566 if (wret)
2567 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002568 }
Chris Masond6025572007-03-30 14:27:56 -04002569 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002570 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002571}
2572
Chris Mason74123bd2007-02-02 11:05:29 -05002573/*
2574 * delete the item at the leaf level in path. If that empties
2575 * the leaf, remove it from the tree
2576 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002577int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2578 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002579{
Chris Mason5f39d392007-10-15 16:14:19 -04002580 struct extent_buffer *leaf;
2581 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002582 int last_off;
2583 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002584 int ret = 0;
2585 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002586 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002587 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002588
Chris Mason5f39d392007-10-15 16:14:19 -04002589 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002590 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2591
2592 for (i = 0; i < nr; i++)
2593 dsize += btrfs_item_size_nr(leaf, slot + i);
2594
Chris Mason5f39d392007-10-15 16:14:19 -04002595 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002596
Chris Mason85e21ba2008-01-29 15:11:36 -05002597 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002599 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002600
2601 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002602 data_end + dsize,
2603 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002604 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002605
Chris Mason85e21ba2008-01-29 15:11:36 -05002606 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002607 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002608
Chris Mason5f39d392007-10-15 16:14:19 -04002609 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002610 if (!leaf->map_token) {
2611 map_extent_buffer(leaf, (unsigned long)item,
2612 sizeof(struct btrfs_item),
2613 &leaf->map_token, &leaf->kaddr,
2614 &leaf->map_start, &leaf->map_len,
2615 KM_USER1);
2616 }
Chris Mason5f39d392007-10-15 16:14:19 -04002617 ioff = btrfs_item_offset(leaf, item);
2618 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002619 }
Chris Masondb945352007-10-15 16:15:53 -04002620
2621 if (leaf->map_token) {
2622 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2623 leaf->map_token = NULL;
2624 }
2625
Chris Mason5f39d392007-10-15 16:14:19 -04002626 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002627 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002628 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002629 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002630 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002631 btrfs_set_header_nritems(leaf, nritems - nr);
2632 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002633
Chris Mason74123bd2007-02-02 11:05:29 -05002634 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002635 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002636 if (leaf == root->node) {
2637 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002638 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002639 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002640 clean_tree_block(trans, root, leaf);
2641 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002642 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002643 if (wret)
2644 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002645 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002646 leaf->start, leaf->len,
2647 btrfs_header_owner(path->nodes[1]),
2648 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002649 if (wret)
2650 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002651 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002652 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002653 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002654 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002655 struct btrfs_disk_key disk_key;
2656
2657 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002658 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002659 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002660 if (wret)
2661 ret = wret;
2662 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002663
Chris Mason74123bd2007-02-02 11:05:29 -05002664 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002665 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002666 /* push_leaf_left fixes the path.
2667 * make sure the path still points to our leaf
2668 * for possible call to del_ptr below
2669 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002670 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002671 extent_buffer_get(leaf);
2672
Chris Mason85e21ba2008-01-29 15:11:36 -05002673 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002674 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002675 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002676
2677 if (path->nodes[0] == leaf &&
2678 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002679 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002680 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002681 ret = wret;
2682 }
Chris Mason5f39d392007-10-15 16:14:19 -04002683
2684 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002685 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002686 u64 bytenr = leaf->start;
2687 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002688
Chris Mason7bb86312007-12-11 09:25:06 -05002689 root_gen = btrfs_header_generation(
2690 path->nodes[1]);
2691
Chris Mason5f39d392007-10-15 16:14:19 -04002692 clean_tree_block(trans, root, leaf);
2693 wait_on_tree_block_writeback(root, leaf);
2694
Chris Masone089f052007-03-16 16:20:31 -04002695 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002696 if (wret)
2697 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002698
2699 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002700 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002701 blocksize,
2702 btrfs_header_owner(path->nodes[1]),
2703 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002704 if (wret)
2705 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002706 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002707 btrfs_mark_buffer_dirty(leaf);
2708 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002709 }
Chris Masond5719762007-03-23 10:01:08 -04002710 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002711 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002712 }
2713 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002714 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002715}
2716
Chris Mason97571fd2007-02-24 13:39:08 -05002717/*
Chris Mason7bb86312007-12-11 09:25:06 -05002718 * walk up the tree as far as required to find the previous leaf.
2719 * returns 0 if it found something or 1 if there are no lesser leaves.
2720 * returns < 0 on io errors.
2721 */
2722int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2723{
Chris Mason8f662a72008-01-02 10:01:11 -05002724 u64 bytenr;
Chris Mason7bb86312007-12-11 09:25:06 -05002725 int slot;
2726 int level = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002727 struct extent_buffer *c;
2728 struct extent_buffer *next = NULL;
2729
2730 while(level < BTRFS_MAX_LEVEL) {
2731 if (!path->nodes[level])
2732 return 1;
2733
2734 slot = path->slots[level];
2735 c = path->nodes[level];
2736 if (slot == 0) {
2737 level++;
2738 if (level == BTRFS_MAX_LEVEL)
2739 return 1;
2740 continue;
2741 }
2742 slot--;
2743
2744 bytenr = btrfs_node_blockptr(c, slot);
2745 if (next)
2746 free_extent_buffer(next);
2747
Chris Mason7bb86312007-12-11 09:25:06 -05002748 next = read_tree_block(root, bytenr,
2749 btrfs_level_size(root, level - 1));
2750 break;
2751 }
2752 path->slots[level] = slot;
2753 while(1) {
2754 level--;
2755 c = path->nodes[level];
2756 free_extent_buffer(c);
Chris Mason8f662a72008-01-02 10:01:11 -05002757 slot = btrfs_header_nritems(next);
2758 if (slot != 0)
2759 slot--;
Chris Mason7bb86312007-12-11 09:25:06 -05002760 path->nodes[level] = next;
Chris Mason8f662a72008-01-02 10:01:11 -05002761 path->slots[level] = slot;
Chris Mason7bb86312007-12-11 09:25:06 -05002762 if (!level)
2763 break;
Chris Mason8f662a72008-01-02 10:01:11 -05002764 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
Chris Mason7bb86312007-12-11 09:25:06 -05002765 btrfs_level_size(root, level - 1));
2766 }
2767 return 0;
2768}
2769
2770/*
Chris Mason97571fd2007-02-24 13:39:08 -05002771 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002772 * returns 0 if it found something or 1 if there are no greater leaves.
2773 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002774 */
Chris Mason234b63a2007-03-13 10:46:10 -04002775int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002776{
2777 int slot;
2778 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002779 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002780 struct extent_buffer *c;
2781 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002782
Chris Mason234b63a2007-03-13 10:46:10 -04002783 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002784 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002785 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002786
Chris Masond97e63b2007-02-20 16:40:44 -05002787 slot = path->slots[level] + 1;
2788 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002789 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002790 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002791 if (level == BTRFS_MAX_LEVEL)
2792 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002793 continue;
2794 }
Chris Mason5f39d392007-10-15 16:14:19 -04002795
Chris Masondb945352007-10-15 16:15:53 -04002796 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002797 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002798 free_extent_buffer(next);
2799
Chris Mason6702ed42007-08-07 16:15:09 -04002800 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002801 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002802
Chris Masondb945352007-10-15 16:15:53 -04002803 next = read_tree_block(root, bytenr,
2804 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002805 break;
2806 }
2807 path->slots[level] = slot;
2808 while(1) {
2809 level--;
2810 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002811 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002812 path->nodes[level] = next;
2813 path->slots[level] = 0;
2814 if (!level)
2815 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002816 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002817 reada_for_search(root, path, level, 0, 0);
Chris Masondb945352007-10-15 16:15:53 -04002818 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2819 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002820 }
2821 return 0;
2822}
Chris Mason0b86a832008-03-24 15:01:56 -04002823
2824int btrfs_previous_item(struct btrfs_root *root,
2825 struct btrfs_path *path, u64 min_objectid,
2826 int type)
2827{
2828 struct btrfs_key found_key;
2829 struct extent_buffer *leaf;
2830 int ret;
2831
2832 while(1) {
2833 if (path->slots[0] == 0) {
2834 ret = btrfs_prev_leaf(root, path);
2835 if (ret != 0)
2836 return ret;
2837 } else {
2838 path->slots[0]--;
2839 }
2840 leaf = path->nodes[0];
2841 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2842 if (found_key.type == type)
2843 return 0;
2844 }
2845 return 1;
2846}
2847