blob: f82f8db02275912ec50dba3e18a31e698bed183a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
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
Chris Masond352ac62008-09-29 15:18:18 -040057/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040058void btrfs_free_path(struct btrfs_path *p)
59{
Chris Masondf24a2b2007-04-04 09:36:31 -040060 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040061 kmem_cache_free(btrfs_path_cachep, p);
62}
63
Chris Masond352ac62008-09-29 15:18:18 -040064/*
65 * path release drops references on the extent buffers in the path
66 * and it drops any locks held by this path
67 *
68 * It is safe to call this on paths that no locks or extent buffers held.
69 */
Chris Masone02119d2008-09-05 16:13:11 -040070void noinline btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050071{
72 int i;
Chris Masona2135012008-06-25 16:01:30 -040073
Chris Mason234b63a2007-03-13 10:46:10 -040074 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -040075 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050076 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040077 continue;
78 if (p->locks[i]) {
79 btrfs_tree_unlock(p->nodes[i]);
80 p->locks[i] = 0;
81 }
Chris Mason5f39d392007-10-15 16:14:19 -040082 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -040083 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -050084 }
85}
86
Chris Masond352ac62008-09-29 15:18:18 -040087/*
88 * safely gets a reference on the root node of a tree. A lock
89 * is not taken, so a concurrent writer may put a different node
90 * at the root of the tree. See btrfs_lock_root_node for the
91 * looping required.
92 *
93 * The extent buffer returned by this has a reference taken, so
94 * it won't disappear. It may stop being the root of the tree
95 * at any time because there are no locks held.
96 */
Chris Mason925baed2008-06-25 16:01:30 -040097struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
98{
99 struct extent_buffer *eb;
100 spin_lock(&root->node_lock);
101 eb = root->node;
102 extent_buffer_get(eb);
103 spin_unlock(&root->node_lock);
104 return eb;
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/* loop around taking references on and locking the root node of the
108 * tree until you end up with a lock on the root. A locked buffer
109 * is returned, with a reference held.
110 */
Chris Mason925baed2008-06-25 16:01:30 -0400111struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
112{
113 struct extent_buffer *eb;
114
115 while(1) {
116 eb = btrfs_root_node(root);
117 btrfs_tree_lock(eb);
118
119 spin_lock(&root->node_lock);
120 if (eb == root->node) {
121 spin_unlock(&root->node_lock);
122 break;
123 }
124 spin_unlock(&root->node_lock);
125
126 btrfs_tree_unlock(eb);
127 free_extent_buffer(eb);
128 }
129 return eb;
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/* cowonly root (everything not a reference counted cow subvolume), just get
133 * put onto a simple dirty list. transaction.c walks this to make sure they
134 * get properly updated on disk.
135 */
Chris Mason0b86a832008-03-24 15:01:56 -0400136static void add_root_to_dirty_list(struct btrfs_root *root)
137{
138 if (root->track_dirty && list_empty(&root->dirty_list)) {
139 list_add(&root->dirty_list,
140 &root->fs_info->dirty_cowonly_roots);
141 }
142}
143
Chris Masond352ac62008-09-29 15:18:18 -0400144/*
145 * used by snapshot creation to make a copy of a root for a tree with
146 * a given objectid. The buffer with the new root node is returned in
147 * cow_ret, and this func returns zero on success or a negative error code.
148 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500149int btrfs_copy_root(struct btrfs_trans_handle *trans,
150 struct btrfs_root *root,
151 struct extent_buffer *buf,
152 struct extent_buffer **cow_ret, u64 new_root_objectid)
153{
154 struct extent_buffer *cow;
155 u32 nritems;
156 int ret = 0;
157 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500158 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500159
Chris Mason4aec2b52007-12-18 16:25:45 -0500160 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
161 if (!new_root)
162 return -ENOMEM;
163
164 memcpy(new_root, root, sizeof(*new_root));
165 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500166
167 WARN_ON(root->ref_cows && trans->transid !=
168 root->fs_info->running_transaction->transid);
169 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
170
171 level = btrfs_header_level(buf);
172 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400173
174 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
175 new_root_objectid, trans->transid,
176 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500177 if (IS_ERR(cow)) {
178 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500179 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500180 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500181
182 copy_extent_buffer(cow, buf, 0, 0, cow->len);
183 btrfs_set_header_bytenr(cow, cow->start);
184 btrfs_set_header_generation(cow, trans->transid);
185 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500187
188 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400189 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500190 kfree(new_root);
191
Chris Masonbe20aa92007-12-17 20:14:01 -0500192 if (ret)
193 return ret;
194
195 btrfs_mark_buffer_dirty(cow);
196 *cow_ret = cow;
197 return 0;
198}
199
Chris Masond352ac62008-09-29 15:18:18 -0400200/*
201 * does the dirty work in cow of a single block. The parent block
202 * (if supplied) is updated to point to the new cow copy. The new
203 * buffer is marked dirty and returned locked. If you modify the block
204 * it needs to be marked dirty again.
205 *
206 * search_start -- an allocation hint for the new block
207 *
208 * empty_size -- a hint that you plan on doing more cow. This is the size in bytes
209 * the allocator should try to find free next to the block it returns. This is
210 * just a hint and may be ignored by the allocator.
211 *
212 * prealloc_dest -- if you have already reserved a destination for the cow,
213 * this uses that block instead of allocating a new one. btrfs_alloc_reserved_extent
214 * is used to finish the allocation.
215 */
Chris Masone02119d2008-09-05 16:13:11 -0400216int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400217 struct btrfs_root *root,
218 struct extent_buffer *buf,
219 struct extent_buffer *parent, int parent_slot,
220 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400221 u64 search_start, u64 empty_size,
222 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400223{
Zheng Yan31840ae2008-09-23 13:14:14 -0400224 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400225 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500226 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400227 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500228 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400229 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400230
Chris Mason925baed2008-06-25 16:01:30 -0400231 if (*cow_ret == buf)
232 unlock_orig = 1;
233
234 WARN_ON(!btrfs_tree_locked(buf));
235
Zheng Yan31840ae2008-09-23 13:14:14 -0400236 if (parent)
237 parent_start = parent->start;
238 else
239 parent_start = 0;
240
Chris Mason7bb86312007-12-11 09:25:06 -0500241 WARN_ON(root->ref_cows && trans->transid !=
242 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400243 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400244
Chris Mason7bb86312007-12-11 09:25:06 -0500245 level = btrfs_header_level(buf);
246 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400247
Chris Mason65b51a02008-08-01 15:11:20 -0400248 if (prealloc_dest) {
249 struct btrfs_key ins;
250
251 ins.objectid = prealloc_dest;
252 ins.offset = buf->len;
253 ins.type = BTRFS_EXTENT_ITEM_KEY;
254
Zheng Yan31840ae2008-09-23 13:14:14 -0400255 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400256 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400257 trans->transid, level, &ins);
Chris Mason65b51a02008-08-01 15:11:20 -0400258 BUG_ON(ret);
259 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
260 buf->len);
261 } else {
262 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400263 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400264 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400265 trans->transid, level,
266 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400267 }
Chris Mason6702ed42007-08-07 16:15:09 -0400268 if (IS_ERR(cow))
269 return PTR_ERR(cow);
270
Chris Mason5f39d392007-10-15 16:14:19 -0400271 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400272 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400273 btrfs_set_header_generation(cow, trans->transid);
274 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400275 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400276
Chris Mason5f39d392007-10-15 16:14:19 -0400277 WARN_ON(btrfs_header_generation(buf) > trans->transid);
278 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400279 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400280 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400281 if (ret)
282 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400283
284 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
285 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400286 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
287 /*
288 * There are only two places that can drop reference to
289 * tree blocks owned by living reloc trees, one is here,
Yan Zhengf82d02d2008-10-29 14:49:05 -0400290 * the other place is btrfs_drop_subtree. In both places,
Zheng Yan1a40e232008-09-26 10:09:34 -0400291 * we check reference count while tree block is locked.
292 * Furthermore, if reference count is one, it won't get
293 * increased by someone else.
294 */
295 u32 refs;
296 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
297 buf->len, &refs);
298 BUG_ON(ret);
299 if (refs == 1) {
300 ret = btrfs_update_ref(trans, root, buf, cow,
301 0, nritems);
302 clean_tree_block(trans, root, buf);
303 } else {
304 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
305 }
306 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400307 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400308 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
309 if (ret)
310 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400311 clean_tree_block(trans, root, buf);
312 }
313
Zheng Yan1a40e232008-09-26 10:09:34 -0400314 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Zheng Yan1a40e232008-09-26 10:09:34 -0400315 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
316 WARN_ON(ret);
317 }
318
Chris Mason6702ed42007-08-07 16:15:09 -0400319 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400320 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400321
322 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400323 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400324 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400325 spin_unlock(&root->node_lock);
326
Chris Mason6702ed42007-08-07 16:15:09 -0400327 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400328 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400329 buf->len, buf->start,
330 root->root_key.objectid,
331 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400332 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400333 }
Chris Mason5f39d392007-10-15 16:14:19 -0400334 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400335 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400336 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400337 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400338 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500339 WARN_ON(trans->transid == 0);
340 btrfs_set_node_ptr_generation(parent, parent_slot,
341 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400342 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400343 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500344 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400345 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400346 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400347 }
Chris Mason925baed2008-06-25 16:01:30 -0400348 if (unlock_orig)
349 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400350 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400351 btrfs_mark_buffer_dirty(cow);
352 *cow_ret = cow;
353 return 0;
354}
355
Chris Masond352ac62008-09-29 15:18:18 -0400356/*
357 * cows a single block, see __btrfs_cow_block for the real work.
358 * This version of it has extra checks so that a block isn't cow'd more than
359 * once per transaction, as long as it hasn't been written yet
360 */
Chris Masone02119d2008-09-05 16:13:11 -0400361int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400362 struct btrfs_root *root, struct extent_buffer *buf,
363 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400364 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500365{
Chris Mason6702ed42007-08-07 16:15:09 -0400366 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400367 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500368
Chris Masonccd467d2007-06-28 15:57:36 -0400369 if (trans->transaction != root->fs_info->running_transaction) {
370 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
371 root->fs_info->running_transaction->transid);
372 WARN_ON(1);
373 }
374 if (trans->transid != root->fs_info->generation) {
375 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
376 root->fs_info->generation);
377 WARN_ON(1);
378 }
Chris Masondc17ff82008-01-08 15:46:30 -0500379
Chris Mason63b10fc2008-04-01 11:21:32 -0400380 spin_lock(&root->fs_info->hash_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400381 if (btrfs_header_generation(buf) == trans->transid &&
382 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400383 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500384 *cow_ret = buf;
Chris Mason63b10fc2008-04-01 11:21:32 -0400385 spin_unlock(&root->fs_info->hash_lock);
Chris Mason65b51a02008-08-01 15:11:20 -0400386 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500387 return 0;
388 }
Chris Mason63b10fc2008-04-01 11:21:32 -0400389 spin_unlock(&root->fs_info->hash_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400390 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonf510cfe2007-10-15 16:14:48 -0400391 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400392 parent_slot, cow_ret, search_start, 0,
393 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400394 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400395}
396
Chris Masond352ac62008-09-29 15:18:18 -0400397/*
398 * helper function for defrag to decide if two blocks pointed to by a
399 * node are actually close by
400 */
Chris Mason6b800532007-10-15 16:17:34 -0400401static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400402{
Chris Mason6b800532007-10-15 16:17:34 -0400403 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400404 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400405 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400406 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500407 return 0;
408}
409
Chris Mason081e9572007-11-06 10:26:24 -0500410/*
411 * compare two keys in a memcmp fashion
412 */
413static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
414{
415 struct btrfs_key k1;
416
417 btrfs_disk_key_to_cpu(&k1, disk);
418
419 if (k1.objectid > k2->objectid)
420 return 1;
421 if (k1.objectid < k2->objectid)
422 return -1;
423 if (k1.type > k2->type)
424 return 1;
425 if (k1.type < k2->type)
426 return -1;
427 if (k1.offset > k2->offset)
428 return 1;
429 if (k1.offset < k2->offset)
430 return -1;
431 return 0;
432}
433
434
Chris Masond352ac62008-09-29 15:18:18 -0400435/*
436 * this is used by the defrag code to go through all the
437 * leaves pointed to by a node and reallocate them so that
438 * disk order is close to key order
439 */
Chris Mason6702ed42007-08-07 16:15:09 -0400440int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400441 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400442 int start_slot, int cache_only, u64 *last_ret,
443 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400444{
Chris Mason6b800532007-10-15 16:17:34 -0400445 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400446 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400447 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400448 u64 search_start = *last_ret;
449 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400450 u64 other;
451 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400452 int end_slot;
453 int i;
454 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400455 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400456 int uptodate;
457 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500458 int progress_passed = 0;
459 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400460
Chris Mason5708b952007-10-25 15:43:18 -0400461 parent_level = btrfs_header_level(parent);
462 if (cache_only && parent_level != 1)
463 return 0;
464
Chris Mason6702ed42007-08-07 16:15:09 -0400465 if (trans->transaction != root->fs_info->running_transaction) {
466 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
467 root->fs_info->running_transaction->transid);
468 WARN_ON(1);
469 }
470 if (trans->transid != root->fs_info->generation) {
471 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
472 root->fs_info->generation);
473 WARN_ON(1);
474 }
Chris Mason86479a02007-09-10 19:58:16 -0400475
Chris Mason6b800532007-10-15 16:17:34 -0400476 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400477 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400478 end_slot = parent_nritems;
479
480 if (parent_nritems == 1)
481 return 0;
482
483 for (i = start_slot; i < end_slot; i++) {
484 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400485
Chris Mason5708b952007-10-25 15:43:18 -0400486 if (!parent->map_token) {
487 map_extent_buffer(parent,
488 btrfs_node_key_ptr_offset(i),
489 sizeof(struct btrfs_key_ptr),
490 &parent->map_token, &parent->kaddr,
491 &parent->map_start, &parent->map_len,
492 KM_USER1);
493 }
Chris Mason081e9572007-11-06 10:26:24 -0500494 btrfs_node_key(parent, &disk_key, i);
495 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
496 continue;
497
498 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400499 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400500 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400501 if (last_block == 0)
502 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400503
Chris Mason6702ed42007-08-07 16:15:09 -0400504 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400505 other = btrfs_node_blockptr(parent, i - 1);
506 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400507 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400508 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400509 other = btrfs_node_blockptr(parent, i + 1);
510 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400511 }
Chris Masone9d0b132007-08-10 14:06:19 -0400512 if (close) {
513 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400514 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400515 }
Chris Mason5708b952007-10-25 15:43:18 -0400516 if (parent->map_token) {
517 unmap_extent_buffer(parent, parent->map_token,
518 KM_USER1);
519 parent->map_token = NULL;
520 }
Chris Mason6702ed42007-08-07 16:15:09 -0400521
Chris Mason6b800532007-10-15 16:17:34 -0400522 cur = btrfs_find_tree_block(root, blocknr, blocksize);
523 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400524 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400525 else
526 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400527 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400528 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400529 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400530 continue;
531 }
Chris Mason6b800532007-10-15 16:17:34 -0400532 if (!cur) {
533 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400534 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400535 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400536 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400537 }
Chris Mason6702ed42007-08-07 16:15:09 -0400538 }
Chris Masone9d0b132007-08-10 14:06:19 -0400539 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400540 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400541
Chris Masone7a84562008-06-25 16:01:31 -0400542 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400543 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400544 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400545 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400546 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400547 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400548 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400549 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400550 break;
Yan252c38f2007-08-29 09:11:44 -0400551 }
Chris Masone7a84562008-06-25 16:01:31 -0400552 search_start = cur->start;
553 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400554 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400555 btrfs_tree_unlock(cur);
556 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400557 }
Chris Mason5708b952007-10-25 15:43:18 -0400558 if (parent->map_token) {
559 unmap_extent_buffer(parent, parent->map_token,
560 KM_USER1);
561 parent->map_token = NULL;
562 }
Chris Mason6702ed42007-08-07 16:15:09 -0400563 return err;
564}
565
Chris Mason74123bd2007-02-02 11:05:29 -0500566/*
567 * The leaf data grows from end-to-front in the node.
568 * this returns the address of the start of the last item,
569 * which is the stop of the leaf data stack
570 */
Chris Mason123abc82007-03-14 14:14:43 -0400571static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400572 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500573{
Chris Mason5f39d392007-10-15 16:14:19 -0400574 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500575 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400576 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400577 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500578}
579
Chris Masond352ac62008-09-29 15:18:18 -0400580/*
581 * extra debugging checks to make sure all the items in a key are
582 * well formed and in the proper order
583 */
Chris Mason123abc82007-03-14 14:14:43 -0400584static int check_node(struct btrfs_root *root, struct btrfs_path *path,
585 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500586{
Chris Mason5f39d392007-10-15 16:14:19 -0400587 struct extent_buffer *parent = NULL;
588 struct extent_buffer *node = path->nodes[level];
589 struct btrfs_disk_key parent_key;
590 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500591 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400592 int slot;
593 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400594 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500595
596 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400597 parent = path->nodes[level + 1];
Aneesha1f396302007-07-11 10:03:27 -0400598
Chris Mason8d7be552007-05-10 11:24:42 -0400599 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400600 BUG_ON(nritems == 0);
601 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400602 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400603 btrfs_node_key(parent, &parent_key, parent_slot);
604 btrfs_node_key(node, &node_key, 0);
605 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400606 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400607 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400608 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500609 }
Chris Mason123abc82007-03-14 14:14:43 -0400610 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400611 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400612 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
613 btrfs_node_key(node, &node_key, slot);
614 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400615 }
616 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400617 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
618 btrfs_node_key(node, &node_key, slot);
619 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500620 }
621 return 0;
622}
623
Chris Masond352ac62008-09-29 15:18:18 -0400624/*
625 * extra checking to make sure all the items in a leaf are
626 * well formed and in the proper order
627 */
Chris Mason123abc82007-03-14 14:14:43 -0400628static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
629 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500630{
Chris Mason5f39d392007-10-15 16:14:19 -0400631 struct extent_buffer *leaf = path->nodes[level];
632 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500633 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400634 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400635 struct btrfs_disk_key parent_key;
636 struct btrfs_disk_key leaf_key;
637 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400638
Chris Mason5f39d392007-10-15 16:14:19 -0400639 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500640
641 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400642 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400643
644 if (nritems == 0)
645 return 0;
646
647 if (parent) {
Aneesha1f396302007-07-11 10:03:27 -0400648 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400649 btrfs_node_key(parent, &parent_key, parent_slot);
650 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400651
Chris Mason5f39d392007-10-15 16:14:19 -0400652 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400653 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400654 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400655 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500656 }
Chris Mason5f39d392007-10-15 16:14:19 -0400657#if 0
658 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
659 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
660 btrfs_item_key(leaf, &leaf_key, i);
661 if (comp_keys(&leaf_key, &cpukey) >= 0) {
662 btrfs_print_leaf(root, leaf);
663 printk("slot %d offset bad key\n", i);
664 BUG_ON(1);
665 }
666 if (btrfs_item_offset_nr(leaf, i) !=
667 btrfs_item_end_nr(leaf, i + 1)) {
668 btrfs_print_leaf(root, leaf);
669 printk("slot %d offset bad\n", i);
670 BUG_ON(1);
671 }
672 if (i == 0) {
673 if (btrfs_item_offset_nr(leaf, i) +
674 btrfs_item_size_nr(leaf, i) !=
675 BTRFS_LEAF_DATA_SIZE(root)) {
676 btrfs_print_leaf(root, leaf);
677 printk("slot %d first offset bad\n", i);
678 BUG_ON(1);
679 }
680 }
681 }
682 if (nritems > 0) {
683 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
684 btrfs_print_leaf(root, leaf);
685 printk("slot %d bad size \n", nritems - 1);
686 BUG_ON(1);
687 }
688 }
689#endif
690 if (slot != 0 && slot < nritems - 1) {
691 btrfs_item_key(leaf, &leaf_key, slot);
692 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
693 if (comp_keys(&leaf_key, &cpukey) <= 0) {
694 btrfs_print_leaf(root, leaf);
695 printk("slot %d offset bad key\n", slot);
696 BUG_ON(1);
697 }
698 if (btrfs_item_offset_nr(leaf, slot - 1) !=
699 btrfs_item_end_nr(leaf, slot)) {
700 btrfs_print_leaf(root, leaf);
701 printk("slot %d offset bad\n", slot);
702 BUG_ON(1);
703 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500704 }
Chris Mason8d7be552007-05-10 11:24:42 -0400705 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400706 btrfs_item_key(leaf, &leaf_key, slot);
707 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
708 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
709 if (btrfs_item_offset_nr(leaf, slot) !=
710 btrfs_item_end_nr(leaf, slot + 1)) {
711 btrfs_print_leaf(root, leaf);
712 printk("slot %d offset bad\n", slot);
713 BUG_ON(1);
714 }
Chris Mason8d7be552007-05-10 11:24:42 -0400715 }
Chris Mason5f39d392007-10-15 16:14:19 -0400716 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
717 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500718 return 0;
719}
720
Chris Mason98ed5172008-01-03 10:01:48 -0500721static int noinline check_block(struct btrfs_root *root,
722 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500723{
Chris Masonf1885912008-04-09 16:28:12 -0400724 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400725 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400726 if (btrfs_header_level(path->nodes[level]) != level)
727 printk("warning: bad level %Lu wanted %d found %d\n",
728 path->nodes[level]->start, level,
729 btrfs_header_level(path->nodes[level]));
730 found_start = btrfs_header_bytenr(path->nodes[level]);
731 if (found_start != path->nodes[level]->start) {
732 printk("warning: bad bytentr %Lu found %Lu\n",
733 path->nodes[level]->start, found_start);
734 }
Chris Masondb945352007-10-15 16:15:53 -0400735#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400736 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400737
Chris Mason479965d2007-10-15 16:14:27 -0400738 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
739 (unsigned long)btrfs_header_fsid(buf),
740 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400741 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400742 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400743 }
Chris Masondb945352007-10-15 16:15:53 -0400744#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500745 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400746 return check_leaf(root, path, level);
747 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500748}
749
Chris Mason74123bd2007-02-02 11:05:29 -0500750/*
Chris Mason5f39d392007-10-15 16:14:19 -0400751 * search for key in the extent_buffer. The items start at offset p,
752 * and they are item_size apart. There are 'max' items in p.
753 *
Chris Mason74123bd2007-02-02 11:05:29 -0500754 * the slot in the array is returned via slot, and it points to
755 * the place where you would insert key if it is not found in
756 * the array.
757 *
758 * slot may point to max if the key is bigger than all of the keys
759 */
Chris Masone02119d2008-09-05 16:13:11 -0400760static noinline int generic_bin_search(struct extent_buffer *eb,
761 unsigned long p,
762 int item_size, struct btrfs_key *key,
763 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500764{
765 int low = 0;
766 int high = max;
767 int mid;
768 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400769 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400770 struct btrfs_disk_key unaligned;
771 unsigned long offset;
772 char *map_token = NULL;
773 char *kaddr = NULL;
774 unsigned long map_start = 0;
775 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400776 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500777
778 while(low < high) {
779 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400780 offset = p + mid * item_size;
781
782 if (!map_token || offset < map_start ||
783 (offset + sizeof(struct btrfs_disk_key)) >
784 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400785 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400786 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400787 map_token = NULL;
788 }
789 err = map_extent_buffer(eb, offset,
790 sizeof(struct btrfs_disk_key),
791 &map_token, &kaddr,
792 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400793
Chris Mason479965d2007-10-15 16:14:27 -0400794 if (!err) {
795 tmp = (struct btrfs_disk_key *)(kaddr + offset -
796 map_start);
797 } else {
798 read_extent_buffer(eb, &unaligned,
799 offset, sizeof(unaligned));
800 tmp = &unaligned;
801 }
802
Chris Mason5f39d392007-10-15 16:14:19 -0400803 } else {
804 tmp = (struct btrfs_disk_key *)(kaddr + offset -
805 map_start);
806 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500807 ret = comp_keys(tmp, key);
808
809 if (ret < 0)
810 low = mid + 1;
811 else if (ret > 0)
812 high = mid;
813 else {
814 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400815 if (map_token)
816 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500817 return 0;
818 }
819 }
820 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400821 if (map_token)
822 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500823 return 1;
824}
825
Chris Mason97571fd2007-02-24 13:39:08 -0500826/*
827 * simple bin_search frontend that does the right thing for
828 * leaves vs nodes
829 */
Chris Mason5f39d392007-10-15 16:14:19 -0400830static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
831 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500832{
Chris Mason5f39d392007-10-15 16:14:19 -0400833 if (level == 0) {
834 return generic_bin_search(eb,
835 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400836 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400837 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400838 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500839 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400840 return generic_bin_search(eb,
841 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400842 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400843 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400844 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500845 }
846 return -1;
847}
848
Chris Masond352ac62008-09-29 15:18:18 -0400849/* given a node and slot number, this reads the blocks it points to. The
850 * extent buffer is returned with a reference taken (but unlocked).
851 * NULL is returned on error.
852 */
Chris Masone02119d2008-09-05 16:13:11 -0400853static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400854 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500855{
Chris Masonca7a79a2008-05-12 12:59:19 -0400856 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500857 if (slot < 0)
858 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400859 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500860 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400861
862 BUG_ON(level == 0);
863
Chris Masondb945352007-10-15 16:15:53 -0400864 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400865 btrfs_level_size(root, level - 1),
866 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500867}
868
Chris Masond352ac62008-09-29 15:18:18 -0400869/*
870 * node level balancing, used to make sure nodes are in proper order for
871 * item deletion. We balance from the top down, so we have to make sure
872 * that a deletion won't leave an node completely empty later on.
873 */
Chris Masone02119d2008-09-05 16:13:11 -0400874static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500875 struct btrfs_root *root,
876 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500877{
Chris Mason5f39d392007-10-15 16:14:19 -0400878 struct extent_buffer *right = NULL;
879 struct extent_buffer *mid;
880 struct extent_buffer *left = NULL;
881 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500882 int ret = 0;
883 int wret;
884 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500885 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400886 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500887 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500888
889 if (level == 0)
890 return 0;
891
Chris Mason5f39d392007-10-15 16:14:19 -0400892 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400893 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500894 WARN_ON(btrfs_header_generation(mid) != trans->transid);
895
Chris Mason1d4f8a02007-03-13 09:28:32 -0400896 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500897
Chris Mason234b63a2007-03-13 10:46:10 -0400898 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400899 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500900 pslot = path->slots[level + 1];
901
Chris Mason40689472007-03-17 14:29:23 -0400902 /*
903 * deal with the case where there is only one pointer in the root
904 * by promoting the node below to a root
905 */
Chris Mason5f39d392007-10-15 16:14:19 -0400906 if (!parent) {
907 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500908
Chris Mason5f39d392007-10-15 16:14:19 -0400909 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500910 return 0;
911
912 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400913 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400914 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500915 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400916 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500917 BUG_ON(ret);
918
Chris Mason925baed2008-06-25 16:01:30 -0400919 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500920 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400921 spin_unlock(&root->node_lock);
922
Zheng Yan31840ae2008-09-23 13:14:14 -0400923 ret = btrfs_update_extent_ref(trans, root, child->start,
924 mid->start, child->start,
925 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400926 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400927 BUG_ON(ret);
928
Chris Mason0b86a832008-03-24 15:01:56 -0400929 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400930 btrfs_tree_unlock(child);
931 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500932 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400933 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400934 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500935 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400936 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500937 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400938 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400939 btrfs_header_generation(mid),
940 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500941 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400942 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400943 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500944 }
Chris Mason5f39d392007-10-15 16:14:19 -0400945 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400946 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500947 return 0;
948
Chris Mason5f39d392007-10-15 16:14:19 -0400949 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400950 err_on_enospc = 1;
951
Chris Mason5f39d392007-10-15 16:14:19 -0400952 left = read_node_slot(root, parent, pslot - 1);
953 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400954 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400955 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400956 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400957 if (wret) {
958 ret = wret;
959 goto enospc;
960 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400961 }
Chris Mason5f39d392007-10-15 16:14:19 -0400962 right = read_node_slot(root, parent, pslot + 1);
963 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400964 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400965 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400966 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400967 if (wret) {
968 ret = wret;
969 goto enospc;
970 }
971 }
972
973 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400974 if (left) {
975 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400976 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500977 if (wret < 0)
978 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400979 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400980 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500981 }
Chris Mason79f95c82007-03-01 15:16:26 -0500982
983 /*
984 * then try to empty the right most buffer into the middle
985 */
Chris Mason5f39d392007-10-15 16:14:19 -0400986 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400987 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400988 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500989 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400990 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400991 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500992 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400993 u32 blocksize = right->len;
994
Chris Mason5f39d392007-10-15 16:14:19 -0400995 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400996 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400997 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -0500998 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -0400999 wret = del_ptr(trans, root, path, level + 1, pslot +
1000 1);
Chris Masonbb803952007-03-01 12:04:21 -05001001 if (wret)
1002 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001003 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001004 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001005 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001006 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001007 if (wret)
1008 ret = wret;
1009 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001010 struct btrfs_disk_key right_key;
1011 btrfs_node_key(right, &right_key, 0);
1012 btrfs_set_node_key(parent, &right_key, pslot + 1);
1013 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001014 }
1015 }
Chris Mason5f39d392007-10-15 16:14:19 -04001016 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001017 /*
1018 * we're not allowed to leave a node with one item in the
1019 * tree during a delete. A deletion from lower in the tree
1020 * could try to delete the only pointer in this node.
1021 * So, pull some keys from the left.
1022 * There has to be a left pointer at this point because
1023 * otherwise we would have pulled some pointers from the
1024 * right
1025 */
Chris Mason5f39d392007-10-15 16:14:19 -04001026 BUG_ON(!left);
1027 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001028 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001029 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001030 goto enospc;
1031 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001032 if (wret == 1) {
1033 wret = push_node_left(trans, root, left, mid, 1);
1034 if (wret < 0)
1035 ret = wret;
1036 }
Chris Mason79f95c82007-03-01 15:16:26 -05001037 BUG_ON(wret == 1);
1038 }
Chris Mason5f39d392007-10-15 16:14:19 -04001039 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001040 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001041 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001042 u64 bytenr = mid->start;
1043 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001044
Chris Mason5f39d392007-10-15 16:14:19 -04001045 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001046 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001047 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001048 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001049 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001050 if (wret)
1051 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001052 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001053 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001054 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001055 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001056 if (wret)
1057 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001058 } else {
1059 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001060 struct btrfs_disk_key mid_key;
1061 btrfs_node_key(mid, &mid_key, 0);
1062 btrfs_set_node_key(parent, &mid_key, pslot);
1063 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001064 }
Chris Masonbb803952007-03-01 12:04:21 -05001065
Chris Mason79f95c82007-03-01 15:16:26 -05001066 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001067 if (left) {
1068 if (btrfs_header_nritems(left) > orig_slot) {
1069 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001070 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001071 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001072 path->slots[level + 1] -= 1;
1073 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001074 if (mid) {
1075 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001076 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001077 }
Chris Masonbb803952007-03-01 12:04:21 -05001078 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001079 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001080 path->slots[level] = orig_slot;
1081 }
1082 }
Chris Mason79f95c82007-03-01 15:16:26 -05001083 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001084 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001085 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001086 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001087 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001088enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001089 if (right) {
1090 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001091 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001092 }
1093 if (left) {
1094 if (path->nodes[level] != left)
1095 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001096 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001097 }
Chris Masonbb803952007-03-01 12:04:21 -05001098 return ret;
1099}
1100
Chris Masond352ac62008-09-29 15:18:18 -04001101/* Node balancing for insertion. Here we only split or push nodes around
1102 * when they are completely full. This is also done top down, so we
1103 * have to be pessimistic.
1104 */
Chris Mason98ed5172008-01-03 10:01:48 -05001105static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1106 struct btrfs_root *root,
1107 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001108{
Chris Mason5f39d392007-10-15 16:14:19 -04001109 struct extent_buffer *right = NULL;
1110 struct extent_buffer *mid;
1111 struct extent_buffer *left = NULL;
1112 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001113 int ret = 0;
1114 int wret;
1115 int pslot;
1116 int orig_slot = path->slots[level];
1117 u64 orig_ptr;
1118
1119 if (level == 0)
1120 return 1;
1121
Chris Mason5f39d392007-10-15 16:14:19 -04001122 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001123 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001124 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1125
1126 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001127 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001128 pslot = path->slots[level + 1];
1129
Chris Mason5f39d392007-10-15 16:14:19 -04001130 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001131 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001132
Chris Mason5f39d392007-10-15 16:14:19 -04001133 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001134
1135 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001136 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001137 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001138
1139 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001140 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001141 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1142 wret = 1;
1143 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001144 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001145 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001146 if (ret)
1147 wret = 1;
1148 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001149 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001150 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001151 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001152 }
Chris Masone66f7092007-04-20 13:16:02 -04001153 if (wret < 0)
1154 ret = wret;
1155 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001156 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001157 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001158 btrfs_node_key(mid, &disk_key, 0);
1159 btrfs_set_node_key(parent, &disk_key, pslot);
1160 btrfs_mark_buffer_dirty(parent);
1161 if (btrfs_header_nritems(left) > orig_slot) {
1162 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001163 path->slots[level + 1] -= 1;
1164 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001165 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001166 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001167 } else {
1168 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001169 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001170 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001171 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001172 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001173 }
Chris Masone66f7092007-04-20 13:16:02 -04001174 return 0;
1175 }
Chris Mason925baed2008-06-25 16:01:30 -04001176 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001177 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001178 }
Chris Mason925baed2008-06-25 16:01:30 -04001179 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001180
1181 /*
1182 * then try to empty the right most buffer into the middle
1183 */
Chris Mason5f39d392007-10-15 16:14:19 -04001184 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001185 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001186 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001187 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001188 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1189 wret = 1;
1190 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001191 ret = btrfs_cow_block(trans, root, right,
1192 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001193 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001194 if (ret)
1195 wret = 1;
1196 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001197 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001198 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001199 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001200 }
Chris Masone66f7092007-04-20 13:16:02 -04001201 if (wret < 0)
1202 ret = wret;
1203 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001204 struct btrfs_disk_key disk_key;
1205
1206 btrfs_node_key(right, &disk_key, 0);
1207 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1208 btrfs_mark_buffer_dirty(parent);
1209
1210 if (btrfs_header_nritems(mid) <= orig_slot) {
1211 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001212 path->slots[level + 1] += 1;
1213 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001214 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001215 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001216 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001217 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001218 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001219 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001220 }
Chris Masone66f7092007-04-20 13:16:02 -04001221 return 0;
1222 }
Chris Mason925baed2008-06-25 16:01:30 -04001223 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001224 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001225 }
Chris Masone66f7092007-04-20 13:16:02 -04001226 return 1;
1227}
1228
Chris Mason74123bd2007-02-02 11:05:29 -05001229/*
Chris Masond352ac62008-09-29 15:18:18 -04001230 * readahead one full node of leaves, finding things that are close
1231 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001232 */
Chris Masone02119d2008-09-05 16:13:11 -04001233static noinline void reada_for_search(struct btrfs_root *root,
1234 struct btrfs_path *path,
1235 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001236{
Chris Mason5f39d392007-10-15 16:14:19 -04001237 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001238 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001239 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001240 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001241 u64 lowest_read;
1242 u64 highest_read;
1243 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001244 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001245 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001246 u32 nr;
1247 u32 blocksize;
1248 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001249
Chris Masona6b6e752007-10-15 16:22:39 -04001250 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001251 return;
1252
Chris Mason6702ed42007-08-07 16:15:09 -04001253 if (!path->nodes[level])
1254 return;
1255
Chris Mason5f39d392007-10-15 16:14:19 -04001256 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001257
Chris Mason3c69fae2007-08-07 15:52:22 -04001258 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001259 blocksize = btrfs_level_size(root, level - 1);
1260 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001261 if (eb) {
1262 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001263 return;
1264 }
1265
Chris Mason6b800532007-10-15 16:17:34 -04001266 highest_read = search;
1267 lowest_read = search;
1268
Chris Mason5f39d392007-10-15 16:14:19 -04001269 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001270 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001271 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001272 if (direction < 0) {
1273 if (nr == 0)
1274 break;
1275 nr--;
1276 } else if (direction > 0) {
1277 nr++;
1278 if (nr >= nritems)
1279 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001280 }
Chris Mason01f46652007-12-21 16:24:26 -05001281 if (path->reada < 0 && objectid) {
1282 btrfs_node_key(node, &disk_key, nr);
1283 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1284 break;
1285 }
Chris Mason6b800532007-10-15 16:17:34 -04001286 search = btrfs_node_blockptr(node, nr);
1287 if ((search >= lowest_read && search <= highest_read) ||
1288 (search < lowest_read && lowest_read - search <= 32768) ||
1289 (search > highest_read && search - highest_read <= 32768)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001290 readahead_tree_block(root, search, blocksize,
1291 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001292 nread += blocksize;
1293 }
1294 nscan++;
1295 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1296 break;
1297 if(nread > (1024 * 1024) || nscan > 128)
1298 break;
1299
1300 if (search < lowest_read)
1301 lowest_read = search;
1302 if (search > highest_read)
1303 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001304 }
1305}
Chris Mason925baed2008-06-25 16:01:30 -04001306
Chris Masond352ac62008-09-29 15:18:18 -04001307/*
1308 * when we walk down the tree, it is usually safe to unlock the higher layers in
1309 * the tree. The exceptions are when our path goes through slot 0, because operations
1310 * on the tree might require changing key pointers higher up in the tree.
1311 *
1312 * callers might also have set path->keep_locks, which tells this code to
1313 * keep the lock if the path points to the last slot in the block. This is
1314 * part of walking through the tree, and selecting the next slot in the higher
1315 * block.
1316 *
1317 * lowest_unlock sets the lowest level in the tree we're allowed to unlock.
1318 * so if lowest_unlock is 1, level 0 won't be unlocked
1319 */
Chris Masone02119d2008-09-05 16:13:11 -04001320static noinline void unlock_up(struct btrfs_path *path, int level,
1321 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001322{
1323 int i;
1324 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001325 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001326 struct extent_buffer *t;
1327
1328 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1329 if (!path->nodes[i])
1330 break;
1331 if (!path->locks[i])
1332 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001333 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001334 skip_level = i + 1;
1335 continue;
1336 }
Chris Mason051e1b92008-06-25 16:01:30 -04001337 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001338 u32 nritems;
1339 t = path->nodes[i];
1340 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001341 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001342 skip_level = i + 1;
1343 continue;
1344 }
1345 }
Chris Mason051e1b92008-06-25 16:01:30 -04001346 if (skip_level < i && i >= lowest_unlock)
1347 no_skips = 1;
1348
Chris Mason925baed2008-06-25 16:01:30 -04001349 t = path->nodes[i];
1350 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1351 btrfs_tree_unlock(t);
1352 path->locks[i] = 0;
1353 }
1354 }
1355}
1356
Chris Mason3c69fae2007-08-07 15:52:22 -04001357/*
Chris Mason74123bd2007-02-02 11:05:29 -05001358 * look for key in the tree. path is filled in with nodes along the way
1359 * if key is found, we return zero and you can find the item in the leaf
1360 * level of the path (level 0)
1361 *
1362 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001363 * be inserted, and 1 is returned. If there are other errors during the
1364 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001365 *
1366 * if ins_len > 0, nodes and leaves will be split as we walk down the
1367 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1368 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001369 */
Chris Masone089f052007-03-16 16:20:31 -04001370int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1371 *root, struct btrfs_key *key, struct btrfs_path *p, int
1372 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001373{
Chris Mason5f39d392007-10-15 16:14:19 -04001374 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001375 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001376 int slot;
1377 int ret;
1378 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001379 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001380 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001381 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001382 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001383 u64 blocknr;
1384 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001385 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001386
Chris Mason6702ed42007-08-07 16:15:09 -04001387 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001388 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001389 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001390
Chris Mason925baed2008-06-25 16:01:30 -04001391 if (ins_len < 0)
1392 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001393
1394 prealloc_block.objectid = 0;
1395
Chris Masonbb803952007-03-01 12:04:21 -05001396again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001397 if (p->skip_locking)
1398 b = btrfs_root_node(root);
1399 else
1400 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001401
Chris Masoneb60cea2007-02-02 09:18:22 -05001402 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001403 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001404
1405 /*
1406 * setup the path here so we can release it under lock
1407 * contention with the cow code
1408 */
1409 p->nodes[level] = b;
1410 if (!p->skip_locking)
1411 p->locks[level] = 1;
1412
Chris Mason02217ed2007-03-02 16:08:05 -05001413 if (cow) {
1414 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001415
1416 /* is a cow on this block not required */
1417 spin_lock(&root->fs_info->hash_lock);
1418 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001419 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001420 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1421 spin_unlock(&root->fs_info->hash_lock);
1422 goto cow_done;
1423 }
1424 spin_unlock(&root->fs_info->hash_lock);
1425
1426 /* ok, we have to cow, is our old prealloc the right
1427 * size?
1428 */
1429 if (prealloc_block.objectid &&
1430 prealloc_block.offset != b->len) {
1431 btrfs_free_reserved_extent(root,
1432 prealloc_block.objectid,
1433 prealloc_block.offset);
1434 prealloc_block.objectid = 0;
1435 }
1436
1437 /*
1438 * for higher level blocks, try not to allocate blocks
1439 * with the block and the parent locks held.
1440 */
1441 if (level > 1 && !prealloc_block.objectid &&
1442 btrfs_path_lock_waiting(p, level)) {
1443 u32 size = b->len;
1444 u64 hint = b->start;
1445
1446 btrfs_release_path(root, p);
1447 ret = btrfs_reserve_extent(trans, root,
1448 size, size, 0,
1449 hint, (u64)-1,
1450 &prealloc_block, 0);
1451 BUG_ON(ret);
1452 goto again;
1453 }
1454
Chris Masone20d96d2007-03-22 12:13:20 -04001455 wret = btrfs_cow_block(trans, root, b,
1456 p->nodes[level + 1],
1457 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001458 &b, prealloc_block.objectid);
1459 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001460 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001461 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001462 ret = wret;
1463 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001464 }
Chris Mason02217ed2007-03-02 16:08:05 -05001465 }
Chris Mason65b51a02008-08-01 15:11:20 -04001466cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001467 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001468 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001469 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001470 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001471
Chris Masoneb60cea2007-02-02 09:18:22 -05001472 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001473 if (!p->skip_locking)
1474 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001475
Chris Mason123abc82007-03-14 14:14:43 -04001476 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001477 if (ret) {
1478 ret = -1;
1479 goto done;
1480 }
Chris Mason925baed2008-06-25 16:01:30 -04001481
Chris Mason5f39d392007-10-15 16:14:19 -04001482 ret = bin_search(b, key, level, &slot);
1483 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001484 if (ret && slot > 0)
1485 slot -= 1;
1486 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001487 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001488 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001489 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001490 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001491 if (sret) {
1492 ret = sret;
1493 goto done;
1494 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001495 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001496 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001497 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001498 int sret = balance_level(trans, root, p,
1499 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001500 if (sret) {
1501 ret = sret;
1502 goto done;
1503 }
Chris Masonbb803952007-03-01 12:04:21 -05001504 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001505 if (!b) {
1506 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001507 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001508 }
Chris Masonbb803952007-03-01 12:04:21 -05001509 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001510 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001511 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001512 unlock_up(p, level, lowest_unlock);
1513
Chris Mason9f3a7422007-08-07 15:52:19 -04001514 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001515 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001516 ret = 0;
1517 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001518 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001519
Chris Mason594a24e2008-06-25 16:01:30 -04001520 blocknr = btrfs_node_blockptr(b, slot);
1521 gen = btrfs_node_ptr_generation(b, slot);
1522 blocksize = btrfs_level_size(root, level - 1);
1523
1524 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1525 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001526 b = tmp;
1527 } else {
1528 /*
1529 * reduce lock contention at high levels
1530 * of the btree by dropping locks before
1531 * we read.
1532 */
1533 if (level > 1) {
1534 btrfs_release_path(NULL, p);
1535 if (tmp)
1536 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001537 if (should_reada)
1538 reada_for_search(root, p,
1539 level, slot,
1540 key->objectid);
1541
Chris Mason594a24e2008-06-25 16:01:30 -04001542 tmp = read_tree_block(root, blocknr,
1543 blocksize, gen);
1544 if (tmp)
1545 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001546 goto again;
1547 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001548 if (tmp)
1549 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001550 if (should_reada)
1551 reada_for_search(root, p,
1552 level, slot,
1553 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001554 b = read_node_slot(root, b, slot);
1555 }
1556 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001557 if (!p->skip_locking)
1558 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001559 } else {
1560 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001561 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001562 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001563 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001564 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001565 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001566 if (sret) {
1567 ret = sret;
1568 goto done;
1569 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001570 }
Chris Mason925baed2008-06-25 16:01:30 -04001571 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001572 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001573 }
1574 }
Chris Mason65b51a02008-08-01 15:11:20 -04001575 ret = 1;
1576done:
1577 if (prealloc_block.objectid) {
1578 btrfs_free_reserved_extent(root,
1579 prealloc_block.objectid,
1580 prealloc_block.offset);
1581 }
1582
1583 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001584}
1585
Zheng Yan1a40e232008-09-26 10:09:34 -04001586int btrfs_merge_path(struct btrfs_trans_handle *trans,
1587 struct btrfs_root *root,
1588 struct btrfs_key *node_keys,
1589 u64 *nodes, int lowest_level)
1590{
1591 struct extent_buffer *eb;
1592 struct extent_buffer *parent;
1593 struct btrfs_key key;
1594 u64 bytenr;
1595 u64 generation;
1596 u32 blocksize;
1597 int level;
1598 int slot;
1599 int key_match;
1600 int ret;
1601
1602 eb = btrfs_lock_root_node(root);
1603 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1604 BUG_ON(ret);
1605
1606 parent = eb;
1607 while (1) {
1608 level = btrfs_header_level(parent);
1609 if (level == 0 || level <= lowest_level)
1610 break;
1611
1612 ret = bin_search(parent, &node_keys[lowest_level], level,
1613 &slot);
1614 if (ret && slot > 0)
1615 slot--;
1616
1617 bytenr = btrfs_node_blockptr(parent, slot);
1618 if (nodes[level - 1] == bytenr)
1619 break;
1620
1621 blocksize = btrfs_level_size(root, level - 1);
1622 generation = btrfs_node_ptr_generation(parent, slot);
1623 btrfs_node_key_to_cpu(eb, &key, slot);
1624 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1625
Yan Zhengf82d02d2008-10-29 14:49:05 -04001626 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001627 eb = read_tree_block(root, bytenr, blocksize,
1628 generation);
1629 btrfs_tree_lock(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001630 }
1631
1632 /*
1633 * if node keys match and node pointer hasn't been modified
1634 * in the running transaction, we can merge the path. for
1635 * blocks owened by reloc trees, the node pointer check is
1636 * skipped, this is because these blocks are fully controlled
1637 * by the space balance code, no one else can modify them.
1638 */
1639 if (!nodes[level - 1] || !key_match ||
1640 (generation == trans->transid &&
1641 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1642 if (level == 1 || level == lowest_level + 1) {
1643 if (generation == trans->transid) {
1644 btrfs_tree_unlock(eb);
1645 free_extent_buffer(eb);
1646 }
1647 break;
1648 }
1649
1650 if (generation != trans->transid) {
1651 eb = read_tree_block(root, bytenr, blocksize,
1652 generation);
1653 btrfs_tree_lock(eb);
1654 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001655
1656 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1657 &eb, 0);
1658 BUG_ON(ret);
1659
Yan Zhengf82d02d2008-10-29 14:49:05 -04001660 if (root->root_key.objectid ==
1661 BTRFS_TREE_RELOC_OBJECTID) {
1662 if (!nodes[level - 1]) {
1663 nodes[level - 1] = eb->start;
1664 memcpy(&node_keys[level - 1], &key,
1665 sizeof(node_keys[0]));
1666 } else {
1667 WARN_ON(1);
1668 }
1669 }
1670
Zheng Yan1a40e232008-09-26 10:09:34 -04001671 btrfs_tree_unlock(parent);
1672 free_extent_buffer(parent);
1673 parent = eb;
1674 continue;
1675 }
1676
Zheng Yan1a40e232008-09-26 10:09:34 -04001677 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1678 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1679 btrfs_mark_buffer_dirty(parent);
1680
1681 ret = btrfs_inc_extent_ref(trans, root,
1682 nodes[level - 1],
1683 blocksize, parent->start,
1684 btrfs_header_owner(parent),
1685 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001686 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001687 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001688
1689 /*
1690 * If the block was created in the running transaction,
1691 * it's possible this is the last reference to it, so we
1692 * should drop the subtree.
1693 */
1694 if (generation == trans->transid) {
1695 ret = btrfs_drop_subtree(trans, root, eb, parent);
1696 BUG_ON(ret);
1697 btrfs_tree_unlock(eb);
1698 free_extent_buffer(eb);
1699 } else {
1700 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001701 blocksize, parent->start,
1702 btrfs_header_owner(parent),
1703 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001704 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001705 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001706 }
1707 break;
1708 }
1709 btrfs_tree_unlock(parent);
1710 free_extent_buffer(parent);
1711 return 0;
1712}
1713
Chris Mason74123bd2007-02-02 11:05:29 -05001714/*
1715 * adjust the pointers going up the tree, starting at level
1716 * making sure the right key of each node is points to 'key'.
1717 * This is used after shifting pointers to the left, so it stops
1718 * fixing up pointers when a given leaf/node is not in slot 0 of the
1719 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001720 *
1721 * If this fails to write a tree block, it returns -1, but continues
1722 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001723 */
Chris Mason5f39d392007-10-15 16:14:19 -04001724static int fixup_low_keys(struct btrfs_trans_handle *trans,
1725 struct btrfs_root *root, struct btrfs_path *path,
1726 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001727{
1728 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001729 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001730 struct extent_buffer *t;
1731
Chris Mason234b63a2007-03-13 10:46:10 -04001732 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001733 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001734 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001735 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001736 t = path->nodes[i];
1737 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001738 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001739 if (tslot != 0)
1740 break;
1741 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001742 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001743}
1744
Chris Mason74123bd2007-02-02 11:05:29 -05001745/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001746 * update item key.
1747 *
1748 * This function isn't completely safe. It's the caller's responsibility
1749 * that the new key won't break the order
1750 */
1751int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1752 struct btrfs_root *root, struct btrfs_path *path,
1753 struct btrfs_key *new_key)
1754{
1755 struct btrfs_disk_key disk_key;
1756 struct extent_buffer *eb;
1757 int slot;
1758
1759 eb = path->nodes[0];
1760 slot = path->slots[0];
1761 if (slot > 0) {
1762 btrfs_item_key(eb, &disk_key, slot - 1);
1763 if (comp_keys(&disk_key, new_key) >= 0)
1764 return -1;
1765 }
1766 if (slot < btrfs_header_nritems(eb) - 1) {
1767 btrfs_item_key(eb, &disk_key, slot + 1);
1768 if (comp_keys(&disk_key, new_key) <= 0)
1769 return -1;
1770 }
1771
1772 btrfs_cpu_key_to_disk(&disk_key, new_key);
1773 btrfs_set_item_key(eb, &disk_key, slot);
1774 btrfs_mark_buffer_dirty(eb);
1775 if (slot == 0)
1776 fixup_low_keys(trans, root, path, &disk_key, 1);
1777 return 0;
1778}
1779
1780/*
Chris Mason74123bd2007-02-02 11:05:29 -05001781 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001782 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001783 *
1784 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1785 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001786 */
Chris Mason98ed5172008-01-03 10:01:48 -05001787static int push_node_left(struct btrfs_trans_handle *trans,
1788 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001789 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001790{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001791 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001792 int src_nritems;
1793 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001794 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001795
Chris Mason5f39d392007-10-15 16:14:19 -04001796 src_nritems = btrfs_header_nritems(src);
1797 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001798 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001799 WARN_ON(btrfs_header_generation(src) != trans->transid);
1800 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001801
Chris Masonbce4eae2008-04-24 14:42:46 -04001802 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001803 return 1;
1804
Chris Masoneb60cea2007-02-02 09:18:22 -05001805 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001806 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001807 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001808
Chris Masonbce4eae2008-04-24 14:42:46 -04001809 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001810 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001811 if (push_items < src_nritems) {
1812 /* leave at least 8 pointers in the node if
1813 * we aren't going to empty it
1814 */
1815 if (src_nritems - push_items < 8) {
1816 if (push_items <= 8)
1817 return 1;
1818 push_items -= 8;
1819 }
1820 }
1821 } else
1822 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001823
Chris Mason5f39d392007-10-15 16:14:19 -04001824 copy_extent_buffer(dst, src,
1825 btrfs_node_key_ptr_offset(dst_nritems),
1826 btrfs_node_key_ptr_offset(0),
1827 push_items * sizeof(struct btrfs_key_ptr));
1828
Chris Masonbb803952007-03-01 12:04:21 -05001829 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001830 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1831 btrfs_node_key_ptr_offset(push_items),
1832 (src_nritems - push_items) *
1833 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001834 }
Chris Mason5f39d392007-10-15 16:14:19 -04001835 btrfs_set_header_nritems(src, src_nritems - push_items);
1836 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1837 btrfs_mark_buffer_dirty(src);
1838 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001839
1840 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1841 BUG_ON(ret);
1842
Chris Masonbb803952007-03-01 12:04:21 -05001843 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001844}
1845
Chris Mason97571fd2007-02-24 13:39:08 -05001846/*
Chris Mason79f95c82007-03-01 15:16:26 -05001847 * try to push data from one node into the next node right in the
1848 * tree.
1849 *
1850 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1851 * error, and > 0 if there was no room in the right hand block.
1852 *
1853 * this will only push up to 1/2 the contents of the left node over
1854 */
Chris Mason5f39d392007-10-15 16:14:19 -04001855static int balance_node_right(struct btrfs_trans_handle *trans,
1856 struct btrfs_root *root,
1857 struct extent_buffer *dst,
1858 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001859{
Chris Mason79f95c82007-03-01 15:16:26 -05001860 int push_items = 0;
1861 int max_push;
1862 int src_nritems;
1863 int dst_nritems;
1864 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001865
Chris Mason7bb86312007-12-11 09:25:06 -05001866 WARN_ON(btrfs_header_generation(src) != trans->transid);
1867 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1868
Chris Mason5f39d392007-10-15 16:14:19 -04001869 src_nritems = btrfs_header_nritems(src);
1870 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001871 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001872 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001873 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001874 }
1875
1876 if (src_nritems < 4) {
1877 return 1;
1878 }
Chris Mason79f95c82007-03-01 15:16:26 -05001879
1880 max_push = src_nritems / 2 + 1;
1881 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001882 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001883 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001884 }
Yan252c38f2007-08-29 09:11:44 -04001885
Chris Mason79f95c82007-03-01 15:16:26 -05001886 if (max_push < push_items)
1887 push_items = max_push;
1888
Chris Mason5f39d392007-10-15 16:14:19 -04001889 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1890 btrfs_node_key_ptr_offset(0),
1891 (dst_nritems) *
1892 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001893
Chris Mason5f39d392007-10-15 16:14:19 -04001894 copy_extent_buffer(dst, src,
1895 btrfs_node_key_ptr_offset(0),
1896 btrfs_node_key_ptr_offset(src_nritems - push_items),
1897 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001898
Chris Mason5f39d392007-10-15 16:14:19 -04001899 btrfs_set_header_nritems(src, src_nritems - push_items);
1900 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001901
Chris Mason5f39d392007-10-15 16:14:19 -04001902 btrfs_mark_buffer_dirty(src);
1903 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001904
1905 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1906 BUG_ON(ret);
1907
Chris Mason79f95c82007-03-01 15:16:26 -05001908 return ret;
1909}
1910
1911/*
Chris Mason97571fd2007-02-24 13:39:08 -05001912 * helper function to insert a new root level in the tree.
1913 * A new node is allocated, and a single item is inserted to
1914 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001915 *
1916 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001917 */
Chris Mason98ed5172008-01-03 10:01:48 -05001918static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001919 struct btrfs_root *root,
1920 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001921{
Chris Mason7bb86312007-12-11 09:25:06 -05001922 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001923 struct extent_buffer *lower;
1924 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001925 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001926 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001927 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001928
1929 BUG_ON(path->nodes[level]);
1930 BUG_ON(path->nodes[level-1] != root->node);
1931
Chris Mason7bb86312007-12-11 09:25:06 -05001932 lower = path->nodes[level-1];
1933 if (level == 1)
1934 btrfs_item_key(lower, &lower_key, 0);
1935 else
1936 btrfs_node_key(lower, &lower_key, 0);
1937
Zheng Yan31840ae2008-09-23 13:14:14 -04001938 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1939 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001940 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001941 if (IS_ERR(c))
1942 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001943
Chris Mason5f39d392007-10-15 16:14:19 -04001944 memset_extent_buffer(c, 0, 0, root->nodesize);
1945 btrfs_set_header_nritems(c, 1);
1946 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001947 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001948 btrfs_set_header_generation(c, trans->transid);
1949 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001950
Chris Mason5f39d392007-10-15 16:14:19 -04001951 write_extent_buffer(c, root->fs_info->fsid,
1952 (unsigned long)btrfs_header_fsid(c),
1953 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001954
1955 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1956 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1957 BTRFS_UUID_SIZE);
1958
Chris Mason5f39d392007-10-15 16:14:19 -04001959 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001960 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001961 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001962 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001963
1964 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001965
1966 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001967
Chris Mason925baed2008-06-25 16:01:30 -04001968 spin_lock(&root->node_lock);
1969 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001970 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001971 spin_unlock(&root->node_lock);
1972
Zheng Yan31840ae2008-09-23 13:14:14 -04001973 ret = btrfs_update_extent_ref(trans, root, lower->start,
1974 lower->start, c->start,
1975 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001976 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001977 BUG_ON(ret);
1978
Chris Mason925baed2008-06-25 16:01:30 -04001979 /* the super has an extra ref to root->node */
1980 free_extent_buffer(old);
1981
Chris Mason0b86a832008-03-24 15:01:56 -04001982 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001983 extent_buffer_get(c);
1984 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001985 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001986 path->slots[level] = 0;
1987 return 0;
1988}
1989
Chris Mason74123bd2007-02-02 11:05:29 -05001990/*
1991 * worker function to insert a single pointer in a node.
1992 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001993 *
Chris Mason74123bd2007-02-02 11:05:29 -05001994 * slot and level indicate where you want the key to go, and
1995 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001996 *
1997 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001998 */
Chris Masone089f052007-03-16 16:20:31 -04001999static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2000 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002001 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002002{
Chris Mason5f39d392007-10-15 16:14:19 -04002003 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002004 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002005
2006 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002007 lower = path->nodes[level];
2008 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002009 if (slot > nritems)
2010 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002011 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002012 BUG();
2013 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002014 memmove_extent_buffer(lower,
2015 btrfs_node_key_ptr_offset(slot + 1),
2016 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002017 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002018 }
Chris Mason5f39d392007-10-15 16:14:19 -04002019 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002020 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002021 WARN_ON(trans->transid == 0);
2022 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002023 btrfs_set_header_nritems(lower, nritems + 1);
2024 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002025 return 0;
2026}
2027
Chris Mason97571fd2007-02-24 13:39:08 -05002028/*
2029 * split the node at the specified level in path in two.
2030 * The path is corrected to point to the appropriate node after the split
2031 *
2032 * Before splitting this tries to make some room in the node by pushing
2033 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002034 *
2035 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002036 */
Chris Masone02119d2008-09-05 16:13:11 -04002037static noinline int split_node(struct btrfs_trans_handle *trans,
2038 struct btrfs_root *root,
2039 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002040{
Chris Mason5f39d392007-10-15 16:14:19 -04002041 struct extent_buffer *c;
2042 struct extent_buffer *split;
2043 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002044 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002045 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002046 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002047 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002048
Chris Mason5f39d392007-10-15 16:14:19 -04002049 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002050 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002052 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002053 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002054 if (ret)
2055 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002056 } else {
2057 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002058 c = path->nodes[level];
2059 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002060 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002061 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002062 if (ret < 0)
2063 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002064 }
Chris Masone66f7092007-04-20 13:16:02 -04002065
Chris Mason5f39d392007-10-15 16:14:19 -04002066 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002067
Chris Mason925baed2008-06-25 16:01:30 -04002068 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002069 path->nodes[level + 1]->start,
2070 root->root_key.objectid,
2071 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002072 if (IS_ERR(split))
2073 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002074
Chris Mason5f39d392007-10-15 16:14:19 -04002075 btrfs_set_header_flags(split, btrfs_header_flags(c));
2076 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002077 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002078 btrfs_set_header_generation(split, trans->transid);
2079 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002080 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002081 write_extent_buffer(split, root->fs_info->fsid,
2082 (unsigned long)btrfs_header_fsid(split),
2083 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002084 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2085 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2086 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002087
Chris Mason7518a232007-03-12 12:01:18 -04002088 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002089
2090 copy_extent_buffer(split, c,
2091 btrfs_node_key_ptr_offset(0),
2092 btrfs_node_key_ptr_offset(mid),
2093 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2094 btrfs_set_header_nritems(split, c_nritems - mid);
2095 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002096 ret = 0;
2097
Chris Mason5f39d392007-10-15 16:14:19 -04002098 btrfs_mark_buffer_dirty(c);
2099 btrfs_mark_buffer_dirty(split);
2100
2101 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002102 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002103 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002104 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002105 if (wret)
2106 ret = wret;
2107
Zheng Yan31840ae2008-09-23 13:14:14 -04002108 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2109 BUG_ON(ret);
2110
Chris Mason5de08d72007-02-24 06:24:44 -05002111 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002112 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002113 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002114 free_extent_buffer(c);
2115 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002116 path->slots[level + 1] += 1;
2117 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002118 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002119 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002120 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002121 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002122}
2123
Chris Mason74123bd2007-02-02 11:05:29 -05002124/*
2125 * how many bytes are required to store the items in a leaf. start
2126 * and nr indicate which items in the leaf to check. This totals up the
2127 * space used both by the item structs and the item data
2128 */
Chris Mason5f39d392007-10-15 16:14:19 -04002129static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002130{
2131 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002132 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002133 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002134
2135 if (!nr)
2136 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002137 data_len = btrfs_item_end_nr(l, start);
2138 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002139 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002140 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002141 return data_len;
2142}
2143
Chris Mason74123bd2007-02-02 11:05:29 -05002144/*
Chris Masond4dbff92007-04-04 14:08:15 -04002145 * The space between the end of the leaf items and
2146 * the start of the leaf data. IOW, how much room
2147 * the leaf has left for both items and data
2148 */
Chris Masone02119d2008-09-05 16:13:11 -04002149int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2150 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002151{
Chris Mason5f39d392007-10-15 16:14:19 -04002152 int nritems = btrfs_header_nritems(leaf);
2153 int ret;
2154 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2155 if (ret < 0) {
2156 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002157 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002158 leaf_space_used(leaf, 0, nritems), nritems);
2159 }
2160 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002161}
2162
2163/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002164 * push some data in the path leaf to the right, trying to free up at
2165 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002166 *
2167 * returns 1 if the push failed because the other node didn't have enough
2168 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002169 */
Chris Masone089f052007-03-16 16:20:31 -04002170static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002171 *root, struct btrfs_path *path, int data_size,
2172 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002173{
Chris Mason5f39d392007-10-15 16:14:19 -04002174 struct extent_buffer *left = path->nodes[0];
2175 struct extent_buffer *right;
2176 struct extent_buffer *upper;
2177 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002178 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002179 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002180 int free_space;
2181 int push_space = 0;
2182 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002183 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002184 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002185 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002186 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002187 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002188 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002189 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002190
2191 slot = path->slots[1];
2192 if (!path->nodes[1]) {
2193 return 1;
2194 }
2195 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002196 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002197 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002198
Chris Masona2135012008-06-25 16:01:30 -04002199 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2200
Chris Masonca7a79a2008-05-12 12:59:19 -04002201 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002202 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002203 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002204 if (free_space < data_size + sizeof(struct btrfs_item))
2205 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002206
Chris Mason5f39d392007-10-15 16:14:19 -04002207 /* cow and double check */
2208 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002209 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002210 if (ret)
2211 goto out_unlock;
2212
Chris Mason5f39d392007-10-15 16:14:19 -04002213 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002214 if (free_space < data_size + sizeof(struct btrfs_item))
2215 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002216
2217 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002218 if (left_nritems == 0)
2219 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002220
Chris Mason34a38212007-11-07 13:31:03 -05002221 if (empty)
2222 nr = 0;
2223 else
2224 nr = 1;
2225
Zheng Yan31840ae2008-09-23 13:14:14 -04002226 if (path->slots[0] >= left_nritems)
2227 push_space += data_size + sizeof(*item);
2228
Chris Mason34a38212007-11-07 13:31:03 -05002229 i = left_nritems - 1;
2230 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002231 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002232
Zheng Yan31840ae2008-09-23 13:14:14 -04002233 if (!empty && push_items > 0) {
2234 if (path->slots[0] > i)
2235 break;
2236 if (path->slots[0] == i) {
2237 int space = btrfs_leaf_free_space(root, left);
2238 if (space + push_space * 2 > free_space)
2239 break;
2240 }
2241 }
2242
Chris Mason00ec4c52007-02-24 12:47:20 -05002243 if (path->slots[0] == i)
2244 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002245
2246 if (!left->map_token) {
2247 map_extent_buffer(left, (unsigned long)item,
2248 sizeof(struct btrfs_item),
2249 &left->map_token, &left->kaddr,
2250 &left->map_start, &left->map_len,
2251 KM_USER1);
2252 }
2253
2254 this_item_size = btrfs_item_size(left, item);
2255 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002256 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002257
Chris Mason00ec4c52007-02-24 12:47:20 -05002258 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002259 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002260 if (i == 0)
2261 break;
2262 i--;
Chris Masondb945352007-10-15 16:15:53 -04002263 }
2264 if (left->map_token) {
2265 unmap_extent_buffer(left, left->map_token, KM_USER1);
2266 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002267 }
Chris Mason5f39d392007-10-15 16:14:19 -04002268
Chris Mason925baed2008-06-25 16:01:30 -04002269 if (push_items == 0)
2270 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002271
Chris Mason34a38212007-11-07 13:31:03 -05002272 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002273 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002274
Chris Mason00ec4c52007-02-24 12:47:20 -05002275 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002276 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002277
Chris Mason5f39d392007-10-15 16:14:19 -04002278 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002279 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002280
Chris Mason00ec4c52007-02-24 12:47:20 -05002281 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002282 data_end = leaf_data_end(root, right);
2283 memmove_extent_buffer(right,
2284 btrfs_leaf_data(right) + data_end - push_space,
2285 btrfs_leaf_data(right) + data_end,
2286 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2287
Chris Mason00ec4c52007-02-24 12:47:20 -05002288 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002289 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002290 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2291 btrfs_leaf_data(left) + leaf_data_end(root, left),
2292 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002293
2294 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2295 btrfs_item_nr_offset(0),
2296 right_nritems * sizeof(struct btrfs_item));
2297
Chris Mason00ec4c52007-02-24 12:47:20 -05002298 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002299 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2300 btrfs_item_nr_offset(left_nritems - push_items),
2301 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002302
2303 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002304 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002305 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002306 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002307 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002308 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002309 if (!right->map_token) {
2310 map_extent_buffer(right, (unsigned long)item,
2311 sizeof(struct btrfs_item),
2312 &right->map_token, &right->kaddr,
2313 &right->map_start, &right->map_len,
2314 KM_USER1);
2315 }
2316 push_space -= btrfs_item_size(right, item);
2317 btrfs_set_item_offset(right, item, push_space);
2318 }
2319
2320 if (right->map_token) {
2321 unmap_extent_buffer(right, right->map_token, KM_USER1);
2322 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002323 }
Chris Mason7518a232007-03-12 12:01:18 -04002324 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002325 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002326
Chris Mason34a38212007-11-07 13:31:03 -05002327 if (left_nritems)
2328 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002329 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002330
Zheng Yan31840ae2008-09-23 13:14:14 -04002331 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2332 BUG_ON(ret);
2333
Chris Mason5f39d392007-10-15 16:14:19 -04002334 btrfs_item_key(right, &disk_key, 0);
2335 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002336 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002337
Chris Mason00ec4c52007-02-24 12:47:20 -05002338 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002339 if (path->slots[0] >= left_nritems) {
2340 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002341 if (btrfs_header_nritems(path->nodes[0]) == 0)
2342 clean_tree_block(trans, root, path->nodes[0]);
2343 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002344 free_extent_buffer(path->nodes[0]);
2345 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002346 path->slots[1] += 1;
2347 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002348 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002349 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002350 }
2351 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002352
2353out_unlock:
2354 btrfs_tree_unlock(right);
2355 free_extent_buffer(right);
2356 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002357}
Chris Mason925baed2008-06-25 16:01:30 -04002358
Chris Mason00ec4c52007-02-24 12:47:20 -05002359/*
Chris Mason74123bd2007-02-02 11:05:29 -05002360 * push some data in the path leaf to the left, trying to free up at
2361 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2362 */
Chris Masone089f052007-03-16 16:20:31 -04002363static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002364 *root, struct btrfs_path *path, int data_size,
2365 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002366{
Chris Mason5f39d392007-10-15 16:14:19 -04002367 struct btrfs_disk_key disk_key;
2368 struct extent_buffer *right = path->nodes[0];
2369 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002370 int slot;
2371 int i;
2372 int free_space;
2373 int push_space = 0;
2374 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002375 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002376 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002377 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002378 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002379 int ret = 0;
2380 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002381 u32 this_item_size;
2382 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002383
2384 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002385 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002386 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002387 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002388 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002389
Chris Mason3685f792007-10-19 09:23:27 -04002390 right_nritems = btrfs_header_nritems(right);
2391 if (right_nritems == 0) {
2392 return 1;
2393 }
2394
Chris Masona2135012008-06-25 16:01:30 -04002395 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2396
Chris Masonca7a79a2008-05-12 12:59:19 -04002397 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002398 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002399 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002400 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002401 ret = 1;
2402 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002403 }
Chris Mason02217ed2007-03-02 16:08:05 -05002404
2405 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002406 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002407 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002408 if (ret) {
2409 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002410 ret = 1;
2411 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002412 }
Chris Mason3685f792007-10-19 09:23:27 -04002413
Chris Mason123abc82007-03-14 14:14:43 -04002414 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002415 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002416 ret = 1;
2417 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002418 }
2419
Chris Mason34a38212007-11-07 13:31:03 -05002420 if (empty)
2421 nr = right_nritems;
2422 else
2423 nr = right_nritems - 1;
2424
2425 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002426 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002427 if (!right->map_token) {
2428 map_extent_buffer(right, (unsigned long)item,
2429 sizeof(struct btrfs_item),
2430 &right->map_token, &right->kaddr,
2431 &right->map_start, &right->map_len,
2432 KM_USER1);
2433 }
2434
Zheng Yan31840ae2008-09-23 13:14:14 -04002435 if (!empty && push_items > 0) {
2436 if (path->slots[0] < i)
2437 break;
2438 if (path->slots[0] == i) {
2439 int space = btrfs_leaf_free_space(root, right);
2440 if (space + push_space * 2 > free_space)
2441 break;
2442 }
2443 }
2444
Chris Masonbe0e5c02007-01-26 15:51:26 -05002445 if (path->slots[0] == i)
2446 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002447
2448 this_item_size = btrfs_item_size(right, item);
2449 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002450 break;
Chris Masondb945352007-10-15 16:15:53 -04002451
Chris Masonbe0e5c02007-01-26 15:51:26 -05002452 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002453 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002454 }
Chris Masondb945352007-10-15 16:15:53 -04002455
2456 if (right->map_token) {
2457 unmap_extent_buffer(right, right->map_token, KM_USER1);
2458 right->map_token = NULL;
2459 }
2460
Chris Masonbe0e5c02007-01-26 15:51:26 -05002461 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002462 ret = 1;
2463 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002464 }
Chris Mason34a38212007-11-07 13:31:03 -05002465 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002466 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002467
Chris Masonbe0e5c02007-01-26 15:51:26 -05002468 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002469 copy_extent_buffer(left, right,
2470 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2471 btrfs_item_nr_offset(0),
2472 push_items * sizeof(struct btrfs_item));
2473
Chris Mason123abc82007-03-14 14:14:43 -04002474 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002475 btrfs_item_offset_nr(right, push_items -1);
2476
2477 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002478 leaf_data_end(root, left) - push_space,
2479 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002480 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002481 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002482 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002483 BUG_ON(old_left_nritems < 0);
2484
Chris Masondb945352007-10-15 16:15:53 -04002485 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002486 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002487 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002488
Chris Mason5f39d392007-10-15 16:14:19 -04002489 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002490 if (!left->map_token) {
2491 map_extent_buffer(left, (unsigned long)item,
2492 sizeof(struct btrfs_item),
2493 &left->map_token, &left->kaddr,
2494 &left->map_start, &left->map_len,
2495 KM_USER1);
2496 }
2497
Chris Mason5f39d392007-10-15 16:14:19 -04002498 ioff = btrfs_item_offset(left, item);
2499 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002500 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002501 }
Chris Mason5f39d392007-10-15 16:14:19 -04002502 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002503 if (left->map_token) {
2504 unmap_extent_buffer(left, left->map_token, KM_USER1);
2505 left->map_token = NULL;
2506 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002507
2508 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002509 if (push_items > right_nritems) {
2510 printk("push items %d nr %u\n", push_items, right_nritems);
2511 WARN_ON(1);
2512 }
Chris Mason5f39d392007-10-15 16:14:19 -04002513
Chris Mason34a38212007-11-07 13:31:03 -05002514 if (push_items < right_nritems) {
2515 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2516 leaf_data_end(root, right);
2517 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2518 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2519 btrfs_leaf_data(right) +
2520 leaf_data_end(root, right), push_space);
2521
2522 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002523 btrfs_item_nr_offset(push_items),
2524 (btrfs_header_nritems(right) - push_items) *
2525 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002526 }
Yaneef1c492007-11-26 10:58:13 -05002527 right_nritems -= push_items;
2528 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002529 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002530 for (i = 0; i < right_nritems; i++) {
2531 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002532
2533 if (!right->map_token) {
2534 map_extent_buffer(right, (unsigned long)item,
2535 sizeof(struct btrfs_item),
2536 &right->map_token, &right->kaddr,
2537 &right->map_start, &right->map_len,
2538 KM_USER1);
2539 }
2540
2541 push_space = push_space - btrfs_item_size(right, item);
2542 btrfs_set_item_offset(right, item, push_space);
2543 }
2544 if (right->map_token) {
2545 unmap_extent_buffer(right, right->map_token, KM_USER1);
2546 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002547 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002548
Chris Mason5f39d392007-10-15 16:14:19 -04002549 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002550 if (right_nritems)
2551 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002552
Zheng Yan31840ae2008-09-23 13:14:14 -04002553 ret = btrfs_update_ref(trans, root, right, left,
2554 old_left_nritems, push_items);
2555 BUG_ON(ret);
2556
Chris Mason5f39d392007-10-15 16:14:19 -04002557 btrfs_item_key(right, &disk_key, 0);
2558 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002559 if (wret)
2560 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561
2562 /* then fixup the leaf pointer in the path */
2563 if (path->slots[0] < push_items) {
2564 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002565 if (btrfs_header_nritems(path->nodes[0]) == 0)
2566 clean_tree_block(trans, root, path->nodes[0]);
2567 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002568 free_extent_buffer(path->nodes[0]);
2569 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570 path->slots[1] -= 1;
2571 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002572 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002573 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002574 path->slots[0] -= push_items;
2575 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002576 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002577 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002578out:
2579 btrfs_tree_unlock(left);
2580 free_extent_buffer(left);
2581 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002582}
2583
Chris Mason74123bd2007-02-02 11:05:29 -05002584/*
2585 * split the path's leaf in two, making sure there is at least data_size
2586 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002587 *
2588 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002589 */
Chris Masone02119d2008-09-05 16:13:11 -04002590static noinline int split_leaf(struct btrfs_trans_handle *trans,
2591 struct btrfs_root *root,
2592 struct btrfs_key *ins_key,
2593 struct btrfs_path *path, int data_size,
2594 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002595{
Chris Mason5f39d392007-10-15 16:14:19 -04002596 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002597 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002598 int mid;
2599 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002600 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002601 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602 int data_copy_size;
2603 int rt_data_off;
2604 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002605 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002606 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002607 int double_split;
2608 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002609 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002610
Chris Masoncc0c5532007-10-25 15:42:57 -04002611 if (extend)
2612 space_needed = data_size;
2613
Chris Mason40689472007-03-17 14:29:23 -04002614 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002615 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002616 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002617 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002618 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002619 }
2620 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002621 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002622 if (wret < 0)
2623 return wret;
2624 }
2625 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002626
Chris Mason3685f792007-10-19 09:23:27 -04002627 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002628 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002629 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002630 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002631
Chris Mason5c680ed2007-02-22 11:39:13 -05002632 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002633 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002634 if (ret)
2635 return ret;
2636 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002637again:
2638 double_split = 0;
2639 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002640 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002641 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002642 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002643
Chris Mason925baed2008-06-25 16:01:30 -04002644 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002645 path->nodes[1]->start,
2646 root->root_key.objectid,
2647 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002648 if (IS_ERR(right)) {
2649 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002650 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002651 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002652
Chris Mason5f39d392007-10-15 16:14:19 -04002653 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002654 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002655 btrfs_set_header_generation(right, trans->transid);
2656 btrfs_set_header_owner(right, root->root_key.objectid);
2657 btrfs_set_header_level(right, 0);
2658 write_extent_buffer(right, root->fs_info->fsid,
2659 (unsigned long)btrfs_header_fsid(right),
2660 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002661
2662 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2663 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2664 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002665 if (mid <= slot) {
2666 if (nritems == 1 ||
2667 leaf_space_used(l, mid, nritems - mid) + space_needed >
2668 BTRFS_LEAF_DATA_SIZE(root)) {
2669 if (slot >= nritems) {
2670 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002672 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002673 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002674 path->slots[1] + 1, 1);
2675 if (wret)
2676 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002677
2678 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002679 free_extent_buffer(path->nodes[0]);
2680 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002681 path->slots[0] = 0;
2682 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002683 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002684 return ret;
2685 }
2686 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002687 if (mid != nritems &&
2688 leaf_space_used(l, mid, nritems - mid) +
2689 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2690 double_split = 1;
2691 }
Chris Masond4dbff92007-04-04 14:08:15 -04002692 }
2693 } else {
2694 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2695 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002696 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002697 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002698 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002699 wret = insert_ptr(trans, root, path,
2700 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002701 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002702 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002703 if (wret)
2704 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002705 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002706 free_extent_buffer(path->nodes[0]);
2707 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002708 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002709 if (path->slots[1] == 0) {
2710 wret = fixup_low_keys(trans, root,
2711 path, &disk_key, 1);
2712 if (wret)
2713 ret = wret;
2714 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002715 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002716 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002717 } else if (extend && slot == 0) {
2718 mid = 1;
2719 } else {
2720 mid = slot;
2721 if (mid != nritems &&
2722 leaf_space_used(l, mid, nritems - mid) +
2723 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2724 double_split = 1;
2725 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002726 }
Chris Masond4dbff92007-04-04 14:08:15 -04002727 }
2728 }
Chris Mason5f39d392007-10-15 16:14:19 -04002729 nritems = nritems - mid;
2730 btrfs_set_header_nritems(right, nritems);
2731 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2732
2733 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2734 btrfs_item_nr_offset(mid),
2735 nritems * sizeof(struct btrfs_item));
2736
2737 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002738 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2739 data_copy_size, btrfs_leaf_data(l) +
2740 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002741
Chris Mason5f39d392007-10-15 16:14:19 -04002742 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2743 btrfs_item_end_nr(l, mid);
2744
2745 for (i = 0; i < nritems; i++) {
2746 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002747 u32 ioff;
2748
2749 if (!right->map_token) {
2750 map_extent_buffer(right, (unsigned long)item,
2751 sizeof(struct btrfs_item),
2752 &right->map_token, &right->kaddr,
2753 &right->map_start, &right->map_len,
2754 KM_USER1);
2755 }
2756
2757 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002758 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002759 }
Chris Mason74123bd2007-02-02 11:05:29 -05002760
Chris Masondb945352007-10-15 16:15:53 -04002761 if (right->map_token) {
2762 unmap_extent_buffer(right, right->map_token, KM_USER1);
2763 right->map_token = NULL;
2764 }
2765
Chris Mason5f39d392007-10-15 16:14:19 -04002766 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002767 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002768 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002769 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2770 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002771 if (wret)
2772 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002773
2774 btrfs_mark_buffer_dirty(right);
2775 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002776 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002777
Zheng Yan31840ae2008-09-23 13:14:14 -04002778 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2779 BUG_ON(ret);
2780
Chris Masonbe0e5c02007-01-26 15:51:26 -05002781 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002782 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002783 free_extent_buffer(path->nodes[0]);
2784 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002785 path->slots[0] -= mid;
2786 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002787 } else {
2788 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002789 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002790 }
Chris Mason5f39d392007-10-15 16:14:19 -04002791
Chris Masoneb60cea2007-02-02 09:18:22 -05002792 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002793
Chris Masoncc0c5532007-10-25 15:42:57 -04002794 if (double_split) {
2795 BUG_ON(num_doubles != 0);
2796 num_doubles++;
2797 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002798 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002799 return ret;
2800}
2801
Chris Masond352ac62008-09-29 15:18:18 -04002802/*
2803 * make the item pointed to by the path smaller. new_size indicates
2804 * how small to make it, and from_end tells us if we just chop bytes
2805 * off the end of the item or if we shift the item to chop bytes off
2806 * the front.
2807 */
Chris Masonb18c6682007-04-17 13:26:50 -04002808int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2809 struct btrfs_root *root,
2810 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002811 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002812{
2813 int ret = 0;
2814 int slot;
2815 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002816 struct extent_buffer *leaf;
2817 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002818 u32 nritems;
2819 unsigned int data_end;
2820 unsigned int old_data_start;
2821 unsigned int old_size;
2822 unsigned int size_diff;
2823 int i;
2824
2825 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002826 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002827 slot = path->slots[0];
2828
2829 old_size = btrfs_item_size_nr(leaf, slot);
2830 if (old_size == new_size)
2831 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002832
Chris Mason5f39d392007-10-15 16:14:19 -04002833 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002834 data_end = leaf_data_end(root, leaf);
2835
Chris Mason5f39d392007-10-15 16:14:19 -04002836 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002837
Chris Masonb18c6682007-04-17 13:26:50 -04002838 size_diff = old_size - new_size;
2839
2840 BUG_ON(slot < 0);
2841 BUG_ON(slot >= nritems);
2842
2843 /*
2844 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2845 */
2846 /* first correct the data pointers */
2847 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002848 u32 ioff;
2849 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002850
2851 if (!leaf->map_token) {
2852 map_extent_buffer(leaf, (unsigned long)item,
2853 sizeof(struct btrfs_item),
2854 &leaf->map_token, &leaf->kaddr,
2855 &leaf->map_start, &leaf->map_len,
2856 KM_USER1);
2857 }
2858
Chris Mason5f39d392007-10-15 16:14:19 -04002859 ioff = btrfs_item_offset(leaf, item);
2860 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002861 }
Chris Masondb945352007-10-15 16:15:53 -04002862
2863 if (leaf->map_token) {
2864 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2865 leaf->map_token = NULL;
2866 }
2867
Chris Masonb18c6682007-04-17 13:26:50 -04002868 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002869 if (from_end) {
2870 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2871 data_end + size_diff, btrfs_leaf_data(leaf) +
2872 data_end, old_data_start + new_size - data_end);
2873 } else {
2874 struct btrfs_disk_key disk_key;
2875 u64 offset;
2876
2877 btrfs_item_key(leaf, &disk_key, slot);
2878
2879 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2880 unsigned long ptr;
2881 struct btrfs_file_extent_item *fi;
2882
2883 fi = btrfs_item_ptr(leaf, slot,
2884 struct btrfs_file_extent_item);
2885 fi = (struct btrfs_file_extent_item *)(
2886 (unsigned long)fi - size_diff);
2887
2888 if (btrfs_file_extent_type(leaf, fi) ==
2889 BTRFS_FILE_EXTENT_INLINE) {
2890 ptr = btrfs_item_ptr_offset(leaf, slot);
2891 memmove_extent_buffer(leaf, ptr,
2892 (unsigned long)fi,
2893 offsetof(struct btrfs_file_extent_item,
2894 disk_bytenr));
2895 }
2896 }
2897
2898 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2899 data_end + size_diff, btrfs_leaf_data(leaf) +
2900 data_end, old_data_start - data_end);
2901
2902 offset = btrfs_disk_key_offset(&disk_key);
2903 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2904 btrfs_set_item_key(leaf, &disk_key, slot);
2905 if (slot == 0)
2906 fixup_low_keys(trans, root, path, &disk_key, 1);
2907 }
Chris Mason5f39d392007-10-15 16:14:19 -04002908
2909 item = btrfs_item_nr(leaf, slot);
2910 btrfs_set_item_size(leaf, item, new_size);
2911 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002912
2913 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002914 if (btrfs_leaf_free_space(root, leaf) < 0) {
2915 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002916 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002917 }
Chris Masonb18c6682007-04-17 13:26:50 -04002918 return ret;
2919}
2920
Chris Masond352ac62008-09-29 15:18:18 -04002921/*
2922 * make the item pointed to by the path bigger, data_size is the new size.
2923 */
Chris Mason5f39d392007-10-15 16:14:19 -04002924int btrfs_extend_item(struct btrfs_trans_handle *trans,
2925 struct btrfs_root *root, struct btrfs_path *path,
2926 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002927{
2928 int ret = 0;
2929 int slot;
2930 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002931 struct extent_buffer *leaf;
2932 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002933 u32 nritems;
2934 unsigned int data_end;
2935 unsigned int old_data;
2936 unsigned int old_size;
2937 int i;
2938
2939 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002940 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002941
Chris Mason5f39d392007-10-15 16:14:19 -04002942 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002943 data_end = leaf_data_end(root, leaf);
2944
Chris Mason5f39d392007-10-15 16:14:19 -04002945 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2946 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002947 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002948 }
Chris Mason6567e832007-04-16 09:22:45 -04002949 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002950 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002951
2952 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002953 if (slot >= nritems) {
2954 btrfs_print_leaf(root, leaf);
2955 printk("slot %d too large, nritems %d\n", slot, nritems);
2956 BUG_ON(1);
2957 }
Chris Mason6567e832007-04-16 09:22:45 -04002958
2959 /*
2960 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2961 */
2962 /* first correct the data pointers */
2963 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002964 u32 ioff;
2965 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002966
2967 if (!leaf->map_token) {
2968 map_extent_buffer(leaf, (unsigned long)item,
2969 sizeof(struct btrfs_item),
2970 &leaf->map_token, &leaf->kaddr,
2971 &leaf->map_start, &leaf->map_len,
2972 KM_USER1);
2973 }
Chris Mason5f39d392007-10-15 16:14:19 -04002974 ioff = btrfs_item_offset(leaf, item);
2975 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002976 }
Chris Mason5f39d392007-10-15 16:14:19 -04002977
Chris Masondb945352007-10-15 16:15:53 -04002978 if (leaf->map_token) {
2979 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2980 leaf->map_token = NULL;
2981 }
2982
Chris Mason6567e832007-04-16 09:22:45 -04002983 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002984 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002985 data_end - data_size, btrfs_leaf_data(leaf) +
2986 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002987
Chris Mason6567e832007-04-16 09:22:45 -04002988 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002989 old_size = btrfs_item_size_nr(leaf, slot);
2990 item = btrfs_item_nr(leaf, slot);
2991 btrfs_set_item_size(leaf, item, old_size + data_size);
2992 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002993
2994 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002995 if (btrfs_leaf_free_space(root, leaf) < 0) {
2996 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002997 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002998 }
Chris Mason6567e832007-04-16 09:22:45 -04002999 return ret;
3000}
3001
Chris Mason74123bd2007-02-02 11:05:29 -05003002/*
Chris Masond352ac62008-09-29 15:18:18 -04003003 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003004 * This does all the path init required, making room in the tree if needed.
3005 */
Chris Mason9c583092008-01-29 15:15:18 -05003006int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003007 struct btrfs_root *root,
3008 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003009 struct btrfs_key *cpu_key, u32 *data_size,
3010 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003011{
Chris Mason5f39d392007-10-15 16:14:19 -04003012 struct extent_buffer *leaf;
3013 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003014 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003015 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003016 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003017 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003018 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003019 u32 total_size = 0;
3020 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003021 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003022 struct btrfs_disk_key disk_key;
3023
Chris Mason9c583092008-01-29 15:15:18 -05003024 for (i = 0; i < nr; i++) {
3025 total_data += data_size[i];
3026 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003027
Josef Bacik7b128762008-07-24 12:17:14 -04003028 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003029 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003030 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003031 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003032 if (ret < 0)
3033 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003034
Chris Mason62e27492007-03-15 12:56:47 -04003035 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003036 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003037
Chris Mason5f39d392007-10-15 16:14:19 -04003038 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003039 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003040
Chris Masonf25956c2008-09-12 15:32:53 -04003041 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003042 btrfs_print_leaf(root, leaf);
3043 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003044 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003045 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003046 }
Chris Mason5f39d392007-10-15 16:14:19 -04003047
Chris Mason62e27492007-03-15 12:56:47 -04003048 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003049 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003050
Chris Masonbe0e5c02007-01-26 15:51:26 -05003051 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003052 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003053
Chris Mason5f39d392007-10-15 16:14:19 -04003054 if (old_data < data_end) {
3055 btrfs_print_leaf(root, leaf);
3056 printk("slot %d old_data %d data_end %d\n",
3057 slot, old_data, data_end);
3058 BUG_ON(1);
3059 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003060 /*
3061 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3062 */
3063 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003064 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003065 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003066 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003067
Chris Mason5f39d392007-10-15 16:14:19 -04003068 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003069 if (!leaf->map_token) {
3070 map_extent_buffer(leaf, (unsigned long)item,
3071 sizeof(struct btrfs_item),
3072 &leaf->map_token, &leaf->kaddr,
3073 &leaf->map_start, &leaf->map_len,
3074 KM_USER1);
3075 }
3076
Chris Mason5f39d392007-10-15 16:14:19 -04003077 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003078 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003079 }
Chris Masondb945352007-10-15 16:15:53 -04003080 if (leaf->map_token) {
3081 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3082 leaf->map_token = NULL;
3083 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003084
3085 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003086 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003087 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003088 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003089
3090 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003091 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003092 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003093 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003094 data_end = old_data;
3095 }
Chris Mason5f39d392007-10-15 16:14:19 -04003096
Chris Mason62e27492007-03-15 12:56:47 -04003097 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003098 for (i = 0; i < nr; i++) {
3099 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3100 btrfs_set_item_key(leaf, &disk_key, slot + i);
3101 item = btrfs_item_nr(leaf, slot + i);
3102 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3103 data_end -= data_size[i];
3104 btrfs_set_item_size(leaf, item, data_size[i]);
3105 }
3106 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003107 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003108
3109 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003110 if (slot == 0) {
3111 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003112 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003113 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003114
Chris Mason5f39d392007-10-15 16:14:19 -04003115 if (btrfs_leaf_free_space(root, leaf) < 0) {
3116 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003117 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003118 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003119out:
Chris Mason62e27492007-03-15 12:56:47 -04003120 return ret;
3121}
3122
3123/*
3124 * Given a key and some data, insert an item into the tree.
3125 * This does all the path init required, making room in the tree if needed.
3126 */
Chris Masone089f052007-03-16 16:20:31 -04003127int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3128 *root, struct btrfs_key *cpu_key, void *data, u32
3129 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003130{
3131 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003132 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003133 struct extent_buffer *leaf;
3134 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003135
Chris Mason2c90e5d2007-04-02 10:50:19 -04003136 path = btrfs_alloc_path();
3137 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003138 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003139 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003140 leaf = path->nodes[0];
3141 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3142 write_extent_buffer(leaf, data, ptr, data_size);
3143 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003144 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003145 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003146 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003147}
3148
Chris Mason74123bd2007-02-02 11:05:29 -05003149/*
Chris Mason5de08d72007-02-24 06:24:44 -05003150 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003151 *
Chris Masond352ac62008-09-29 15:18:18 -04003152 * the tree should have been previously balanced so the deletion does not
3153 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003154 */
Chris Masone089f052007-03-16 16:20:31 -04003155static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3156 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003157{
Chris Mason5f39d392007-10-15 16:14:19 -04003158 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003159 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003160 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003161 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003162
Chris Mason5f39d392007-10-15 16:14:19 -04003163 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05003164 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003165 memmove_extent_buffer(parent,
3166 btrfs_node_key_ptr_offset(slot),
3167 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003168 sizeof(struct btrfs_key_ptr) *
3169 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003170 }
Chris Mason7518a232007-03-12 12:01:18 -04003171 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003172 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003173 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003174 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003175 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003176 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003177 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003178 struct btrfs_disk_key disk_key;
3179
3180 btrfs_node_key(parent, &disk_key, 0);
3181 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003182 if (wret)
3183 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003184 }
Chris Masond6025572007-03-30 14:27:56 -04003185 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003186 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003187}
3188
Chris Mason74123bd2007-02-02 11:05:29 -05003189/*
Chris Mason323ac952008-10-01 19:05:46 -04003190 * a helper function to delete the leaf pointed to by path->slots[1] and
3191 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3192 * already know it, it is faster to have them pass it down than to
3193 * read it out of the node again.
3194 *
3195 * This deletes the pointer in path->nodes[1] and frees the leaf
3196 * block extent. zero is returned if it all worked out, < 0 otherwise.
3197 *
3198 * The path must have already been setup for deleting the leaf, including
3199 * all the proper balancing. path->nodes[1] must be locked.
3200 */
3201noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3202 struct btrfs_root *root,
3203 struct btrfs_path *path, u64 bytenr)
3204{
3205 int ret;
3206 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3207
3208 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3209 if (ret)
3210 return ret;
3211
3212 ret = btrfs_free_extent(trans, root, bytenr,
3213 btrfs_level_size(root, 0),
3214 path->nodes[1]->start,
3215 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003216 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003217 return ret;
3218}
3219/*
Chris Mason74123bd2007-02-02 11:05:29 -05003220 * delete the item at the leaf level in path. If that empties
3221 * the leaf, remove it from the tree
3222 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003223int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3224 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003225{
Chris Mason5f39d392007-10-15 16:14:19 -04003226 struct extent_buffer *leaf;
3227 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003228 int last_off;
3229 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003230 int ret = 0;
3231 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003232 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003233 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003234
Chris Mason5f39d392007-10-15 16:14:19 -04003235 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003236 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3237
3238 for (i = 0; i < nr; i++)
3239 dsize += btrfs_item_size_nr(leaf, slot + i);
3240
Chris Mason5f39d392007-10-15 16:14:19 -04003241 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003242
Chris Mason85e21ba2008-01-29 15:11:36 -05003243 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003244 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003245
3246 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003247 data_end + dsize,
3248 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003249 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003250
Chris Mason85e21ba2008-01-29 15:11:36 -05003251 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003252 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003253
Chris Mason5f39d392007-10-15 16:14:19 -04003254 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003255 if (!leaf->map_token) {
3256 map_extent_buffer(leaf, (unsigned long)item,
3257 sizeof(struct btrfs_item),
3258 &leaf->map_token, &leaf->kaddr,
3259 &leaf->map_start, &leaf->map_len,
3260 KM_USER1);
3261 }
Chris Mason5f39d392007-10-15 16:14:19 -04003262 ioff = btrfs_item_offset(leaf, item);
3263 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003264 }
Chris Masondb945352007-10-15 16:15:53 -04003265
3266 if (leaf->map_token) {
3267 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3268 leaf->map_token = NULL;
3269 }
3270
Chris Mason5f39d392007-10-15 16:14:19 -04003271 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003272 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003273 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003274 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003275 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003276 btrfs_set_header_nritems(leaf, nritems - nr);
3277 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003278
Chris Mason74123bd2007-02-02 11:05:29 -05003279 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003280 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003281 if (leaf == root->node) {
3282 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003283 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003284 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3285 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003286 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003287 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003288 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003289 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003290 struct btrfs_disk_key disk_key;
3291
3292 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003293 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003294 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003295 if (wret)
3296 ret = wret;
3297 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003298
Chris Mason74123bd2007-02-02 11:05:29 -05003299 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003300 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003301 /* push_leaf_left fixes the path.
3302 * make sure the path still points to our leaf
3303 * for possible call to del_ptr below
3304 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003305 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003306 extent_buffer_get(leaf);
3307
Chris Mason85e21ba2008-01-29 15:11:36 -05003308 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003309 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003310 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003311
3312 if (path->nodes[0] == leaf &&
3313 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003314 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003315 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003316 ret = wret;
3317 }
Chris Mason5f39d392007-10-15 16:14:19 -04003318
3319 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003320 path->slots[1] = slot;
3321 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3322 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003323 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003324 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003325 /* if we're still in the path, make sure
3326 * we're dirty. Otherwise, one of the
3327 * push_leaf functions must have already
3328 * dirtied this buffer
3329 */
3330 if (path->nodes[0] == leaf)
3331 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003332 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003333 }
Chris Masond5719762007-03-23 10:01:08 -04003334 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003335 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003336 }
3337 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003338 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003339}
3340
Chris Mason97571fd2007-02-24 13:39:08 -05003341/*
Chris Mason925baed2008-06-25 16:01:30 -04003342 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003343 * returns 0 if it found something or 1 if there are no lesser leaves.
3344 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003345 *
3346 * This may release the path, and so you may lose any locks held at the
3347 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003348 */
3349int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3350{
Chris Mason925baed2008-06-25 16:01:30 -04003351 struct btrfs_key key;
3352 struct btrfs_disk_key found_key;
3353 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003354
Chris Mason925baed2008-06-25 16:01:30 -04003355 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003356
Chris Mason925baed2008-06-25 16:01:30 -04003357 if (key.offset > 0)
3358 key.offset--;
3359 else if (key.type > 0)
3360 key.type--;
3361 else if (key.objectid > 0)
3362 key.objectid--;
3363 else
3364 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003365
Chris Mason925baed2008-06-25 16:01:30 -04003366 btrfs_release_path(root, path);
3367 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3368 if (ret < 0)
3369 return ret;
3370 btrfs_item_key(path->nodes[0], &found_key, 0);
3371 ret = comp_keys(&found_key, &key);
3372 if (ret < 0)
3373 return 0;
3374 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003375}
3376
Chris Mason3f157a22008-06-25 16:01:31 -04003377/*
3378 * A helper function to walk down the tree starting at min_key, and looking
3379 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003380 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003381 *
3382 * This does not cow, but it does stuff the starting key it finds back
3383 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3384 * key and get a writable path.
3385 *
3386 * This does lock as it descends, and path->keep_locks should be set
3387 * to 1 by the caller.
3388 *
3389 * This honors path->lowest_level to prevent descent past a given level
3390 * of the tree.
3391 *
Chris Masond352ac62008-09-29 15:18:18 -04003392 * min_trans indicates the oldest transaction that you are interested
3393 * in walking through. Any nodes or leaves older than min_trans are
3394 * skipped over (without reading them).
3395 *
Chris Mason3f157a22008-06-25 16:01:31 -04003396 * returns zero if something useful was found, < 0 on error and 1 if there
3397 * was nothing in the tree that matched the search criteria.
3398 */
3399int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003400 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003401 struct btrfs_path *path, int cache_only,
3402 u64 min_trans)
3403{
3404 struct extent_buffer *cur;
3405 struct btrfs_key found_key;
3406 int slot;
Yan96524802008-07-24 12:19:49 -04003407 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003408 u32 nritems;
3409 int level;
3410 int ret = 1;
3411
3412again:
3413 cur = btrfs_lock_root_node(root);
3414 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003415 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003416 path->nodes[level] = cur;
3417 path->locks[level] = 1;
3418
3419 if (btrfs_header_generation(cur) < min_trans) {
3420 ret = 1;
3421 goto out;
3422 }
3423 while(1) {
3424 nritems = btrfs_header_nritems(cur);
3425 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003426 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003427
Chris Mason323ac952008-10-01 19:05:46 -04003428 /* at the lowest level, we're done, setup the path and exit */
3429 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003430 if (slot >= nritems)
3431 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003432 ret = 0;
3433 path->slots[level] = slot;
3434 btrfs_item_key_to_cpu(cur, &found_key, slot);
3435 goto out;
3436 }
Yan96524802008-07-24 12:19:49 -04003437 if (sret && slot > 0)
3438 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003439 /*
3440 * check this node pointer against the cache_only and
3441 * min_trans parameters. If it isn't in cache or is too
3442 * old, skip to the next one.
3443 */
3444 while(slot < nritems) {
3445 u64 blockptr;
3446 u64 gen;
3447 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003448 struct btrfs_disk_key disk_key;
3449
Chris Mason3f157a22008-06-25 16:01:31 -04003450 blockptr = btrfs_node_blockptr(cur, slot);
3451 gen = btrfs_node_ptr_generation(cur, slot);
3452 if (gen < min_trans) {
3453 slot++;
3454 continue;
3455 }
3456 if (!cache_only)
3457 break;
3458
Chris Masone02119d2008-09-05 16:13:11 -04003459 if (max_key) {
3460 btrfs_node_key(cur, &disk_key, slot);
3461 if (comp_keys(&disk_key, max_key) >= 0) {
3462 ret = 1;
3463 goto out;
3464 }
3465 }
3466
Chris Mason3f157a22008-06-25 16:01:31 -04003467 tmp = btrfs_find_tree_block(root, blockptr,
3468 btrfs_level_size(root, level - 1));
3469
3470 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3471 free_extent_buffer(tmp);
3472 break;
3473 }
3474 if (tmp)
3475 free_extent_buffer(tmp);
3476 slot++;
3477 }
Chris Masone02119d2008-09-05 16:13:11 -04003478find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003479 /*
3480 * we didn't find a candidate key in this node, walk forward
3481 * and find another one
3482 */
3483 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003484 path->slots[level] = slot;
3485 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003486 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003487 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003488 btrfs_release_path(root, path);
3489 goto again;
3490 } else {
3491 goto out;
3492 }
3493 }
3494 /* save our key for returning back */
3495 btrfs_node_key_to_cpu(cur, &found_key, slot);
3496 path->slots[level] = slot;
3497 if (level == path->lowest_level) {
3498 ret = 0;
3499 unlock_up(path, level, 1);
3500 goto out;
3501 }
3502 cur = read_node_slot(root, cur, slot);
3503
3504 btrfs_tree_lock(cur);
3505 path->locks[level - 1] = 1;
3506 path->nodes[level - 1] = cur;
3507 unlock_up(path, level, 1);
3508 }
3509out:
3510 if (ret == 0)
3511 memcpy(min_key, &found_key, sizeof(found_key));
3512 return ret;
3513}
3514
3515/*
3516 * this is similar to btrfs_next_leaf, but does not try to preserve
3517 * and fixup the path. It looks for and returns the next key in the
3518 * tree based on the current path and the cache_only and min_trans
3519 * parameters.
3520 *
3521 * 0 is returned if another key is found, < 0 if there are any errors
3522 * and 1 is returned if there are no higher keys in the tree
3523 *
3524 * path->keep_locks should be set to 1 on the search made before
3525 * calling this function.
3526 */
Chris Masone7a84562008-06-25 16:01:31 -04003527int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003528 struct btrfs_key *key, int lowest_level,
3529 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003530{
3531 int level = lowest_level;
3532 int slot;
3533 struct extent_buffer *c;
3534
3535 while(level < BTRFS_MAX_LEVEL) {
3536 if (!path->nodes[level])
3537 return 1;
3538
3539 slot = path->slots[level] + 1;
3540 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003541next:
Chris Masone7a84562008-06-25 16:01:31 -04003542 if (slot >= btrfs_header_nritems(c)) {
3543 level++;
3544 if (level == BTRFS_MAX_LEVEL) {
3545 return 1;
3546 }
3547 continue;
3548 }
3549 if (level == 0)
3550 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003551 else {
3552 u64 blockptr = btrfs_node_blockptr(c, slot);
3553 u64 gen = btrfs_node_ptr_generation(c, slot);
3554
3555 if (cache_only) {
3556 struct extent_buffer *cur;
3557 cur = btrfs_find_tree_block(root, blockptr,
3558 btrfs_level_size(root, level - 1));
3559 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3560 slot++;
3561 if (cur)
3562 free_extent_buffer(cur);
3563 goto next;
3564 }
3565 free_extent_buffer(cur);
3566 }
3567 if (gen < min_trans) {
3568 slot++;
3569 goto next;
3570 }
Chris Masone7a84562008-06-25 16:01:31 -04003571 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003572 }
Chris Masone7a84562008-06-25 16:01:31 -04003573 return 0;
3574 }
3575 return 1;
3576}
3577
Chris Mason7bb86312007-12-11 09:25:06 -05003578/*
Chris Mason925baed2008-06-25 16:01:30 -04003579 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003580 * returns 0 if it found something or 1 if there are no greater leaves.
3581 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003582 */
Chris Mason234b63a2007-03-13 10:46:10 -04003583int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003584{
3585 int slot;
3586 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003587 struct extent_buffer *c;
3588 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003589 struct btrfs_key key;
3590 u32 nritems;
3591 int ret;
3592
3593 nritems = btrfs_header_nritems(path->nodes[0]);
3594 if (nritems == 0) {
3595 return 1;
3596 }
3597
3598 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3599
Chris Mason925baed2008-06-25 16:01:30 -04003600 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003601 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003602 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3603 path->keep_locks = 0;
3604
3605 if (ret < 0)
3606 return ret;
3607
Chris Masona2135012008-06-25 16:01:30 -04003608 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003609 /*
3610 * by releasing the path above we dropped all our locks. A balance
3611 * could have added more items next to the key that used to be
3612 * at the very end of the block. So, check again here and
3613 * advance the path if there are now more items available.
3614 */
Chris Masona2135012008-06-25 16:01:30 -04003615 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003616 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003617 goto done;
3618 }
Chris Masond97e63b2007-02-20 16:40:44 -05003619
Chris Mason234b63a2007-03-13 10:46:10 -04003620 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003621 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003622 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003623
Chris Masond97e63b2007-02-20 16:40:44 -05003624 slot = path->slots[level] + 1;
3625 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003626 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003627 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003628 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003629 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003630 }
Chris Masond97e63b2007-02-20 16:40:44 -05003631 continue;
3632 }
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Chris Mason925baed2008-06-25 16:01:30 -04003634 if (next) {
3635 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003636 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003637 }
Chris Mason5f39d392007-10-15 16:14:19 -04003638
Chris Mason0bd40a72008-07-17 12:54:43 -04003639 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3640 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003641 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003642
Chris Masonca7a79a2008-05-12 12:59:19 -04003643 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003644 if (!path->skip_locking) {
3645 WARN_ON(!btrfs_tree_locked(c));
3646 btrfs_tree_lock(next);
3647 }
Chris Masond97e63b2007-02-20 16:40:44 -05003648 break;
3649 }
3650 path->slots[level] = slot;
3651 while(1) {
3652 level--;
3653 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003654 if (path->locks[level])
3655 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003656 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003657 path->nodes[level] = next;
3658 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003659 if (!path->skip_locking)
3660 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003661 if (!level)
3662 break;
Chris Mason925baed2008-06-25 16:01:30 -04003663 if (level == 1 && path->locks[1] && path->reada)
3664 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003665 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003666 if (!path->skip_locking) {
3667 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3668 btrfs_tree_lock(next);
3669 }
Chris Masond97e63b2007-02-20 16:40:44 -05003670 }
Chris Mason925baed2008-06-25 16:01:30 -04003671done:
3672 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003673 return 0;
3674}
Chris Mason0b86a832008-03-24 15:01:56 -04003675
Chris Mason3f157a22008-06-25 16:01:31 -04003676/*
3677 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3678 * searching until it gets past min_objectid or finds an item of 'type'
3679 *
3680 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3681 */
Chris Mason0b86a832008-03-24 15:01:56 -04003682int btrfs_previous_item(struct btrfs_root *root,
3683 struct btrfs_path *path, u64 min_objectid,
3684 int type)
3685{
3686 struct btrfs_key found_key;
3687 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003688 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003689 int ret;
3690
3691 while(1) {
3692 if (path->slots[0] == 0) {
3693 ret = btrfs_prev_leaf(root, path);
3694 if (ret != 0)
3695 return ret;
3696 } else {
3697 path->slots[0]--;
3698 }
3699 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003700 nritems = btrfs_header_nritems(leaf);
3701 if (nritems == 0)
3702 return 1;
3703 if (path->slots[0] == nritems)
3704 path->slots[0]--;
3705
Chris Mason0b86a832008-03-24 15:01:56 -04003706 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3707 if (found_key.type == type)
3708 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003709 if (found_key.objectid < min_objectid)
3710 break;
3711 if (found_key.objectid == min_objectid &&
3712 found_key.type < type)
3713 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003714 }
3715 return 1;
3716}