blob: 50e81f43e6d421c0fdaa61d9b9410e89b1ffe46d [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 Mason925baed2008-06-25 16:01:30 -040024#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050025
Chris Masone089f052007-03-16 16:20:31 -040026static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040029 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040030 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040031static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040033 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
Chris Masone089f052007-03-16 16:20:31 -040038static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050040
Chris Masondf24a2b2007-04-04 09:36:31 -040041inline void btrfs_init_path(struct btrfs_path *p)
42{
43 memset(p, 0, sizeof(*p));
44}
45
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2cc58cf2007-08-27 16:49:44 -040050 if (path) {
Chris Masondf24a2b2007-04-04 09:36:31 -040051 btrfs_init_path(path);
Chris Mason2cc58cf2007-08-27 16:49:44 -040052 path->reada = 1;
53 }
Chris Masondf24a2b2007-04-04 09:36:31 -040054 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040055}
56
57void btrfs_free_path(struct btrfs_path *p)
58{
Chris Masondf24a2b2007-04-04 09:36:31 -040059 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040060 kmem_cache_free(btrfs_path_cachep, p);
61}
62
Chris Masone02119d2008-09-05 16:13:11 -040063void noinline btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050064{
65 int i;
Chris Masona2135012008-06-25 16:01:30 -040066
Chris Mason234b63a2007-03-13 10:46:10 -040067 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -040068 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050069 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040070 continue;
71 if (p->locks[i]) {
72 btrfs_tree_unlock(p->nodes[i]);
73 p->locks[i] = 0;
74 }
Chris Mason5f39d392007-10-15 16:14:19 -040075 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -040076 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -050077 }
78}
79
Chris Mason925baed2008-06-25 16:01:30 -040080struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
81{
82 struct extent_buffer *eb;
83 spin_lock(&root->node_lock);
84 eb = root->node;
85 extent_buffer_get(eb);
86 spin_unlock(&root->node_lock);
87 return eb;
88}
89
90struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
91{
92 struct extent_buffer *eb;
93
94 while(1) {
95 eb = btrfs_root_node(root);
96 btrfs_tree_lock(eb);
97
98 spin_lock(&root->node_lock);
99 if (eb == root->node) {
100 spin_unlock(&root->node_lock);
101 break;
102 }
103 spin_unlock(&root->node_lock);
104
105 btrfs_tree_unlock(eb);
106 free_extent_buffer(eb);
107 }
108 return eb;
109}
110
Chris Mason0b86a832008-03-24 15:01:56 -0400111static void add_root_to_dirty_list(struct btrfs_root *root)
112{
113 if (root->track_dirty && list_empty(&root->dirty_list)) {
114 list_add(&root->dirty_list,
115 &root->fs_info->dirty_cowonly_roots);
116 }
117}
118
Chris Masonbe20aa92007-12-17 20:14:01 -0500119int btrfs_copy_root(struct btrfs_trans_handle *trans,
120 struct btrfs_root *root,
121 struct extent_buffer *buf,
122 struct extent_buffer **cow_ret, u64 new_root_objectid)
123{
124 struct extent_buffer *cow;
125 u32 nritems;
126 int ret = 0;
127 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500128 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500129
Chris Mason4aec2b52007-12-18 16:25:45 -0500130 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
131 if (!new_root)
132 return -ENOMEM;
133
134 memcpy(new_root, root, sizeof(*new_root));
135 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500136
137 WARN_ON(root->ref_cows && trans->transid !=
138 root->fs_info->running_transaction->transid);
139 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
140
141 level = btrfs_header_level(buf);
142 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400143
144 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
145 new_root_objectid, trans->transid,
146 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500147 if (IS_ERR(cow)) {
148 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500149 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500150 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500151
152 copy_extent_buffer(cow, buf, 0, 0, cow->len);
153 btrfs_set_header_bytenr(cow, cow->start);
154 btrfs_set_header_generation(cow, trans->transid);
155 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400156 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500157
158 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400159 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500160 kfree(new_root);
161
Chris Masonbe20aa92007-12-17 20:14:01 -0500162 if (ret)
163 return ret;
164
165 btrfs_mark_buffer_dirty(cow);
166 *cow_ret = cow;
167 return 0;
168}
169
Chris Masone02119d2008-09-05 16:13:11 -0400170int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400171 struct btrfs_root *root,
172 struct extent_buffer *buf,
173 struct extent_buffer *parent, int parent_slot,
174 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400175 u64 search_start, u64 empty_size,
176 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400177{
Zheng Yan31840ae2008-09-23 13:14:14 -0400178 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400179 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500180 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400181 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500182 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400183 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400184
Chris Mason925baed2008-06-25 16:01:30 -0400185 if (*cow_ret == buf)
186 unlock_orig = 1;
187
188 WARN_ON(!btrfs_tree_locked(buf));
189
Zheng Yan31840ae2008-09-23 13:14:14 -0400190 if (parent)
191 parent_start = parent->start;
192 else
193 parent_start = 0;
194
Chris Mason7bb86312007-12-11 09:25:06 -0500195 WARN_ON(root->ref_cows && trans->transid !=
196 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400197 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400198
Chris Mason7bb86312007-12-11 09:25:06 -0500199 level = btrfs_header_level(buf);
200 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400201
Chris Mason65b51a02008-08-01 15:11:20 -0400202 if (prealloc_dest) {
203 struct btrfs_key ins;
204
205 ins.objectid = prealloc_dest;
206 ins.offset = buf->len;
207 ins.type = BTRFS_EXTENT_ITEM_KEY;
208
Zheng Yan31840ae2008-09-23 13:14:14 -0400209 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400210 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400211 trans->transid, level, 0,
Chris Mason65b51a02008-08-01 15:11:20 -0400212 &ins);
213 BUG_ON(ret);
214 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
215 buf->len);
216 } else {
217 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400218 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400219 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400220 trans->transid, level,
221 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400222 }
Chris Mason6702ed42007-08-07 16:15:09 -0400223 if (IS_ERR(cow))
224 return PTR_ERR(cow);
225
Chris Mason5f39d392007-10-15 16:14:19 -0400226 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400227 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400228 btrfs_set_header_generation(cow, trans->transid);
229 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400230 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400231
Chris Mason5f39d392007-10-15 16:14:19 -0400232 WARN_ON(btrfs_header_generation(buf) > trans->transid);
233 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400234 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400235 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400236 if (ret)
237 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400238
239 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
240 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400241 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
242 /*
243 * There are only two places that can drop reference to
244 * tree blocks owned by living reloc trees, one is here,
245 * the other place is btrfs_merge_path. In both places,
246 * we check reference count while tree block is locked.
247 * Furthermore, if reference count is one, it won't get
248 * increased by someone else.
249 */
250 u32 refs;
251 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
252 buf->len, &refs);
253 BUG_ON(ret);
254 if (refs == 1) {
255 ret = btrfs_update_ref(trans, root, buf, cow,
256 0, nritems);
257 clean_tree_block(trans, root, buf);
258 } else {
259 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
260 }
261 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400262 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400263 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
264 if (ret)
265 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400266 clean_tree_block(trans, root, buf);
267 }
268
Zheng Yan1a40e232008-09-26 10:09:34 -0400269 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
270 ret = btrfs_add_reloc_mapping(root, buf->start,
271 buf->len, cow->start);
272 BUG_ON(ret);
273 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
274 WARN_ON(ret);
275 }
276
Chris Mason6702ed42007-08-07 16:15:09 -0400277 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400278 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400279
280 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400281 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400282 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400283 spin_unlock(&root->node_lock);
284
Chris Mason6702ed42007-08-07 16:15:09 -0400285 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400286 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400287 buf->len, buf->start,
288 root->root_key.objectid,
289 btrfs_header_generation(buf),
290 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400291 }
Chris Mason5f39d392007-10-15 16:14:19 -0400292 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400293 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400294 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400295 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400296 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500297 WARN_ON(trans->transid == 0);
298 btrfs_set_node_ptr_generation(parent, parent_slot,
299 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400300 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400301 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500302 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400303 parent_start, btrfs_header_owner(parent),
304 btrfs_header_generation(parent), 0, 0, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400305 }
Chris Mason925baed2008-06-25 16:01:30 -0400306 if (unlock_orig)
307 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400308 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400309 btrfs_mark_buffer_dirty(cow);
310 *cow_ret = cow;
311 return 0;
312}
313
Chris Masone02119d2008-09-05 16:13:11 -0400314int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400315 struct btrfs_root *root, struct extent_buffer *buf,
316 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400317 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500318{
Chris Mason6702ed42007-08-07 16:15:09 -0400319 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400320 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500321
Chris Masonccd467d2007-06-28 15:57:36 -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 Masondc17ff82008-01-08 15:46:30 -0500332
Chris Mason63b10fc2008-04-01 11:21:32 -0400333 spin_lock(&root->fs_info->hash_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400334 if (btrfs_header_generation(buf) == trans->transid &&
335 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400336 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500337 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400338 spin_unlock(&root->fs_info->hash_lock);
Chris Mason65b51a02008-08-01 15:11:20 -0400339 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500340 return 0;
341 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400342 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400343 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400344 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400345 parent_slot, cow_ret, search_start, 0,
346 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400347 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400348}
349
Chris Mason6b800532007-10-15 16:17:34 -0400350static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400351{
Chris Mason6b800532007-10-15 16:17:34 -0400352 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400353 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400354 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400355 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500356 return 0;
357}
358
Chris Mason081e9572007-11-06 10:26:24 -0500359/*
360 * compare two keys in a memcmp fashion
361 */
362static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
363{
364 struct btrfs_key k1;
365
366 btrfs_disk_key_to_cpu(&k1, disk);
367
368 if (k1.objectid > k2->objectid)
369 return 1;
370 if (k1.objectid < k2->objectid)
371 return -1;
372 if (k1.type > k2->type)
373 return 1;
374 if (k1.type < k2->type)
375 return -1;
376 if (k1.offset > k2->offset)
377 return 1;
378 if (k1.offset < k2->offset)
379 return -1;
380 return 0;
381}
382
383
Chris Mason6702ed42007-08-07 16:15:09 -0400384int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400385 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400386 int start_slot, int cache_only, u64 *last_ret,
387 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400388{
Chris Mason6b800532007-10-15 16:17:34 -0400389 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400390 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400391 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400392 u64 search_start = *last_ret;
393 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400394 u64 other;
395 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400396 int end_slot;
397 int i;
398 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400399 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400400 int uptodate;
401 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500402 int progress_passed = 0;
403 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400404
Chris Mason5708b952007-10-25 15:43:18 -0400405 parent_level = btrfs_header_level(parent);
406 if (cache_only && parent_level != 1)
407 return 0;
408
Chris Mason6702ed42007-08-07 16:15:09 -0400409 if (trans->transaction != root->fs_info->running_transaction) {
410 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
411 root->fs_info->running_transaction->transid);
412 WARN_ON(1);
413 }
414 if (trans->transid != root->fs_info->generation) {
415 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
416 root->fs_info->generation);
417 WARN_ON(1);
418 }
Chris Mason86479a02007-09-10 19:58:16 -0400419
Chris Mason6b800532007-10-15 16:17:34 -0400420 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400421 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400422 end_slot = parent_nritems;
423
424 if (parent_nritems == 1)
425 return 0;
426
427 for (i = start_slot; i < end_slot; i++) {
428 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400429
Chris Mason5708b952007-10-25 15:43:18 -0400430 if (!parent->map_token) {
431 map_extent_buffer(parent,
432 btrfs_node_key_ptr_offset(i),
433 sizeof(struct btrfs_key_ptr),
434 &parent->map_token, &parent->kaddr,
435 &parent->map_start, &parent->map_len,
436 KM_USER1);
437 }
Chris Mason081e9572007-11-06 10:26:24 -0500438 btrfs_node_key(parent, &disk_key, i);
439 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
440 continue;
441
442 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400443 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400444 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400445 if (last_block == 0)
446 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400447
Chris Mason6702ed42007-08-07 16:15:09 -0400448 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400449 other = btrfs_node_blockptr(parent, i - 1);
450 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400451 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400452 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400453 other = btrfs_node_blockptr(parent, i + 1);
454 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400455 }
Chris Masone9d0b132007-08-10 14:06:19 -0400456 if (close) {
457 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400458 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400459 }
Chris Mason5708b952007-10-25 15:43:18 -0400460 if (parent->map_token) {
461 unmap_extent_buffer(parent, parent->map_token,
462 KM_USER1);
463 parent->map_token = NULL;
464 }
Chris Mason6702ed42007-08-07 16:15:09 -0400465
Chris Mason6b800532007-10-15 16:17:34 -0400466 cur = btrfs_find_tree_block(root, blocknr, blocksize);
467 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400468 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400469 else
470 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400471 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400472 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400473 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400474 continue;
475 }
Chris Mason6b800532007-10-15 16:17:34 -0400476 if (!cur) {
477 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400478 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400479 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400480 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400481 }
Chris Mason6702ed42007-08-07 16:15:09 -0400482 }
Chris Masone9d0b132007-08-10 14:06:19 -0400483 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400484 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400485
Chris Masone7a84562008-06-25 16:01:31 -0400486 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400487 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400488 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400489 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400490 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400491 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400492 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400493 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400494 break;
Yan252c38f2007-08-29 09:11:44 -0400495 }
Chris Masone7a84562008-06-25 16:01:31 -0400496 search_start = cur->start;
497 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400498 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400499 btrfs_tree_unlock(cur);
500 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400501 }
Chris Mason5708b952007-10-25 15:43:18 -0400502 if (parent->map_token) {
503 unmap_extent_buffer(parent, parent->map_token,
504 KM_USER1);
505 parent->map_token = NULL;
506 }
Chris Mason6702ed42007-08-07 16:15:09 -0400507 return err;
508}
509
Chris Mason74123bd2007-02-02 11:05:29 -0500510/*
511 * The leaf data grows from end-to-front in the node.
512 * this returns the address of the start of the last item,
513 * which is the stop of the leaf data stack
514 */
Chris Mason123abc82007-03-14 14:14:43 -0400515static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400516 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500517{
Chris Mason5f39d392007-10-15 16:14:19 -0400518 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500519 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400520 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400521 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500522}
523
Chris Mason123abc82007-03-14 14:14:43 -0400524static int check_node(struct btrfs_root *root, struct btrfs_path *path,
525 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500526{
Chris Mason5f39d392007-10-15 16:14:19 -0400527 struct extent_buffer *parent = NULL;
528 struct extent_buffer *node = path->nodes[level];
529 struct btrfs_disk_key parent_key;
530 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500531 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400532 int slot;
533 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400534 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500535
536 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400537 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400538
Chris Mason8d7be552007-05-10 11:24:42 -0400539 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400540 BUG_ON(nritems == 0);
541 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400542 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400543 btrfs_node_key(parent, &parent_key, parent_slot);
544 btrfs_node_key(node, &node_key, 0);
545 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400546 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400547 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400548 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500549 }
Chris Mason123abc82007-03-14 14:14:43 -0400550 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400551 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400552 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
553 btrfs_node_key(node, &node_key, slot);
554 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400555 }
556 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400557 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
558 btrfs_node_key(node, &node_key, slot);
559 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500560 }
561 return 0;
562}
563
Chris Mason123abc82007-03-14 14:14:43 -0400564static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
565 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500566{
Chris Mason5f39d392007-10-15 16:14:19 -0400567 struct extent_buffer *leaf = path->nodes[level];
568 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500569 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400570 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400571 struct btrfs_disk_key parent_key;
572 struct btrfs_disk_key leaf_key;
573 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400574
Chris Mason5f39d392007-10-15 16:14:19 -0400575 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500576
577 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400578 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400579
580 if (nritems == 0)
581 return 0;
582
583 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400584 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400585 btrfs_node_key(parent, &parent_key, parent_slot);
586 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400587
Chris Mason5f39d392007-10-15 16:14:19 -0400588 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400589 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400590 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400591 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500592 }
Chris Mason5f39d392007-10-15 16:14:19 -0400593#if 0
594 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
595 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
596 btrfs_item_key(leaf, &leaf_key, i);
597 if (comp_keys(&leaf_key, &cpukey) >= 0) {
598 btrfs_print_leaf(root, leaf);
599 printk("slot %d offset bad key\n", i);
600 BUG_ON(1);
601 }
602 if (btrfs_item_offset_nr(leaf, i) !=
603 btrfs_item_end_nr(leaf, i + 1)) {
604 btrfs_print_leaf(root, leaf);
605 printk("slot %d offset bad\n", i);
606 BUG_ON(1);
607 }
608 if (i == 0) {
609 if (btrfs_item_offset_nr(leaf, i) +
610 btrfs_item_size_nr(leaf, i) !=
611 BTRFS_LEAF_DATA_SIZE(root)) {
612 btrfs_print_leaf(root, leaf);
613 printk("slot %d first offset bad\n", i);
614 BUG_ON(1);
615 }
616 }
617 }
618 if (nritems > 0) {
619 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
620 btrfs_print_leaf(root, leaf);
621 printk("slot %d bad size \n", nritems - 1);
622 BUG_ON(1);
623 }
624 }
625#endif
626 if (slot != 0 && slot < nritems - 1) {
627 btrfs_item_key(leaf, &leaf_key, slot);
628 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
629 if (comp_keys(&leaf_key, &cpukey) <= 0) {
630 btrfs_print_leaf(root, leaf);
631 printk("slot %d offset bad key\n", slot);
632 BUG_ON(1);
633 }
634 if (btrfs_item_offset_nr(leaf, slot - 1) !=
635 btrfs_item_end_nr(leaf, slot)) {
636 btrfs_print_leaf(root, leaf);
637 printk("slot %d offset bad\n", slot);
638 BUG_ON(1);
639 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500640 }
Chris Mason8d7be552007-05-10 11:24:42 -0400641 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400642 btrfs_item_key(leaf, &leaf_key, slot);
643 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
644 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
645 if (btrfs_item_offset_nr(leaf, slot) !=
646 btrfs_item_end_nr(leaf, slot + 1)) {
647 btrfs_print_leaf(root, leaf);
648 printk("slot %d offset bad\n", slot);
649 BUG_ON(1);
650 }
Chris Mason8d7be552007-05-10 11:24:42 -0400651 }
Chris Mason5f39d392007-10-15 16:14:19 -0400652 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
653 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500654 return 0;
655}
656
Chris Mason98ed5172008-01-03 10:01:48 -0500657static int noinline check_block(struct btrfs_root *root,
658 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500659{
Chris Masonf1885912008-04-09 16:28:12 -0400660 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400661 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400662 if (btrfs_header_level(path->nodes[level]) != level)
663 printk("warning: bad level %Lu wanted %d found %d\n",
664 path->nodes[level]->start, level,
665 btrfs_header_level(path->nodes[level]));
666 found_start = btrfs_header_bytenr(path->nodes[level]);
667 if (found_start != path->nodes[level]->start) {
668 printk("warning: bad bytentr %Lu found %Lu\n",
669 path->nodes[level]->start, found_start);
670 }
Chris Masondb945352007-10-15 16:15:53 -0400671#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400672 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400673
Chris Mason479965d2007-10-15 16:14:27 -0400674 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
675 (unsigned long)btrfs_header_fsid(buf),
676 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400677 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400678 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400679 }
Chris Masondb945352007-10-15 16:15:53 -0400680#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500681 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400682 return check_leaf(root, path, level);
683 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500684}
685
Chris Mason74123bd2007-02-02 11:05:29 -0500686/*
Chris Mason5f39d392007-10-15 16:14:19 -0400687 * search for key in the extent_buffer. The items start at offset p,
688 * and they are item_size apart. There are 'max' items in p.
689 *
Chris Mason74123bd2007-02-02 11:05:29 -0500690 * the slot in the array is returned via slot, and it points to
691 * the place where you would insert key if it is not found in
692 * the array.
693 *
694 * slot may point to max if the key is bigger than all of the keys
695 */
Chris Masone02119d2008-09-05 16:13:11 -0400696static noinline int generic_bin_search(struct extent_buffer *eb,
697 unsigned long p,
698 int item_size, struct btrfs_key *key,
699 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500700{
701 int low = 0;
702 int high = max;
703 int mid;
704 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400705 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400706 struct btrfs_disk_key unaligned;
707 unsigned long offset;
708 char *map_token = NULL;
709 char *kaddr = NULL;
710 unsigned long map_start = 0;
711 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400712 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500713
714 while(low < high) {
715 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400716 offset = p + mid * item_size;
717
718 if (!map_token || offset < map_start ||
719 (offset + sizeof(struct btrfs_disk_key)) >
720 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400721 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400722 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400723 map_token = NULL;
724 }
725 err = map_extent_buffer(eb, offset,
726 sizeof(struct btrfs_disk_key),
727 &map_token, &kaddr,
728 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400729
Chris Mason479965d2007-10-15 16:14:27 -0400730 if (!err) {
731 tmp = (struct btrfs_disk_key *)(kaddr + offset -
732 map_start);
733 } else {
734 read_extent_buffer(eb, &unaligned,
735 offset, sizeof(unaligned));
736 tmp = &unaligned;
737 }
738
Chris Mason5f39d392007-10-15 16:14:19 -0400739 } else {
740 tmp = (struct btrfs_disk_key *)(kaddr + offset -
741 map_start);
742 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500743 ret = comp_keys(tmp, key);
744
745 if (ret < 0)
746 low = mid + 1;
747 else if (ret > 0)
748 high = mid;
749 else {
750 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400751 if (map_token)
752 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500753 return 0;
754 }
755 }
756 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400757 if (map_token)
758 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500759 return 1;
760}
761
Chris Mason97571fd2007-02-24 13:39:08 -0500762/*
763 * simple bin_search frontend that does the right thing for
764 * leaves vs nodes
765 */
Chris Mason5f39d392007-10-15 16:14:19 -0400766static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
767 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500768{
Chris Mason5f39d392007-10-15 16:14:19 -0400769 if (level == 0) {
770 return generic_bin_search(eb,
771 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400772 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400773 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400774 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500775 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400776 return generic_bin_search(eb,
777 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400778 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400779 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400780 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500781 }
782 return -1;
783}
784
Chris Masone02119d2008-09-05 16:13:11 -0400785static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400786 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500787{
Chris Masonca7a79a2008-05-12 12:59:19 -0400788 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500789 if (slot < 0)
790 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400791 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500792 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400793
794 BUG_ON(level == 0);
795
Chris Masondb945352007-10-15 16:15:53 -0400796 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400797 btrfs_level_size(root, level - 1),
798 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500799}
800
Chris Masone02119d2008-09-05 16:13:11 -0400801static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500802 struct btrfs_root *root,
803 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500804{
Chris Mason5f39d392007-10-15 16:14:19 -0400805 struct extent_buffer *right = NULL;
806 struct extent_buffer *mid;
807 struct extent_buffer *left = NULL;
808 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500809 int ret = 0;
810 int wret;
811 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500812 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400813 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500814 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500815
816 if (level == 0)
817 return 0;
818
Chris Mason5f39d392007-10-15 16:14:19 -0400819 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400820 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500821 WARN_ON(btrfs_header_generation(mid) != trans->transid);
822
Chris Mason1d4f8a02007-03-13 09:28:32 -0400823 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500824
Chris Mason234b63a2007-03-13 10:46:10 -0400825 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400826 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500827 pslot = path->slots[level + 1];
828
Chris Mason40689472007-03-17 14:29:23 -0400829 /*
830 * deal with the case where there is only one pointer in the root
831 * by promoting the node below to a root
832 */
Chris Mason5f39d392007-10-15 16:14:19 -0400833 if (!parent) {
834 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500835
Chris Mason5f39d392007-10-15 16:14:19 -0400836 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500837 return 0;
838
839 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400840 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400841 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500842 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400843 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500844 BUG_ON(ret);
845
Chris Mason925baed2008-06-25 16:01:30 -0400846 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500847 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400848 spin_unlock(&root->node_lock);
849
Zheng Yan31840ae2008-09-23 13:14:14 -0400850 ret = btrfs_update_extent_ref(trans, root, child->start,
851 mid->start, child->start,
852 root->root_key.objectid,
853 trans->transid, level - 1, 0);
854 BUG_ON(ret);
855
Chris Mason0b86a832008-03-24 15:01:56 -0400856 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400857 btrfs_tree_unlock(child);
858 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500859 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400860 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400861 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500862 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400863 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500864 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400865 mid->start, root->root_key.objectid,
Chris Mason7bb86312007-12-11 09:25:06 -0500866 btrfs_header_generation(mid), 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500867 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400868 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400869 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500870 }
Chris Mason5f39d392007-10-15 16:14:19 -0400871 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400872 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500873 return 0;
874
Chris Mason5f39d392007-10-15 16:14:19 -0400875 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400876 err_on_enospc = 1;
877
Chris Mason5f39d392007-10-15 16:14:19 -0400878 left = read_node_slot(root, parent, pslot - 1);
879 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400880 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400881 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400882 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400883 if (wret) {
884 ret = wret;
885 goto enospc;
886 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400887 }
Chris Mason5f39d392007-10-15 16:14:19 -0400888 right = read_node_slot(root, parent, pslot + 1);
889 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400890 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400891 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400892 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400893 if (wret) {
894 ret = wret;
895 goto enospc;
896 }
897 }
898
899 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400900 if (left) {
901 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400902 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500903 if (wret < 0)
904 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400905 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400906 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500907 }
Chris Mason79f95c82007-03-01 15:16:26 -0500908
909 /*
910 * then try to empty the right most buffer into the middle
911 */
Chris Mason5f39d392007-10-15 16:14:19 -0400912 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400913 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400914 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500915 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400916 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400917 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500918 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400919 u32 blocksize = right->len;
920
Chris Mason5f39d392007-10-15 16:14:19 -0400921 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400922 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400923 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500924 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400925 wret = del_ptr(trans, root, path, level + 1, pslot +
926 1);
Chris Masonbb803952007-03-01 12:04:21 -0500927 if (wret)
928 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -0400929 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -0400930 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500931 btrfs_header_owner(parent),
932 generation, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500933 if (wret)
934 ret = wret;
935 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400936 struct btrfs_disk_key right_key;
937 btrfs_node_key(right, &right_key, 0);
938 btrfs_set_node_key(parent, &right_key, pslot + 1);
939 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500940 }
941 }
Chris Mason5f39d392007-10-15 16:14:19 -0400942 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -0500943 /*
944 * we're not allowed to leave a node with one item in the
945 * tree during a delete. A deletion from lower in the tree
946 * could try to delete the only pointer in this node.
947 * So, pull some keys from the left.
948 * There has to be a left pointer at this point because
949 * otherwise we would have pulled some pointers from the
950 * right
951 */
Chris Mason5f39d392007-10-15 16:14:19 -0400952 BUG_ON(!left);
953 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -0400954 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500955 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400956 goto enospc;
957 }
Chris Masonbce4eae2008-04-24 14:42:46 -0400958 if (wret == 1) {
959 wret = push_node_left(trans, root, left, mid, 1);
960 if (wret < 0)
961 ret = wret;
962 }
Chris Mason79f95c82007-03-01 15:16:26 -0500963 BUG_ON(wret == 1);
964 }
Chris Mason5f39d392007-10-15 16:14:19 -0400965 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500966 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -0500967 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400968 u64 bytenr = mid->start;
969 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -0400970
Chris Mason5f39d392007-10-15 16:14:19 -0400971 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400972 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -0400973 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500974 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400975 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -0500976 if (wret)
977 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -0500978 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -0400979 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500980 btrfs_header_owner(parent),
981 root_gen, 0, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500982 if (wret)
983 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500984 } else {
985 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -0400986 struct btrfs_disk_key mid_key;
987 btrfs_node_key(mid, &mid_key, 0);
988 btrfs_set_node_key(parent, &mid_key, pslot);
989 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -0500990 }
Chris Masonbb803952007-03-01 12:04:21 -0500991
Chris Mason79f95c82007-03-01 15:16:26 -0500992 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400993 if (left) {
994 if (btrfs_header_nritems(left) > orig_slot) {
995 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -0400996 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -0400997 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -0500998 path->slots[level + 1] -= 1;
999 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001000 if (mid) {
1001 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001002 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001003 }
Chris Masonbb803952007-03-01 12:04:21 -05001004 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001005 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001006 path->slots[level] = orig_slot;
1007 }
1008 }
Chris Mason79f95c82007-03-01 15:16:26 -05001009 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001010 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001011 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001012 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001013 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001014enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001015 if (right) {
1016 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001017 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001018 }
1019 if (left) {
1020 if (path->nodes[level] != left)
1021 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001022 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001023 }
Chris Masonbb803952007-03-01 12:04:21 -05001024 return ret;
1025}
1026
Chris Masone66f7092007-04-20 13:16:02 -04001027/* returns zero if the push worked, non-zero otherwise */
Chris Mason98ed5172008-01-03 10:01:48 -05001028static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1029 struct btrfs_root *root,
1030 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001031{
Chris Mason5f39d392007-10-15 16:14:19 -04001032 struct extent_buffer *right = NULL;
1033 struct extent_buffer *mid;
1034 struct extent_buffer *left = NULL;
1035 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001036 int ret = 0;
1037 int wret;
1038 int pslot;
1039 int orig_slot = path->slots[level];
1040 u64 orig_ptr;
1041
1042 if (level == 0)
1043 return 1;
1044
Chris Mason5f39d392007-10-15 16:14:19 -04001045 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001046 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001047 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1048
1049 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001050 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001051 pslot = path->slots[level + 1];
1052
Chris Mason5f39d392007-10-15 16:14:19 -04001053 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001054 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001055
Chris Mason5f39d392007-10-15 16:14:19 -04001056 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001057
1058 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001059 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001060 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001061
1062 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001063 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001064 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1065 wret = 1;
1066 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001067 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001068 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001069 if (ret)
1070 wret = 1;
1071 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001072 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001073 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001074 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001075 }
Chris Masone66f7092007-04-20 13:16:02 -04001076 if (wret < 0)
1077 ret = wret;
1078 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001079 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001080 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001081 btrfs_node_key(mid, &disk_key, 0);
1082 btrfs_set_node_key(parent, &disk_key, pslot);
1083 btrfs_mark_buffer_dirty(parent);
1084 if (btrfs_header_nritems(left) > orig_slot) {
1085 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001086 path->slots[level + 1] -= 1;
1087 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001088 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001089 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001090 } else {
1091 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001092 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001093 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001094 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001095 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001096 }
Chris Masone66f7092007-04-20 13:16:02 -04001097 return 0;
1098 }
Chris Mason925baed2008-06-25 16:01:30 -04001099 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001100 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001101 }
Chris Mason925baed2008-06-25 16:01:30 -04001102 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001103
1104 /*
1105 * then try to empty the right most buffer into the middle
1106 */
Chris Mason5f39d392007-10-15 16:14:19 -04001107 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001108 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001109 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001110 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001111 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1112 wret = 1;
1113 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001114 ret = btrfs_cow_block(trans, root, right,
1115 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001116 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001117 if (ret)
1118 wret = 1;
1119 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001120 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001121 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001122 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001123 }
Chris Masone66f7092007-04-20 13:16:02 -04001124 if (wret < 0)
1125 ret = wret;
1126 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001127 struct btrfs_disk_key disk_key;
1128
1129 btrfs_node_key(right, &disk_key, 0);
1130 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1131 btrfs_mark_buffer_dirty(parent);
1132
1133 if (btrfs_header_nritems(mid) <= orig_slot) {
1134 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001135 path->slots[level + 1] += 1;
1136 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001137 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001138 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001139 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001140 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001141 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001142 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001143 }
Chris Masone66f7092007-04-20 13:16:02 -04001144 return 0;
1145 }
Chris Mason925baed2008-06-25 16:01:30 -04001146 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001147 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001148 }
Chris Masone66f7092007-04-20 13:16:02 -04001149 return 1;
1150}
1151
Chris Mason74123bd2007-02-02 11:05:29 -05001152/*
Chris Mason3c69fae2007-08-07 15:52:22 -04001153 * readahead one full node of leaves
1154 */
Chris Masone02119d2008-09-05 16:13:11 -04001155static noinline void reada_for_search(struct btrfs_root *root,
1156 struct btrfs_path *path,
1157 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001158{
Chris Mason5f39d392007-10-15 16:14:19 -04001159 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001160 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001161 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001162 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001163 u64 lowest_read;
1164 u64 highest_read;
1165 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001166 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001167 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001168 u32 nr;
1169 u32 blocksize;
1170 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001171
Chris Masona6b6e752007-10-15 16:22:39 -04001172 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001173 return;
1174
Chris Mason6702ed42007-08-07 16:15:09 -04001175 if (!path->nodes[level])
1176 return;
1177
Chris Mason5f39d392007-10-15 16:14:19 -04001178 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001179
Chris Mason3c69fae2007-08-07 15:52:22 -04001180 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001181 blocksize = btrfs_level_size(root, level - 1);
1182 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001183 if (eb) {
1184 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001185 return;
1186 }
1187
Chris Mason6b800532007-10-15 16:17:34 -04001188 highest_read = search;
1189 lowest_read = search;
1190
Chris Mason5f39d392007-10-15 16:14:19 -04001191 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001192 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001193 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001194 if (direction < 0) {
1195 if (nr == 0)
1196 break;
1197 nr--;
1198 } else if (direction > 0) {
1199 nr++;
1200 if (nr >= nritems)
1201 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001202 }
Chris Mason01f46652007-12-21 16:24:26 -05001203 if (path->reada < 0 && objectid) {
1204 btrfs_node_key(node, &disk_key, nr);
1205 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1206 break;
1207 }
Chris Mason6b800532007-10-15 16:17:34 -04001208 search = btrfs_node_blockptr(node, nr);
1209 if ((search >= lowest_read && search <= highest_read) ||
1210 (search < lowest_read && lowest_read - search <= 32768) ||
1211 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001212 readahead_tree_block(root, search, blocksize,
1213 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001214 nread += blocksize;
1215 }
1216 nscan++;
1217 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1218 break;
1219 if(nread > (1024 * 1024) || nscan > 128)
1220 break;
1221
1222 if (search < lowest_read)
1223 lowest_read = search;
1224 if (search > highest_read)
1225 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001226 }
1227}
Chris Mason925baed2008-06-25 16:01:30 -04001228
Chris Masone02119d2008-09-05 16:13:11 -04001229static noinline void unlock_up(struct btrfs_path *path, int level,
1230 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001231{
1232 int i;
1233 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001234 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001235 struct extent_buffer *t;
1236
1237 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1238 if (!path->nodes[i])
1239 break;
1240 if (!path->locks[i])
1241 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001242 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001243 skip_level = i + 1;
1244 continue;
1245 }
Chris Mason051e1b92008-06-25 16:01:30 -04001246 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001247 u32 nritems;
1248 t = path->nodes[i];
1249 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001250 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001251 skip_level = i + 1;
1252 continue;
1253 }
1254 }
Chris Mason051e1b92008-06-25 16:01:30 -04001255 if (skip_level < i && i >= lowest_unlock)
1256 no_skips = 1;
1257
Chris Mason925baed2008-06-25 16:01:30 -04001258 t = path->nodes[i];
1259 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1260 btrfs_tree_unlock(t);
1261 path->locks[i] = 0;
1262 }
1263 }
1264}
1265
Chris Mason3c69fae2007-08-07 15:52:22 -04001266/*
Chris Mason74123bd2007-02-02 11:05:29 -05001267 * look for key in the tree. path is filled in with nodes along the way
1268 * if key is found, we return zero and you can find the item in the leaf
1269 * level of the path (level 0)
1270 *
1271 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001272 * be inserted, and 1 is returned. If there are other errors during the
1273 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001274 *
1275 * if ins_len > 0, nodes and leaves will be split as we walk down the
1276 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1277 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001278 */
Chris Masone089f052007-03-16 16:20:31 -04001279int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1280 *root, struct btrfs_key *key, struct btrfs_path *p, int
1281 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001282{
Chris Mason5f39d392007-10-15 16:14:19 -04001283 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001284 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001285 int slot;
1286 int ret;
1287 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001288 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001289 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001290 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001291 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001292 u64 blocknr;
1293 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001294 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001295
Chris Mason6702ed42007-08-07 16:15:09 -04001296 lowest_level = p->lowest_level;
1297 WARN_ON(lowest_level && ins_len);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001298 WARN_ON(p->nodes[0] != NULL);
Chris Mason333db942008-06-25 16:01:30 -04001299 WARN_ON(cow && root == root->fs_info->extent_root &&
Chris Mason925baed2008-06-25 16:01:30 -04001300 !mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason925baed2008-06-25 16:01:30 -04001301 if (ins_len < 0)
1302 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001303
1304 prealloc_block.objectid = 0;
1305
Chris Masonbb803952007-03-01 12:04:21 -05001306again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001307 if (p->skip_locking)
1308 b = btrfs_root_node(root);
1309 else
1310 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001311
Chris Masoneb60cea2007-02-02 09:18:22 -05001312 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001313 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001314
1315 /*
1316 * setup the path here so we can release it under lock
1317 * contention with the cow code
1318 */
1319 p->nodes[level] = b;
1320 if (!p->skip_locking)
1321 p->locks[level] = 1;
1322
Chris Mason02217ed2007-03-02 16:08:05 -05001323 if (cow) {
1324 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001325
1326 /* is a cow on this block not required */
1327 spin_lock(&root->fs_info->hash_lock);
1328 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001329 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001330 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1331 spin_unlock(&root->fs_info->hash_lock);
1332 goto cow_done;
1333 }
1334 spin_unlock(&root->fs_info->hash_lock);
1335
1336 /* ok, we have to cow, is our old prealloc the right
1337 * size?
1338 */
1339 if (prealloc_block.objectid &&
1340 prealloc_block.offset != b->len) {
1341 btrfs_free_reserved_extent(root,
1342 prealloc_block.objectid,
1343 prealloc_block.offset);
1344 prealloc_block.objectid = 0;
1345 }
1346
1347 /*
1348 * for higher level blocks, try not to allocate blocks
1349 * with the block and the parent locks held.
1350 */
1351 if (level > 1 && !prealloc_block.objectid &&
1352 btrfs_path_lock_waiting(p, level)) {
1353 u32 size = b->len;
1354 u64 hint = b->start;
1355
1356 btrfs_release_path(root, p);
1357 ret = btrfs_reserve_extent(trans, root,
1358 size, size, 0,
1359 hint, (u64)-1,
1360 &prealloc_block, 0);
1361 BUG_ON(ret);
1362 goto again;
1363 }
1364
Chris Masone20d96d2007-03-22 12:13:20 -04001365 wret = btrfs_cow_block(trans, root, b,
1366 p->nodes[level + 1],
1367 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001368 &b, prealloc_block.objectid);
1369 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001370 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001371 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001372 ret = wret;
1373 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001374 }
Chris Mason02217ed2007-03-02 16:08:05 -05001375 }
Chris Mason65b51a02008-08-01 15:11:20 -04001376cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001377 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001378 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001379 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001380 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001381
Chris Masoneb60cea2007-02-02 09:18:22 -05001382 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001383 if (!p->skip_locking)
1384 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001385
Chris Mason123abc82007-03-14 14:14:43 -04001386 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001387 if (ret) {
1388 ret = -1;
1389 goto done;
1390 }
Chris Mason925baed2008-06-25 16:01:30 -04001391
Chris Mason5f39d392007-10-15 16:14:19 -04001392 ret = bin_search(b, key, level, &slot);
1393 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001394 if (ret && slot > 0)
1395 slot -= 1;
1396 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001397 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001398 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001399 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001400 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001401 if (sret) {
1402 ret = sret;
1403 goto done;
1404 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001405 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001406 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001407 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001408 int sret = balance_level(trans, root, p,
1409 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001410 if (sret) {
1411 ret = sret;
1412 goto done;
1413 }
Chris Masonbb803952007-03-01 12:04:21 -05001414 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001415 if (!b) {
1416 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001417 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001418 }
Chris Masonbb803952007-03-01 12:04:21 -05001419 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001420 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001421 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001422 unlock_up(p, level, lowest_unlock);
1423
Chris Mason9f3a7422007-08-07 15:52:19 -04001424 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001425 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001426 ret = 0;
1427 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001428 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001429
Chris Mason594a24e2008-06-25 16:01:30 -04001430 blocknr = btrfs_node_blockptr(b, slot);
1431 gen = btrfs_node_ptr_generation(b, slot);
1432 blocksize = btrfs_level_size(root, level - 1);
1433
1434 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1435 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001436 b = tmp;
1437 } else {
1438 /*
1439 * reduce lock contention at high levels
1440 * of the btree by dropping locks before
1441 * we read.
1442 */
1443 if (level > 1) {
1444 btrfs_release_path(NULL, p);
1445 if (tmp)
1446 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001447 if (should_reada)
1448 reada_for_search(root, p,
1449 level, slot,
1450 key->objectid);
1451
Chris Mason594a24e2008-06-25 16:01:30 -04001452 tmp = read_tree_block(root, blocknr,
1453 blocksize, gen);
1454 if (tmp)
1455 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001456 goto again;
1457 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001458 if (tmp)
1459 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001460 if (should_reada)
1461 reada_for_search(root, p,
1462 level, slot,
1463 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001464 b = read_node_slot(root, b, slot);
1465 }
1466 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001467 if (!p->skip_locking)
1468 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001469 } else {
1470 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001471 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001472 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001473 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001474 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001475 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001476 if (sret) {
1477 ret = sret;
1478 goto done;
1479 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001480 }
Chris Mason925baed2008-06-25 16:01:30 -04001481 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001482 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001483 }
1484 }
Chris Mason65b51a02008-08-01 15:11:20 -04001485 ret = 1;
1486done:
1487 if (prealloc_block.objectid) {
1488 btrfs_free_reserved_extent(root,
1489 prealloc_block.objectid,
1490 prealloc_block.offset);
1491 }
1492
1493 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001494}
1495
Zheng Yan1a40e232008-09-26 10:09:34 -04001496int btrfs_merge_path(struct btrfs_trans_handle *trans,
1497 struct btrfs_root *root,
1498 struct btrfs_key *node_keys,
1499 u64 *nodes, int lowest_level)
1500{
1501 struct extent_buffer *eb;
1502 struct extent_buffer *parent;
1503 struct btrfs_key key;
1504 u64 bytenr;
1505 u64 generation;
1506 u32 blocksize;
1507 int level;
1508 int slot;
1509 int key_match;
1510 int ret;
1511
1512 eb = btrfs_lock_root_node(root);
1513 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1514 BUG_ON(ret);
1515
1516 parent = eb;
1517 while (1) {
1518 level = btrfs_header_level(parent);
1519 if (level == 0 || level <= lowest_level)
1520 break;
1521
1522 ret = bin_search(parent, &node_keys[lowest_level], level,
1523 &slot);
1524 if (ret && slot > 0)
1525 slot--;
1526
1527 bytenr = btrfs_node_blockptr(parent, slot);
1528 if (nodes[level - 1] == bytenr)
1529 break;
1530
1531 blocksize = btrfs_level_size(root, level - 1);
1532 generation = btrfs_node_ptr_generation(parent, slot);
1533 btrfs_node_key_to_cpu(eb, &key, slot);
1534 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1535
1536 /*
1537 * if node keys match and node pointer hasn't been modified
1538 * in the running transaction, we can merge the path. for
1539 * reloc trees, the node pointer check is skipped, this is
1540 * because the reloc trees are fully controlled by the space
1541 * balance code, no one else can modify them.
1542 */
1543 if (!nodes[level - 1] || !key_match ||
1544 (generation == trans->transid &&
1545 root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)) {
1546next_level:
1547 if (level == 1 || level == lowest_level + 1)
1548 break;
1549
1550 eb = read_tree_block(root, bytenr, blocksize,
1551 generation);
1552 btrfs_tree_lock(eb);
1553
1554 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1555 &eb, 0);
1556 BUG_ON(ret);
1557
1558 btrfs_tree_unlock(parent);
1559 free_extent_buffer(parent);
1560 parent = eb;
1561 continue;
1562 }
1563
1564 if (generation == trans->transid) {
1565 u32 refs;
1566 BUG_ON(btrfs_header_owner(eb) !=
1567 BTRFS_TREE_RELOC_OBJECTID);
1568 /*
1569 * lock the block to keep __btrfs_cow_block from
1570 * changing the reference count.
1571 */
1572 eb = read_tree_block(root, bytenr, blocksize,
1573 generation);
1574 btrfs_tree_lock(eb);
1575
1576 ret = btrfs_lookup_extent_ref(trans, root, bytenr,
1577 blocksize, &refs);
1578 BUG_ON(ret);
1579 /*
1580 * if replace block whose reference count is one,
1581 * we have to "drop the subtree". so skip it for
1582 * simplicity
1583 */
1584 if (refs == 1) {
1585 btrfs_tree_unlock(eb);
1586 free_extent_buffer(eb);
1587 goto next_level;
1588 }
1589 }
1590
1591 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1592 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1593 btrfs_mark_buffer_dirty(parent);
1594
1595 ret = btrfs_inc_extent_ref(trans, root,
1596 nodes[level - 1],
1597 blocksize, parent->start,
1598 btrfs_header_owner(parent),
1599 btrfs_header_generation(parent),
1600 level - 1, 0);
1601 BUG_ON(ret);
1602 ret = btrfs_free_extent(trans, root, bytenr,
1603 blocksize, parent->start,
1604 btrfs_header_owner(parent),
1605 btrfs_header_generation(parent),
1606 level - 1, 0, 1);
1607 BUG_ON(ret);
1608
1609 if (generation == trans->transid) {
1610 btrfs_tree_unlock(eb);
1611 free_extent_buffer(eb);
1612 }
1613 break;
1614 }
1615 btrfs_tree_unlock(parent);
1616 free_extent_buffer(parent);
1617 return 0;
1618}
1619
Chris Mason74123bd2007-02-02 11:05:29 -05001620/*
1621 * adjust the pointers going up the tree, starting at level
1622 * making sure the right key of each node is points to 'key'.
1623 * This is used after shifting pointers to the left, so it stops
1624 * fixing up pointers when a given leaf/node is not in slot 0 of the
1625 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001626 *
1627 * If this fails to write a tree block, it returns -1, but continues
1628 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001629 */
Chris Mason5f39d392007-10-15 16:14:19 -04001630static int fixup_low_keys(struct btrfs_trans_handle *trans,
1631 struct btrfs_root *root, struct btrfs_path *path,
1632 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001633{
1634 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001635 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001636 struct extent_buffer *t;
1637
Chris Mason234b63a2007-03-13 10:46:10 -04001638 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001639 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001640 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001641 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001642 t = path->nodes[i];
1643 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001644 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001645 if (tslot != 0)
1646 break;
1647 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001648 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001649}
1650
Chris Mason74123bd2007-02-02 11:05:29 -05001651/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001652 * update item key.
1653 *
1654 * This function isn't completely safe. It's the caller's responsibility
1655 * that the new key won't break the order
1656 */
1657int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1658 struct btrfs_root *root, struct btrfs_path *path,
1659 struct btrfs_key *new_key)
1660{
1661 struct btrfs_disk_key disk_key;
1662 struct extent_buffer *eb;
1663 int slot;
1664
1665 eb = path->nodes[0];
1666 slot = path->slots[0];
1667 if (slot > 0) {
1668 btrfs_item_key(eb, &disk_key, slot - 1);
1669 if (comp_keys(&disk_key, new_key) >= 0)
1670 return -1;
1671 }
1672 if (slot < btrfs_header_nritems(eb) - 1) {
1673 btrfs_item_key(eb, &disk_key, slot + 1);
1674 if (comp_keys(&disk_key, new_key) <= 0)
1675 return -1;
1676 }
1677
1678 btrfs_cpu_key_to_disk(&disk_key, new_key);
1679 btrfs_set_item_key(eb, &disk_key, slot);
1680 btrfs_mark_buffer_dirty(eb);
1681 if (slot == 0)
1682 fixup_low_keys(trans, root, path, &disk_key, 1);
1683 return 0;
1684}
1685
1686/*
Chris Mason74123bd2007-02-02 11:05:29 -05001687 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001688 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001689 *
1690 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1691 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001692 */
Chris Mason98ed5172008-01-03 10:01:48 -05001693static int push_node_left(struct btrfs_trans_handle *trans,
1694 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001695 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001696{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001697 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001698 int src_nritems;
1699 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001700 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001701
Chris Mason5f39d392007-10-15 16:14:19 -04001702 src_nritems = btrfs_header_nritems(src);
1703 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001704 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001705 WARN_ON(btrfs_header_generation(src) != trans->transid);
1706 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001707
Chris Masonbce4eae2008-04-24 14:42:46 -04001708 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001709 return 1;
1710
Chris Masoneb60cea2007-02-02 09:18:22 -05001711 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001712 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001713 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001714
Chris Masonbce4eae2008-04-24 14:42:46 -04001715 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001716 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001717 if (push_items < src_nritems) {
1718 /* leave at least 8 pointers in the node if
1719 * we aren't going to empty it
1720 */
1721 if (src_nritems - push_items < 8) {
1722 if (push_items <= 8)
1723 return 1;
1724 push_items -= 8;
1725 }
1726 }
1727 } else
1728 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001729
Chris Mason5f39d392007-10-15 16:14:19 -04001730 copy_extent_buffer(dst, src,
1731 btrfs_node_key_ptr_offset(dst_nritems),
1732 btrfs_node_key_ptr_offset(0),
1733 push_items * sizeof(struct btrfs_key_ptr));
1734
Chris Masonbb803952007-03-01 12:04:21 -05001735 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001736 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1737 btrfs_node_key_ptr_offset(push_items),
1738 (src_nritems - push_items) *
1739 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001740 }
Chris Mason5f39d392007-10-15 16:14:19 -04001741 btrfs_set_header_nritems(src, src_nritems - push_items);
1742 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1743 btrfs_mark_buffer_dirty(src);
1744 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001745
1746 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1747 BUG_ON(ret);
1748
Chris Masonbb803952007-03-01 12:04:21 -05001749 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001750}
1751
Chris Mason97571fd2007-02-24 13:39:08 -05001752/*
Chris Mason79f95c82007-03-01 15:16:26 -05001753 * try to push data from one node into the next node right in the
1754 * tree.
1755 *
1756 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1757 * error, and > 0 if there was no room in the right hand block.
1758 *
1759 * this will only push up to 1/2 the contents of the left node over
1760 */
Chris Mason5f39d392007-10-15 16:14:19 -04001761static int balance_node_right(struct btrfs_trans_handle *trans,
1762 struct btrfs_root *root,
1763 struct extent_buffer *dst,
1764 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001765{
Chris Mason79f95c82007-03-01 15:16:26 -05001766 int push_items = 0;
1767 int max_push;
1768 int src_nritems;
1769 int dst_nritems;
1770 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001771
Chris Mason7bb86312007-12-11 09:25:06 -05001772 WARN_ON(btrfs_header_generation(src) != trans->transid);
1773 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1774
Chris Mason5f39d392007-10-15 16:14:19 -04001775 src_nritems = btrfs_header_nritems(src);
1776 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001777 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001778 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001779 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001780 }
1781
1782 if (src_nritems < 4) {
1783 return 1;
1784 }
Chris Mason79f95c82007-03-01 15:16:26 -05001785
1786 max_push = src_nritems / 2 + 1;
1787 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001788 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001789 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001790 }
Yan252c38f2007-08-29 09:11:44 -04001791
Chris Mason79f95c82007-03-01 15:16:26 -05001792 if (max_push < push_items)
1793 push_items = max_push;
1794
Chris Mason5f39d392007-10-15 16:14:19 -04001795 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1796 btrfs_node_key_ptr_offset(0),
1797 (dst_nritems) *
1798 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001799
Chris Mason5f39d392007-10-15 16:14:19 -04001800 copy_extent_buffer(dst, src,
1801 btrfs_node_key_ptr_offset(0),
1802 btrfs_node_key_ptr_offset(src_nritems - push_items),
1803 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001804
Chris Mason5f39d392007-10-15 16:14:19 -04001805 btrfs_set_header_nritems(src, src_nritems - push_items);
1806 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001807
Chris Mason5f39d392007-10-15 16:14:19 -04001808 btrfs_mark_buffer_dirty(src);
1809 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001810
1811 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1812 BUG_ON(ret);
1813
Chris Mason79f95c82007-03-01 15:16:26 -05001814 return ret;
1815}
1816
1817/*
Chris Mason97571fd2007-02-24 13:39:08 -05001818 * helper function to insert a new root level in the tree.
1819 * A new node is allocated, and a single item is inserted to
1820 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001821 *
1822 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001823 */
Chris Mason98ed5172008-01-03 10:01:48 -05001824static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001825 struct btrfs_root *root,
1826 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001827{
Chris Mason7bb86312007-12-11 09:25:06 -05001828 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001829 struct extent_buffer *lower;
1830 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001831 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001832 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001833 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001834
1835 BUG_ON(path->nodes[level]);
1836 BUG_ON(path->nodes[level-1] != root->node);
1837
Chris Mason7bb86312007-12-11 09:25:06 -05001838 lower = path->nodes[level-1];
1839 if (level == 1)
1840 btrfs_item_key(lower, &lower_key, 0);
1841 else
1842 btrfs_node_key(lower, &lower_key, 0);
1843
Zheng Yan31840ae2008-09-23 13:14:14 -04001844 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1845 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001846 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001847 if (IS_ERR(c))
1848 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001849
Chris Mason5f39d392007-10-15 16:14:19 -04001850 memset_extent_buffer(c, 0, 0, root->nodesize);
1851 btrfs_set_header_nritems(c, 1);
1852 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001853 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001854 btrfs_set_header_generation(c, trans->transid);
1855 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001856
Chris Mason5f39d392007-10-15 16:14:19 -04001857 write_extent_buffer(c, root->fs_info->fsid,
1858 (unsigned long)btrfs_header_fsid(c),
1859 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001860
1861 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1862 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1863 BTRFS_UUID_SIZE);
1864
Chris Mason5f39d392007-10-15 16:14:19 -04001865 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001866 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001867 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001868 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001869
1870 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001871
1872 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001873
Chris Mason925baed2008-06-25 16:01:30 -04001874 spin_lock(&root->node_lock);
1875 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001876 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001877 spin_unlock(&root->node_lock);
1878
Zheng Yan31840ae2008-09-23 13:14:14 -04001879 ret = btrfs_update_extent_ref(trans, root, lower->start,
1880 lower->start, c->start,
1881 root->root_key.objectid,
1882 trans->transid, level - 1, 0);
1883 BUG_ON(ret);
1884
Chris Mason925baed2008-06-25 16:01:30 -04001885 /* the super has an extra ref to root->node */
1886 free_extent_buffer(old);
1887
Chris Mason0b86a832008-03-24 15:01:56 -04001888 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001889 extent_buffer_get(c);
1890 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001891 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001892 path->slots[level] = 0;
1893 return 0;
1894}
1895
Chris Mason74123bd2007-02-02 11:05:29 -05001896/*
1897 * worker function to insert a single pointer in a node.
1898 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001899 *
Chris Mason74123bd2007-02-02 11:05:29 -05001900 * slot and level indicate where you want the key to go, and
1901 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001902 *
1903 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001904 */
Chris Masone089f052007-03-16 16:20:31 -04001905static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1906 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04001907 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05001908{
Chris Mason5f39d392007-10-15 16:14:19 -04001909 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05001910 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05001911
1912 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04001913 lower = path->nodes[level];
1914 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001915 if (slot > nritems)
1916 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04001917 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05001918 BUG();
1919 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001920 memmove_extent_buffer(lower,
1921 btrfs_node_key_ptr_offset(slot + 1),
1922 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04001923 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05001924 }
Chris Mason5f39d392007-10-15 16:14:19 -04001925 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04001926 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05001927 WARN_ON(trans->transid == 0);
1928 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001929 btrfs_set_header_nritems(lower, nritems + 1);
1930 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05001931 return 0;
1932}
1933
Chris Mason97571fd2007-02-24 13:39:08 -05001934/*
1935 * split the node at the specified level in path in two.
1936 * The path is corrected to point to the appropriate node after the split
1937 *
1938 * Before splitting this tries to make some room in the node by pushing
1939 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001940 *
1941 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05001942 */
Chris Masone02119d2008-09-05 16:13:11 -04001943static noinline int split_node(struct btrfs_trans_handle *trans,
1944 struct btrfs_root *root,
1945 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001946{
Chris Mason5f39d392007-10-15 16:14:19 -04001947 struct extent_buffer *c;
1948 struct extent_buffer *split;
1949 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001950 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05001951 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001952 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04001953 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001954
Chris Mason5f39d392007-10-15 16:14:19 -04001955 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001956 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001957 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05001958 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04001959 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001960 if (ret)
1961 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04001962 } else {
1963 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04001964 c = path->nodes[level];
1965 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04001966 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04001967 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001968 if (ret < 0)
1969 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001970 }
Chris Masone66f7092007-04-20 13:16:02 -04001971
Chris Mason5f39d392007-10-15 16:14:19 -04001972 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05001973
Chris Mason925baed2008-06-25 16:01:30 -04001974 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001975 path->nodes[level + 1]->start,
1976 root->root_key.objectid,
1977 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001978 if (IS_ERR(split))
1979 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04001980
Chris Mason5f39d392007-10-15 16:14:19 -04001981 btrfs_set_header_flags(split, btrfs_header_flags(c));
1982 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04001983 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001984 btrfs_set_header_generation(split, trans->transid);
1985 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04001986 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001987 write_extent_buffer(split, root->fs_info->fsid,
1988 (unsigned long)btrfs_header_fsid(split),
1989 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001990 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1991 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1992 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04001993
Chris Mason7518a232007-03-12 12:01:18 -04001994 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001995
1996 copy_extent_buffer(split, c,
1997 btrfs_node_key_ptr_offset(0),
1998 btrfs_node_key_ptr_offset(mid),
1999 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2000 btrfs_set_header_nritems(split, c_nritems - mid);
2001 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002002 ret = 0;
2003
Chris Mason5f39d392007-10-15 16:14:19 -04002004 btrfs_mark_buffer_dirty(c);
2005 btrfs_mark_buffer_dirty(split);
2006
2007 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002008 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002009 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002010 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002011 if (wret)
2012 ret = wret;
2013
Zheng Yan31840ae2008-09-23 13:14:14 -04002014 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2015 BUG_ON(ret);
2016
Chris Mason5de08d72007-02-24 06:24:44 -05002017 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002018 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002019 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002020 free_extent_buffer(c);
2021 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002022 path->slots[level + 1] += 1;
2023 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002024 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002025 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002026 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002027 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002028}
2029
Chris Mason74123bd2007-02-02 11:05:29 -05002030/*
2031 * how many bytes are required to store the items in a leaf. start
2032 * and nr indicate which items in the leaf to check. This totals up the
2033 * space used both by the item structs and the item data
2034 */
Chris Mason5f39d392007-10-15 16:14:19 -04002035static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002036{
2037 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002038 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002039 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002040
2041 if (!nr)
2042 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002043 data_len = btrfs_item_end_nr(l, start);
2044 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002045 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002046 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002047 return data_len;
2048}
2049
Chris Mason74123bd2007-02-02 11:05:29 -05002050/*
Chris Masond4dbff92007-04-04 14:08:15 -04002051 * The space between the end of the leaf items and
2052 * the start of the leaf data. IOW, how much room
2053 * the leaf has left for both items and data
2054 */
Chris Masone02119d2008-09-05 16:13:11 -04002055int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2056 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002057{
Chris Mason5f39d392007-10-15 16:14:19 -04002058 int nritems = btrfs_header_nritems(leaf);
2059 int ret;
2060 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2061 if (ret < 0) {
2062 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002063 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002064 leaf_space_used(leaf, 0, nritems), nritems);
2065 }
2066 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002067}
2068
2069/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002070 * push some data in the path leaf to the right, trying to free up at
2071 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002072 *
2073 * returns 1 if the push failed because the other node didn't have enough
2074 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002075 */
Chris Masone089f052007-03-16 16:20:31 -04002076static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002077 *root, struct btrfs_path *path, int data_size,
2078 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002079{
Chris Mason5f39d392007-10-15 16:14:19 -04002080 struct extent_buffer *left = path->nodes[0];
2081 struct extent_buffer *right;
2082 struct extent_buffer *upper;
2083 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002084 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002085 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002086 int free_space;
2087 int push_space = 0;
2088 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002089 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002090 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002091 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002092 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002093 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002094 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002095 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002096
2097 slot = path->slots[1];
2098 if (!path->nodes[1]) {
2099 return 1;
2100 }
2101 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002102 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002103 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002104
Chris Masona2135012008-06-25 16:01:30 -04002105 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2106
Chris Masonca7a79a2008-05-12 12:59:19 -04002107 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002108 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002109 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002110 if (free_space < data_size + sizeof(struct btrfs_item))
2111 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002112
Chris Mason5f39d392007-10-15 16:14:19 -04002113 /* cow and double check */
2114 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002115 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002116 if (ret)
2117 goto out_unlock;
2118
Chris Mason5f39d392007-10-15 16:14:19 -04002119 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002120 if (free_space < data_size + sizeof(struct btrfs_item))
2121 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002122
2123 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002124 if (left_nritems == 0)
2125 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002126
Chris Mason34a38212007-11-07 13:31:03 -05002127 if (empty)
2128 nr = 0;
2129 else
2130 nr = 1;
2131
Zheng Yan31840ae2008-09-23 13:14:14 -04002132 if (path->slots[0] >= left_nritems)
2133 push_space += data_size + sizeof(*item);
2134
Chris Mason34a38212007-11-07 13:31:03 -05002135 i = left_nritems - 1;
2136 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002137 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002138
Zheng Yan31840ae2008-09-23 13:14:14 -04002139 if (!empty && push_items > 0) {
2140 if (path->slots[0] > i)
2141 break;
2142 if (path->slots[0] == i) {
2143 int space = btrfs_leaf_free_space(root, left);
2144 if (space + push_space * 2 > free_space)
2145 break;
2146 }
2147 }
2148
Chris Mason00ec4c52007-02-24 12:47:20 -05002149 if (path->slots[0] == i)
2150 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002151
2152 if (!left->map_token) {
2153 map_extent_buffer(left, (unsigned long)item,
2154 sizeof(struct btrfs_item),
2155 &left->map_token, &left->kaddr,
2156 &left->map_start, &left->map_len,
2157 KM_USER1);
2158 }
2159
2160 this_item_size = btrfs_item_size(left, item);
2161 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002162 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002163
Chris Mason00ec4c52007-02-24 12:47:20 -05002164 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002165 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002166 if (i == 0)
2167 break;
2168 i--;
Chris Masondb945352007-10-15 16:15:53 -04002169 }
2170 if (left->map_token) {
2171 unmap_extent_buffer(left, left->map_token, KM_USER1);
2172 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002173 }
Chris Mason5f39d392007-10-15 16:14:19 -04002174
Chris Mason925baed2008-06-25 16:01:30 -04002175 if (push_items == 0)
2176 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002177
Chris Mason34a38212007-11-07 13:31:03 -05002178 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002179 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002180
Chris Mason00ec4c52007-02-24 12:47:20 -05002181 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002182 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002183
Chris Mason5f39d392007-10-15 16:14:19 -04002184 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002185 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002186
Chris Mason00ec4c52007-02-24 12:47:20 -05002187 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002188 data_end = leaf_data_end(root, right);
2189 memmove_extent_buffer(right,
2190 btrfs_leaf_data(right) + data_end - push_space,
2191 btrfs_leaf_data(right) + data_end,
2192 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2193
Chris Mason00ec4c52007-02-24 12:47:20 -05002194 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002195 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002196 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2197 btrfs_leaf_data(left) + leaf_data_end(root, left),
2198 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002199
2200 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2201 btrfs_item_nr_offset(0),
2202 right_nritems * sizeof(struct btrfs_item));
2203
Chris Mason00ec4c52007-02-24 12:47:20 -05002204 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002205 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2206 btrfs_item_nr_offset(left_nritems - push_items),
2207 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002208
2209 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002210 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002211 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002212 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002213 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002214 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002215 if (!right->map_token) {
2216 map_extent_buffer(right, (unsigned long)item,
2217 sizeof(struct btrfs_item),
2218 &right->map_token, &right->kaddr,
2219 &right->map_start, &right->map_len,
2220 KM_USER1);
2221 }
2222 push_space -= btrfs_item_size(right, item);
2223 btrfs_set_item_offset(right, item, push_space);
2224 }
2225
2226 if (right->map_token) {
2227 unmap_extent_buffer(right, right->map_token, KM_USER1);
2228 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002229 }
Chris Mason7518a232007-03-12 12:01:18 -04002230 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002231 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002232
Chris Mason34a38212007-11-07 13:31:03 -05002233 if (left_nritems)
2234 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002235 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002236
Zheng Yan31840ae2008-09-23 13:14:14 -04002237 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2238 BUG_ON(ret);
2239
Chris Mason5f39d392007-10-15 16:14:19 -04002240 btrfs_item_key(right, &disk_key, 0);
2241 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002242 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002243
Chris Mason00ec4c52007-02-24 12:47:20 -05002244 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002245 if (path->slots[0] >= left_nritems) {
2246 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002247 if (btrfs_header_nritems(path->nodes[0]) == 0)
2248 clean_tree_block(trans, root, path->nodes[0]);
2249 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002250 free_extent_buffer(path->nodes[0]);
2251 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002252 path->slots[1] += 1;
2253 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002254 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002255 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002256 }
2257 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002258
2259out_unlock:
2260 btrfs_tree_unlock(right);
2261 free_extent_buffer(right);
2262 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002263}
Chris Mason925baed2008-06-25 16:01:30 -04002264
Chris Mason00ec4c52007-02-24 12:47:20 -05002265/*
Chris Mason74123bd2007-02-02 11:05:29 -05002266 * push some data in the path leaf to the left, trying to free up at
2267 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2268 */
Chris Masone089f052007-03-16 16:20:31 -04002269static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002270 *root, struct btrfs_path *path, int data_size,
2271 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002272{
Chris Mason5f39d392007-10-15 16:14:19 -04002273 struct btrfs_disk_key disk_key;
2274 struct extent_buffer *right = path->nodes[0];
2275 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002276 int slot;
2277 int i;
2278 int free_space;
2279 int push_space = 0;
2280 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002281 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002282 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002283 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002284 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002285 int ret = 0;
2286 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002287 u32 this_item_size;
2288 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002289
2290 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002291 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002292 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002293 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002294 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002295
Chris Mason3685f792007-10-19 09:23:27 -04002296 right_nritems = btrfs_header_nritems(right);
2297 if (right_nritems == 0) {
2298 return 1;
2299 }
2300
Chris Masona2135012008-06-25 16:01:30 -04002301 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2302
Chris Masonca7a79a2008-05-12 12:59:19 -04002303 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002304 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002305 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002306 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002307 ret = 1;
2308 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002309 }
Chris Mason02217ed2007-03-02 16:08:05 -05002310
2311 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002312 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002313 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002314 if (ret) {
2315 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002316 ret = 1;
2317 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002318 }
Chris Mason3685f792007-10-19 09:23:27 -04002319
Chris Mason123abc82007-03-14 14:14:43 -04002320 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002321 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002322 ret = 1;
2323 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002324 }
2325
Chris Mason34a38212007-11-07 13:31:03 -05002326 if (empty)
2327 nr = right_nritems;
2328 else
2329 nr = right_nritems - 1;
2330
2331 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002332 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002333 if (!right->map_token) {
2334 map_extent_buffer(right, (unsigned long)item,
2335 sizeof(struct btrfs_item),
2336 &right->map_token, &right->kaddr,
2337 &right->map_start, &right->map_len,
2338 KM_USER1);
2339 }
2340
Zheng Yan31840ae2008-09-23 13:14:14 -04002341 if (!empty && push_items > 0) {
2342 if (path->slots[0] < i)
2343 break;
2344 if (path->slots[0] == i) {
2345 int space = btrfs_leaf_free_space(root, right);
2346 if (space + push_space * 2 > free_space)
2347 break;
2348 }
2349 }
2350
Chris Masonbe0e5c02007-01-26 15:51:26 -05002351 if (path->slots[0] == i)
2352 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002353
2354 this_item_size = btrfs_item_size(right, item);
2355 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002356 break;
Chris Masondb945352007-10-15 16:15:53 -04002357
Chris Masonbe0e5c02007-01-26 15:51:26 -05002358 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002359 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002360 }
Chris Masondb945352007-10-15 16:15:53 -04002361
2362 if (right->map_token) {
2363 unmap_extent_buffer(right, right->map_token, KM_USER1);
2364 right->map_token = NULL;
2365 }
2366
Chris Masonbe0e5c02007-01-26 15:51:26 -05002367 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002368 ret = 1;
2369 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002370 }
Chris Mason34a38212007-11-07 13:31:03 -05002371 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002372 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002373
Chris Masonbe0e5c02007-01-26 15:51:26 -05002374 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002375 copy_extent_buffer(left, right,
2376 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2377 btrfs_item_nr_offset(0),
2378 push_items * sizeof(struct btrfs_item));
2379
Chris Mason123abc82007-03-14 14:14:43 -04002380 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002381 btrfs_item_offset_nr(right, push_items -1);
2382
2383 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002384 leaf_data_end(root, left) - push_space,
2385 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002386 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002387 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002388 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002389 BUG_ON(old_left_nritems < 0);
2390
Chris Masondb945352007-10-15 16:15:53 -04002391 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002392 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002393 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002394
Chris Mason5f39d392007-10-15 16:14:19 -04002395 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002396 if (!left->map_token) {
2397 map_extent_buffer(left, (unsigned long)item,
2398 sizeof(struct btrfs_item),
2399 &left->map_token, &left->kaddr,
2400 &left->map_start, &left->map_len,
2401 KM_USER1);
2402 }
2403
Chris Mason5f39d392007-10-15 16:14:19 -04002404 ioff = btrfs_item_offset(left, item);
2405 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002406 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002407 }
Chris Mason5f39d392007-10-15 16:14:19 -04002408 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002409 if (left->map_token) {
2410 unmap_extent_buffer(left, left->map_token, KM_USER1);
2411 left->map_token = NULL;
2412 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002413
2414 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002415 if (push_items > right_nritems) {
2416 printk("push items %d nr %u\n", push_items, right_nritems);
2417 WARN_ON(1);
2418 }
Chris Mason5f39d392007-10-15 16:14:19 -04002419
Chris Mason34a38212007-11-07 13:31:03 -05002420 if (push_items < right_nritems) {
2421 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2422 leaf_data_end(root, right);
2423 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2424 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2425 btrfs_leaf_data(right) +
2426 leaf_data_end(root, right), push_space);
2427
2428 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002429 btrfs_item_nr_offset(push_items),
2430 (btrfs_header_nritems(right) - push_items) *
2431 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002432 }
Yaneef1c492007-11-26 10:58:13 -05002433 right_nritems -= push_items;
2434 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002435 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002436 for (i = 0; i < right_nritems; i++) {
2437 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002438
2439 if (!right->map_token) {
2440 map_extent_buffer(right, (unsigned long)item,
2441 sizeof(struct btrfs_item),
2442 &right->map_token, &right->kaddr,
2443 &right->map_start, &right->map_len,
2444 KM_USER1);
2445 }
2446
2447 push_space = push_space - btrfs_item_size(right, item);
2448 btrfs_set_item_offset(right, item, push_space);
2449 }
2450 if (right->map_token) {
2451 unmap_extent_buffer(right, right->map_token, KM_USER1);
2452 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002453 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002454
Chris Mason5f39d392007-10-15 16:14:19 -04002455 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002456 if (right_nritems)
2457 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002458
Zheng Yan31840ae2008-09-23 13:14:14 -04002459 ret = btrfs_update_ref(trans, root, right, left,
2460 old_left_nritems, push_items);
2461 BUG_ON(ret);
2462
Chris Mason5f39d392007-10-15 16:14:19 -04002463 btrfs_item_key(right, &disk_key, 0);
2464 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002465 if (wret)
2466 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002467
2468 /* then fixup the leaf pointer in the path */
2469 if (path->slots[0] < push_items) {
2470 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002471 if (btrfs_header_nritems(path->nodes[0]) == 0)
2472 clean_tree_block(trans, root, path->nodes[0]);
2473 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002474 free_extent_buffer(path->nodes[0]);
2475 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002476 path->slots[1] -= 1;
2477 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002478 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002479 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002480 path->slots[0] -= push_items;
2481 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002482 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002483 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002484out:
2485 btrfs_tree_unlock(left);
2486 free_extent_buffer(left);
2487 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002488}
2489
Chris Mason74123bd2007-02-02 11:05:29 -05002490/*
2491 * split the path's leaf in two, making sure there is at least data_size
2492 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002493 *
2494 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002495 */
Chris Masone02119d2008-09-05 16:13:11 -04002496static noinline int split_leaf(struct btrfs_trans_handle *trans,
2497 struct btrfs_root *root,
2498 struct btrfs_key *ins_key,
2499 struct btrfs_path *path, int data_size,
2500 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002501{
Chris Mason5f39d392007-10-15 16:14:19 -04002502 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002503 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002504 int mid;
2505 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002506 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002507 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002508 int data_copy_size;
2509 int rt_data_off;
2510 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002511 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002512 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002513 int double_split;
2514 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002515 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002516
Chris Masoncc0c5532007-10-25 15:42:57 -04002517 if (extend)
2518 space_needed = data_size;
2519
Chris Mason40689472007-03-17 14:29:23 -04002520 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002521 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002522 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002523 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002524 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002525 }
2526 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002527 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002528 if (wret < 0)
2529 return wret;
2530 }
2531 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002532
Chris Mason3685f792007-10-19 09:23:27 -04002533 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002534 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002535 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002536 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002537
Chris Mason5c680ed2007-02-22 11:39:13 -05002538 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002539 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002540 if (ret)
2541 return ret;
2542 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002543again:
2544 double_split = 0;
2545 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002546 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002547 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002548 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002549
Chris Mason925baed2008-06-25 16:01:30 -04002550 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002551 path->nodes[1]->start,
2552 root->root_key.objectid,
2553 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002554 if (IS_ERR(right)) {
2555 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002556 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002557 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002558
Chris Mason5f39d392007-10-15 16:14:19 -04002559 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002560 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002561 btrfs_set_header_generation(right, trans->transid);
2562 btrfs_set_header_owner(right, root->root_key.objectid);
2563 btrfs_set_header_level(right, 0);
2564 write_extent_buffer(right, root->fs_info->fsid,
2565 (unsigned long)btrfs_header_fsid(right),
2566 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002567
2568 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2569 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2570 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002571 if (mid <= slot) {
2572 if (nritems == 1 ||
2573 leaf_space_used(l, mid, nritems - mid) + space_needed >
2574 BTRFS_LEAF_DATA_SIZE(root)) {
2575 if (slot >= nritems) {
2576 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002577 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002578 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002579 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002580 path->slots[1] + 1, 1);
2581 if (wret)
2582 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002583
2584 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002585 free_extent_buffer(path->nodes[0]);
2586 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002587 path->slots[0] = 0;
2588 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002589 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002590 return ret;
2591 }
2592 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002593 if (mid != nritems &&
2594 leaf_space_used(l, mid, nritems - mid) +
2595 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2596 double_split = 1;
2597 }
Chris Masond4dbff92007-04-04 14:08:15 -04002598 }
2599 } else {
2600 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2601 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002602 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002603 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002604 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002605 wret = insert_ptr(trans, root, path,
2606 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002607 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002608 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002609 if (wret)
2610 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002611 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002612 free_extent_buffer(path->nodes[0]);
2613 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002614 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002615 if (path->slots[1] == 0) {
2616 wret = fixup_low_keys(trans, root,
2617 path, &disk_key, 1);
2618 if (wret)
2619 ret = wret;
2620 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002621 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002622 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002623 } else if (extend && slot == 0) {
2624 mid = 1;
2625 } else {
2626 mid = slot;
2627 if (mid != nritems &&
2628 leaf_space_used(l, mid, nritems - mid) +
2629 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2630 double_split = 1;
2631 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002632 }
Chris Masond4dbff92007-04-04 14:08:15 -04002633 }
2634 }
Chris Mason5f39d392007-10-15 16:14:19 -04002635 nritems = nritems - mid;
2636 btrfs_set_header_nritems(right, nritems);
2637 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2638
2639 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2640 btrfs_item_nr_offset(mid),
2641 nritems * sizeof(struct btrfs_item));
2642
2643 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002644 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2645 data_copy_size, btrfs_leaf_data(l) +
2646 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002647
Chris Mason5f39d392007-10-15 16:14:19 -04002648 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2649 btrfs_item_end_nr(l, mid);
2650
2651 for (i = 0; i < nritems; i++) {
2652 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002653 u32 ioff;
2654
2655 if (!right->map_token) {
2656 map_extent_buffer(right, (unsigned long)item,
2657 sizeof(struct btrfs_item),
2658 &right->map_token, &right->kaddr,
2659 &right->map_start, &right->map_len,
2660 KM_USER1);
2661 }
2662
2663 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002664 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002665 }
Chris Mason74123bd2007-02-02 11:05:29 -05002666
Chris Masondb945352007-10-15 16:15:53 -04002667 if (right->map_token) {
2668 unmap_extent_buffer(right, right->map_token, KM_USER1);
2669 right->map_token = NULL;
2670 }
2671
Chris Mason5f39d392007-10-15 16:14:19 -04002672 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002673 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002674 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002675 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2676 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002677 if (wret)
2678 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002679
2680 btrfs_mark_buffer_dirty(right);
2681 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002682 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002683
Zheng Yan31840ae2008-09-23 13:14:14 -04002684 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2685 BUG_ON(ret);
2686
Chris Masonbe0e5c02007-01-26 15:51:26 -05002687 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002688 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002689 free_extent_buffer(path->nodes[0]);
2690 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002691 path->slots[0] -= mid;
2692 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002693 } else {
2694 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002695 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002696 }
Chris Mason5f39d392007-10-15 16:14:19 -04002697
Chris Masoneb60cea2007-02-02 09:18:22 -05002698 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002699
Chris Masoncc0c5532007-10-25 15:42:57 -04002700 if (double_split) {
2701 BUG_ON(num_doubles != 0);
2702 num_doubles++;
2703 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002704 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002705 return ret;
2706}
2707
Chris Masonb18c6682007-04-17 13:26:50 -04002708int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2709 struct btrfs_root *root,
2710 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002711 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002712{
2713 int ret = 0;
2714 int slot;
2715 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002716 struct extent_buffer *leaf;
2717 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002718 u32 nritems;
2719 unsigned int data_end;
2720 unsigned int old_data_start;
2721 unsigned int old_size;
2722 unsigned int size_diff;
2723 int i;
2724
2725 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002726 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002727 slot = path->slots[0];
2728
2729 old_size = btrfs_item_size_nr(leaf, slot);
2730 if (old_size == new_size)
2731 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002732
Chris Mason5f39d392007-10-15 16:14:19 -04002733 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002734 data_end = leaf_data_end(root, leaf);
2735
Chris Mason5f39d392007-10-15 16:14:19 -04002736 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002737
Chris Masonb18c6682007-04-17 13:26:50 -04002738 size_diff = old_size - new_size;
2739
2740 BUG_ON(slot < 0);
2741 BUG_ON(slot >= nritems);
2742
2743 /*
2744 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2745 */
2746 /* first correct the data pointers */
2747 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002748 u32 ioff;
2749 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002750
2751 if (!leaf->map_token) {
2752 map_extent_buffer(leaf, (unsigned long)item,
2753 sizeof(struct btrfs_item),
2754 &leaf->map_token, &leaf->kaddr,
2755 &leaf->map_start, &leaf->map_len,
2756 KM_USER1);
2757 }
2758
Chris Mason5f39d392007-10-15 16:14:19 -04002759 ioff = btrfs_item_offset(leaf, item);
2760 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002761 }
Chris Masondb945352007-10-15 16:15:53 -04002762
2763 if (leaf->map_token) {
2764 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2765 leaf->map_token = NULL;
2766 }
2767
Chris Masonb18c6682007-04-17 13:26:50 -04002768 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002769 if (from_end) {
2770 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2771 data_end + size_diff, btrfs_leaf_data(leaf) +
2772 data_end, old_data_start + new_size - data_end);
2773 } else {
2774 struct btrfs_disk_key disk_key;
2775 u64 offset;
2776
2777 btrfs_item_key(leaf, &disk_key, slot);
2778
2779 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2780 unsigned long ptr;
2781 struct btrfs_file_extent_item *fi;
2782
2783 fi = btrfs_item_ptr(leaf, slot,
2784 struct btrfs_file_extent_item);
2785 fi = (struct btrfs_file_extent_item *)(
2786 (unsigned long)fi - size_diff);
2787
2788 if (btrfs_file_extent_type(leaf, fi) ==
2789 BTRFS_FILE_EXTENT_INLINE) {
2790 ptr = btrfs_item_ptr_offset(leaf, slot);
2791 memmove_extent_buffer(leaf, ptr,
2792 (unsigned long)fi,
2793 offsetof(struct btrfs_file_extent_item,
2794 disk_bytenr));
2795 }
2796 }
2797
2798 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2799 data_end + size_diff, btrfs_leaf_data(leaf) +
2800 data_end, old_data_start - data_end);
2801
2802 offset = btrfs_disk_key_offset(&disk_key);
2803 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2804 btrfs_set_item_key(leaf, &disk_key, slot);
2805 if (slot == 0)
2806 fixup_low_keys(trans, root, path, &disk_key, 1);
2807 }
Chris Mason5f39d392007-10-15 16:14:19 -04002808
2809 item = btrfs_item_nr(leaf, slot);
2810 btrfs_set_item_size(leaf, item, new_size);
2811 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002812
2813 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002814 if (btrfs_leaf_free_space(root, leaf) < 0) {
2815 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002816 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002817 }
Chris Masonb18c6682007-04-17 13:26:50 -04002818 return ret;
2819}
2820
Chris Mason5f39d392007-10-15 16:14:19 -04002821int btrfs_extend_item(struct btrfs_trans_handle *trans,
2822 struct btrfs_root *root, struct btrfs_path *path,
2823 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002824{
2825 int ret = 0;
2826 int slot;
2827 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002828 struct extent_buffer *leaf;
2829 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002830 u32 nritems;
2831 unsigned int data_end;
2832 unsigned int old_data;
2833 unsigned int old_size;
2834 int i;
2835
2836 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002837 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002838
Chris Mason5f39d392007-10-15 16:14:19 -04002839 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002840 data_end = leaf_data_end(root, leaf);
2841
Chris Mason5f39d392007-10-15 16:14:19 -04002842 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2843 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002844 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002845 }
Chris Mason6567e832007-04-16 09:22:45 -04002846 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002847 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002848
2849 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002850 if (slot >= nritems) {
2851 btrfs_print_leaf(root, leaf);
2852 printk("slot %d too large, nritems %d\n", slot, nritems);
2853 BUG_ON(1);
2854 }
Chris Mason6567e832007-04-16 09:22:45 -04002855
2856 /*
2857 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2858 */
2859 /* first correct the data pointers */
2860 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002861 u32 ioff;
2862 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002863
2864 if (!leaf->map_token) {
2865 map_extent_buffer(leaf, (unsigned long)item,
2866 sizeof(struct btrfs_item),
2867 &leaf->map_token, &leaf->kaddr,
2868 &leaf->map_start, &leaf->map_len,
2869 KM_USER1);
2870 }
Chris Mason5f39d392007-10-15 16:14:19 -04002871 ioff = btrfs_item_offset(leaf, item);
2872 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002873 }
Chris Mason5f39d392007-10-15 16:14:19 -04002874
Chris Masondb945352007-10-15 16:15:53 -04002875 if (leaf->map_token) {
2876 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2877 leaf->map_token = NULL;
2878 }
2879
Chris Mason6567e832007-04-16 09:22:45 -04002880 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002881 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002882 data_end - data_size, btrfs_leaf_data(leaf) +
2883 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002884
Chris Mason6567e832007-04-16 09:22:45 -04002885 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002886 old_size = btrfs_item_size_nr(leaf, slot);
2887 item = btrfs_item_nr(leaf, slot);
2888 btrfs_set_item_size(leaf, item, old_size + data_size);
2889 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002890
2891 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002892 if (btrfs_leaf_free_space(root, leaf) < 0) {
2893 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002894 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002895 }
Chris Mason6567e832007-04-16 09:22:45 -04002896 return ret;
2897}
2898
Chris Mason74123bd2007-02-02 11:05:29 -05002899/*
2900 * Given a key and some data, insert an item into the tree.
2901 * This does all the path init required, making room in the tree if needed.
2902 */
Chris Mason9c583092008-01-29 15:15:18 -05002903int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002904 struct btrfs_root *root,
2905 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05002906 struct btrfs_key *cpu_key, u32 *data_size,
2907 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002908{
Chris Mason5f39d392007-10-15 16:14:19 -04002909 struct extent_buffer *leaf;
2910 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002911 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002912 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05002913 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05002914 int i;
Chris Mason7518a232007-03-12 12:01:18 -04002915 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05002916 u32 total_size = 0;
2917 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002918 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04002919 struct btrfs_disk_key disk_key;
2920
Chris Mason9c583092008-01-29 15:15:18 -05002921 for (i = 0; i < nr; i++) {
2922 total_data += data_size[i];
2923 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002924
Josef Bacik7b128762008-07-24 12:17:14 -04002925 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05002926 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002927 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05002928 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05002929 if (ret < 0)
2930 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002931
Chris Mason62e27492007-03-15 12:56:47 -04002932 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002933 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05002934
Chris Mason5f39d392007-10-15 16:14:19 -04002935 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04002936 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05002937
Chris Masonf25956c2008-09-12 15:32:53 -04002938 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002939 btrfs_print_leaf(root, leaf);
2940 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05002941 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002942 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04002943 }
Chris Mason5f39d392007-10-15 16:14:19 -04002944
Chris Mason62e27492007-03-15 12:56:47 -04002945 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002946 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002947
Chris Masonbe0e5c02007-01-26 15:51:26 -05002948 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002949 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002950
Chris Mason5f39d392007-10-15 16:14:19 -04002951 if (old_data < data_end) {
2952 btrfs_print_leaf(root, leaf);
2953 printk("slot %d old_data %d data_end %d\n",
2954 slot, old_data, data_end);
2955 BUG_ON(1);
2956 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002957 /*
2958 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2959 */
2960 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04002961 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04002962 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002963 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002964
Chris Mason5f39d392007-10-15 16:14:19 -04002965 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002966 if (!leaf->map_token) {
2967 map_extent_buffer(leaf, (unsigned long)item,
2968 sizeof(struct btrfs_item),
2969 &leaf->map_token, &leaf->kaddr,
2970 &leaf->map_start, &leaf->map_len,
2971 KM_USER1);
2972 }
2973
Chris Mason5f39d392007-10-15 16:14:19 -04002974 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05002975 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04002976 }
Chris Masondb945352007-10-15 16:15:53 -04002977 if (leaf->map_token) {
2978 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2979 leaf->map_token = NULL;
2980 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002981
2982 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05002983 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04002984 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002985 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002986
2987 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002988 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05002989 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04002990 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002991 data_end = old_data;
2992 }
Chris Mason5f39d392007-10-15 16:14:19 -04002993
Chris Mason62e27492007-03-15 12:56:47 -04002994 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05002995 for (i = 0; i < nr; i++) {
2996 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2997 btrfs_set_item_key(leaf, &disk_key, slot + i);
2998 item = btrfs_item_nr(leaf, slot + i);
2999 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3000 data_end -= data_size[i];
3001 btrfs_set_item_size(leaf, item, data_size[i]);
3002 }
3003 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003004 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003005
3006 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003007 if (slot == 0) {
3008 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003009 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003010 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003011
Chris Mason5f39d392007-10-15 16:14:19 -04003012 if (btrfs_leaf_free_space(root, leaf) < 0) {
3013 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003014 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003015 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003016out:
Chris Mason62e27492007-03-15 12:56:47 -04003017 return ret;
3018}
3019
3020/*
3021 * Given a key and some data, insert an item into the tree.
3022 * This does all the path init required, making room in the tree if needed.
3023 */
Chris Masone089f052007-03-16 16:20:31 -04003024int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3025 *root, struct btrfs_key *cpu_key, void *data, u32
3026 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003027{
3028 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003029 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003030 struct extent_buffer *leaf;
3031 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003032
Chris Mason2c90e5d2007-04-02 10:50:19 -04003033 path = btrfs_alloc_path();
3034 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003035 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003036 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003037 leaf = path->nodes[0];
3038 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3039 write_extent_buffer(leaf, data, ptr, data_size);
3040 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003041 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003042 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003043 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003044}
3045
Chris Mason74123bd2007-02-02 11:05:29 -05003046/*
Chris Mason5de08d72007-02-24 06:24:44 -05003047 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003048 *
3049 * If the delete empties a node, the node is removed from the tree,
3050 * continuing all the way the root if required. The root is converted into
3051 * a leaf if all the nodes are emptied.
3052 */
Chris Masone089f052007-03-16 16:20:31 -04003053static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3054 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003055{
Chris Mason5f39d392007-10-15 16:14:19 -04003056 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003057 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003058 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003059 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003060
Chris Mason5f39d392007-10-15 16:14:19 -04003061 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05003062 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003063 memmove_extent_buffer(parent,
3064 btrfs_node_key_ptr_offset(slot),
3065 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003066 sizeof(struct btrfs_key_ptr) *
3067 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003068 }
Chris Mason7518a232007-03-12 12:01:18 -04003069 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003070 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003071 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003072 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003073 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003074 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003075 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003076 struct btrfs_disk_key disk_key;
3077
3078 btrfs_node_key(parent, &disk_key, 0);
3079 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003080 if (wret)
3081 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003082 }
Chris Masond6025572007-03-30 14:27:56 -04003083 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003084 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003085}
3086
Chris Mason74123bd2007-02-02 11:05:29 -05003087/*
3088 * delete the item at the leaf level in path. If that empties
3089 * the leaf, remove it from the tree
3090 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003091int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3092 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003093{
Chris Mason5f39d392007-10-15 16:14:19 -04003094 struct extent_buffer *leaf;
3095 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003096 int last_off;
3097 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003098 int ret = 0;
3099 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003100 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003101 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003102
Chris Mason5f39d392007-10-15 16:14:19 -04003103 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003104 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3105
3106 for (i = 0; i < nr; i++)
3107 dsize += btrfs_item_size_nr(leaf, slot + i);
3108
Chris Mason5f39d392007-10-15 16:14:19 -04003109 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003110
Chris Mason85e21ba2008-01-29 15:11:36 -05003111 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003112 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003113
3114 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003115 data_end + dsize,
3116 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003117 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003118
Chris Mason85e21ba2008-01-29 15:11:36 -05003119 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003120 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003121
Chris Mason5f39d392007-10-15 16:14:19 -04003122 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003123 if (!leaf->map_token) {
3124 map_extent_buffer(leaf, (unsigned long)item,
3125 sizeof(struct btrfs_item),
3126 &leaf->map_token, &leaf->kaddr,
3127 &leaf->map_start, &leaf->map_len,
3128 KM_USER1);
3129 }
Chris Mason5f39d392007-10-15 16:14:19 -04003130 ioff = btrfs_item_offset(leaf, item);
3131 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003132 }
Chris Masondb945352007-10-15 16:15:53 -04003133
3134 if (leaf->map_token) {
3135 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3136 leaf->map_token = NULL;
3137 }
3138
Chris Mason5f39d392007-10-15 16:14:19 -04003139 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003140 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003141 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003142 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003143 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003144 btrfs_set_header_nritems(leaf, nritems - nr);
3145 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003146
Chris Mason74123bd2007-02-02 11:05:29 -05003147 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003148 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003149 if (leaf == root->node) {
3150 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003151 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05003152 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Masone089f052007-03-16 16:20:31 -04003153 wret = del_ptr(trans, root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003154 if (wret)
3155 ret = wret;
Chris Masone089f052007-03-16 16:20:31 -04003156 wret = btrfs_free_extent(trans, root,
Chris Mason7bb86312007-12-11 09:25:06 -05003157 leaf->start, leaf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04003158 path->nodes[1]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003159 btrfs_header_owner(path->nodes[1]),
3160 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003161 if (wret)
3162 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05003163 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003164 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003165 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003166 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003167 struct btrfs_disk_key disk_key;
3168
3169 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003170 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003171 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003172 if (wret)
3173 ret = wret;
3174 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003175
Chris Mason74123bd2007-02-02 11:05:29 -05003176 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003177 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003178 /* push_leaf_left fixes the path.
3179 * make sure the path still points to our leaf
3180 * for possible call to del_ptr below
3181 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003182 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003183 extent_buffer_get(leaf);
3184
Chris Mason85e21ba2008-01-29 15:11:36 -05003185 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003186 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003187 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003188
3189 if (path->nodes[0] == leaf &&
3190 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003191 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003192 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003193 ret = wret;
3194 }
Chris Mason5f39d392007-10-15 16:14:19 -04003195
3196 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason7bb86312007-12-11 09:25:06 -05003197 u64 root_gen;
Chris Masondb945352007-10-15 16:15:53 -04003198 u64 bytenr = leaf->start;
3199 u32 blocksize = leaf->len;
Chris Mason5f39d392007-10-15 16:14:19 -04003200
Chris Mason7bb86312007-12-11 09:25:06 -05003201 root_gen = btrfs_header_generation(
3202 path->nodes[1]);
3203
Chris Masone089f052007-03-16 16:20:31 -04003204 wret = del_ptr(trans, root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003205 if (wret)
3206 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003207
3208 free_extent_buffer(leaf);
Chris Masondb945352007-10-15 16:15:53 -04003209 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003210 blocksize, path->nodes[1]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003211 btrfs_header_owner(path->nodes[1]),
3212 root_gen, 0, 0, 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003213 if (wret)
3214 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05003215 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003216 /* if we're still in the path, make sure
3217 * we're dirty. Otherwise, one of the
3218 * push_leaf functions must have already
3219 * dirtied this buffer
3220 */
3221 if (path->nodes[0] == leaf)
3222 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003223 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003224 }
Chris Masond5719762007-03-23 10:01:08 -04003225 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003226 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003227 }
3228 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003229 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003230}
3231
Chris Mason97571fd2007-02-24 13:39:08 -05003232/*
Chris Mason925baed2008-06-25 16:01:30 -04003233 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003234 * returns 0 if it found something or 1 if there are no lesser leaves.
3235 * returns < 0 on io errors.
3236 */
3237int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3238{
Chris Mason925baed2008-06-25 16:01:30 -04003239 struct btrfs_key key;
3240 struct btrfs_disk_key found_key;
3241 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003242
Chris Mason925baed2008-06-25 16:01:30 -04003243 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003244
Chris Mason925baed2008-06-25 16:01:30 -04003245 if (key.offset > 0)
3246 key.offset--;
3247 else if (key.type > 0)
3248 key.type--;
3249 else if (key.objectid > 0)
3250 key.objectid--;
3251 else
3252 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003253
Chris Mason925baed2008-06-25 16:01:30 -04003254 btrfs_release_path(root, path);
3255 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3256 if (ret < 0)
3257 return ret;
3258 btrfs_item_key(path->nodes[0], &found_key, 0);
3259 ret = comp_keys(&found_key, &key);
3260 if (ret < 0)
3261 return 0;
3262 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003263}
3264
Chris Mason3f157a22008-06-25 16:01:31 -04003265/*
3266 * A helper function to walk down the tree starting at min_key, and looking
3267 * for nodes or leaves that are either in cache or have a minimum
3268 * transaction id. This is used by the btree defrag code, but could
3269 * also be used to search for blocks that have changed since a given
3270 * transaction id.
3271 *
3272 * This does not cow, but it does stuff the starting key it finds back
3273 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3274 * key and get a writable path.
3275 *
3276 * This does lock as it descends, and path->keep_locks should be set
3277 * to 1 by the caller.
3278 *
3279 * This honors path->lowest_level to prevent descent past a given level
3280 * of the tree.
3281 *
3282 * returns zero if something useful was found, < 0 on error and 1 if there
3283 * was nothing in the tree that matched the search criteria.
3284 */
3285int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003286 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003287 struct btrfs_path *path, int cache_only,
3288 u64 min_trans)
3289{
3290 struct extent_buffer *cur;
3291 struct btrfs_key found_key;
3292 int slot;
Yan96524802008-07-24 12:19:49 -04003293 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003294 u32 nritems;
3295 int level;
3296 int ret = 1;
3297
3298again:
3299 cur = btrfs_lock_root_node(root);
3300 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003301 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003302 path->nodes[level] = cur;
3303 path->locks[level] = 1;
3304
3305 if (btrfs_header_generation(cur) < min_trans) {
3306 ret = 1;
3307 goto out;
3308 }
3309 while(1) {
3310 nritems = btrfs_header_nritems(cur);
3311 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003312 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003313
3314 /* at level = 0, we're done, setup the path and exit */
3315 if (level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003316 if (slot >= nritems)
3317 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003318 ret = 0;
3319 path->slots[level] = slot;
3320 btrfs_item_key_to_cpu(cur, &found_key, slot);
3321 goto out;
3322 }
Yan96524802008-07-24 12:19:49 -04003323 if (sret && slot > 0)
3324 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003325 /*
3326 * check this node pointer against the cache_only and
3327 * min_trans parameters. If it isn't in cache or is too
3328 * old, skip to the next one.
3329 */
3330 while(slot < nritems) {
3331 u64 blockptr;
3332 u64 gen;
3333 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003334 struct btrfs_disk_key disk_key;
3335
Chris Mason3f157a22008-06-25 16:01:31 -04003336 blockptr = btrfs_node_blockptr(cur, slot);
3337 gen = btrfs_node_ptr_generation(cur, slot);
3338 if (gen < min_trans) {
3339 slot++;
3340 continue;
3341 }
3342 if (!cache_only)
3343 break;
3344
Chris Masone02119d2008-09-05 16:13:11 -04003345 if (max_key) {
3346 btrfs_node_key(cur, &disk_key, slot);
3347 if (comp_keys(&disk_key, max_key) >= 0) {
3348 ret = 1;
3349 goto out;
3350 }
3351 }
3352
Chris Mason3f157a22008-06-25 16:01:31 -04003353 tmp = btrfs_find_tree_block(root, blockptr,
3354 btrfs_level_size(root, level - 1));
3355
3356 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3357 free_extent_buffer(tmp);
3358 break;
3359 }
3360 if (tmp)
3361 free_extent_buffer(tmp);
3362 slot++;
3363 }
Chris Masone02119d2008-09-05 16:13:11 -04003364find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003365 /*
3366 * we didn't find a candidate key in this node, walk forward
3367 * and find another one
3368 */
3369 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003370 path->slots[level] = slot;
3371 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003372 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003373 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003374 btrfs_release_path(root, path);
3375 goto again;
3376 } else {
3377 goto out;
3378 }
3379 }
3380 /* save our key for returning back */
3381 btrfs_node_key_to_cpu(cur, &found_key, slot);
3382 path->slots[level] = slot;
3383 if (level == path->lowest_level) {
3384 ret = 0;
3385 unlock_up(path, level, 1);
3386 goto out;
3387 }
3388 cur = read_node_slot(root, cur, slot);
3389
3390 btrfs_tree_lock(cur);
3391 path->locks[level - 1] = 1;
3392 path->nodes[level - 1] = cur;
3393 unlock_up(path, level, 1);
3394 }
3395out:
3396 if (ret == 0)
3397 memcpy(min_key, &found_key, sizeof(found_key));
3398 return ret;
3399}
3400
3401/*
3402 * this is similar to btrfs_next_leaf, but does not try to preserve
3403 * and fixup the path. It looks for and returns the next key in the
3404 * tree based on the current path and the cache_only and min_trans
3405 * parameters.
3406 *
3407 * 0 is returned if another key is found, < 0 if there are any errors
3408 * and 1 is returned if there are no higher keys in the tree
3409 *
3410 * path->keep_locks should be set to 1 on the search made before
3411 * calling this function.
3412 */
Chris Masone7a84562008-06-25 16:01:31 -04003413int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003414 struct btrfs_key *key, int lowest_level,
3415 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003416{
3417 int level = lowest_level;
3418 int slot;
3419 struct extent_buffer *c;
3420
3421 while(level < BTRFS_MAX_LEVEL) {
3422 if (!path->nodes[level])
3423 return 1;
3424
3425 slot = path->slots[level] + 1;
3426 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003427next:
Chris Masone7a84562008-06-25 16:01:31 -04003428 if (slot >= btrfs_header_nritems(c)) {
3429 level++;
3430 if (level == BTRFS_MAX_LEVEL) {
3431 return 1;
3432 }
3433 continue;
3434 }
3435 if (level == 0)
3436 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003437 else {
3438 u64 blockptr = btrfs_node_blockptr(c, slot);
3439 u64 gen = btrfs_node_ptr_generation(c, slot);
3440
3441 if (cache_only) {
3442 struct extent_buffer *cur;
3443 cur = btrfs_find_tree_block(root, blockptr,
3444 btrfs_level_size(root, level - 1));
3445 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3446 slot++;
3447 if (cur)
3448 free_extent_buffer(cur);
3449 goto next;
3450 }
3451 free_extent_buffer(cur);
3452 }
3453 if (gen < min_trans) {
3454 slot++;
3455 goto next;
3456 }
Chris Masone7a84562008-06-25 16:01:31 -04003457 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003458 }
Chris Masone7a84562008-06-25 16:01:31 -04003459 return 0;
3460 }
3461 return 1;
3462}
3463
Chris Mason7bb86312007-12-11 09:25:06 -05003464/*
Chris Mason925baed2008-06-25 16:01:30 -04003465 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003466 * returns 0 if it found something or 1 if there are no greater leaves.
3467 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003468 */
Chris Mason234b63a2007-03-13 10:46:10 -04003469int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003470{
3471 int slot;
3472 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003473 struct extent_buffer *c;
3474 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003475 struct btrfs_key key;
3476 u32 nritems;
3477 int ret;
3478
3479 nritems = btrfs_header_nritems(path->nodes[0]);
3480 if (nritems == 0) {
3481 return 1;
3482 }
3483
3484 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3485
Chris Mason925baed2008-06-25 16:01:30 -04003486 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003487 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003488 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3489 path->keep_locks = 0;
3490
3491 if (ret < 0)
3492 return ret;
3493
Chris Masona2135012008-06-25 16:01:30 -04003494 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003495 /*
3496 * by releasing the path above we dropped all our locks. A balance
3497 * could have added more items next to the key that used to be
3498 * at the very end of the block. So, check again here and
3499 * advance the path if there are now more items available.
3500 */
Chris Masona2135012008-06-25 16:01:30 -04003501 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003502 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003503 goto done;
3504 }
Chris Masond97e63b2007-02-20 16:40:44 -05003505
Chris Mason234b63a2007-03-13 10:46:10 -04003506 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003507 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003508 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003509
Chris Masond97e63b2007-02-20 16:40:44 -05003510 slot = path->slots[level] + 1;
3511 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003512 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003513 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003514 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003515 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003516 }
Chris Masond97e63b2007-02-20 16:40:44 -05003517 continue;
3518 }
Chris Mason5f39d392007-10-15 16:14:19 -04003519
Chris Mason925baed2008-06-25 16:01:30 -04003520 if (next) {
3521 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003522 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003523 }
Chris Mason5f39d392007-10-15 16:14:19 -04003524
Chris Mason0bd40a72008-07-17 12:54:43 -04003525 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3526 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003527 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003528
Chris Masonca7a79a2008-05-12 12:59:19 -04003529 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003530 if (!path->skip_locking) {
3531 WARN_ON(!btrfs_tree_locked(c));
3532 btrfs_tree_lock(next);
3533 }
Chris Masond97e63b2007-02-20 16:40:44 -05003534 break;
3535 }
3536 path->slots[level] = slot;
3537 while(1) {
3538 level--;
3539 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003540 if (path->locks[level])
3541 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003542 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003543 path->nodes[level] = next;
3544 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003545 if (!path->skip_locking)
3546 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003547 if (!level)
3548 break;
Chris Mason925baed2008-06-25 16:01:30 -04003549 if (level == 1 && path->locks[1] && path->reada)
3550 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003551 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003552 if (!path->skip_locking) {
3553 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3554 btrfs_tree_lock(next);
3555 }
Chris Masond97e63b2007-02-20 16:40:44 -05003556 }
Chris Mason925baed2008-06-25 16:01:30 -04003557done:
3558 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003559 return 0;
3560}
Chris Mason0b86a832008-03-24 15:01:56 -04003561
Chris Mason3f157a22008-06-25 16:01:31 -04003562/*
3563 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3564 * searching until it gets past min_objectid or finds an item of 'type'
3565 *
3566 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3567 */
Chris Mason0b86a832008-03-24 15:01:56 -04003568int btrfs_previous_item(struct btrfs_root *root,
3569 struct btrfs_path *path, u64 min_objectid,
3570 int type)
3571{
3572 struct btrfs_key found_key;
3573 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003574 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003575 int ret;
3576
3577 while(1) {
3578 if (path->slots[0] == 0) {
3579 ret = btrfs_prev_leaf(root, path);
3580 if (ret != 0)
3581 return ret;
3582 } else {
3583 path->slots[0]--;
3584 }
3585 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003586 nritems = btrfs_header_nritems(leaf);
3587 if (nritems == 0)
3588 return 1;
3589 if (path->slots[0] == nritems)
3590 path->slots[0]--;
3591
Chris Mason0b86a832008-03-24 15:01:56 -04003592 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3593 if (found_key.type == type)
3594 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003595 if (found_key.objectid < min_objectid)
3596 break;
3597 if (found_key.objectid == min_objectid &&
3598 found_key.type < type)
3599 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003600 }
3601 return 1;
3602}