blob: 73899d0f9d8f1287212d7636036506b1d79ec8d9 [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);
Chris Mason333db942008-06-25 16:01:30 -04001390 WARN_ON(cow && root == root->fs_info->extent_root &&
Chris Mason925baed2008-06-25 16:01:30 -04001391 !mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason925baed2008-06-25 16:01:30 -04001392 if (ins_len < 0)
1393 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001394
1395 prealloc_block.objectid = 0;
1396
Chris Masonbb803952007-03-01 12:04:21 -05001397again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001398 if (p->skip_locking)
1399 b = btrfs_root_node(root);
1400 else
1401 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001402
Chris Masoneb60cea2007-02-02 09:18:22 -05001403 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001404 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001405
1406 /*
1407 * setup the path here so we can release it under lock
1408 * contention with the cow code
1409 */
1410 p->nodes[level] = b;
1411 if (!p->skip_locking)
1412 p->locks[level] = 1;
1413
Chris Mason02217ed2007-03-02 16:08:05 -05001414 if (cow) {
1415 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001416
1417 /* is a cow on this block not required */
1418 spin_lock(&root->fs_info->hash_lock);
1419 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001420 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001421 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1422 spin_unlock(&root->fs_info->hash_lock);
1423 goto cow_done;
1424 }
1425 spin_unlock(&root->fs_info->hash_lock);
1426
1427 /* ok, we have to cow, is our old prealloc the right
1428 * size?
1429 */
1430 if (prealloc_block.objectid &&
1431 prealloc_block.offset != b->len) {
1432 btrfs_free_reserved_extent(root,
1433 prealloc_block.objectid,
1434 prealloc_block.offset);
1435 prealloc_block.objectid = 0;
1436 }
1437
1438 /*
1439 * for higher level blocks, try not to allocate blocks
1440 * with the block and the parent locks held.
1441 */
1442 if (level > 1 && !prealloc_block.objectid &&
1443 btrfs_path_lock_waiting(p, level)) {
1444 u32 size = b->len;
1445 u64 hint = b->start;
1446
1447 btrfs_release_path(root, p);
1448 ret = btrfs_reserve_extent(trans, root,
1449 size, size, 0,
1450 hint, (u64)-1,
1451 &prealloc_block, 0);
1452 BUG_ON(ret);
1453 goto again;
1454 }
1455
Chris Masone20d96d2007-03-22 12:13:20 -04001456 wret = btrfs_cow_block(trans, root, b,
1457 p->nodes[level + 1],
1458 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001459 &b, prealloc_block.objectid);
1460 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001461 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001462 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001463 ret = wret;
1464 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001465 }
Chris Mason02217ed2007-03-02 16:08:05 -05001466 }
Chris Mason65b51a02008-08-01 15:11:20 -04001467cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001468 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001469 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001470 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001471 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001472
Chris Masoneb60cea2007-02-02 09:18:22 -05001473 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001474 if (!p->skip_locking)
1475 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001476
Chris Mason123abc82007-03-14 14:14:43 -04001477 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001478 if (ret) {
1479 ret = -1;
1480 goto done;
1481 }
Chris Mason925baed2008-06-25 16:01:30 -04001482
Chris Mason5f39d392007-10-15 16:14:19 -04001483 ret = bin_search(b, key, level, &slot);
1484 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001485 if (ret && slot > 0)
1486 slot -= 1;
1487 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001488 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001489 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001490 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001491 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001492 if (sret) {
1493 ret = sret;
1494 goto done;
1495 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001496 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001497 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001498 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001499 int sret = balance_level(trans, root, p,
1500 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001501 if (sret) {
1502 ret = sret;
1503 goto done;
1504 }
Chris Masonbb803952007-03-01 12:04:21 -05001505 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001506 if (!b) {
1507 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001508 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001509 }
Chris Masonbb803952007-03-01 12:04:21 -05001510 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001511 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001512 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001513 unlock_up(p, level, lowest_unlock);
1514
Chris Mason9f3a7422007-08-07 15:52:19 -04001515 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001516 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001517 ret = 0;
1518 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001519 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001520
Chris Mason594a24e2008-06-25 16:01:30 -04001521 blocknr = btrfs_node_blockptr(b, slot);
1522 gen = btrfs_node_ptr_generation(b, slot);
1523 blocksize = btrfs_level_size(root, level - 1);
1524
1525 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1526 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001527 b = tmp;
1528 } else {
1529 /*
1530 * reduce lock contention at high levels
1531 * of the btree by dropping locks before
1532 * we read.
1533 */
1534 if (level > 1) {
1535 btrfs_release_path(NULL, p);
1536 if (tmp)
1537 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001538 if (should_reada)
1539 reada_for_search(root, p,
1540 level, slot,
1541 key->objectid);
1542
Chris Mason594a24e2008-06-25 16:01:30 -04001543 tmp = read_tree_block(root, blocknr,
1544 blocksize, gen);
1545 if (tmp)
1546 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001547 goto again;
1548 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001549 if (tmp)
1550 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001551 if (should_reada)
1552 reada_for_search(root, p,
1553 level, slot,
1554 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001555 b = read_node_slot(root, b, slot);
1556 }
1557 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001558 if (!p->skip_locking)
1559 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001560 } else {
1561 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001562 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001563 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001564 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001565 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001566 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001567 if (sret) {
1568 ret = sret;
1569 goto done;
1570 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001571 }
Chris Mason925baed2008-06-25 16:01:30 -04001572 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001573 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001574 }
1575 }
Chris Mason65b51a02008-08-01 15:11:20 -04001576 ret = 1;
1577done:
1578 if (prealloc_block.objectid) {
1579 btrfs_free_reserved_extent(root,
1580 prealloc_block.objectid,
1581 prealloc_block.offset);
1582 }
1583
1584 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001585}
1586
Zheng Yan1a40e232008-09-26 10:09:34 -04001587int btrfs_merge_path(struct btrfs_trans_handle *trans,
1588 struct btrfs_root *root,
1589 struct btrfs_key *node_keys,
1590 u64 *nodes, int lowest_level)
1591{
1592 struct extent_buffer *eb;
1593 struct extent_buffer *parent;
1594 struct btrfs_key key;
1595 u64 bytenr;
1596 u64 generation;
1597 u32 blocksize;
1598 int level;
1599 int slot;
1600 int key_match;
1601 int ret;
1602
1603 eb = btrfs_lock_root_node(root);
1604 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1605 BUG_ON(ret);
1606
1607 parent = eb;
1608 while (1) {
1609 level = btrfs_header_level(parent);
1610 if (level == 0 || level <= lowest_level)
1611 break;
1612
1613 ret = bin_search(parent, &node_keys[lowest_level], level,
1614 &slot);
1615 if (ret && slot > 0)
1616 slot--;
1617
1618 bytenr = btrfs_node_blockptr(parent, slot);
1619 if (nodes[level - 1] == bytenr)
1620 break;
1621
1622 blocksize = btrfs_level_size(root, level - 1);
1623 generation = btrfs_node_ptr_generation(parent, slot);
1624 btrfs_node_key_to_cpu(eb, &key, slot);
1625 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1626
Yan Zhengf82d02d2008-10-29 14:49:05 -04001627 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001628 eb = read_tree_block(root, bytenr, blocksize,
1629 generation);
1630 btrfs_tree_lock(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001631 }
1632
1633 /*
1634 * if node keys match and node pointer hasn't been modified
1635 * in the running transaction, we can merge the path. for
1636 * blocks owened by reloc trees, the node pointer check is
1637 * skipped, this is because these blocks are fully controlled
1638 * by the space balance code, no one else can modify them.
1639 */
1640 if (!nodes[level - 1] || !key_match ||
1641 (generation == trans->transid &&
1642 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1643 if (level == 1 || level == lowest_level + 1) {
1644 if (generation == trans->transid) {
1645 btrfs_tree_unlock(eb);
1646 free_extent_buffer(eb);
1647 }
1648 break;
1649 }
1650
1651 if (generation != trans->transid) {
1652 eb = read_tree_block(root, bytenr, blocksize,
1653 generation);
1654 btrfs_tree_lock(eb);
1655 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001656
1657 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1658 &eb, 0);
1659 BUG_ON(ret);
1660
Yan Zhengf82d02d2008-10-29 14:49:05 -04001661 if (root->root_key.objectid ==
1662 BTRFS_TREE_RELOC_OBJECTID) {
1663 if (!nodes[level - 1]) {
1664 nodes[level - 1] = eb->start;
1665 memcpy(&node_keys[level - 1], &key,
1666 sizeof(node_keys[0]));
1667 } else {
1668 WARN_ON(1);
1669 }
1670 }
1671
Zheng Yan1a40e232008-09-26 10:09:34 -04001672 btrfs_tree_unlock(parent);
1673 free_extent_buffer(parent);
1674 parent = eb;
1675 continue;
1676 }
1677
Zheng Yan1a40e232008-09-26 10:09:34 -04001678 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1679 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1680 btrfs_mark_buffer_dirty(parent);
1681
1682 ret = btrfs_inc_extent_ref(trans, root,
1683 nodes[level - 1],
1684 blocksize, parent->start,
1685 btrfs_header_owner(parent),
1686 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001687 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001688 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001689
1690 /*
1691 * If the block was created in the running transaction,
1692 * it's possible this is the last reference to it, so we
1693 * should drop the subtree.
1694 */
1695 if (generation == trans->transid) {
1696 ret = btrfs_drop_subtree(trans, root, eb, parent);
1697 BUG_ON(ret);
1698 btrfs_tree_unlock(eb);
1699 free_extent_buffer(eb);
1700 } else {
1701 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001702 blocksize, parent->start,
1703 btrfs_header_owner(parent),
1704 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001705 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001706 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001707 }
1708 break;
1709 }
1710 btrfs_tree_unlock(parent);
1711 free_extent_buffer(parent);
1712 return 0;
1713}
1714
Chris Mason74123bd2007-02-02 11:05:29 -05001715/*
1716 * adjust the pointers going up the tree, starting at level
1717 * making sure the right key of each node is points to 'key'.
1718 * This is used after shifting pointers to the left, so it stops
1719 * fixing up pointers when a given leaf/node is not in slot 0 of the
1720 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001721 *
1722 * If this fails to write a tree block, it returns -1, but continues
1723 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001724 */
Chris Mason5f39d392007-10-15 16:14:19 -04001725static int fixup_low_keys(struct btrfs_trans_handle *trans,
1726 struct btrfs_root *root, struct btrfs_path *path,
1727 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001728{
1729 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001730 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001731 struct extent_buffer *t;
1732
Chris Mason234b63a2007-03-13 10:46:10 -04001733 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001734 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001735 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001736 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001737 t = path->nodes[i];
1738 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001739 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001740 if (tslot != 0)
1741 break;
1742 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001743 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001744}
1745
Chris Mason74123bd2007-02-02 11:05:29 -05001746/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001747 * update item key.
1748 *
1749 * This function isn't completely safe. It's the caller's responsibility
1750 * that the new key won't break the order
1751 */
1752int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1753 struct btrfs_root *root, struct btrfs_path *path,
1754 struct btrfs_key *new_key)
1755{
1756 struct btrfs_disk_key disk_key;
1757 struct extent_buffer *eb;
1758 int slot;
1759
1760 eb = path->nodes[0];
1761 slot = path->slots[0];
1762 if (slot > 0) {
1763 btrfs_item_key(eb, &disk_key, slot - 1);
1764 if (comp_keys(&disk_key, new_key) >= 0)
1765 return -1;
1766 }
1767 if (slot < btrfs_header_nritems(eb) - 1) {
1768 btrfs_item_key(eb, &disk_key, slot + 1);
1769 if (comp_keys(&disk_key, new_key) <= 0)
1770 return -1;
1771 }
1772
1773 btrfs_cpu_key_to_disk(&disk_key, new_key);
1774 btrfs_set_item_key(eb, &disk_key, slot);
1775 btrfs_mark_buffer_dirty(eb);
1776 if (slot == 0)
1777 fixup_low_keys(trans, root, path, &disk_key, 1);
1778 return 0;
1779}
1780
1781/*
Chris Mason74123bd2007-02-02 11:05:29 -05001782 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001783 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001784 *
1785 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1786 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001787 */
Chris Mason98ed5172008-01-03 10:01:48 -05001788static int push_node_left(struct btrfs_trans_handle *trans,
1789 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001790 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001791{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001792 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001793 int src_nritems;
1794 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001795 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001796
Chris Mason5f39d392007-10-15 16:14:19 -04001797 src_nritems = btrfs_header_nritems(src);
1798 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001799 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001800 WARN_ON(btrfs_header_generation(src) != trans->transid);
1801 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001802
Chris Masonbce4eae2008-04-24 14:42:46 -04001803 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001804 return 1;
1805
Chris Masoneb60cea2007-02-02 09:18:22 -05001806 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001807 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001808 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001809
Chris Masonbce4eae2008-04-24 14:42:46 -04001810 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001811 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001812 if (push_items < src_nritems) {
1813 /* leave at least 8 pointers in the node if
1814 * we aren't going to empty it
1815 */
1816 if (src_nritems - push_items < 8) {
1817 if (push_items <= 8)
1818 return 1;
1819 push_items -= 8;
1820 }
1821 }
1822 } else
1823 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001824
Chris Mason5f39d392007-10-15 16:14:19 -04001825 copy_extent_buffer(dst, src,
1826 btrfs_node_key_ptr_offset(dst_nritems),
1827 btrfs_node_key_ptr_offset(0),
1828 push_items * sizeof(struct btrfs_key_ptr));
1829
Chris Masonbb803952007-03-01 12:04:21 -05001830 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001831 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1832 btrfs_node_key_ptr_offset(push_items),
1833 (src_nritems - push_items) *
1834 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001835 }
Chris Mason5f39d392007-10-15 16:14:19 -04001836 btrfs_set_header_nritems(src, src_nritems - push_items);
1837 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1838 btrfs_mark_buffer_dirty(src);
1839 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001840
1841 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1842 BUG_ON(ret);
1843
Chris Masonbb803952007-03-01 12:04:21 -05001844 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001845}
1846
Chris Mason97571fd2007-02-24 13:39:08 -05001847/*
Chris Mason79f95c82007-03-01 15:16:26 -05001848 * try to push data from one node into the next node right in the
1849 * tree.
1850 *
1851 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1852 * error, and > 0 if there was no room in the right hand block.
1853 *
1854 * this will only push up to 1/2 the contents of the left node over
1855 */
Chris Mason5f39d392007-10-15 16:14:19 -04001856static int balance_node_right(struct btrfs_trans_handle *trans,
1857 struct btrfs_root *root,
1858 struct extent_buffer *dst,
1859 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001860{
Chris Mason79f95c82007-03-01 15:16:26 -05001861 int push_items = 0;
1862 int max_push;
1863 int src_nritems;
1864 int dst_nritems;
1865 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001866
Chris Mason7bb86312007-12-11 09:25:06 -05001867 WARN_ON(btrfs_header_generation(src) != trans->transid);
1868 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1869
Chris Mason5f39d392007-10-15 16:14:19 -04001870 src_nritems = btrfs_header_nritems(src);
1871 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001872 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001873 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001874 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001875 }
1876
1877 if (src_nritems < 4) {
1878 return 1;
1879 }
Chris Mason79f95c82007-03-01 15:16:26 -05001880
1881 max_push = src_nritems / 2 + 1;
1882 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001883 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001884 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001885 }
Yan252c38f2007-08-29 09:11:44 -04001886
Chris Mason79f95c82007-03-01 15:16:26 -05001887 if (max_push < push_items)
1888 push_items = max_push;
1889
Chris Mason5f39d392007-10-15 16:14:19 -04001890 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1891 btrfs_node_key_ptr_offset(0),
1892 (dst_nritems) *
1893 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001894
Chris Mason5f39d392007-10-15 16:14:19 -04001895 copy_extent_buffer(dst, src,
1896 btrfs_node_key_ptr_offset(0),
1897 btrfs_node_key_ptr_offset(src_nritems - push_items),
1898 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001899
Chris Mason5f39d392007-10-15 16:14:19 -04001900 btrfs_set_header_nritems(src, src_nritems - push_items);
1901 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001902
Chris Mason5f39d392007-10-15 16:14:19 -04001903 btrfs_mark_buffer_dirty(src);
1904 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001905
1906 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1907 BUG_ON(ret);
1908
Chris Mason79f95c82007-03-01 15:16:26 -05001909 return ret;
1910}
1911
1912/*
Chris Mason97571fd2007-02-24 13:39:08 -05001913 * helper function to insert a new root level in the tree.
1914 * A new node is allocated, and a single item is inserted to
1915 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001916 *
1917 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001918 */
Chris Mason98ed5172008-01-03 10:01:48 -05001919static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001920 struct btrfs_root *root,
1921 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001922{
Chris Mason7bb86312007-12-11 09:25:06 -05001923 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001924 struct extent_buffer *lower;
1925 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001926 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001927 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001928 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001929
1930 BUG_ON(path->nodes[level]);
1931 BUG_ON(path->nodes[level-1] != root->node);
1932
Chris Mason7bb86312007-12-11 09:25:06 -05001933 lower = path->nodes[level-1];
1934 if (level == 1)
1935 btrfs_item_key(lower, &lower_key, 0);
1936 else
1937 btrfs_node_key(lower, &lower_key, 0);
1938
Zheng Yan31840ae2008-09-23 13:14:14 -04001939 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1940 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001941 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001942 if (IS_ERR(c))
1943 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001944
Chris Mason5f39d392007-10-15 16:14:19 -04001945 memset_extent_buffer(c, 0, 0, root->nodesize);
1946 btrfs_set_header_nritems(c, 1);
1947 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001948 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001949 btrfs_set_header_generation(c, trans->transid);
1950 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001951
Chris Mason5f39d392007-10-15 16:14:19 -04001952 write_extent_buffer(c, root->fs_info->fsid,
1953 (unsigned long)btrfs_header_fsid(c),
1954 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001955
1956 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1957 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1958 BTRFS_UUID_SIZE);
1959
Chris Mason5f39d392007-10-15 16:14:19 -04001960 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001961 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001962 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001963 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001964
1965 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001966
1967 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001968
Chris Mason925baed2008-06-25 16:01:30 -04001969 spin_lock(&root->node_lock);
1970 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001971 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001972 spin_unlock(&root->node_lock);
1973
Zheng Yan31840ae2008-09-23 13:14:14 -04001974 ret = btrfs_update_extent_ref(trans, root, lower->start,
1975 lower->start, c->start,
1976 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001977 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001978 BUG_ON(ret);
1979
Chris Mason925baed2008-06-25 16:01:30 -04001980 /* the super has an extra ref to root->node */
1981 free_extent_buffer(old);
1982
Chris Mason0b86a832008-03-24 15:01:56 -04001983 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001984 extent_buffer_get(c);
1985 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04001986 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05001987 path->slots[level] = 0;
1988 return 0;
1989}
1990
Chris Mason74123bd2007-02-02 11:05:29 -05001991/*
1992 * worker function to insert a single pointer in a node.
1993 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05001994 *
Chris Mason74123bd2007-02-02 11:05:29 -05001995 * slot and level indicate where you want the key to go, and
1996 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001997 *
1998 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05001999 */
Chris Masone089f052007-03-16 16:20:31 -04002000static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2001 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002002 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002003{
Chris Mason5f39d392007-10-15 16:14:19 -04002004 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002005 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002006
2007 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002008 lower = path->nodes[level];
2009 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002010 if (slot > nritems)
2011 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002012 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002013 BUG();
2014 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002015 memmove_extent_buffer(lower,
2016 btrfs_node_key_ptr_offset(slot + 1),
2017 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002018 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002019 }
Chris Mason5f39d392007-10-15 16:14:19 -04002020 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002021 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002022 WARN_ON(trans->transid == 0);
2023 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002024 btrfs_set_header_nritems(lower, nritems + 1);
2025 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002026 return 0;
2027}
2028
Chris Mason97571fd2007-02-24 13:39:08 -05002029/*
2030 * split the node at the specified level in path in two.
2031 * The path is corrected to point to the appropriate node after the split
2032 *
2033 * Before splitting this tries to make some room in the node by pushing
2034 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002035 *
2036 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002037 */
Chris Masone02119d2008-09-05 16:13:11 -04002038static noinline int split_node(struct btrfs_trans_handle *trans,
2039 struct btrfs_root *root,
2040 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002041{
Chris Mason5f39d392007-10-15 16:14:19 -04002042 struct extent_buffer *c;
2043 struct extent_buffer *split;
2044 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002045 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002046 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002047 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002048 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002049
Chris Mason5f39d392007-10-15 16:14:19 -04002050 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002051 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002052 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002053 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002054 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002055 if (ret)
2056 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002057 } else {
2058 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002059 c = path->nodes[level];
2060 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002061 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002062 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002063 if (ret < 0)
2064 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002065 }
Chris Masone66f7092007-04-20 13:16:02 -04002066
Chris Mason5f39d392007-10-15 16:14:19 -04002067 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002068
Chris Mason925baed2008-06-25 16:01:30 -04002069 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002070 path->nodes[level + 1]->start,
2071 root->root_key.objectid,
2072 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002073 if (IS_ERR(split))
2074 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002075
Chris Mason5f39d392007-10-15 16:14:19 -04002076 btrfs_set_header_flags(split, btrfs_header_flags(c));
2077 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002078 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002079 btrfs_set_header_generation(split, trans->transid);
2080 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002081 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002082 write_extent_buffer(split, root->fs_info->fsid,
2083 (unsigned long)btrfs_header_fsid(split),
2084 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002085 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2086 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2087 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002088
Chris Mason7518a232007-03-12 12:01:18 -04002089 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002090
2091 copy_extent_buffer(split, c,
2092 btrfs_node_key_ptr_offset(0),
2093 btrfs_node_key_ptr_offset(mid),
2094 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2095 btrfs_set_header_nritems(split, c_nritems - mid);
2096 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002097 ret = 0;
2098
Chris Mason5f39d392007-10-15 16:14:19 -04002099 btrfs_mark_buffer_dirty(c);
2100 btrfs_mark_buffer_dirty(split);
2101
2102 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002103 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002104 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002105 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002106 if (wret)
2107 ret = wret;
2108
Zheng Yan31840ae2008-09-23 13:14:14 -04002109 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2110 BUG_ON(ret);
2111
Chris Mason5de08d72007-02-24 06:24:44 -05002112 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002113 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002114 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002115 free_extent_buffer(c);
2116 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002117 path->slots[level + 1] += 1;
2118 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002119 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002120 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002121 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002122 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002123}
2124
Chris Mason74123bd2007-02-02 11:05:29 -05002125/*
2126 * how many bytes are required to store the items in a leaf. start
2127 * and nr indicate which items in the leaf to check. This totals up the
2128 * space used both by the item structs and the item data
2129 */
Chris Mason5f39d392007-10-15 16:14:19 -04002130static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002131{
2132 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002133 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002134 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002135
2136 if (!nr)
2137 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002138 data_len = btrfs_item_end_nr(l, start);
2139 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002140 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002141 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002142 return data_len;
2143}
2144
Chris Mason74123bd2007-02-02 11:05:29 -05002145/*
Chris Masond4dbff92007-04-04 14:08:15 -04002146 * The space between the end of the leaf items and
2147 * the start of the leaf data. IOW, how much room
2148 * the leaf has left for both items and data
2149 */
Chris Masone02119d2008-09-05 16:13:11 -04002150int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2151 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002152{
Chris Mason5f39d392007-10-15 16:14:19 -04002153 int nritems = btrfs_header_nritems(leaf);
2154 int ret;
2155 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2156 if (ret < 0) {
2157 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002158 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002159 leaf_space_used(leaf, 0, nritems), nritems);
2160 }
2161 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002162}
2163
2164/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002165 * push some data in the path leaf to the right, trying to free up at
2166 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002167 *
2168 * returns 1 if the push failed because the other node didn't have enough
2169 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002170 */
Chris Masone089f052007-03-16 16:20:31 -04002171static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002172 *root, struct btrfs_path *path, int data_size,
2173 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002174{
Chris Mason5f39d392007-10-15 16:14:19 -04002175 struct extent_buffer *left = path->nodes[0];
2176 struct extent_buffer *right;
2177 struct extent_buffer *upper;
2178 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002179 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002180 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002181 int free_space;
2182 int push_space = 0;
2183 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002184 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002185 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002186 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002187 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002188 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002189 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002190 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002191
2192 slot = path->slots[1];
2193 if (!path->nodes[1]) {
2194 return 1;
2195 }
2196 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002197 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002198 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002199
Chris Masona2135012008-06-25 16:01:30 -04002200 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2201
Chris Masonca7a79a2008-05-12 12:59:19 -04002202 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002203 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002204 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002205 if (free_space < data_size + sizeof(struct btrfs_item))
2206 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002207
Chris Mason5f39d392007-10-15 16:14:19 -04002208 /* cow and double check */
2209 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002210 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002211 if (ret)
2212 goto out_unlock;
2213
Chris Mason5f39d392007-10-15 16:14:19 -04002214 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002215 if (free_space < data_size + sizeof(struct btrfs_item))
2216 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002217
2218 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002219 if (left_nritems == 0)
2220 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002221
Chris Mason34a38212007-11-07 13:31:03 -05002222 if (empty)
2223 nr = 0;
2224 else
2225 nr = 1;
2226
Zheng Yan31840ae2008-09-23 13:14:14 -04002227 if (path->slots[0] >= left_nritems)
2228 push_space += data_size + sizeof(*item);
2229
Chris Mason34a38212007-11-07 13:31:03 -05002230 i = left_nritems - 1;
2231 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002232 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002233
Zheng Yan31840ae2008-09-23 13:14:14 -04002234 if (!empty && push_items > 0) {
2235 if (path->slots[0] > i)
2236 break;
2237 if (path->slots[0] == i) {
2238 int space = btrfs_leaf_free_space(root, left);
2239 if (space + push_space * 2 > free_space)
2240 break;
2241 }
2242 }
2243
Chris Mason00ec4c52007-02-24 12:47:20 -05002244 if (path->slots[0] == i)
2245 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002246
2247 if (!left->map_token) {
2248 map_extent_buffer(left, (unsigned long)item,
2249 sizeof(struct btrfs_item),
2250 &left->map_token, &left->kaddr,
2251 &left->map_start, &left->map_len,
2252 KM_USER1);
2253 }
2254
2255 this_item_size = btrfs_item_size(left, item);
2256 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002257 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002258
Chris Mason00ec4c52007-02-24 12:47:20 -05002259 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002260 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002261 if (i == 0)
2262 break;
2263 i--;
Chris Masondb945352007-10-15 16:15:53 -04002264 }
2265 if (left->map_token) {
2266 unmap_extent_buffer(left, left->map_token, KM_USER1);
2267 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002268 }
Chris Mason5f39d392007-10-15 16:14:19 -04002269
Chris Mason925baed2008-06-25 16:01:30 -04002270 if (push_items == 0)
2271 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002272
Chris Mason34a38212007-11-07 13:31:03 -05002273 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002274 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002275
Chris Mason00ec4c52007-02-24 12:47:20 -05002276 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002277 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002278
Chris Mason5f39d392007-10-15 16:14:19 -04002279 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002280 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002281
Chris Mason00ec4c52007-02-24 12:47:20 -05002282 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002283 data_end = leaf_data_end(root, right);
2284 memmove_extent_buffer(right,
2285 btrfs_leaf_data(right) + data_end - push_space,
2286 btrfs_leaf_data(right) + data_end,
2287 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2288
Chris Mason00ec4c52007-02-24 12:47:20 -05002289 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002290 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002291 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2292 btrfs_leaf_data(left) + leaf_data_end(root, left),
2293 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002294
2295 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2296 btrfs_item_nr_offset(0),
2297 right_nritems * sizeof(struct btrfs_item));
2298
Chris Mason00ec4c52007-02-24 12:47:20 -05002299 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002300 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2301 btrfs_item_nr_offset(left_nritems - push_items),
2302 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002303
2304 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002305 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002306 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002307 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002308 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002309 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002310 if (!right->map_token) {
2311 map_extent_buffer(right, (unsigned long)item,
2312 sizeof(struct btrfs_item),
2313 &right->map_token, &right->kaddr,
2314 &right->map_start, &right->map_len,
2315 KM_USER1);
2316 }
2317 push_space -= btrfs_item_size(right, item);
2318 btrfs_set_item_offset(right, item, push_space);
2319 }
2320
2321 if (right->map_token) {
2322 unmap_extent_buffer(right, right->map_token, KM_USER1);
2323 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002324 }
Chris Mason7518a232007-03-12 12:01:18 -04002325 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002326 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002327
Chris Mason34a38212007-11-07 13:31:03 -05002328 if (left_nritems)
2329 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002330 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002331
Zheng Yan31840ae2008-09-23 13:14:14 -04002332 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2333 BUG_ON(ret);
2334
Chris Mason5f39d392007-10-15 16:14:19 -04002335 btrfs_item_key(right, &disk_key, 0);
2336 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002337 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002338
Chris Mason00ec4c52007-02-24 12:47:20 -05002339 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002340 if (path->slots[0] >= left_nritems) {
2341 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002342 if (btrfs_header_nritems(path->nodes[0]) == 0)
2343 clean_tree_block(trans, root, path->nodes[0]);
2344 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002345 free_extent_buffer(path->nodes[0]);
2346 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002347 path->slots[1] += 1;
2348 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002349 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002350 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002351 }
2352 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002353
2354out_unlock:
2355 btrfs_tree_unlock(right);
2356 free_extent_buffer(right);
2357 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002358}
Chris Mason925baed2008-06-25 16:01:30 -04002359
Chris Mason00ec4c52007-02-24 12:47:20 -05002360/*
Chris Mason74123bd2007-02-02 11:05:29 -05002361 * push some data in the path leaf to the left, trying to free up at
2362 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2363 */
Chris Masone089f052007-03-16 16:20:31 -04002364static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002365 *root, struct btrfs_path *path, int data_size,
2366 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002367{
Chris Mason5f39d392007-10-15 16:14:19 -04002368 struct btrfs_disk_key disk_key;
2369 struct extent_buffer *right = path->nodes[0];
2370 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002371 int slot;
2372 int i;
2373 int free_space;
2374 int push_space = 0;
2375 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002376 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002377 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002378 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002379 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002380 int ret = 0;
2381 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002382 u32 this_item_size;
2383 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002384
2385 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002386 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002387 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002388 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002389 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002390
Chris Mason3685f792007-10-19 09:23:27 -04002391 right_nritems = btrfs_header_nritems(right);
2392 if (right_nritems == 0) {
2393 return 1;
2394 }
2395
Chris Masona2135012008-06-25 16:01:30 -04002396 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2397
Chris Masonca7a79a2008-05-12 12:59:19 -04002398 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002399 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002400 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002401 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002402 ret = 1;
2403 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002404 }
Chris Mason02217ed2007-03-02 16:08:05 -05002405
2406 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002407 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002408 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002409 if (ret) {
2410 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002411 ret = 1;
2412 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002413 }
Chris Mason3685f792007-10-19 09:23:27 -04002414
Chris Mason123abc82007-03-14 14:14:43 -04002415 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002416 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002417 ret = 1;
2418 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002419 }
2420
Chris Mason34a38212007-11-07 13:31:03 -05002421 if (empty)
2422 nr = right_nritems;
2423 else
2424 nr = right_nritems - 1;
2425
2426 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002427 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002428 if (!right->map_token) {
2429 map_extent_buffer(right, (unsigned long)item,
2430 sizeof(struct btrfs_item),
2431 &right->map_token, &right->kaddr,
2432 &right->map_start, &right->map_len,
2433 KM_USER1);
2434 }
2435
Zheng Yan31840ae2008-09-23 13:14:14 -04002436 if (!empty && push_items > 0) {
2437 if (path->slots[0] < i)
2438 break;
2439 if (path->slots[0] == i) {
2440 int space = btrfs_leaf_free_space(root, right);
2441 if (space + push_space * 2 > free_space)
2442 break;
2443 }
2444 }
2445
Chris Masonbe0e5c02007-01-26 15:51:26 -05002446 if (path->slots[0] == i)
2447 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002448
2449 this_item_size = btrfs_item_size(right, item);
2450 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002451 break;
Chris Masondb945352007-10-15 16:15:53 -04002452
Chris Masonbe0e5c02007-01-26 15:51:26 -05002453 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002454 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002455 }
Chris Masondb945352007-10-15 16:15:53 -04002456
2457 if (right->map_token) {
2458 unmap_extent_buffer(right, right->map_token, KM_USER1);
2459 right->map_token = NULL;
2460 }
2461
Chris Masonbe0e5c02007-01-26 15:51:26 -05002462 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002463 ret = 1;
2464 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002465 }
Chris Mason34a38212007-11-07 13:31:03 -05002466 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002467 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002468
Chris Masonbe0e5c02007-01-26 15:51:26 -05002469 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002470 copy_extent_buffer(left, right,
2471 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2472 btrfs_item_nr_offset(0),
2473 push_items * sizeof(struct btrfs_item));
2474
Chris Mason123abc82007-03-14 14:14:43 -04002475 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002476 btrfs_item_offset_nr(right, push_items -1);
2477
2478 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002479 leaf_data_end(root, left) - push_space,
2480 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002481 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002482 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002483 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002484 BUG_ON(old_left_nritems < 0);
2485
Chris Masondb945352007-10-15 16:15:53 -04002486 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002487 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002488 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002489
Chris Mason5f39d392007-10-15 16:14:19 -04002490 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002491 if (!left->map_token) {
2492 map_extent_buffer(left, (unsigned long)item,
2493 sizeof(struct btrfs_item),
2494 &left->map_token, &left->kaddr,
2495 &left->map_start, &left->map_len,
2496 KM_USER1);
2497 }
2498
Chris Mason5f39d392007-10-15 16:14:19 -04002499 ioff = btrfs_item_offset(left, item);
2500 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002501 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002502 }
Chris Mason5f39d392007-10-15 16:14:19 -04002503 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002504 if (left->map_token) {
2505 unmap_extent_buffer(left, left->map_token, KM_USER1);
2506 left->map_token = NULL;
2507 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002508
2509 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002510 if (push_items > right_nritems) {
2511 printk("push items %d nr %u\n", push_items, right_nritems);
2512 WARN_ON(1);
2513 }
Chris Mason5f39d392007-10-15 16:14:19 -04002514
Chris Mason34a38212007-11-07 13:31:03 -05002515 if (push_items < right_nritems) {
2516 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2517 leaf_data_end(root, right);
2518 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2519 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2520 btrfs_leaf_data(right) +
2521 leaf_data_end(root, right), push_space);
2522
2523 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002524 btrfs_item_nr_offset(push_items),
2525 (btrfs_header_nritems(right) - push_items) *
2526 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002527 }
Yaneef1c492007-11-26 10:58:13 -05002528 right_nritems -= push_items;
2529 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002530 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002531 for (i = 0; i < right_nritems; i++) {
2532 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002533
2534 if (!right->map_token) {
2535 map_extent_buffer(right, (unsigned long)item,
2536 sizeof(struct btrfs_item),
2537 &right->map_token, &right->kaddr,
2538 &right->map_start, &right->map_len,
2539 KM_USER1);
2540 }
2541
2542 push_space = push_space - btrfs_item_size(right, item);
2543 btrfs_set_item_offset(right, item, push_space);
2544 }
2545 if (right->map_token) {
2546 unmap_extent_buffer(right, right->map_token, KM_USER1);
2547 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002548 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002549
Chris Mason5f39d392007-10-15 16:14:19 -04002550 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002551 if (right_nritems)
2552 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002553
Zheng Yan31840ae2008-09-23 13:14:14 -04002554 ret = btrfs_update_ref(trans, root, right, left,
2555 old_left_nritems, push_items);
2556 BUG_ON(ret);
2557
Chris Mason5f39d392007-10-15 16:14:19 -04002558 btrfs_item_key(right, &disk_key, 0);
2559 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002560 if (wret)
2561 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002562
2563 /* then fixup the leaf pointer in the path */
2564 if (path->slots[0] < push_items) {
2565 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002566 if (btrfs_header_nritems(path->nodes[0]) == 0)
2567 clean_tree_block(trans, root, path->nodes[0]);
2568 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002569 free_extent_buffer(path->nodes[0]);
2570 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002571 path->slots[1] -= 1;
2572 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002573 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002574 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002575 path->slots[0] -= push_items;
2576 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002577 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002578 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002579out:
2580 btrfs_tree_unlock(left);
2581 free_extent_buffer(left);
2582 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002583}
2584
Chris Mason74123bd2007-02-02 11:05:29 -05002585/*
2586 * split the path's leaf in two, making sure there is at least data_size
2587 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002588 *
2589 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002590 */
Chris Masone02119d2008-09-05 16:13:11 -04002591static noinline int split_leaf(struct btrfs_trans_handle *trans,
2592 struct btrfs_root *root,
2593 struct btrfs_key *ins_key,
2594 struct btrfs_path *path, int data_size,
2595 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002596{
Chris Mason5f39d392007-10-15 16:14:19 -04002597 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002598 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002599 int mid;
2600 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002601 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002602 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002603 int data_copy_size;
2604 int rt_data_off;
2605 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002606 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002607 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002608 int double_split;
2609 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002610 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002611
Chris Masoncc0c5532007-10-25 15:42:57 -04002612 if (extend)
2613 space_needed = data_size;
2614
Chris Mason40689472007-03-17 14:29:23 -04002615 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002616 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002617 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002618 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002619 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002620 }
2621 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002622 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002623 if (wret < 0)
2624 return wret;
2625 }
2626 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002627
Chris Mason3685f792007-10-19 09:23:27 -04002628 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002629 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002630 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002631 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002632
Chris Mason5c680ed2007-02-22 11:39:13 -05002633 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002634 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002635 if (ret)
2636 return ret;
2637 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002638again:
2639 double_split = 0;
2640 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002641 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002642 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002643 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002644
Chris Mason925baed2008-06-25 16:01:30 -04002645 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002646 path->nodes[1]->start,
2647 root->root_key.objectid,
2648 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002649 if (IS_ERR(right)) {
2650 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002651 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002652 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002653
Chris Mason5f39d392007-10-15 16:14:19 -04002654 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002655 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002656 btrfs_set_header_generation(right, trans->transid);
2657 btrfs_set_header_owner(right, root->root_key.objectid);
2658 btrfs_set_header_level(right, 0);
2659 write_extent_buffer(right, root->fs_info->fsid,
2660 (unsigned long)btrfs_header_fsid(right),
2661 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002662
2663 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2664 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2665 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002666 if (mid <= slot) {
2667 if (nritems == 1 ||
2668 leaf_space_used(l, mid, nritems - mid) + space_needed >
2669 BTRFS_LEAF_DATA_SIZE(root)) {
2670 if (slot >= nritems) {
2671 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002672 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002673 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002674 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002675 path->slots[1] + 1, 1);
2676 if (wret)
2677 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002678
2679 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002680 free_extent_buffer(path->nodes[0]);
2681 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002682 path->slots[0] = 0;
2683 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002684 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002685 return ret;
2686 }
2687 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002688 if (mid != nritems &&
2689 leaf_space_used(l, mid, nritems - mid) +
2690 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2691 double_split = 1;
2692 }
Chris Masond4dbff92007-04-04 14:08:15 -04002693 }
2694 } else {
2695 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2696 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002697 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002698 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002699 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002700 wret = insert_ptr(trans, root, path,
2701 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002702 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002703 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002704 if (wret)
2705 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002706 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002707 free_extent_buffer(path->nodes[0]);
2708 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002709 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002710 if (path->slots[1] == 0) {
2711 wret = fixup_low_keys(trans, root,
2712 path, &disk_key, 1);
2713 if (wret)
2714 ret = wret;
2715 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002716 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002717 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002718 } else if (extend && slot == 0) {
2719 mid = 1;
2720 } else {
2721 mid = slot;
2722 if (mid != nritems &&
2723 leaf_space_used(l, mid, nritems - mid) +
2724 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2725 double_split = 1;
2726 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002727 }
Chris Masond4dbff92007-04-04 14:08:15 -04002728 }
2729 }
Chris Mason5f39d392007-10-15 16:14:19 -04002730 nritems = nritems - mid;
2731 btrfs_set_header_nritems(right, nritems);
2732 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2733
2734 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2735 btrfs_item_nr_offset(mid),
2736 nritems * sizeof(struct btrfs_item));
2737
2738 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002739 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2740 data_copy_size, btrfs_leaf_data(l) +
2741 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002742
Chris Mason5f39d392007-10-15 16:14:19 -04002743 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2744 btrfs_item_end_nr(l, mid);
2745
2746 for (i = 0; i < nritems; i++) {
2747 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002748 u32 ioff;
2749
2750 if (!right->map_token) {
2751 map_extent_buffer(right, (unsigned long)item,
2752 sizeof(struct btrfs_item),
2753 &right->map_token, &right->kaddr,
2754 &right->map_start, &right->map_len,
2755 KM_USER1);
2756 }
2757
2758 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002759 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002760 }
Chris Mason74123bd2007-02-02 11:05:29 -05002761
Chris Masondb945352007-10-15 16:15:53 -04002762 if (right->map_token) {
2763 unmap_extent_buffer(right, right->map_token, KM_USER1);
2764 right->map_token = NULL;
2765 }
2766
Chris Mason5f39d392007-10-15 16:14:19 -04002767 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002768 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002769 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002770 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2771 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002772 if (wret)
2773 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002774
2775 btrfs_mark_buffer_dirty(right);
2776 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002777 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002778
Zheng Yan31840ae2008-09-23 13:14:14 -04002779 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2780 BUG_ON(ret);
2781
Chris Masonbe0e5c02007-01-26 15:51:26 -05002782 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002783 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002784 free_extent_buffer(path->nodes[0]);
2785 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002786 path->slots[0] -= mid;
2787 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002788 } else {
2789 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002790 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002791 }
Chris Mason5f39d392007-10-15 16:14:19 -04002792
Chris Masoneb60cea2007-02-02 09:18:22 -05002793 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002794
Chris Masoncc0c5532007-10-25 15:42:57 -04002795 if (double_split) {
2796 BUG_ON(num_doubles != 0);
2797 num_doubles++;
2798 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002799 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002800 return ret;
2801}
2802
Chris Masond352ac62008-09-29 15:18:18 -04002803/*
2804 * make the item pointed to by the path smaller. new_size indicates
2805 * how small to make it, and from_end tells us if we just chop bytes
2806 * off the end of the item or if we shift the item to chop bytes off
2807 * the front.
2808 */
Chris Masonb18c6682007-04-17 13:26:50 -04002809int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2810 struct btrfs_root *root,
2811 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002812 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002813{
2814 int ret = 0;
2815 int slot;
2816 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002817 struct extent_buffer *leaf;
2818 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002819 u32 nritems;
2820 unsigned int data_end;
2821 unsigned int old_data_start;
2822 unsigned int old_size;
2823 unsigned int size_diff;
2824 int i;
2825
2826 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002827 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002828 slot = path->slots[0];
2829
2830 old_size = btrfs_item_size_nr(leaf, slot);
2831 if (old_size == new_size)
2832 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002833
Chris Mason5f39d392007-10-15 16:14:19 -04002834 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002835 data_end = leaf_data_end(root, leaf);
2836
Chris Mason5f39d392007-10-15 16:14:19 -04002837 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002838
Chris Masonb18c6682007-04-17 13:26:50 -04002839 size_diff = old_size - new_size;
2840
2841 BUG_ON(slot < 0);
2842 BUG_ON(slot >= nritems);
2843
2844 /*
2845 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2846 */
2847 /* first correct the data pointers */
2848 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002849 u32 ioff;
2850 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002851
2852 if (!leaf->map_token) {
2853 map_extent_buffer(leaf, (unsigned long)item,
2854 sizeof(struct btrfs_item),
2855 &leaf->map_token, &leaf->kaddr,
2856 &leaf->map_start, &leaf->map_len,
2857 KM_USER1);
2858 }
2859
Chris Mason5f39d392007-10-15 16:14:19 -04002860 ioff = btrfs_item_offset(leaf, item);
2861 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002862 }
Chris Masondb945352007-10-15 16:15:53 -04002863
2864 if (leaf->map_token) {
2865 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2866 leaf->map_token = NULL;
2867 }
2868
Chris Masonb18c6682007-04-17 13:26:50 -04002869 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002870 if (from_end) {
2871 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2872 data_end + size_diff, btrfs_leaf_data(leaf) +
2873 data_end, old_data_start + new_size - data_end);
2874 } else {
2875 struct btrfs_disk_key disk_key;
2876 u64 offset;
2877
2878 btrfs_item_key(leaf, &disk_key, slot);
2879
2880 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2881 unsigned long ptr;
2882 struct btrfs_file_extent_item *fi;
2883
2884 fi = btrfs_item_ptr(leaf, slot,
2885 struct btrfs_file_extent_item);
2886 fi = (struct btrfs_file_extent_item *)(
2887 (unsigned long)fi - size_diff);
2888
2889 if (btrfs_file_extent_type(leaf, fi) ==
2890 BTRFS_FILE_EXTENT_INLINE) {
2891 ptr = btrfs_item_ptr_offset(leaf, slot);
2892 memmove_extent_buffer(leaf, ptr,
2893 (unsigned long)fi,
2894 offsetof(struct btrfs_file_extent_item,
2895 disk_bytenr));
2896 }
2897 }
2898
2899 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2900 data_end + size_diff, btrfs_leaf_data(leaf) +
2901 data_end, old_data_start - data_end);
2902
2903 offset = btrfs_disk_key_offset(&disk_key);
2904 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2905 btrfs_set_item_key(leaf, &disk_key, slot);
2906 if (slot == 0)
2907 fixup_low_keys(trans, root, path, &disk_key, 1);
2908 }
Chris Mason5f39d392007-10-15 16:14:19 -04002909
2910 item = btrfs_item_nr(leaf, slot);
2911 btrfs_set_item_size(leaf, item, new_size);
2912 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002913
2914 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002915 if (btrfs_leaf_free_space(root, leaf) < 0) {
2916 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002917 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002918 }
Chris Masonb18c6682007-04-17 13:26:50 -04002919 return ret;
2920}
2921
Chris Masond352ac62008-09-29 15:18:18 -04002922/*
2923 * make the item pointed to by the path bigger, data_size is the new size.
2924 */
Chris Mason5f39d392007-10-15 16:14:19 -04002925int btrfs_extend_item(struct btrfs_trans_handle *trans,
2926 struct btrfs_root *root, struct btrfs_path *path,
2927 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002928{
2929 int ret = 0;
2930 int slot;
2931 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002932 struct extent_buffer *leaf;
2933 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002934 u32 nritems;
2935 unsigned int data_end;
2936 unsigned int old_data;
2937 unsigned int old_size;
2938 int i;
2939
2940 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002941 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002942
Chris Mason5f39d392007-10-15 16:14:19 -04002943 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002944 data_end = leaf_data_end(root, leaf);
2945
Chris Mason5f39d392007-10-15 16:14:19 -04002946 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2947 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002948 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002949 }
Chris Mason6567e832007-04-16 09:22:45 -04002950 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002951 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002952
2953 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002954 if (slot >= nritems) {
2955 btrfs_print_leaf(root, leaf);
2956 printk("slot %d too large, nritems %d\n", slot, nritems);
2957 BUG_ON(1);
2958 }
Chris Mason6567e832007-04-16 09:22:45 -04002959
2960 /*
2961 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2962 */
2963 /* first correct the data pointers */
2964 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002965 u32 ioff;
2966 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002967
2968 if (!leaf->map_token) {
2969 map_extent_buffer(leaf, (unsigned long)item,
2970 sizeof(struct btrfs_item),
2971 &leaf->map_token, &leaf->kaddr,
2972 &leaf->map_start, &leaf->map_len,
2973 KM_USER1);
2974 }
Chris Mason5f39d392007-10-15 16:14:19 -04002975 ioff = btrfs_item_offset(leaf, item);
2976 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002977 }
Chris Mason5f39d392007-10-15 16:14:19 -04002978
Chris Masondb945352007-10-15 16:15:53 -04002979 if (leaf->map_token) {
2980 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2981 leaf->map_token = NULL;
2982 }
2983
Chris Mason6567e832007-04-16 09:22:45 -04002984 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04002985 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04002986 data_end - data_size, btrfs_leaf_data(leaf) +
2987 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002988
Chris Mason6567e832007-04-16 09:22:45 -04002989 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04002990 old_size = btrfs_item_size_nr(leaf, slot);
2991 item = btrfs_item_nr(leaf, slot);
2992 btrfs_set_item_size(leaf, item, old_size + data_size);
2993 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002994
2995 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002996 if (btrfs_leaf_free_space(root, leaf) < 0) {
2997 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002998 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002999 }
Chris Mason6567e832007-04-16 09:22:45 -04003000 return ret;
3001}
3002
Chris Mason74123bd2007-02-02 11:05:29 -05003003/*
Chris Masond352ac62008-09-29 15:18:18 -04003004 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003005 * This does all the path init required, making room in the tree if needed.
3006 */
Chris Mason9c583092008-01-29 15:15:18 -05003007int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003008 struct btrfs_root *root,
3009 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003010 struct btrfs_key *cpu_key, u32 *data_size,
3011 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003012{
Chris Mason5f39d392007-10-15 16:14:19 -04003013 struct extent_buffer *leaf;
3014 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003015 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003016 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003017 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003018 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003019 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003020 u32 total_size = 0;
3021 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003022 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003023 struct btrfs_disk_key disk_key;
3024
Chris Mason9c583092008-01-29 15:15:18 -05003025 for (i = 0; i < nr; i++) {
3026 total_data += data_size[i];
3027 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003028
Josef Bacik7b128762008-07-24 12:17:14 -04003029 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003030 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003031 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003032 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003033 if (ret < 0)
3034 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003035
Chris Mason62e27492007-03-15 12:56:47 -04003036 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003037 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003038
Chris Mason5f39d392007-10-15 16:14:19 -04003039 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003040 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003041
Chris Masonf25956c2008-09-12 15:32:53 -04003042 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003043 btrfs_print_leaf(root, leaf);
3044 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003045 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003046 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003047 }
Chris Mason5f39d392007-10-15 16:14:19 -04003048
Chris Mason62e27492007-03-15 12:56:47 -04003049 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003050 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003051
Chris Masonbe0e5c02007-01-26 15:51:26 -05003052 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003053 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003054
Chris Mason5f39d392007-10-15 16:14:19 -04003055 if (old_data < data_end) {
3056 btrfs_print_leaf(root, leaf);
3057 printk("slot %d old_data %d data_end %d\n",
3058 slot, old_data, data_end);
3059 BUG_ON(1);
3060 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003061 /*
3062 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3063 */
3064 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003065 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003066 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003067 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003068
Chris Mason5f39d392007-10-15 16:14:19 -04003069 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003070 if (!leaf->map_token) {
3071 map_extent_buffer(leaf, (unsigned long)item,
3072 sizeof(struct btrfs_item),
3073 &leaf->map_token, &leaf->kaddr,
3074 &leaf->map_start, &leaf->map_len,
3075 KM_USER1);
3076 }
3077
Chris Mason5f39d392007-10-15 16:14:19 -04003078 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003079 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003080 }
Chris Masondb945352007-10-15 16:15:53 -04003081 if (leaf->map_token) {
3082 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3083 leaf->map_token = NULL;
3084 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003085
3086 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003087 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003088 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003089 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003090
3091 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003092 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003093 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003094 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003095 data_end = old_data;
3096 }
Chris Mason5f39d392007-10-15 16:14:19 -04003097
Chris Mason62e27492007-03-15 12:56:47 -04003098 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003099 for (i = 0; i < nr; i++) {
3100 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3101 btrfs_set_item_key(leaf, &disk_key, slot + i);
3102 item = btrfs_item_nr(leaf, slot + i);
3103 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3104 data_end -= data_size[i];
3105 btrfs_set_item_size(leaf, item, data_size[i]);
3106 }
3107 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003108 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003109
3110 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003111 if (slot == 0) {
3112 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003113 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003114 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003115
Chris Mason5f39d392007-10-15 16:14:19 -04003116 if (btrfs_leaf_free_space(root, leaf) < 0) {
3117 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003118 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003119 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003120out:
Chris Mason62e27492007-03-15 12:56:47 -04003121 return ret;
3122}
3123
3124/*
3125 * Given a key and some data, insert an item into the tree.
3126 * This does all the path init required, making room in the tree if needed.
3127 */
Chris Masone089f052007-03-16 16:20:31 -04003128int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3129 *root, struct btrfs_key *cpu_key, void *data, u32
3130 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003131{
3132 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003133 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003134 struct extent_buffer *leaf;
3135 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003136
Chris Mason2c90e5d2007-04-02 10:50:19 -04003137 path = btrfs_alloc_path();
3138 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003139 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003140 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003141 leaf = path->nodes[0];
3142 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3143 write_extent_buffer(leaf, data, ptr, data_size);
3144 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003145 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003146 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003147 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003148}
3149
Chris Mason74123bd2007-02-02 11:05:29 -05003150/*
Chris Mason5de08d72007-02-24 06:24:44 -05003151 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003152 *
Chris Masond352ac62008-09-29 15:18:18 -04003153 * the tree should have been previously balanced so the deletion does not
3154 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003155 */
Chris Masone089f052007-03-16 16:20:31 -04003156static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3157 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003158{
Chris Mason5f39d392007-10-15 16:14:19 -04003159 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003160 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003161 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003162 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003163
Chris Mason5f39d392007-10-15 16:14:19 -04003164 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05003165 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003166 memmove_extent_buffer(parent,
3167 btrfs_node_key_ptr_offset(slot),
3168 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003169 sizeof(struct btrfs_key_ptr) *
3170 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003171 }
Chris Mason7518a232007-03-12 12:01:18 -04003172 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003173 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003174 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003175 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003176 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003177 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003178 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003179 struct btrfs_disk_key disk_key;
3180
3181 btrfs_node_key(parent, &disk_key, 0);
3182 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003183 if (wret)
3184 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003185 }
Chris Masond6025572007-03-30 14:27:56 -04003186 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003187 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003188}
3189
Chris Mason74123bd2007-02-02 11:05:29 -05003190/*
Chris Mason323ac952008-10-01 19:05:46 -04003191 * a helper function to delete the leaf pointed to by path->slots[1] and
3192 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3193 * already know it, it is faster to have them pass it down than to
3194 * read it out of the node again.
3195 *
3196 * This deletes the pointer in path->nodes[1] and frees the leaf
3197 * block extent. zero is returned if it all worked out, < 0 otherwise.
3198 *
3199 * The path must have already been setup for deleting the leaf, including
3200 * all the proper balancing. path->nodes[1] must be locked.
3201 */
3202noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3203 struct btrfs_root *root,
3204 struct btrfs_path *path, u64 bytenr)
3205{
3206 int ret;
3207 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3208
3209 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3210 if (ret)
3211 return ret;
3212
3213 ret = btrfs_free_extent(trans, root, bytenr,
3214 btrfs_level_size(root, 0),
3215 path->nodes[1]->start,
3216 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003217 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003218 return ret;
3219}
3220/*
Chris Mason74123bd2007-02-02 11:05:29 -05003221 * delete the item at the leaf level in path. If that empties
3222 * the leaf, remove it from the tree
3223 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003224int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3225 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003226{
Chris Mason5f39d392007-10-15 16:14:19 -04003227 struct extent_buffer *leaf;
3228 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003229 int last_off;
3230 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003231 int ret = 0;
3232 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003233 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003234 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003235
Chris Mason5f39d392007-10-15 16:14:19 -04003236 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003237 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3238
3239 for (i = 0; i < nr; i++)
3240 dsize += btrfs_item_size_nr(leaf, slot + i);
3241
Chris Mason5f39d392007-10-15 16:14:19 -04003242 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003243
Chris Mason85e21ba2008-01-29 15:11:36 -05003244 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003245 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003246
3247 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003248 data_end + dsize,
3249 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003250 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003251
Chris Mason85e21ba2008-01-29 15:11:36 -05003252 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003253 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003256 if (!leaf->map_token) {
3257 map_extent_buffer(leaf, (unsigned long)item,
3258 sizeof(struct btrfs_item),
3259 &leaf->map_token, &leaf->kaddr,
3260 &leaf->map_start, &leaf->map_len,
3261 KM_USER1);
3262 }
Chris Mason5f39d392007-10-15 16:14:19 -04003263 ioff = btrfs_item_offset(leaf, item);
3264 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003265 }
Chris Masondb945352007-10-15 16:15:53 -04003266
3267 if (leaf->map_token) {
3268 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3269 leaf->map_token = NULL;
3270 }
3271
Chris Mason5f39d392007-10-15 16:14:19 -04003272 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003273 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003274 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003275 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003276 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003277 btrfs_set_header_nritems(leaf, nritems - nr);
3278 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003279
Chris Mason74123bd2007-02-02 11:05:29 -05003280 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003281 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003282 if (leaf == root->node) {
3283 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003284 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003285 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3286 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003287 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003288 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003289 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003290 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003291 struct btrfs_disk_key disk_key;
3292
3293 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003294 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003295 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003296 if (wret)
3297 ret = wret;
3298 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003299
Chris Mason74123bd2007-02-02 11:05:29 -05003300 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003301 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003302 /* push_leaf_left fixes the path.
3303 * make sure the path still points to our leaf
3304 * for possible call to del_ptr below
3305 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003306 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003307 extent_buffer_get(leaf);
3308
Chris Mason85e21ba2008-01-29 15:11:36 -05003309 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003310 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003311 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003312
3313 if (path->nodes[0] == leaf &&
3314 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003315 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003316 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003317 ret = wret;
3318 }
Chris Mason5f39d392007-10-15 16:14:19 -04003319
3320 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003321 path->slots[1] = slot;
3322 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3323 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003324 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003325 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003326 /* if we're still in the path, make sure
3327 * we're dirty. Otherwise, one of the
3328 * push_leaf functions must have already
3329 * dirtied this buffer
3330 */
3331 if (path->nodes[0] == leaf)
3332 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003333 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003334 }
Chris Masond5719762007-03-23 10:01:08 -04003335 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003336 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003337 }
3338 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003339 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003340}
3341
Chris Mason97571fd2007-02-24 13:39:08 -05003342/*
Chris Mason925baed2008-06-25 16:01:30 -04003343 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003344 * returns 0 if it found something or 1 if there are no lesser leaves.
3345 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003346 *
3347 * This may release the path, and so you may lose any locks held at the
3348 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003349 */
3350int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3351{
Chris Mason925baed2008-06-25 16:01:30 -04003352 struct btrfs_key key;
3353 struct btrfs_disk_key found_key;
3354 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003355
Chris Mason925baed2008-06-25 16:01:30 -04003356 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003357
Chris Mason925baed2008-06-25 16:01:30 -04003358 if (key.offset > 0)
3359 key.offset--;
3360 else if (key.type > 0)
3361 key.type--;
3362 else if (key.objectid > 0)
3363 key.objectid--;
3364 else
3365 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003366
Chris Mason925baed2008-06-25 16:01:30 -04003367 btrfs_release_path(root, path);
3368 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3369 if (ret < 0)
3370 return ret;
3371 btrfs_item_key(path->nodes[0], &found_key, 0);
3372 ret = comp_keys(&found_key, &key);
3373 if (ret < 0)
3374 return 0;
3375 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003376}
3377
Chris Mason3f157a22008-06-25 16:01:31 -04003378/*
3379 * A helper function to walk down the tree starting at min_key, and looking
3380 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003381 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003382 *
3383 * This does not cow, but it does stuff the starting key it finds back
3384 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3385 * key and get a writable path.
3386 *
3387 * This does lock as it descends, and path->keep_locks should be set
3388 * to 1 by the caller.
3389 *
3390 * This honors path->lowest_level to prevent descent past a given level
3391 * of the tree.
3392 *
Chris Masond352ac62008-09-29 15:18:18 -04003393 * min_trans indicates the oldest transaction that you are interested
3394 * in walking through. Any nodes or leaves older than min_trans are
3395 * skipped over (without reading them).
3396 *
Chris Mason3f157a22008-06-25 16:01:31 -04003397 * returns zero if something useful was found, < 0 on error and 1 if there
3398 * was nothing in the tree that matched the search criteria.
3399 */
3400int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003401 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003402 struct btrfs_path *path, int cache_only,
3403 u64 min_trans)
3404{
3405 struct extent_buffer *cur;
3406 struct btrfs_key found_key;
3407 int slot;
Yan96524802008-07-24 12:19:49 -04003408 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003409 u32 nritems;
3410 int level;
3411 int ret = 1;
3412
3413again:
3414 cur = btrfs_lock_root_node(root);
3415 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003416 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003417 path->nodes[level] = cur;
3418 path->locks[level] = 1;
3419
3420 if (btrfs_header_generation(cur) < min_trans) {
3421 ret = 1;
3422 goto out;
3423 }
3424 while(1) {
3425 nritems = btrfs_header_nritems(cur);
3426 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003427 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003428
Chris Mason323ac952008-10-01 19:05:46 -04003429 /* at the lowest level, we're done, setup the path and exit */
3430 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003431 if (slot >= nritems)
3432 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003433 ret = 0;
3434 path->slots[level] = slot;
3435 btrfs_item_key_to_cpu(cur, &found_key, slot);
3436 goto out;
3437 }
Yan96524802008-07-24 12:19:49 -04003438 if (sret && slot > 0)
3439 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003440 /*
3441 * check this node pointer against the cache_only and
3442 * min_trans parameters. If it isn't in cache or is too
3443 * old, skip to the next one.
3444 */
3445 while(slot < nritems) {
3446 u64 blockptr;
3447 u64 gen;
3448 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003449 struct btrfs_disk_key disk_key;
3450
Chris Mason3f157a22008-06-25 16:01:31 -04003451 blockptr = btrfs_node_blockptr(cur, slot);
3452 gen = btrfs_node_ptr_generation(cur, slot);
3453 if (gen < min_trans) {
3454 slot++;
3455 continue;
3456 }
3457 if (!cache_only)
3458 break;
3459
Chris Masone02119d2008-09-05 16:13:11 -04003460 if (max_key) {
3461 btrfs_node_key(cur, &disk_key, slot);
3462 if (comp_keys(&disk_key, max_key) >= 0) {
3463 ret = 1;
3464 goto out;
3465 }
3466 }
3467
Chris Mason3f157a22008-06-25 16:01:31 -04003468 tmp = btrfs_find_tree_block(root, blockptr,
3469 btrfs_level_size(root, level - 1));
3470
3471 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3472 free_extent_buffer(tmp);
3473 break;
3474 }
3475 if (tmp)
3476 free_extent_buffer(tmp);
3477 slot++;
3478 }
Chris Masone02119d2008-09-05 16:13:11 -04003479find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003480 /*
3481 * we didn't find a candidate key in this node, walk forward
3482 * and find another one
3483 */
3484 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003485 path->slots[level] = slot;
3486 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003487 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003488 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003489 btrfs_release_path(root, path);
3490 goto again;
3491 } else {
3492 goto out;
3493 }
3494 }
3495 /* save our key for returning back */
3496 btrfs_node_key_to_cpu(cur, &found_key, slot);
3497 path->slots[level] = slot;
3498 if (level == path->lowest_level) {
3499 ret = 0;
3500 unlock_up(path, level, 1);
3501 goto out;
3502 }
3503 cur = read_node_slot(root, cur, slot);
3504
3505 btrfs_tree_lock(cur);
3506 path->locks[level - 1] = 1;
3507 path->nodes[level - 1] = cur;
3508 unlock_up(path, level, 1);
3509 }
3510out:
3511 if (ret == 0)
3512 memcpy(min_key, &found_key, sizeof(found_key));
3513 return ret;
3514}
3515
3516/*
3517 * this is similar to btrfs_next_leaf, but does not try to preserve
3518 * and fixup the path. It looks for and returns the next key in the
3519 * tree based on the current path and the cache_only and min_trans
3520 * parameters.
3521 *
3522 * 0 is returned if another key is found, < 0 if there are any errors
3523 * and 1 is returned if there are no higher keys in the tree
3524 *
3525 * path->keep_locks should be set to 1 on the search made before
3526 * calling this function.
3527 */
Chris Masone7a84562008-06-25 16:01:31 -04003528int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003529 struct btrfs_key *key, int lowest_level,
3530 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003531{
3532 int level = lowest_level;
3533 int slot;
3534 struct extent_buffer *c;
3535
3536 while(level < BTRFS_MAX_LEVEL) {
3537 if (!path->nodes[level])
3538 return 1;
3539
3540 slot = path->slots[level] + 1;
3541 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003542next:
Chris Masone7a84562008-06-25 16:01:31 -04003543 if (slot >= btrfs_header_nritems(c)) {
3544 level++;
3545 if (level == BTRFS_MAX_LEVEL) {
3546 return 1;
3547 }
3548 continue;
3549 }
3550 if (level == 0)
3551 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003552 else {
3553 u64 blockptr = btrfs_node_blockptr(c, slot);
3554 u64 gen = btrfs_node_ptr_generation(c, slot);
3555
3556 if (cache_only) {
3557 struct extent_buffer *cur;
3558 cur = btrfs_find_tree_block(root, blockptr,
3559 btrfs_level_size(root, level - 1));
3560 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3561 slot++;
3562 if (cur)
3563 free_extent_buffer(cur);
3564 goto next;
3565 }
3566 free_extent_buffer(cur);
3567 }
3568 if (gen < min_trans) {
3569 slot++;
3570 goto next;
3571 }
Chris Masone7a84562008-06-25 16:01:31 -04003572 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003573 }
Chris Masone7a84562008-06-25 16:01:31 -04003574 return 0;
3575 }
3576 return 1;
3577}
3578
Chris Mason7bb86312007-12-11 09:25:06 -05003579/*
Chris Mason925baed2008-06-25 16:01:30 -04003580 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003581 * returns 0 if it found something or 1 if there are no greater leaves.
3582 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003583 */
Chris Mason234b63a2007-03-13 10:46:10 -04003584int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003585{
3586 int slot;
3587 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003588 struct extent_buffer *c;
3589 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003590 struct btrfs_key key;
3591 u32 nritems;
3592 int ret;
3593
3594 nritems = btrfs_header_nritems(path->nodes[0]);
3595 if (nritems == 0) {
3596 return 1;
3597 }
3598
3599 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3600
Chris Mason925baed2008-06-25 16:01:30 -04003601 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003602 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003603 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3604 path->keep_locks = 0;
3605
3606 if (ret < 0)
3607 return ret;
3608
Chris Masona2135012008-06-25 16:01:30 -04003609 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003610 /*
3611 * by releasing the path above we dropped all our locks. A balance
3612 * could have added more items next to the key that used to be
3613 * at the very end of the block. So, check again here and
3614 * advance the path if there are now more items available.
3615 */
Chris Masona2135012008-06-25 16:01:30 -04003616 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003617 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003618 goto done;
3619 }
Chris Masond97e63b2007-02-20 16:40:44 -05003620
Chris Mason234b63a2007-03-13 10:46:10 -04003621 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003622 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003623 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003624
Chris Masond97e63b2007-02-20 16:40:44 -05003625 slot = path->slots[level] + 1;
3626 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003627 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003628 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003629 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003630 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003631 }
Chris Masond97e63b2007-02-20 16:40:44 -05003632 continue;
3633 }
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Chris Mason925baed2008-06-25 16:01:30 -04003635 if (next) {
3636 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003637 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003638 }
Chris Mason5f39d392007-10-15 16:14:19 -04003639
Chris Mason0bd40a72008-07-17 12:54:43 -04003640 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3641 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003642 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003643
Chris Masonca7a79a2008-05-12 12:59:19 -04003644 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003645 if (!path->skip_locking) {
3646 WARN_ON(!btrfs_tree_locked(c));
3647 btrfs_tree_lock(next);
3648 }
Chris Masond97e63b2007-02-20 16:40:44 -05003649 break;
3650 }
3651 path->slots[level] = slot;
3652 while(1) {
3653 level--;
3654 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003655 if (path->locks[level])
3656 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003657 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003658 path->nodes[level] = next;
3659 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003660 if (!path->skip_locking)
3661 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003662 if (!level)
3663 break;
Chris Mason925baed2008-06-25 16:01:30 -04003664 if (level == 1 && path->locks[1] && path->reada)
3665 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003666 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003667 if (!path->skip_locking) {
3668 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3669 btrfs_tree_lock(next);
3670 }
Chris Masond97e63b2007-02-20 16:40:44 -05003671 }
Chris Mason925baed2008-06-25 16:01:30 -04003672done:
3673 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003674 return 0;
3675}
Chris Mason0b86a832008-03-24 15:01:56 -04003676
Chris Mason3f157a22008-06-25 16:01:31 -04003677/*
3678 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3679 * searching until it gets past min_objectid or finds an item of 'type'
3680 *
3681 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3682 */
Chris Mason0b86a832008-03-24 15:01:56 -04003683int btrfs_previous_item(struct btrfs_root *root,
3684 struct btrfs_path *path, u64 min_objectid,
3685 int type)
3686{
3687 struct btrfs_key found_key;
3688 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003689 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003690 int ret;
3691
3692 while(1) {
3693 if (path->slots[0] == 0) {
3694 ret = btrfs_prev_leaf(root, path);
3695 if (ret != 0)
3696 return ret;
3697 } else {
3698 path->slots[0]--;
3699 }
3700 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003701 nritems = btrfs_header_nritems(leaf);
3702 if (nritems == 0)
3703 return 1;
3704 if (path->slots[0] == nritems)
3705 path->slots[0]--;
3706
Chris Mason0b86a832008-03-24 15:01:56 -04003707 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3708 if (found_key.type == type)
3709 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003710 if (found_key.objectid < min_objectid)
3711 break;
3712 if (found_key.objectid == min_objectid &&
3713 found_key.type < type)
3714 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003715 }
3716 return 1;
3717}