blob: e8bf6c221e4e4be8260f9ac3e8c83ee186743506 [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 Mason5f39d392007-10-15 16:14:19 -04001385 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001386 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001387 lower_gen = btrfs_header_generation(lower);
1388 WARN_ON(lower_gen == 0);
1389
1390 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001391
1392 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001393
Chris Mason5c680ed2007-02-22 11:39:13 -05001394 /* the super has an extra ref to root->node */
Chris Mason5f39d392007-10-15 16:14:19 -04001395 free_extent_buffer(root->node);
1396 root->node = c;
Chris Mason0b86a832008-03-24 15:01:56 -04001397 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001398 extent_buffer_get(c);
1399 path->nodes[level] = c;
Chris Mason5c680ed2007-02-22 11:39:13 -05001400 path->slots[level] = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001401
1402 if (root->ref_cows && lower_gen != trans->transid) {
1403 struct btrfs_path *back_path = btrfs_alloc_path();
1404 int ret;
1405 ret = btrfs_insert_extent_backref(trans,
1406 root->fs_info->extent_root,
1407 path, lower->start,
1408 root->root_key.objectid,
1409 trans->transid, 0, 0);
1410 BUG_ON(ret);
1411 btrfs_free_path(back_path);
1412 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001413 return 0;
1414}
1415
Chris Mason74123bd2007-02-02 11:05:29 -05001416/*
1417 * worker function to insert a single pointer in a node.
1418 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001419 *
Chris Mason74123bd2007-02-02 11:05:29 -05001420 * slot and level indicate where you want the key to go, and
1421 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001422 *
1423 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001424 */
Chris Masone089f052007-03-16 16:20:31 -04001425static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1426 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001427 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001428{
Chris Mason5f39d392007-10-15 16:14:19 -04001429 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001430 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001431
1432 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001433 lower = path->nodes[level];
1434 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001435 if (slot > nritems)
1436 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001437 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001438 BUG();
1439 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001440 memmove_extent_buffer(lower,
1441 btrfs_node_key_ptr_offset(slot + 1),
1442 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001443 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001444 }
Chris Mason5f39d392007-10-15 16:14:19 -04001445 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001446 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001447 WARN_ON(trans->transid == 0);
1448 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001449 btrfs_set_header_nritems(lower, nritems + 1);
1450 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001451 return 0;
1452}
1453
Chris Mason97571fd2007-02-24 13:39:08 -05001454/*
1455 * split the node at the specified level in path in two.
1456 * The path is corrected to point to the appropriate node after the split
1457 *
1458 * Before splitting this tries to make some room in the node by pushing
1459 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001460 *
1461 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001462 */
Chris Masone089f052007-03-16 16:20:31 -04001463static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1464 *root, struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001465{
Chris Mason7bb86312007-12-11 09:25:06 -05001466 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001467 struct extent_buffer *c;
1468 struct extent_buffer *split;
1469 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001470 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001471 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001472 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001473 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001474
Chris Mason5f39d392007-10-15 16:14:19 -04001475 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001476 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001477 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001478 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001479 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001480 if (ret)
1481 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001482 } else {
1483 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001484 c = path->nodes[level];
1485 if (!ret && btrfs_header_nritems(c) <
Chris Masone66f7092007-04-20 13:16:02 -04001486 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1487 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001488 if (ret < 0)
1489 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001490 }
Chris Masone66f7092007-04-20 13:16:02 -04001491
Chris Mason5f39d392007-10-15 16:14:19 -04001492 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001493 if (root->ref_cows)
1494 root_gen = trans->transid;
1495 else
1496 root_gen = 0;
1497
1498 btrfs_node_key(c, &disk_key, 0);
1499 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1500 root->root_key.objectid,
1501 root_gen,
1502 btrfs_disk_key_objectid(&disk_key),
1503 level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001504 if (IS_ERR(split))
1505 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001506
Chris Mason5f39d392007-10-15 16:14:19 -04001507 btrfs_set_header_flags(split, btrfs_header_flags(c));
1508 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001509 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001510 btrfs_set_header_generation(split, trans->transid);
1511 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001512 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001513 write_extent_buffer(split, root->fs_info->fsid,
1514 (unsigned long)btrfs_header_fsid(split),
1515 BTRFS_FSID_SIZE);
1516
Chris Mason7518a232007-03-12 12:01:18 -04001517 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001518
1519 copy_extent_buffer(split, c,
1520 btrfs_node_key_ptr_offset(0),
1521 btrfs_node_key_ptr_offset(mid),
1522 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1523 btrfs_set_header_nritems(split, c_nritems - mid);
1524 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001525 ret = 0;
1526
Chris Mason5f39d392007-10-15 16:14:19 -04001527 btrfs_mark_buffer_dirty(c);
1528 btrfs_mark_buffer_dirty(split);
1529
1530 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001531 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04001532 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04001533 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001534 if (wret)
1535 ret = wret;
1536
Chris Mason5de08d72007-02-24 06:24:44 -05001537 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001538 path->slots[level] -= mid;
Chris Mason5f39d392007-10-15 16:14:19 -04001539 free_extent_buffer(c);
1540 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05001541 path->slots[level + 1] += 1;
1542 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001543 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001544 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001545 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001546}
1547
Chris Mason74123bd2007-02-02 11:05:29 -05001548/*
1549 * how many bytes are required to store the items in a leaf. start
1550 * and nr indicate which items in the leaf to check. This totals up the
1551 * space used both by the item structs and the item data
1552 */
Chris Mason5f39d392007-10-15 16:14:19 -04001553static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001554{
1555 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04001556 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04001557 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001558
1559 if (!nr)
1560 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001561 data_len = btrfs_item_end_nr(l, start);
1562 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04001563 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04001564 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001565 return data_len;
1566}
1567
Chris Mason74123bd2007-02-02 11:05:29 -05001568/*
Chris Masond4dbff92007-04-04 14:08:15 -04001569 * The space between the end of the leaf items and
1570 * the start of the leaf data. IOW, how much room
1571 * the leaf has left for both items and data
1572 */
Chris Mason5f39d392007-10-15 16:14:19 -04001573int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04001574{
Chris Mason5f39d392007-10-15 16:14:19 -04001575 int nritems = btrfs_header_nritems(leaf);
1576 int ret;
1577 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1578 if (ret < 0) {
1579 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04001580 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04001581 leaf_space_used(leaf, 0, nritems), nritems);
1582 }
1583 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04001584}
1585
1586/*
Chris Mason00ec4c52007-02-24 12:47:20 -05001587 * push some data in the path leaf to the right, trying to free up at
1588 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05001589 *
1590 * returns 1 if the push failed because the other node didn't have enough
1591 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05001592 */
Chris Masone089f052007-03-16 16:20:31 -04001593static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001594 *root, struct btrfs_path *path, int data_size,
1595 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05001596{
Chris Mason5f39d392007-10-15 16:14:19 -04001597 struct extent_buffer *left = path->nodes[0];
1598 struct extent_buffer *right;
1599 struct extent_buffer *upper;
1600 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001601 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05001602 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05001603 int free_space;
1604 int push_space = 0;
1605 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001606 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001607 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001608 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04001609 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001610 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04001611 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04001612 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05001613
1614 slot = path->slots[1];
1615 if (!path->nodes[1]) {
1616 return 1;
1617 }
1618 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001619 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05001620 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001621
Chris Masondb945352007-10-15 16:15:53 -04001622 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1623 root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001624 free_space = btrfs_leaf_free_space(root, right);
Chris Mason0783fcf2007-03-12 20:12:07 -04001625 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001626 free_extent_buffer(right);
Chris Mason02217ed2007-03-02 16:08:05 -05001627 return 1;
1628 }
1629
Chris Mason5f39d392007-10-15 16:14:19 -04001630 /* cow and double check */
1631 ret = btrfs_cow_block(trans, root, right, upper,
1632 slot + 1, &right);
1633 if (ret) {
1634 free_extent_buffer(right);
Chris Masona429e512007-04-18 16:15:28 -04001635 return 1;
1636 }
Chris Mason5f39d392007-10-15 16:14:19 -04001637 free_space = btrfs_leaf_free_space(root, right);
1638 if (free_space < data_size + sizeof(struct btrfs_item)) {
1639 free_extent_buffer(right);
1640 return 1;
1641 }
1642
1643 left_nritems = btrfs_header_nritems(left);
1644 if (left_nritems == 0) {
1645 free_extent_buffer(right);
1646 return 1;
1647 }
1648
Chris Mason34a38212007-11-07 13:31:03 -05001649 if (empty)
1650 nr = 0;
1651 else
1652 nr = 1;
1653
1654 i = left_nritems - 1;
1655 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04001656 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001657
Chris Mason00ec4c52007-02-24 12:47:20 -05001658 if (path->slots[0] == i)
1659 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001660
1661 if (!left->map_token) {
1662 map_extent_buffer(left, (unsigned long)item,
1663 sizeof(struct btrfs_item),
1664 &left->map_token, &left->kaddr,
1665 &left->map_start, &left->map_len,
1666 KM_USER1);
1667 }
1668
1669 this_item_size = btrfs_item_size(left, item);
1670 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05001671 break;
1672 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001673 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05001674 if (i == 0)
1675 break;
1676 i--;
Chris Masondb945352007-10-15 16:15:53 -04001677 }
1678 if (left->map_token) {
1679 unmap_extent_buffer(left, left->map_token, KM_USER1);
1680 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001681 }
Chris Mason5f39d392007-10-15 16:14:19 -04001682
Chris Mason00ec4c52007-02-24 12:47:20 -05001683 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001684 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001685 return 1;
1686 }
Chris Mason5f39d392007-10-15 16:14:19 -04001687
Chris Mason34a38212007-11-07 13:31:03 -05001688 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04001689 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001690
Chris Mason00ec4c52007-02-24 12:47:20 -05001691 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001692 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05001693
Chris Mason5f39d392007-10-15 16:14:19 -04001694 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04001695 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04001696
Chris Mason00ec4c52007-02-24 12:47:20 -05001697 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001698 data_end = leaf_data_end(root, right);
1699 memmove_extent_buffer(right,
1700 btrfs_leaf_data(right) + data_end - push_space,
1701 btrfs_leaf_data(right) + data_end,
1702 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1703
Chris Mason00ec4c52007-02-24 12:47:20 -05001704 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04001705 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04001706 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1707 btrfs_leaf_data(left) + leaf_data_end(root, left),
1708 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001709
1710 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1711 btrfs_item_nr_offset(0),
1712 right_nritems * sizeof(struct btrfs_item));
1713
Chris Mason00ec4c52007-02-24 12:47:20 -05001714 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04001715 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1716 btrfs_item_nr_offset(left_nritems - push_items),
1717 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05001718
1719 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04001720 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001721 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001722 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04001723 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001724 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001725 if (!right->map_token) {
1726 map_extent_buffer(right, (unsigned long)item,
1727 sizeof(struct btrfs_item),
1728 &right->map_token, &right->kaddr,
1729 &right->map_start, &right->map_len,
1730 KM_USER1);
1731 }
1732 push_space -= btrfs_item_size(right, item);
1733 btrfs_set_item_offset(right, item, push_space);
1734 }
1735
1736 if (right->map_token) {
1737 unmap_extent_buffer(right, right->map_token, KM_USER1);
1738 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05001739 }
Chris Mason7518a232007-03-12 12:01:18 -04001740 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04001741 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05001742
Chris Mason34a38212007-11-07 13:31:03 -05001743 if (left_nritems)
1744 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001745 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04001746
Chris Mason5f39d392007-10-15 16:14:19 -04001747 btrfs_item_key(right, &disk_key, 0);
1748 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04001749 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05001750
Chris Mason00ec4c52007-02-24 12:47:20 -05001751 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04001752 if (path->slots[0] >= left_nritems) {
1753 path->slots[0] -= left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001754 free_extent_buffer(path->nodes[0]);
1755 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05001756 path->slots[1] += 1;
1757 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001758 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05001759 }
1760 return 0;
1761}
1762/*
Chris Mason74123bd2007-02-02 11:05:29 -05001763 * push some data in the path leaf to the left, trying to free up at
1764 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1765 */
Chris Masone089f052007-03-16 16:20:31 -04001766static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05001767 *root, struct btrfs_path *path, int data_size,
1768 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001769{
Chris Mason5f39d392007-10-15 16:14:19 -04001770 struct btrfs_disk_key disk_key;
1771 struct extent_buffer *right = path->nodes[0];
1772 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001773 int slot;
1774 int i;
1775 int free_space;
1776 int push_space = 0;
1777 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04001778 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04001779 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001780 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05001781 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001782 int ret = 0;
1783 int wret;
Chris Masondb945352007-10-15 16:15:53 -04001784 u32 this_item_size;
1785 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001786
1787 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04001788 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001789 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001790 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001791 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001792
Chris Mason3685f792007-10-19 09:23:27 -04001793 right_nritems = btrfs_header_nritems(right);
1794 if (right_nritems == 0) {
1795 return 1;
1796 }
1797
Chris Mason5f39d392007-10-15 16:14:19 -04001798 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
Chris Masondb945352007-10-15 16:15:53 -04001799 slot - 1), root->leafsize);
Chris Mason123abc82007-03-14 14:14:43 -04001800 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001801 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001802 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001803 return 1;
1804 }
Chris Mason02217ed2007-03-02 16:08:05 -05001805
1806 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04001807 ret = btrfs_cow_block(trans, root, left,
1808 path->nodes[1], slot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001809 if (ret) {
1810 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason5f39d392007-10-15 16:14:19 -04001811 free_extent_buffer(left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001812 return 1;
1813 }
Chris Mason3685f792007-10-19 09:23:27 -04001814
Chris Mason123abc82007-03-14 14:14:43 -04001815 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04001816 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001817 free_extent_buffer(left);
Chris Mason02217ed2007-03-02 16:08:05 -05001818 return 1;
1819 }
1820
Chris Mason34a38212007-11-07 13:31:03 -05001821 if (empty)
1822 nr = right_nritems;
1823 else
1824 nr = right_nritems - 1;
1825
1826 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001827 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001828 if (!right->map_token) {
1829 map_extent_buffer(right, (unsigned long)item,
1830 sizeof(struct btrfs_item),
1831 &right->map_token, &right->kaddr,
1832 &right->map_start, &right->map_len,
1833 KM_USER1);
1834 }
1835
Chris Masonbe0e5c02007-01-26 15:51:26 -05001836 if (path->slots[0] == i)
1837 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04001838
1839 this_item_size = btrfs_item_size(right, item);
1840 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001841 break;
Chris Masondb945352007-10-15 16:15:53 -04001842
Chris Masonbe0e5c02007-01-26 15:51:26 -05001843 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04001844 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001845 }
Chris Masondb945352007-10-15 16:15:53 -04001846
1847 if (right->map_token) {
1848 unmap_extent_buffer(right, right->map_token, KM_USER1);
1849 right->map_token = NULL;
1850 }
1851
Chris Masonbe0e5c02007-01-26 15:51:26 -05001852 if (push_items == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001853 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001854 return 1;
1855 }
Chris Mason34a38212007-11-07 13:31:03 -05001856 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04001857 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001858
Chris Masonbe0e5c02007-01-26 15:51:26 -05001859 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04001860 copy_extent_buffer(left, right,
1861 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1862 btrfs_item_nr_offset(0),
1863 push_items * sizeof(struct btrfs_item));
1864
Chris Mason123abc82007-03-14 14:14:43 -04001865 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04001866 btrfs_item_offset_nr(right, push_items -1);
1867
1868 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04001869 leaf_data_end(root, left) - push_space,
1870 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04001871 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04001872 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04001873 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05001874 BUG_ON(old_left_nritems < 0);
1875
Chris Masondb945352007-10-15 16:15:53 -04001876 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04001877 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04001878 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04001879
Chris Mason5f39d392007-10-15 16:14:19 -04001880 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04001881 if (!left->map_token) {
1882 map_extent_buffer(left, (unsigned long)item,
1883 sizeof(struct btrfs_item),
1884 &left->map_token, &left->kaddr,
1885 &left->map_start, &left->map_len,
1886 KM_USER1);
1887 }
1888
Chris Mason5f39d392007-10-15 16:14:19 -04001889 ioff = btrfs_item_offset(left, item);
1890 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04001891 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001892 }
Chris Mason5f39d392007-10-15 16:14:19 -04001893 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04001894 if (left->map_token) {
1895 unmap_extent_buffer(left, left->map_token, KM_USER1);
1896 left->map_token = NULL;
1897 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001898
1899 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05001900 if (push_items > right_nritems) {
1901 printk("push items %d nr %u\n", push_items, right_nritems);
1902 WARN_ON(1);
1903 }
Chris Mason5f39d392007-10-15 16:14:19 -04001904
Chris Mason34a38212007-11-07 13:31:03 -05001905 if (push_items < right_nritems) {
1906 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1907 leaf_data_end(root, right);
1908 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1909 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1910 btrfs_leaf_data(right) +
1911 leaf_data_end(root, right), push_space);
1912
1913 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04001914 btrfs_item_nr_offset(push_items),
1915 (btrfs_header_nritems(right) - push_items) *
1916 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05001917 }
Yaneef1c492007-11-26 10:58:13 -05001918 right_nritems -= push_items;
1919 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04001920 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001921 for (i = 0; i < right_nritems; i++) {
1922 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04001923
1924 if (!right->map_token) {
1925 map_extent_buffer(right, (unsigned long)item,
1926 sizeof(struct btrfs_item),
1927 &right->map_token, &right->kaddr,
1928 &right->map_start, &right->map_len,
1929 KM_USER1);
1930 }
1931
1932 push_space = push_space - btrfs_item_size(right, item);
1933 btrfs_set_item_offset(right, item, push_space);
1934 }
1935 if (right->map_token) {
1936 unmap_extent_buffer(right, right->map_token, KM_USER1);
1937 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001939
Chris Mason5f39d392007-10-15 16:14:19 -04001940 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05001941 if (right_nritems)
1942 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04001943
Chris Mason5f39d392007-10-15 16:14:19 -04001944 btrfs_item_key(right, &disk_key, 0);
1945 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001946 if (wret)
1947 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001948
1949 /* then fixup the leaf pointer in the path */
1950 if (path->slots[0] < push_items) {
1951 path->slots[0] += old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001952 free_extent_buffer(path->nodes[0]);
1953 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001954 path->slots[1] -= 1;
1955 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001956 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001957 path->slots[0] -= push_items;
1958 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001959 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001960 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001961}
1962
Chris Mason74123bd2007-02-02 11:05:29 -05001963/*
1964 * split the path's leaf in two, making sure there is at least data_size
1965 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001966 *
1967 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05001968 */
Chris Masone089f052007-03-16 16:20:31 -04001969static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -04001970 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001971 struct btrfs_path *path, int data_size, int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001972{
Chris Mason7bb86312007-12-11 09:25:06 -05001973 u64 root_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001974 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04001975 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05001976 int mid;
1977 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001978 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04001979 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001980 int data_copy_size;
1981 int rt_data_off;
1982 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04001983 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001984 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04001985 int double_split;
1986 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04001987 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001988
Chris Masoncc0c5532007-10-25 15:42:57 -04001989 if (extend)
1990 space_needed = data_size;
1991
Chris Mason7bb86312007-12-11 09:25:06 -05001992 if (root->ref_cows)
1993 root_gen = trans->transid;
1994 else
1995 root_gen = 0;
1996
Chris Mason40689472007-03-17 14:29:23 -04001997 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04001998 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05001999 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002000 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002001 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002002 }
2003 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002004 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002005 if (wret < 0)
2006 return wret;
2007 }
2008 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002009
Chris Mason3685f792007-10-19 09:23:27 -04002010 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002011 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002012 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002013 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002014
Chris Mason5c680ed2007-02-22 11:39:13 -05002015 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002016 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002017 if (ret)
2018 return ret;
2019 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002020again:
2021 double_split = 0;
2022 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002023 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002024 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002025 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002026
Chris Mason7bb86312007-12-11 09:25:06 -05002027 btrfs_item_key(l, &disk_key, 0);
2028
2029 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2030 root->root_key.objectid,
2031 root_gen, disk_key.objectid, 0,
2032 l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002033 if (IS_ERR(right)) {
2034 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002035 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002036 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002037
Chris Mason5f39d392007-10-15 16:14:19 -04002038 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002039 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002040 btrfs_set_header_generation(right, trans->transid);
2041 btrfs_set_header_owner(right, root->root_key.objectid);
2042 btrfs_set_header_level(right, 0);
2043 write_extent_buffer(right, root->fs_info->fsid,
2044 (unsigned long)btrfs_header_fsid(right),
2045 BTRFS_FSID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002046 if (mid <= slot) {
2047 if (nritems == 1 ||
2048 leaf_space_used(l, mid, nritems - mid) + space_needed >
2049 BTRFS_LEAF_DATA_SIZE(root)) {
2050 if (slot >= nritems) {
2051 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002052 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002053 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002054 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002055 path->slots[1] + 1, 1);
2056 if (wret)
2057 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002058 free_extent_buffer(path->nodes[0]);
2059 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002060 path->slots[0] = 0;
2061 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002062 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002063 return ret;
2064 }
2065 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002066 if (mid != nritems &&
2067 leaf_space_used(l, mid, nritems - mid) +
2068 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2069 double_split = 1;
2070 }
Chris Masond4dbff92007-04-04 14:08:15 -04002071 }
2072 } else {
2073 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2074 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002075 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002076 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002077 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002078 wret = insert_ptr(trans, root, path,
2079 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002080 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002081 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002082 if (wret)
2083 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002084 free_extent_buffer(path->nodes[0]);
2085 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002086 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002087 if (path->slots[1] == 0) {
2088 wret = fixup_low_keys(trans, root,
2089 path, &disk_key, 1);
2090 if (wret)
2091 ret = wret;
2092 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002093 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002094 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002095 } else if (extend && slot == 0) {
2096 mid = 1;
2097 } else {
2098 mid = slot;
2099 if (mid != nritems &&
2100 leaf_space_used(l, mid, nritems - mid) +
2101 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2102 double_split = 1;
2103 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002104 }
Chris Masond4dbff92007-04-04 14:08:15 -04002105 }
2106 }
Chris Mason5f39d392007-10-15 16:14:19 -04002107 nritems = nritems - mid;
2108 btrfs_set_header_nritems(right, nritems);
2109 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2110
2111 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2112 btrfs_item_nr_offset(mid),
2113 nritems * sizeof(struct btrfs_item));
2114
2115 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002116 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2117 data_copy_size, btrfs_leaf_data(l) +
2118 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002119
Chris Mason5f39d392007-10-15 16:14:19 -04002120 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2121 btrfs_item_end_nr(l, mid);
2122
2123 for (i = 0; i < nritems; i++) {
2124 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002125 u32 ioff;
2126
2127 if (!right->map_token) {
2128 map_extent_buffer(right, (unsigned long)item,
2129 sizeof(struct btrfs_item),
2130 &right->map_token, &right->kaddr,
2131 &right->map_start, &right->map_len,
2132 KM_USER1);
2133 }
2134
2135 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002136 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002137 }
Chris Mason74123bd2007-02-02 11:05:29 -05002138
Chris Masondb945352007-10-15 16:15:53 -04002139 if (right->map_token) {
2140 unmap_extent_buffer(right, right->map_token, KM_USER1);
2141 right->map_token = NULL;
2142 }
2143
Chris Mason5f39d392007-10-15 16:14:19 -04002144 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002145 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002146 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002147 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2148 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002149 if (wret)
2150 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002151
2152 btrfs_mark_buffer_dirty(right);
2153 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002154 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002155
Chris Masonbe0e5c02007-01-26 15:51:26 -05002156 if (mid <= slot) {
Chris Mason5f39d392007-10-15 16:14:19 -04002157 free_extent_buffer(path->nodes[0]);
2158 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002159 path->slots[0] -= mid;
2160 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05002161 } else
Chris Mason5f39d392007-10-15 16:14:19 -04002162 free_extent_buffer(right);
2163
Chris Masoneb60cea2007-02-02 09:18:22 -05002164 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002165
Chris Masoncc0c5532007-10-25 15:42:57 -04002166 if (double_split) {
2167 BUG_ON(num_doubles != 0);
2168 num_doubles++;
2169 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002170 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002171 return ret;
2172}
2173
Chris Masonb18c6682007-04-17 13:26:50 -04002174int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2175 struct btrfs_root *root,
2176 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002177 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002178{
2179 int ret = 0;
2180 int slot;
2181 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002182 struct extent_buffer *leaf;
2183 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002184 u32 nritems;
2185 unsigned int data_end;
2186 unsigned int old_data_start;
2187 unsigned int old_size;
2188 unsigned int size_diff;
2189 int i;
2190
2191 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002192 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002193 slot = path->slots[0];
2194
2195 old_size = btrfs_item_size_nr(leaf, slot);
2196 if (old_size == new_size)
2197 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002198
Chris Mason5f39d392007-10-15 16:14:19 -04002199 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002200 data_end = leaf_data_end(root, leaf);
2201
Chris Mason5f39d392007-10-15 16:14:19 -04002202 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002203
Chris Masonb18c6682007-04-17 13:26:50 -04002204 size_diff = old_size - new_size;
2205
2206 BUG_ON(slot < 0);
2207 BUG_ON(slot >= nritems);
2208
2209 /*
2210 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2211 */
2212 /* first correct the data pointers */
2213 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002214 u32 ioff;
2215 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002216
2217 if (!leaf->map_token) {
2218 map_extent_buffer(leaf, (unsigned long)item,
2219 sizeof(struct btrfs_item),
2220 &leaf->map_token, &leaf->kaddr,
2221 &leaf->map_start, &leaf->map_len,
2222 KM_USER1);
2223 }
2224
Chris Mason5f39d392007-10-15 16:14:19 -04002225 ioff = btrfs_item_offset(leaf, item);
2226 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002227 }
Chris Masondb945352007-10-15 16:15:53 -04002228
2229 if (leaf->map_token) {
2230 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2231 leaf->map_token = NULL;
2232 }
2233
Chris Masonb18c6682007-04-17 13:26:50 -04002234 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002235 if (from_end) {
2236 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2237 data_end + size_diff, btrfs_leaf_data(leaf) +
2238 data_end, old_data_start + new_size - data_end);
2239 } else {
2240 struct btrfs_disk_key disk_key;
2241 u64 offset;
2242
2243 btrfs_item_key(leaf, &disk_key, slot);
2244
2245 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2246 unsigned long ptr;
2247 struct btrfs_file_extent_item *fi;
2248
2249 fi = btrfs_item_ptr(leaf, slot,
2250 struct btrfs_file_extent_item);
2251 fi = (struct btrfs_file_extent_item *)(
2252 (unsigned long)fi - size_diff);
2253
2254 if (btrfs_file_extent_type(leaf, fi) ==
2255 BTRFS_FILE_EXTENT_INLINE) {
2256 ptr = btrfs_item_ptr_offset(leaf, slot);
2257 memmove_extent_buffer(leaf, ptr,
2258 (unsigned long)fi,
2259 offsetof(struct btrfs_file_extent_item,
2260 disk_bytenr));
2261 }
2262 }
2263
2264 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2265 data_end + size_diff, btrfs_leaf_data(leaf) +
2266 data_end, old_data_start - data_end);
2267
2268 offset = btrfs_disk_key_offset(&disk_key);
2269 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2270 btrfs_set_item_key(leaf, &disk_key, slot);
2271 if (slot == 0)
2272 fixup_low_keys(trans, root, path, &disk_key, 1);
2273 }
Chris Mason5f39d392007-10-15 16:14:19 -04002274
2275 item = btrfs_item_nr(leaf, slot);
2276 btrfs_set_item_size(leaf, item, new_size);
2277 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002278
2279 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002280 if (btrfs_leaf_free_space(root, leaf) < 0) {
2281 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002282 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002283 }
Chris Masonb18c6682007-04-17 13:26:50 -04002284 return ret;
2285}
2286
Chris Mason5f39d392007-10-15 16:14:19 -04002287int btrfs_extend_item(struct btrfs_trans_handle *trans,
2288 struct btrfs_root *root, struct btrfs_path *path,
2289 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002290{
2291 int ret = 0;
2292 int slot;
2293 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002294 struct extent_buffer *leaf;
2295 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002296 u32 nritems;
2297 unsigned int data_end;
2298 unsigned int old_data;
2299 unsigned int old_size;
2300 int i;
2301
2302 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002303 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002304
Chris Mason5f39d392007-10-15 16:14:19 -04002305 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002306 data_end = leaf_data_end(root, leaf);
2307
Chris Mason5f39d392007-10-15 16:14:19 -04002308 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2309 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002310 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002311 }
Chris Mason6567e832007-04-16 09:22:45 -04002312 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002313 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002314
2315 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002316 if (slot >= nritems) {
2317 btrfs_print_leaf(root, leaf);
2318 printk("slot %d too large, nritems %d\n", slot, nritems);
2319 BUG_ON(1);
2320 }
Chris Mason6567e832007-04-16 09:22:45 -04002321
2322 /*
2323 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2324 */
2325 /* first correct the data pointers */
2326 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002327 u32 ioff;
2328 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002329
2330 if (!leaf->map_token) {
2331 map_extent_buffer(leaf, (unsigned long)item,
2332 sizeof(struct btrfs_item),
2333 &leaf->map_token, &leaf->kaddr,
2334 &leaf->map_start, &leaf->map_len,
2335 KM_USER1);
2336 }
Chris Mason5f39d392007-10-15 16:14:19 -04002337 ioff = btrfs_item_offset(leaf, item);
2338 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002339 }
Chris Mason5f39d392007-10-15 16:14:19 -04002340
Chris Masondb945352007-10-15 16:15:53 -04002341 if (leaf->map_token) {
2342 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2343 leaf->map_token = NULL;
2344 }
2345
Chris Mason6567e832007-04-16 09:22:45 -04002346 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002347 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002348 data_end - data_size, btrfs_leaf_data(leaf) +
2349 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002350
Chris Mason6567e832007-04-16 09:22:45 -04002351 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002352 old_size = btrfs_item_size_nr(leaf, slot);
2353 item = btrfs_item_nr(leaf, slot);
2354 btrfs_set_item_size(leaf, item, old_size + data_size);
2355 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002356
2357 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002358 if (btrfs_leaf_free_space(root, leaf) < 0) {
2359 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002360 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002361 }
Chris Mason6567e832007-04-16 09:22:45 -04002362 return ret;
2363}
2364
Chris Mason74123bd2007-02-02 11:05:29 -05002365/*
2366 * Given a key and some data, insert an item into the tree.
2367 * This does all the path init required, making room in the tree if needed.
2368 */
Chris Mason9c583092008-01-29 15:15:18 -05002369int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002370 struct btrfs_root *root,
2371 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002372 struct btrfs_key *cpu_key, u32 *data_size,
2373 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002374{
Chris Mason5f39d392007-10-15 16:14:19 -04002375 struct extent_buffer *leaf;
2376 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002377 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002378 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002379 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002380 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002381 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002382 u32 total_size = 0;
2383 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002384 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002385 struct btrfs_disk_key disk_key;
2386
Chris Mason9c583092008-01-29 15:15:18 -05002387 for (i = 0; i < nr; i++) {
2388 total_data += data_size[i];
2389 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002390
Chris Mason74123bd2007-02-02 11:05:29 -05002391 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05002392 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05002393 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002394
Chris Mason9c583092008-01-29 15:15:18 -05002395 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2396 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05002397 if (ret == 0) {
Chris Masonf0930a32007-03-02 09:47:58 -05002398 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002399 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05002400 if (ret < 0)
2401 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002402
Chris Mason62e27492007-03-15 12:56:47 -04002403 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002404 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002405
Chris Mason5f39d392007-10-15 16:14:19 -04002406 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002407 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002408
Chris Mason123abc82007-03-14 14:14:43 -04002409 if (btrfs_leaf_free_space(root, leaf) <
Chris Mason9c583092008-01-29 15:15:18 -05002410 sizeof(struct btrfs_item) + total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002411 btrfs_print_leaf(root, leaf);
2412 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002413 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002414 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002415 }
Chris Mason5f39d392007-10-15 16:14:19 -04002416
Chris Mason62e27492007-03-15 12:56:47 -04002417 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002418 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002419
Chris Masonbe0e5c02007-01-26 15:51:26 -05002420 if (slot != nritems) {
2421 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002422 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002423
Chris Mason5f39d392007-10-15 16:14:19 -04002424 if (old_data < data_end) {
2425 btrfs_print_leaf(root, leaf);
2426 printk("slot %d old_data %d data_end %d\n",
2427 slot, old_data, data_end);
2428 BUG_ON(1);
2429 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002430 /*
2431 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2432 */
2433 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002434 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002435 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002436 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002437
Chris Mason5f39d392007-10-15 16:14:19 -04002438 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002439 if (!leaf->map_token) {
2440 map_extent_buffer(leaf, (unsigned long)item,
2441 sizeof(struct btrfs_item),
2442 &leaf->map_token, &leaf->kaddr,
2443 &leaf->map_start, &leaf->map_len,
2444 KM_USER1);
2445 }
2446
Chris Mason5f39d392007-10-15 16:14:19 -04002447 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002448 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002449 }
Chris Masondb945352007-10-15 16:15:53 -04002450 if (leaf->map_token) {
2451 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2452 leaf->map_token = NULL;
2453 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002454
2455 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002456 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002457 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002458 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002459
2460 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002461 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002462 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002463 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002464 data_end = old_data;
2465 }
Chris Mason5f39d392007-10-15 16:14:19 -04002466
Chris Mason62e27492007-03-15 12:56:47 -04002467 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002468 for (i = 0; i < nr; i++) {
2469 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2470 btrfs_set_item_key(leaf, &disk_key, slot + i);
2471 item = btrfs_item_nr(leaf, slot + i);
2472 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2473 data_end -= data_size[i];
2474 btrfs_set_item_size(leaf, item, data_size[i]);
2475 }
2476 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04002477 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002478
2479 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05002480 if (slot == 0) {
2481 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04002482 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05002483 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002484
Chris Mason5f39d392007-10-15 16:14:19 -04002485 if (btrfs_leaf_free_space(root, leaf) < 0) {
2486 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002487 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002488 }
Chris Mason5a01a2e2008-01-30 11:43:54 -05002489
Chris Masoned2ff2c2007-03-01 18:59:40 -05002490out:
Chris Mason62e27492007-03-15 12:56:47 -04002491 return ret;
2492}
2493
2494/*
2495 * Given a key and some data, insert an item into the tree.
2496 * This does all the path init required, making room in the tree if needed.
2497 */
Chris Masone089f052007-03-16 16:20:31 -04002498int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2499 *root, struct btrfs_key *cpu_key, void *data, u32
2500 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04002501{
2502 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04002503 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002504 struct extent_buffer *leaf;
2505 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04002506
Chris Mason2c90e5d2007-04-02 10:50:19 -04002507 path = btrfs_alloc_path();
2508 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04002509 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04002510 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04002511 leaf = path->nodes[0];
2512 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2513 write_extent_buffer(leaf, data, ptr, data_size);
2514 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04002515 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04002516 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002517 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002518}
2519
Chris Mason74123bd2007-02-02 11:05:29 -05002520/*
Chris Mason5de08d72007-02-24 06:24:44 -05002521 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05002522 *
2523 * If the delete empties a node, the node is removed from the tree,
2524 * continuing all the way the root if required. The root is converted into
2525 * a leaf if all the nodes are emptied.
2526 */
Chris Masone089f052007-03-16 16:20:31 -04002527static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2528 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002529{
Chris Mason5f39d392007-10-15 16:14:19 -04002530 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04002531 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002532 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002533 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002534
Chris Mason5f39d392007-10-15 16:14:19 -04002535 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002536 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002537 memmove_extent_buffer(parent,
2538 btrfs_node_key_ptr_offset(slot),
2539 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04002540 sizeof(struct btrfs_key_ptr) *
2541 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05002542 }
Chris Mason7518a232007-03-12 12:01:18 -04002543 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04002544 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04002545 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04002546 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05002547 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04002548 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05002549 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002550 struct btrfs_disk_key disk_key;
2551
2552 btrfs_node_key(parent, &disk_key, 0);
2553 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002554 if (wret)
2555 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002556 }
Chris Masond6025572007-03-30 14:27:56 -04002557 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002558 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002559}
2560
Chris Mason74123bd2007-02-02 11:05:29 -05002561/*
2562 * delete the item at the leaf level in path. If that empties
2563 * the leaf, remove it from the tree
2564 */
Chris Mason85e21ba2008-01-29 15:11:36 -05002565int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2566 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002567{
Chris Mason5f39d392007-10-15 16:14:19 -04002568 struct extent_buffer *leaf;
2569 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002570 int last_off;
2571 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002572 int ret = 0;
2573 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05002574 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002575 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002576
Chris Mason5f39d392007-10-15 16:14:19 -04002577 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05002578 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2579
2580 for (i = 0; i < nr; i++)
2581 dsize += btrfs_item_size_nr(leaf, slot + i);
2582
Chris Mason5f39d392007-10-15 16:14:19 -04002583 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002584
Chris Mason85e21ba2008-01-29 15:11:36 -05002585 if (slot + nr != nritems) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002586 int i;
Chris Mason123abc82007-03-14 14:14:43 -04002587 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04002588
2589 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002590 data_end + dsize,
2591 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05002592 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002593
Chris Mason85e21ba2008-01-29 15:11:36 -05002594 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002595 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002596
Chris Mason5f39d392007-10-15 16:14:19 -04002597 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002598 if (!leaf->map_token) {
2599 map_extent_buffer(leaf, (unsigned long)item,
2600 sizeof(struct btrfs_item),
2601 &leaf->map_token, &leaf->kaddr,
2602 &leaf->map_start, &leaf->map_len,
2603 KM_USER1);
2604 }
Chris Mason5f39d392007-10-15 16:14:19 -04002605 ioff = btrfs_item_offset(leaf, item);
2606 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04002607 }
Chris Masondb945352007-10-15 16:15:53 -04002608
2609 if (leaf->map_token) {
2610 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2611 leaf->map_token = NULL;
2612 }
2613
Chris Mason5f39d392007-10-15 16:14:19 -04002614 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05002615 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04002616 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05002617 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002618 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002619 btrfs_set_header_nritems(leaf, nritems - nr);
2620 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002621
Chris Mason74123bd2007-02-02 11:05:29 -05002622 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04002623 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002624 if (leaf == root->node) {
2625 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05002626 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002627 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason5f39d392007-10-15 16:14:19 -04002628 clean_tree_block(trans, root, leaf);
2629 wait_on_tree_block_writeback(root, leaf);
Chris Masone089f052007-03-16 16:20:31 -04002630 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002631 if (wret)
2632 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04002633 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05002634 leaf->start, leaf->len,
2635 btrfs_header_owner(path->nodes[1]),
2636 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002637 if (wret)
2638 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05002639 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002640 } else {
Chris Mason7518a232007-03-12 12:01:18 -04002641 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002642 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002643 struct btrfs_disk_key disk_key;
2644
2645 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04002646 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04002647 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002648 if (wret)
2649 ret = wret;
2650 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002651
Chris Mason74123bd2007-02-02 11:05:29 -05002652 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05002653 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002654 /* push_leaf_left fixes the path.
2655 * make sure the path still points to our leaf
2656 * for possible call to del_ptr below
2657 */
Chris Mason4920c9a2007-01-26 16:38:42 -05002658 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002659 extent_buffer_get(leaf);
2660
Chris Mason85e21ba2008-01-29 15:11:36 -05002661 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002662 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002663 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002664
2665 if (path->nodes[0] == leaf &&
2666 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002667 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04002668 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05002669 ret = wret;
2670 }
Chris Mason5f39d392007-10-15 16:14:19 -04002671
2672 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05002673 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04002674 u64 bytenr = leaf->start;
2675 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04002676
Chris Mason7bb86312007-12-11 09:25:06 -05002677 root_gen = btrfs_header_generation(
2678 path->nodes[1]);
2679
Chris Mason5f39d392007-10-15 16:14:19 -04002680 clean_tree_block(trans, root, leaf);
2681 wait_on_tree_block_writeback(root, leaf);
2682
Chris Masone089f052007-03-16 16:20:31 -04002683 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002684 if (wret)
2685 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002686
2687 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04002688 wret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002689 blocksize,
2690 btrfs_header_owner(path->nodes[1]),
2691 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05002692 if (wret)
2693 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05002694 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002695 btrfs_mark_buffer_dirty(leaf);
2696 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002697 }
Chris Masond5719762007-03-23 10:01:08 -04002698 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002699 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002700 }
2701 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002702 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002703}
2704
Chris Mason97571fd2007-02-24 13:39:08 -05002705/*
Chris Mason7bb86312007-12-11 09:25:06 -05002706 * walk up the tree as far as required to find the previous leaf.
2707 * returns 0 if it found something or 1 if there are no lesser leaves.
2708 * returns < 0 on io errors.
2709 */
2710int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2711{
Chris Mason8f662a72008-01-02 10:01:11 -05002712 u64 bytenr;
Chris Mason7bb86312007-12-11 09:25:06 -05002713 int slot;
2714 int level = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05002715 struct extent_buffer *c;
2716 struct extent_buffer *next = NULL;
2717
2718 while(level < BTRFS_MAX_LEVEL) {
2719 if (!path->nodes[level])
2720 return 1;
2721
2722 slot = path->slots[level];
2723 c = path->nodes[level];
2724 if (slot == 0) {
2725 level++;
2726 if (level == BTRFS_MAX_LEVEL)
2727 return 1;
2728 continue;
2729 }
2730 slot--;
2731
2732 bytenr = btrfs_node_blockptr(c, slot);
2733 if (next)
2734 free_extent_buffer(next);
2735
Chris Mason7bb86312007-12-11 09:25:06 -05002736 next = read_tree_block(root, bytenr,
2737 btrfs_level_size(root, level - 1));
2738 break;
2739 }
2740 path->slots[level] = slot;
2741 while(1) {
2742 level--;
2743 c = path->nodes[level];
2744 free_extent_buffer(c);
Chris Mason8f662a72008-01-02 10:01:11 -05002745 slot = btrfs_header_nritems(next);
2746 if (slot != 0)
2747 slot--;
Chris Mason7bb86312007-12-11 09:25:06 -05002748 path->nodes[level] = next;
Chris Mason8f662a72008-01-02 10:01:11 -05002749 path->slots[level] = slot;
Chris Mason7bb86312007-12-11 09:25:06 -05002750 if (!level)
2751 break;
Chris Mason8f662a72008-01-02 10:01:11 -05002752 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
Chris Mason7bb86312007-12-11 09:25:06 -05002753 btrfs_level_size(root, level - 1));
2754 }
2755 return 0;
2756}
2757
2758/*
Chris Mason97571fd2007-02-24 13:39:08 -05002759 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05002760 * returns 0 if it found something or 1 if there are no greater leaves.
2761 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05002762 */
Chris Mason234b63a2007-03-13 10:46:10 -04002763int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05002764{
2765 int slot;
2766 int level = 1;
Chris Masondb945352007-10-15 16:15:53 -04002767 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002768 struct extent_buffer *c;
2769 struct extent_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05002770
Chris Mason234b63a2007-03-13 10:46:10 -04002771 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05002772 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05002773 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002774
Chris Masond97e63b2007-02-20 16:40:44 -05002775 slot = path->slots[level] + 1;
2776 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002777 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05002778 level++;
Chris Mason7bb86312007-12-11 09:25:06 -05002779 if (level == BTRFS_MAX_LEVEL)
2780 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05002781 continue;
2782 }
Chris Mason5f39d392007-10-15 16:14:19 -04002783
Chris Masondb945352007-10-15 16:15:53 -04002784 bytenr = btrfs_node_blockptr(c, slot);
Chris Masoncfaa7292007-02-21 17:04:57 -05002785 if (next)
Chris Mason5f39d392007-10-15 16:14:19 -04002786 free_extent_buffer(next);
2787
Chris Mason6702ed42007-08-07 16:15:09 -04002788 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002789 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002790
Chris Masondb945352007-10-15 16:15:53 -04002791 next = read_tree_block(root, bytenr,
2792 btrfs_level_size(root, level -1));
Chris Masond97e63b2007-02-20 16:40:44 -05002793 break;
2794 }
2795 path->slots[level] = slot;
2796 while(1) {
2797 level--;
2798 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04002799 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05002800 path->nodes[level] = next;
2801 path->slots[level] = 0;
2802 if (!level)
2803 break;
Chris Mason6702ed42007-08-07 16:15:09 -04002804 if (path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05002805 reada_for_search(root, path, level, 0, 0);
Chris Masondb945352007-10-15 16:15:53 -04002806 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2807 btrfs_level_size(root, level - 1));
Chris Masond97e63b2007-02-20 16:40:44 -05002808 }
2809 return 0;
2810}
Chris Mason0b86a832008-03-24 15:01:56 -04002811
2812int btrfs_previous_item(struct btrfs_root *root,
2813 struct btrfs_path *path, u64 min_objectid,
2814 int type)
2815{
2816 struct btrfs_key found_key;
2817 struct extent_buffer *leaf;
2818 int ret;
2819
2820 while(1) {
2821 if (path->slots[0] == 0) {
2822 ret = btrfs_prev_leaf(root, path);
2823 if (ret != 0)
2824 return ret;
2825 } else {
2826 path->slots[0]--;
2827 }
2828 leaf = path->nodes[0];
2829 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2830 if (found_key.type == type)
2831 return 0;
2832 }
2833 return 1;
2834}
2835