blob: 8bb452456d90ef8e58d857f57d553443d58b2be9 [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
Josef Bacikf3465ca2008-11-12 14:19:50 -0500434/*
435 * same as comp_keys only with two btrfs_key's
436 */
437static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
438{
439 if (k1->objectid > k2->objectid)
440 return 1;
441 if (k1->objectid < k2->objectid)
442 return -1;
443 if (k1->type > k2->type)
444 return 1;
445 if (k1->type < k2->type)
446 return -1;
447 if (k1->offset > k2->offset)
448 return 1;
449 if (k1->offset < k2->offset)
450 return -1;
451 return 0;
452}
Chris Mason081e9572007-11-06 10:26:24 -0500453
Chris Masond352ac62008-09-29 15:18:18 -0400454/*
455 * this is used by the defrag code to go through all the
456 * leaves pointed to by a node and reallocate them so that
457 * disk order is close to key order
458 */
Chris Mason6702ed42007-08-07 16:15:09 -0400459int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400460 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400461 int start_slot, int cache_only, u64 *last_ret,
462 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400463{
Chris Mason6b800532007-10-15 16:17:34 -0400464 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400465 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400466 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400467 u64 search_start = *last_ret;
468 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400469 u64 other;
470 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400471 int end_slot;
472 int i;
473 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400474 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400475 int uptodate;
476 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500477 int progress_passed = 0;
478 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400479
Chris Mason5708b952007-10-25 15:43:18 -0400480 parent_level = btrfs_header_level(parent);
481 if (cache_only && parent_level != 1)
482 return 0;
483
Chris Mason6702ed42007-08-07 16:15:09 -0400484 if (trans->transaction != root->fs_info->running_transaction) {
485 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
486 root->fs_info->running_transaction->transid);
487 WARN_ON(1);
488 }
489 if (trans->transid != root->fs_info->generation) {
490 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
491 root->fs_info->generation);
492 WARN_ON(1);
493 }
Chris Mason86479a02007-09-10 19:58:16 -0400494
Chris Mason6b800532007-10-15 16:17:34 -0400495 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400496 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400497 end_slot = parent_nritems;
498
499 if (parent_nritems == 1)
500 return 0;
501
502 for (i = start_slot; i < end_slot; i++) {
503 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400504
Chris Mason5708b952007-10-25 15:43:18 -0400505 if (!parent->map_token) {
506 map_extent_buffer(parent,
507 btrfs_node_key_ptr_offset(i),
508 sizeof(struct btrfs_key_ptr),
509 &parent->map_token, &parent->kaddr,
510 &parent->map_start, &parent->map_len,
511 KM_USER1);
512 }
Chris Mason081e9572007-11-06 10:26:24 -0500513 btrfs_node_key(parent, &disk_key, i);
514 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
515 continue;
516
517 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400518 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400519 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400520 if (last_block == 0)
521 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400522
Chris Mason6702ed42007-08-07 16:15:09 -0400523 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400524 other = btrfs_node_blockptr(parent, i - 1);
525 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400526 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400527 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400528 other = btrfs_node_blockptr(parent, i + 1);
529 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400530 }
Chris Masone9d0b132007-08-10 14:06:19 -0400531 if (close) {
532 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400533 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400534 }
Chris Mason5708b952007-10-25 15:43:18 -0400535 if (parent->map_token) {
536 unmap_extent_buffer(parent, parent->map_token,
537 KM_USER1);
538 parent->map_token = NULL;
539 }
Chris Mason6702ed42007-08-07 16:15:09 -0400540
Chris Mason6b800532007-10-15 16:17:34 -0400541 cur = btrfs_find_tree_block(root, blocknr, blocksize);
542 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400543 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400544 else
545 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400546 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400547 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400548 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400549 continue;
550 }
Chris Mason6b800532007-10-15 16:17:34 -0400551 if (!cur) {
552 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400553 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400554 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400555 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400556 }
Chris Mason6702ed42007-08-07 16:15:09 -0400557 }
Chris Masone9d0b132007-08-10 14:06:19 -0400558 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400559 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400560
Chris Masone7a84562008-06-25 16:01:31 -0400561 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400562 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400563 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400564 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400565 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400566 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400567 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400568 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400569 break;
Yan252c38f2007-08-29 09:11:44 -0400570 }
Chris Masone7a84562008-06-25 16:01:31 -0400571 search_start = cur->start;
572 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400573 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400574 btrfs_tree_unlock(cur);
575 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400576 }
Chris Mason5708b952007-10-25 15:43:18 -0400577 if (parent->map_token) {
578 unmap_extent_buffer(parent, parent->map_token,
579 KM_USER1);
580 parent->map_token = NULL;
581 }
Chris Mason6702ed42007-08-07 16:15:09 -0400582 return err;
583}
584
Chris Mason74123bd2007-02-02 11:05:29 -0500585/*
586 * The leaf data grows from end-to-front in the node.
587 * this returns the address of the start of the last item,
588 * which is the stop of the leaf data stack
589 */
Chris Mason123abc82007-03-14 14:14:43 -0400590static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400591 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500592{
Chris Mason5f39d392007-10-15 16:14:19 -0400593 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500594 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400595 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400596 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500597}
598
Chris Masond352ac62008-09-29 15:18:18 -0400599/*
600 * extra debugging checks to make sure all the items in a key are
601 * well formed and in the proper order
602 */
Chris Mason123abc82007-03-14 14:14:43 -0400603static int check_node(struct btrfs_root *root, struct btrfs_path *path,
604 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500605{
Chris Mason5f39d392007-10-15 16:14:19 -0400606 struct extent_buffer *parent = NULL;
607 struct extent_buffer *node = path->nodes[level];
608 struct btrfs_disk_key parent_key;
609 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500610 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400611 int slot;
612 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400613 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500614
615 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400616 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400617
Chris Mason8d7be552007-05-10 11:24:42 -0400618 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400619 BUG_ON(nritems == 0);
620 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400621 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400622 btrfs_node_key(parent, &parent_key, parent_slot);
623 btrfs_node_key(node, &node_key, 0);
624 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400625 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400626 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400627 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500628 }
Chris Mason123abc82007-03-14 14:14:43 -0400629 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400630 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400631 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
632 btrfs_node_key(node, &node_key, slot);
633 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400634 }
635 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400636 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
637 btrfs_node_key(node, &node_key, slot);
638 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500639 }
640 return 0;
641}
642
Chris Masond352ac62008-09-29 15:18:18 -0400643/*
644 * extra checking to make sure all the items in a leaf are
645 * well formed and in the proper order
646 */
Chris Mason123abc82007-03-14 14:14:43 -0400647static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
648 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500649{
Chris Mason5f39d392007-10-15 16:14:19 -0400650 struct extent_buffer *leaf = path->nodes[level];
651 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500652 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400653 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400654 struct btrfs_disk_key parent_key;
655 struct btrfs_disk_key leaf_key;
656 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400657
Chris Mason5f39d392007-10-15 16:14:19 -0400658 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500659
660 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400661 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400662
663 if (nritems == 0)
664 return 0;
665
666 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400667 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400668 btrfs_node_key(parent, &parent_key, parent_slot);
669 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400670
Chris Mason5f39d392007-10-15 16:14:19 -0400671 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400672 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400673 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400674 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500675 }
Chris Mason5f39d392007-10-15 16:14:19 -0400676#if 0
677 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
678 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
679 btrfs_item_key(leaf, &leaf_key, i);
680 if (comp_keys(&leaf_key, &cpukey) >= 0) {
681 btrfs_print_leaf(root, leaf);
682 printk("slot %d offset bad key\n", i);
683 BUG_ON(1);
684 }
685 if (btrfs_item_offset_nr(leaf, i) !=
686 btrfs_item_end_nr(leaf, i + 1)) {
687 btrfs_print_leaf(root, leaf);
688 printk("slot %d offset bad\n", i);
689 BUG_ON(1);
690 }
691 if (i == 0) {
692 if (btrfs_item_offset_nr(leaf, i) +
693 btrfs_item_size_nr(leaf, i) !=
694 BTRFS_LEAF_DATA_SIZE(root)) {
695 btrfs_print_leaf(root, leaf);
696 printk("slot %d first offset bad\n", i);
697 BUG_ON(1);
698 }
699 }
700 }
701 if (nritems > 0) {
702 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
703 btrfs_print_leaf(root, leaf);
704 printk("slot %d bad size \n", nritems - 1);
705 BUG_ON(1);
706 }
707 }
708#endif
709 if (slot != 0 && slot < nritems - 1) {
710 btrfs_item_key(leaf, &leaf_key, slot);
711 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
712 if (comp_keys(&leaf_key, &cpukey) <= 0) {
713 btrfs_print_leaf(root, leaf);
714 printk("slot %d offset bad key\n", slot);
715 BUG_ON(1);
716 }
717 if (btrfs_item_offset_nr(leaf, slot - 1) !=
718 btrfs_item_end_nr(leaf, slot)) {
719 btrfs_print_leaf(root, leaf);
720 printk("slot %d offset bad\n", slot);
721 BUG_ON(1);
722 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500723 }
Chris Mason8d7be552007-05-10 11:24:42 -0400724 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400725 btrfs_item_key(leaf, &leaf_key, slot);
726 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
727 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
728 if (btrfs_item_offset_nr(leaf, slot) !=
729 btrfs_item_end_nr(leaf, slot + 1)) {
730 btrfs_print_leaf(root, leaf);
731 printk("slot %d offset bad\n", slot);
732 BUG_ON(1);
733 }
Chris Mason8d7be552007-05-10 11:24:42 -0400734 }
Chris Mason5f39d392007-10-15 16:14:19 -0400735 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
736 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500737 return 0;
738}
739
Chris Mason98ed5172008-01-03 10:01:48 -0500740static int noinline check_block(struct btrfs_root *root,
741 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500742{
Chris Masonf1885912008-04-09 16:28:12 -0400743 u64 found_start;
Chris Mason85d824c2008-04-10 10:23:19 -0400744 return 0;
Chris Masonf1885912008-04-09 16:28:12 -0400745 if (btrfs_header_level(path->nodes[level]) != level)
746 printk("warning: bad level %Lu wanted %d found %d\n",
747 path->nodes[level]->start, level,
748 btrfs_header_level(path->nodes[level]));
749 found_start = btrfs_header_bytenr(path->nodes[level]);
750 if (found_start != path->nodes[level]->start) {
751 printk("warning: bad bytentr %Lu found %Lu\n",
752 path->nodes[level]->start, found_start);
753 }
Chris Masondb945352007-10-15 16:15:53 -0400754#if 0
Chris Mason5f39d392007-10-15 16:14:19 -0400755 struct extent_buffer *buf = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -0400756
Chris Mason479965d2007-10-15 16:14:27 -0400757 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
758 (unsigned long)btrfs_header_fsid(buf),
759 BTRFS_FSID_SIZE)) {
Chris Mason5f39d392007-10-15 16:14:19 -0400760 printk("warning bad block %Lu\n", buf->start);
Chris Masondb945352007-10-15 16:15:53 -0400761 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400762 }
Chris Masondb945352007-10-15 16:15:53 -0400763#endif
Chris Masonaa5d6be2007-02-28 16:35:06 -0500764 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400765 return check_leaf(root, path, level);
766 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500767}
768
Chris Mason74123bd2007-02-02 11:05:29 -0500769/*
Chris Mason5f39d392007-10-15 16:14:19 -0400770 * search for key in the extent_buffer. The items start at offset p,
771 * and they are item_size apart. There are 'max' items in p.
772 *
Chris Mason74123bd2007-02-02 11:05:29 -0500773 * the slot in the array is returned via slot, and it points to
774 * the place where you would insert key if it is not found in
775 * the array.
776 *
777 * slot may point to max if the key is bigger than all of the keys
778 */
Chris Masone02119d2008-09-05 16:13:11 -0400779static noinline int generic_bin_search(struct extent_buffer *eb,
780 unsigned long p,
781 int item_size, struct btrfs_key *key,
782 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500783{
784 int low = 0;
785 int high = max;
786 int mid;
787 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400788 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400789 struct btrfs_disk_key unaligned;
790 unsigned long offset;
791 char *map_token = NULL;
792 char *kaddr = NULL;
793 unsigned long map_start = 0;
794 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400795 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500796
797 while(low < high) {
798 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400799 offset = p + mid * item_size;
800
801 if (!map_token || offset < map_start ||
802 (offset + sizeof(struct btrfs_disk_key)) >
803 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400804 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400805 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400806 map_token = NULL;
807 }
808 err = map_extent_buffer(eb, offset,
809 sizeof(struct btrfs_disk_key),
810 &map_token, &kaddr,
811 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400812
Chris Mason479965d2007-10-15 16:14:27 -0400813 if (!err) {
814 tmp = (struct btrfs_disk_key *)(kaddr + offset -
815 map_start);
816 } else {
817 read_extent_buffer(eb, &unaligned,
818 offset, sizeof(unaligned));
819 tmp = &unaligned;
820 }
821
Chris Mason5f39d392007-10-15 16:14:19 -0400822 } else {
823 tmp = (struct btrfs_disk_key *)(kaddr + offset -
824 map_start);
825 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500826 ret = comp_keys(tmp, key);
827
828 if (ret < 0)
829 low = mid + 1;
830 else if (ret > 0)
831 high = mid;
832 else {
833 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400834 if (map_token)
835 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500836 return 0;
837 }
838 }
839 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400840 if (map_token)
841 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500842 return 1;
843}
844
Chris Mason97571fd2007-02-24 13:39:08 -0500845/*
846 * simple bin_search frontend that does the right thing for
847 * leaves vs nodes
848 */
Chris Mason5f39d392007-10-15 16:14:19 -0400849static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
850 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500851{
Chris Mason5f39d392007-10-15 16:14:19 -0400852 if (level == 0) {
853 return generic_bin_search(eb,
854 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400855 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400856 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400857 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500858 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400859 return generic_bin_search(eb,
860 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400861 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400862 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400863 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500864 }
865 return -1;
866}
867
Chris Masond352ac62008-09-29 15:18:18 -0400868/* given a node and slot number, this reads the blocks it points to. The
869 * extent buffer is returned with a reference taken (but unlocked).
870 * NULL is returned on error.
871 */
Chris Masone02119d2008-09-05 16:13:11 -0400872static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400873 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500874{
Chris Masonca7a79a2008-05-12 12:59:19 -0400875 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500876 if (slot < 0)
877 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400878 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500879 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400880
881 BUG_ON(level == 0);
882
Chris Masondb945352007-10-15 16:15:53 -0400883 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400884 btrfs_level_size(root, level - 1),
885 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500886}
887
Chris Masond352ac62008-09-29 15:18:18 -0400888/*
889 * node level balancing, used to make sure nodes are in proper order for
890 * item deletion. We balance from the top down, so we have to make sure
891 * that a deletion won't leave an node completely empty later on.
892 */
Chris Masone02119d2008-09-05 16:13:11 -0400893static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500894 struct btrfs_root *root,
895 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500896{
Chris Mason5f39d392007-10-15 16:14:19 -0400897 struct extent_buffer *right = NULL;
898 struct extent_buffer *mid;
899 struct extent_buffer *left = NULL;
900 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500901 int ret = 0;
902 int wret;
903 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500904 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400905 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500906 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500907
908 if (level == 0)
909 return 0;
910
Chris Mason5f39d392007-10-15 16:14:19 -0400911 mid = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -0400912 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500913 WARN_ON(btrfs_header_generation(mid) != trans->transid);
914
Chris Mason1d4f8a02007-03-13 09:28:32 -0400915 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500916
Chris Mason234b63a2007-03-13 10:46:10 -0400917 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400918 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500919 pslot = path->slots[level + 1];
920
Chris Mason40689472007-03-17 14:29:23 -0400921 /*
922 * deal with the case where there is only one pointer in the root
923 * by promoting the node below to a root
924 */
Chris Mason5f39d392007-10-15 16:14:19 -0400925 if (!parent) {
926 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500927
Chris Mason5f39d392007-10-15 16:14:19 -0400928 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500929 return 0;
930
931 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400932 child = read_node_slot(root, mid, 0);
Chris Mason925baed2008-06-25 16:01:30 -0400933 btrfs_tree_lock(child);
Chris Masonbb803952007-03-01 12:04:21 -0500934 BUG_ON(!child);
Chris Mason65b51a02008-08-01 15:11:20 -0400935 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500936 BUG_ON(ret);
937
Chris Mason925baed2008-06-25 16:01:30 -0400938 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500939 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400940 spin_unlock(&root->node_lock);
941
Zheng Yan31840ae2008-09-23 13:14:14 -0400942 ret = btrfs_update_extent_ref(trans, root, child->start,
943 mid->start, child->start,
944 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400945 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400946 BUG_ON(ret);
947
Chris Mason0b86a832008-03-24 15:01:56 -0400948 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400949 btrfs_tree_unlock(child);
950 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500951 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400952 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400953 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500954 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400955 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500956 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400957 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400958 btrfs_header_generation(mid),
959 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500960 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400961 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400962 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500963 }
Chris Mason5f39d392007-10-15 16:14:19 -0400964 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400965 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500966 return 0;
967
Chris Mason5f39d392007-10-15 16:14:19 -0400968 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400969 err_on_enospc = 1;
970
Chris Mason5f39d392007-10-15 16:14:19 -0400971 left = read_node_slot(root, parent, pslot - 1);
972 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400973 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400974 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400975 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400976 if (wret) {
977 ret = wret;
978 goto enospc;
979 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400980 }
Chris Mason5f39d392007-10-15 16:14:19 -0400981 right = read_node_slot(root, parent, pslot + 1);
982 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400983 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400984 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400985 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400986 if (wret) {
987 ret = wret;
988 goto enospc;
989 }
990 }
991
992 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400993 if (left) {
994 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400995 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500996 if (wret < 0)
997 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400998 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400999 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -05001000 }
Chris Mason79f95c82007-03-01 15:16:26 -05001001
1002 /*
1003 * then try to empty the right most buffer into the middle
1004 */
Chris Mason5f39d392007-10-15 16:14:19 -04001005 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001006 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001007 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001008 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001009 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001010 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -05001011 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001012 u32 blocksize = right->len;
1013
Chris Mason5f39d392007-10-15 16:14:19 -04001014 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001015 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001016 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001017 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001018 wret = del_ptr(trans, root, path, level + 1, pslot +
1019 1);
Chris Masonbb803952007-03-01 12:04:21 -05001020 if (wret)
1021 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001022 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001023 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001024 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001025 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001026 if (wret)
1027 ret = wret;
1028 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001029 struct btrfs_disk_key right_key;
1030 btrfs_node_key(right, &right_key, 0);
1031 btrfs_set_node_key(parent, &right_key, pslot + 1);
1032 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001033 }
1034 }
Chris Mason5f39d392007-10-15 16:14:19 -04001035 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001036 /*
1037 * we're not allowed to leave a node with one item in the
1038 * tree during a delete. A deletion from lower in the tree
1039 * could try to delete the only pointer in this node.
1040 * So, pull some keys from the left.
1041 * There has to be a left pointer at this point because
1042 * otherwise we would have pulled some pointers from the
1043 * right
1044 */
Chris Mason5f39d392007-10-15 16:14:19 -04001045 BUG_ON(!left);
1046 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001047 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001048 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001049 goto enospc;
1050 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001051 if (wret == 1) {
1052 wret = push_node_left(trans, root, left, mid, 1);
1053 if (wret < 0)
1054 ret = wret;
1055 }
Chris Mason79f95c82007-03-01 15:16:26 -05001056 BUG_ON(wret == 1);
1057 }
Chris Mason5f39d392007-10-15 16:14:19 -04001058 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001059 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001060 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001061 u64 bytenr = mid->start;
1062 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001063
Chris Mason5f39d392007-10-15 16:14:19 -04001064 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001065 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001066 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001067 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001068 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001069 if (wret)
1070 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001071 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001072 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001073 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001074 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001075 if (wret)
1076 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001077 } else {
1078 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001079 struct btrfs_disk_key mid_key;
1080 btrfs_node_key(mid, &mid_key, 0);
1081 btrfs_set_node_key(parent, &mid_key, pslot);
1082 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001083 }
Chris Masonbb803952007-03-01 12:04:21 -05001084
Chris Mason79f95c82007-03-01 15:16:26 -05001085 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001086 if (left) {
1087 if (btrfs_header_nritems(left) > orig_slot) {
1088 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001089 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001090 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001091 path->slots[level + 1] -= 1;
1092 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001093 if (mid) {
1094 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001095 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001096 }
Chris Masonbb803952007-03-01 12:04:21 -05001097 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001098 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001099 path->slots[level] = orig_slot;
1100 }
1101 }
Chris Mason79f95c82007-03-01 15:16:26 -05001102 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001103 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001104 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001105 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001106 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001107enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001108 if (right) {
1109 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001110 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001111 }
1112 if (left) {
1113 if (path->nodes[level] != left)
1114 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001115 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001116 }
Chris Masonbb803952007-03-01 12:04:21 -05001117 return ret;
1118}
1119
Chris Masond352ac62008-09-29 15:18:18 -04001120/* Node balancing for insertion. Here we only split or push nodes around
1121 * when they are completely full. This is also done top down, so we
1122 * have to be pessimistic.
1123 */
Chris Mason98ed5172008-01-03 10:01:48 -05001124static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1125 struct btrfs_root *root,
1126 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001127{
Chris Mason5f39d392007-10-15 16:14:19 -04001128 struct extent_buffer *right = NULL;
1129 struct extent_buffer *mid;
1130 struct extent_buffer *left = NULL;
1131 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001132 int ret = 0;
1133 int wret;
1134 int pslot;
1135 int orig_slot = path->slots[level];
1136 u64 orig_ptr;
1137
1138 if (level == 0)
1139 return 1;
1140
Chris Mason5f39d392007-10-15 16:14:19 -04001141 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001142 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001143 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1144
1145 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001146 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001147 pslot = path->slots[level + 1];
1148
Chris Mason5f39d392007-10-15 16:14:19 -04001149 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001150 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001151
Chris Mason5f39d392007-10-15 16:14:19 -04001152 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001153
1154 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001155 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001156 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001157
1158 btrfs_tree_lock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001159 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001160 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1161 wret = 1;
1162 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001163 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001164 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001165 if (ret)
1166 wret = 1;
1167 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001168 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001169 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001170 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001171 }
Chris Masone66f7092007-04-20 13:16:02 -04001172 if (wret < 0)
1173 ret = wret;
1174 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001175 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001176 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001177 btrfs_node_key(mid, &disk_key, 0);
1178 btrfs_set_node_key(parent, &disk_key, pslot);
1179 btrfs_mark_buffer_dirty(parent);
1180 if (btrfs_header_nritems(left) > orig_slot) {
1181 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001182 path->slots[level + 1] -= 1;
1183 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001184 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001185 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001186 } else {
1187 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001188 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001189 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001190 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001191 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001192 }
Chris Masone66f7092007-04-20 13:16:02 -04001193 return 0;
1194 }
Chris Mason925baed2008-06-25 16:01:30 -04001195 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001196 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001197 }
Chris Mason925baed2008-06-25 16:01:30 -04001198 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001199
1200 /*
1201 * then try to empty the right most buffer into the middle
1202 */
Chris Mason5f39d392007-10-15 16:14:19 -04001203 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001204 u32 right_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001205 btrfs_tree_lock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001206 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001207 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1208 wret = 1;
1209 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001210 ret = btrfs_cow_block(trans, root, right,
1211 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001212 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001213 if (ret)
1214 wret = 1;
1215 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001216 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001217 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001218 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001219 }
Chris Masone66f7092007-04-20 13:16:02 -04001220 if (wret < 0)
1221 ret = wret;
1222 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001223 struct btrfs_disk_key disk_key;
1224
1225 btrfs_node_key(right, &disk_key, 0);
1226 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1227 btrfs_mark_buffer_dirty(parent);
1228
1229 if (btrfs_header_nritems(mid) <= orig_slot) {
1230 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001231 path->slots[level + 1] += 1;
1232 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001233 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001234 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001235 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001236 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001237 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001238 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001239 }
Chris Masone66f7092007-04-20 13:16:02 -04001240 return 0;
1241 }
Chris Mason925baed2008-06-25 16:01:30 -04001242 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001243 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001244 }
Chris Masone66f7092007-04-20 13:16:02 -04001245 return 1;
1246}
1247
Chris Mason74123bd2007-02-02 11:05:29 -05001248/*
Chris Masond352ac62008-09-29 15:18:18 -04001249 * readahead one full node of leaves, finding things that are close
1250 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001251 */
Chris Masone02119d2008-09-05 16:13:11 -04001252static noinline void reada_for_search(struct btrfs_root *root,
1253 struct btrfs_path *path,
1254 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001255{
Chris Mason5f39d392007-10-15 16:14:19 -04001256 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001257 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001258 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001259 u64 search;
Chris Mason6b800532007-10-15 16:17:34 -04001260 u64 lowest_read;
1261 u64 highest_read;
1262 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001263 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001264 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001265 u32 nr;
1266 u32 blocksize;
1267 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001268
Chris Masona6b6e752007-10-15 16:22:39 -04001269 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001270 return;
1271
Chris Mason6702ed42007-08-07 16:15:09 -04001272 if (!path->nodes[level])
1273 return;
1274
Chris Mason5f39d392007-10-15 16:14:19 -04001275 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001276
Chris Mason3c69fae2007-08-07 15:52:22 -04001277 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001278 blocksize = btrfs_level_size(root, level - 1);
1279 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001280 if (eb) {
1281 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001282 return;
1283 }
1284
Chris Mason6b800532007-10-15 16:17:34 -04001285 highest_read = search;
1286 lowest_read = search;
1287
Chris Mason5f39d392007-10-15 16:14:19 -04001288 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001289 nr = slot;
Chris Mason3c69fae2007-08-07 15:52:22 -04001290 while(1) {
Chris Mason6b800532007-10-15 16:17:34 -04001291 if (direction < 0) {
1292 if (nr == 0)
1293 break;
1294 nr--;
1295 } else if (direction > 0) {
1296 nr++;
1297 if (nr >= nritems)
1298 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001299 }
Chris Mason01f46652007-12-21 16:24:26 -05001300 if (path->reada < 0 && objectid) {
1301 btrfs_node_key(node, &disk_key, nr);
1302 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1303 break;
1304 }
Chris Mason6b800532007-10-15 16:17:34 -04001305 search = btrfs_node_blockptr(node, nr);
1306 if ((search >= lowest_read && search <= highest_read) ||
Chris Mason6f3577b2008-11-13 09:59:36 -05001307 (search < lowest_read && lowest_read - search <= 16384) ||
1308 (search > highest_read && search - highest_read <= 16384)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001309 readahead_tree_block(root, search, blocksize,
1310 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001311 nread += blocksize;
1312 }
1313 nscan++;
Chris Mason6f3577b2008-11-13 09:59:36 -05001314 if (path->reada < 2 && (nread > (64 * 1024) || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001315 break;
Chris Mason6f3577b2008-11-13 09:59:36 -05001316 if(nread > (256 * 1024) || nscan > 128)
Chris Mason6b800532007-10-15 16:17:34 -04001317 break;
1318
1319 if (search < lowest_read)
1320 lowest_read = search;
1321 if (search > highest_read)
1322 highest_read = search;
Chris Mason3c69fae2007-08-07 15:52:22 -04001323 }
1324}
Chris Mason925baed2008-06-25 16:01:30 -04001325
Chris Masond352ac62008-09-29 15:18:18 -04001326/*
1327 * when we walk down the tree, it is usually safe to unlock the higher layers in
1328 * the tree. The exceptions are when our path goes through slot 0, because operations
1329 * on the tree might require changing key pointers higher up in the tree.
1330 *
1331 * callers might also have set path->keep_locks, which tells this code to
1332 * keep the lock if the path points to the last slot in the block. This is
1333 * part of walking through the tree, and selecting the next slot in the higher
1334 * block.
1335 *
1336 * lowest_unlock sets the lowest level in the tree we're allowed to unlock.
1337 * so if lowest_unlock is 1, level 0 won't be unlocked
1338 */
Chris Masone02119d2008-09-05 16:13:11 -04001339static noinline void unlock_up(struct btrfs_path *path, int level,
1340 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001341{
1342 int i;
1343 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001344 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001345 struct extent_buffer *t;
1346
1347 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1348 if (!path->nodes[i])
1349 break;
1350 if (!path->locks[i])
1351 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001352 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001353 skip_level = i + 1;
1354 continue;
1355 }
Chris Mason051e1b92008-06-25 16:01:30 -04001356 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001357 u32 nritems;
1358 t = path->nodes[i];
1359 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001360 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001361 skip_level = i + 1;
1362 continue;
1363 }
1364 }
Chris Mason051e1b92008-06-25 16:01:30 -04001365 if (skip_level < i && i >= lowest_unlock)
1366 no_skips = 1;
1367
Chris Mason925baed2008-06-25 16:01:30 -04001368 t = path->nodes[i];
1369 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1370 btrfs_tree_unlock(t);
1371 path->locks[i] = 0;
1372 }
1373 }
1374}
1375
Chris Mason3c69fae2007-08-07 15:52:22 -04001376/*
Chris Mason74123bd2007-02-02 11:05:29 -05001377 * look for key in the tree. path is filled in with nodes along the way
1378 * if key is found, we return zero and you can find the item in the leaf
1379 * level of the path (level 0)
1380 *
1381 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001382 * be inserted, and 1 is returned. If there are other errors during the
1383 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001384 *
1385 * if ins_len > 0, nodes and leaves will be split as we walk down the
1386 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1387 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001388 */
Chris Masone089f052007-03-16 16:20:31 -04001389int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1390 *root, struct btrfs_key *key, struct btrfs_path *p, int
1391 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001392{
Chris Mason5f39d392007-10-15 16:14:19 -04001393 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001394 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001395 int slot;
1396 int ret;
1397 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001398 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001399 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001400 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001401 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001402 u64 blocknr;
1403 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001404 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001405
Chris Mason6702ed42007-08-07 16:15:09 -04001406 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001407 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001408 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001409
Chris Mason925baed2008-06-25 16:01:30 -04001410 if (ins_len < 0)
1411 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001412
1413 prealloc_block.objectid = 0;
1414
Chris Masonbb803952007-03-01 12:04:21 -05001415again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001416 if (p->skip_locking)
1417 b = btrfs_root_node(root);
1418 else
1419 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001420
Chris Masoneb60cea2007-02-02 09:18:22 -05001421 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001422 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001423
1424 /*
1425 * setup the path here so we can release it under lock
1426 * contention with the cow code
1427 */
1428 p->nodes[level] = b;
1429 if (!p->skip_locking)
1430 p->locks[level] = 1;
1431
Chris Mason02217ed2007-03-02 16:08:05 -05001432 if (cow) {
1433 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001434
1435 /* is a cow on this block not required */
1436 spin_lock(&root->fs_info->hash_lock);
1437 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001438 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001439 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1440 spin_unlock(&root->fs_info->hash_lock);
1441 goto cow_done;
1442 }
1443 spin_unlock(&root->fs_info->hash_lock);
1444
1445 /* ok, we have to cow, is our old prealloc the right
1446 * size?
1447 */
1448 if (prealloc_block.objectid &&
1449 prealloc_block.offset != b->len) {
1450 btrfs_free_reserved_extent(root,
1451 prealloc_block.objectid,
1452 prealloc_block.offset);
1453 prealloc_block.objectid = 0;
1454 }
1455
1456 /*
1457 * for higher level blocks, try not to allocate blocks
1458 * with the block and the parent locks held.
1459 */
1460 if (level > 1 && !prealloc_block.objectid &&
1461 btrfs_path_lock_waiting(p, level)) {
1462 u32 size = b->len;
1463 u64 hint = b->start;
1464
1465 btrfs_release_path(root, p);
1466 ret = btrfs_reserve_extent(trans, root,
1467 size, size, 0,
1468 hint, (u64)-1,
1469 &prealloc_block, 0);
1470 BUG_ON(ret);
1471 goto again;
1472 }
1473
Chris Masone20d96d2007-03-22 12:13:20 -04001474 wret = btrfs_cow_block(trans, root, b,
1475 p->nodes[level + 1],
1476 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001477 &b, prealloc_block.objectid);
1478 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001479 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001480 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001481 ret = wret;
1482 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001483 }
Chris Mason02217ed2007-03-02 16:08:05 -05001484 }
Chris Mason65b51a02008-08-01 15:11:20 -04001485cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001486 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001487 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001488 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001489 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001490
Chris Masoneb60cea2007-02-02 09:18:22 -05001491 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001492 if (!p->skip_locking)
1493 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001494
Chris Mason123abc82007-03-14 14:14:43 -04001495 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001496 if (ret) {
1497 ret = -1;
1498 goto done;
1499 }
Chris Mason925baed2008-06-25 16:01:30 -04001500
Chris Mason5f39d392007-10-15 16:14:19 -04001501 ret = bin_search(b, key, level, &slot);
1502 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001503 if (ret && slot > 0)
1504 slot -= 1;
1505 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001506 if (ins_len > 0 && btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001507 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masone089f052007-03-16 16:20:31 -04001508 int sret = split_node(trans, root, p, level);
Chris Mason5c680ed2007-02-22 11:39:13 -05001509 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001510 if (sret) {
1511 ret = sret;
1512 goto done;
1513 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001514 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001515 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -05001516 } else if (ins_len < 0) {
Chris Masone089f052007-03-16 16:20:31 -04001517 int sret = balance_level(trans, root, p,
1518 level);
Chris Mason65b51a02008-08-01 15:11:20 -04001519 if (sret) {
1520 ret = sret;
1521 goto done;
1522 }
Chris Masonbb803952007-03-01 12:04:21 -05001523 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001524 if (!b) {
1525 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001526 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001527 }
Chris Masonbb803952007-03-01 12:04:21 -05001528 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001529 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001530 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001531 unlock_up(p, level, lowest_unlock);
1532
Chris Mason9f3a7422007-08-07 15:52:19 -04001533 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001534 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001535 ret = 0;
1536 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001537 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001538
Chris Mason594a24e2008-06-25 16:01:30 -04001539 blocknr = btrfs_node_blockptr(b, slot);
1540 gen = btrfs_node_ptr_generation(b, slot);
1541 blocksize = btrfs_level_size(root, level - 1);
1542
1543 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1544 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001545 b = tmp;
1546 } else {
1547 /*
1548 * reduce lock contention at high levels
1549 * of the btree by dropping locks before
1550 * we read.
1551 */
1552 if (level > 1) {
1553 btrfs_release_path(NULL, p);
1554 if (tmp)
1555 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001556 if (should_reada)
1557 reada_for_search(root, p,
1558 level, slot,
1559 key->objectid);
1560
Chris Mason594a24e2008-06-25 16:01:30 -04001561 tmp = read_tree_block(root, blocknr,
1562 blocksize, gen);
1563 if (tmp)
1564 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001565 goto again;
1566 } else {
Chris Masona74a4b92008-06-25 16:01:31 -04001567 if (tmp)
1568 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001569 if (should_reada)
1570 reada_for_search(root, p,
1571 level, slot,
1572 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001573 b = read_node_slot(root, b, slot);
1574 }
1575 }
Chris Mason5cd57b22008-06-25 16:01:30 -04001576 if (!p->skip_locking)
1577 btrfs_tree_lock(b);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001578 } else {
1579 p->slots[level] = slot;
Chris Mason5f39d392007-10-15 16:14:19 -04001580 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
Chris Mason0783fcf2007-03-12 20:12:07 -04001581 sizeof(struct btrfs_item) + ins_len) {
Chris Masond4dbff92007-04-04 14:08:15 -04001582 int sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001583 p, ins_len, ret == 0);
Chris Mason5c680ed2007-02-22 11:39:13 -05001584 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001585 if (sret) {
1586 ret = sret;
1587 goto done;
1588 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001589 }
Chris Mason925baed2008-06-25 16:01:30 -04001590 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001591 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001592 }
1593 }
Chris Mason65b51a02008-08-01 15:11:20 -04001594 ret = 1;
1595done:
1596 if (prealloc_block.objectid) {
1597 btrfs_free_reserved_extent(root,
1598 prealloc_block.objectid,
1599 prealloc_block.offset);
1600 }
1601
1602 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001603}
1604
Zheng Yan1a40e232008-09-26 10:09:34 -04001605int btrfs_merge_path(struct btrfs_trans_handle *trans,
1606 struct btrfs_root *root,
1607 struct btrfs_key *node_keys,
1608 u64 *nodes, int lowest_level)
1609{
1610 struct extent_buffer *eb;
1611 struct extent_buffer *parent;
1612 struct btrfs_key key;
1613 u64 bytenr;
1614 u64 generation;
1615 u32 blocksize;
1616 int level;
1617 int slot;
1618 int key_match;
1619 int ret;
1620
1621 eb = btrfs_lock_root_node(root);
1622 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1623 BUG_ON(ret);
1624
1625 parent = eb;
1626 while (1) {
1627 level = btrfs_header_level(parent);
1628 if (level == 0 || level <= lowest_level)
1629 break;
1630
1631 ret = bin_search(parent, &node_keys[lowest_level], level,
1632 &slot);
1633 if (ret && slot > 0)
1634 slot--;
1635
1636 bytenr = btrfs_node_blockptr(parent, slot);
1637 if (nodes[level - 1] == bytenr)
1638 break;
1639
1640 blocksize = btrfs_level_size(root, level - 1);
1641 generation = btrfs_node_ptr_generation(parent, slot);
1642 btrfs_node_key_to_cpu(eb, &key, slot);
1643 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1644
Yan Zhengf82d02d2008-10-29 14:49:05 -04001645 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001646 eb = read_tree_block(root, bytenr, blocksize,
1647 generation);
1648 btrfs_tree_lock(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001649 }
1650
1651 /*
1652 * if node keys match and node pointer hasn't been modified
1653 * in the running transaction, we can merge the path. for
1654 * blocks owened by reloc trees, the node pointer check is
1655 * skipped, this is because these blocks are fully controlled
1656 * by the space balance code, no one else can modify them.
1657 */
1658 if (!nodes[level - 1] || !key_match ||
1659 (generation == trans->transid &&
1660 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1661 if (level == 1 || level == lowest_level + 1) {
1662 if (generation == trans->transid) {
1663 btrfs_tree_unlock(eb);
1664 free_extent_buffer(eb);
1665 }
1666 break;
1667 }
1668
1669 if (generation != trans->transid) {
1670 eb = read_tree_block(root, bytenr, blocksize,
1671 generation);
1672 btrfs_tree_lock(eb);
1673 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001674
1675 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1676 &eb, 0);
1677 BUG_ON(ret);
1678
Yan Zhengf82d02d2008-10-29 14:49:05 -04001679 if (root->root_key.objectid ==
1680 BTRFS_TREE_RELOC_OBJECTID) {
1681 if (!nodes[level - 1]) {
1682 nodes[level - 1] = eb->start;
1683 memcpy(&node_keys[level - 1], &key,
1684 sizeof(node_keys[0]));
1685 } else {
1686 WARN_ON(1);
1687 }
1688 }
1689
Zheng Yan1a40e232008-09-26 10:09:34 -04001690 btrfs_tree_unlock(parent);
1691 free_extent_buffer(parent);
1692 parent = eb;
1693 continue;
1694 }
1695
Zheng Yan1a40e232008-09-26 10:09:34 -04001696 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1697 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1698 btrfs_mark_buffer_dirty(parent);
1699
1700 ret = btrfs_inc_extent_ref(trans, root,
1701 nodes[level - 1],
1702 blocksize, parent->start,
1703 btrfs_header_owner(parent),
1704 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001705 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001706 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001707
1708 /*
1709 * If the block was created in the running transaction,
1710 * it's possible this is the last reference to it, so we
1711 * should drop the subtree.
1712 */
1713 if (generation == trans->transid) {
1714 ret = btrfs_drop_subtree(trans, root, eb, parent);
1715 BUG_ON(ret);
1716 btrfs_tree_unlock(eb);
1717 free_extent_buffer(eb);
1718 } else {
1719 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001720 blocksize, parent->start,
1721 btrfs_header_owner(parent),
1722 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001723 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001724 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001725 }
1726 break;
1727 }
1728 btrfs_tree_unlock(parent);
1729 free_extent_buffer(parent);
1730 return 0;
1731}
1732
Chris Mason74123bd2007-02-02 11:05:29 -05001733/*
1734 * adjust the pointers going up the tree, starting at level
1735 * making sure the right key of each node is points to 'key'.
1736 * This is used after shifting pointers to the left, so it stops
1737 * fixing up pointers when a given leaf/node is not in slot 0 of the
1738 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001739 *
1740 * If this fails to write a tree block, it returns -1, but continues
1741 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001742 */
Chris Mason5f39d392007-10-15 16:14:19 -04001743static int fixup_low_keys(struct btrfs_trans_handle *trans,
1744 struct btrfs_root *root, struct btrfs_path *path,
1745 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001746{
1747 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001748 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001749 struct extent_buffer *t;
1750
Chris Mason234b63a2007-03-13 10:46:10 -04001751 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001752 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001753 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001754 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001755 t = path->nodes[i];
1756 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001757 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001758 if (tslot != 0)
1759 break;
1760 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001761 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001762}
1763
Chris Mason74123bd2007-02-02 11:05:29 -05001764/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001765 * update item key.
1766 *
1767 * This function isn't completely safe. It's the caller's responsibility
1768 * that the new key won't break the order
1769 */
1770int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1771 struct btrfs_root *root, struct btrfs_path *path,
1772 struct btrfs_key *new_key)
1773{
1774 struct btrfs_disk_key disk_key;
1775 struct extent_buffer *eb;
1776 int slot;
1777
1778 eb = path->nodes[0];
1779 slot = path->slots[0];
1780 if (slot > 0) {
1781 btrfs_item_key(eb, &disk_key, slot - 1);
1782 if (comp_keys(&disk_key, new_key) >= 0)
1783 return -1;
1784 }
1785 if (slot < btrfs_header_nritems(eb) - 1) {
1786 btrfs_item_key(eb, &disk_key, slot + 1);
1787 if (comp_keys(&disk_key, new_key) <= 0)
1788 return -1;
1789 }
1790
1791 btrfs_cpu_key_to_disk(&disk_key, new_key);
1792 btrfs_set_item_key(eb, &disk_key, slot);
1793 btrfs_mark_buffer_dirty(eb);
1794 if (slot == 0)
1795 fixup_low_keys(trans, root, path, &disk_key, 1);
1796 return 0;
1797}
1798
1799/*
Chris Mason74123bd2007-02-02 11:05:29 -05001800 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001801 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001802 *
1803 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1804 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001805 */
Chris Mason98ed5172008-01-03 10:01:48 -05001806static int push_node_left(struct btrfs_trans_handle *trans,
1807 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001808 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001809{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001810 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001811 int src_nritems;
1812 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001813 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001814
Chris Mason5f39d392007-10-15 16:14:19 -04001815 src_nritems = btrfs_header_nritems(src);
1816 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001817 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001818 WARN_ON(btrfs_header_generation(src) != trans->transid);
1819 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001820
Chris Masonbce4eae2008-04-24 14:42:46 -04001821 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001822 return 1;
1823
Chris Masoneb60cea2007-02-02 09:18:22 -05001824 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001825 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001826 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001827
Chris Masonbce4eae2008-04-24 14:42:46 -04001828 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001829 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001830 if (push_items < src_nritems) {
1831 /* leave at least 8 pointers in the node if
1832 * we aren't going to empty it
1833 */
1834 if (src_nritems - push_items < 8) {
1835 if (push_items <= 8)
1836 return 1;
1837 push_items -= 8;
1838 }
1839 }
1840 } else
1841 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001842
Chris Mason5f39d392007-10-15 16:14:19 -04001843 copy_extent_buffer(dst, src,
1844 btrfs_node_key_ptr_offset(dst_nritems),
1845 btrfs_node_key_ptr_offset(0),
1846 push_items * sizeof(struct btrfs_key_ptr));
1847
Chris Masonbb803952007-03-01 12:04:21 -05001848 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001849 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1850 btrfs_node_key_ptr_offset(push_items),
1851 (src_nritems - push_items) *
1852 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001853 }
Chris Mason5f39d392007-10-15 16:14:19 -04001854 btrfs_set_header_nritems(src, src_nritems - push_items);
1855 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1856 btrfs_mark_buffer_dirty(src);
1857 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001858
1859 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1860 BUG_ON(ret);
1861
Chris Masonbb803952007-03-01 12:04:21 -05001862 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001863}
1864
Chris Mason97571fd2007-02-24 13:39:08 -05001865/*
Chris Mason79f95c82007-03-01 15:16:26 -05001866 * try to push data from one node into the next node right in the
1867 * tree.
1868 *
1869 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1870 * error, and > 0 if there was no room in the right hand block.
1871 *
1872 * this will only push up to 1/2 the contents of the left node over
1873 */
Chris Mason5f39d392007-10-15 16:14:19 -04001874static int balance_node_right(struct btrfs_trans_handle *trans,
1875 struct btrfs_root *root,
1876 struct extent_buffer *dst,
1877 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05001878{
Chris Mason79f95c82007-03-01 15:16:26 -05001879 int push_items = 0;
1880 int max_push;
1881 int src_nritems;
1882 int dst_nritems;
1883 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05001884
Chris Mason7bb86312007-12-11 09:25:06 -05001885 WARN_ON(btrfs_header_generation(src) != trans->transid);
1886 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1887
Chris Mason5f39d392007-10-15 16:14:19 -04001888 src_nritems = btrfs_header_nritems(src);
1889 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001890 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masonbce4eae2008-04-24 14:42:46 -04001891 if (push_items <= 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001892 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001893 }
1894
1895 if (src_nritems < 4) {
1896 return 1;
1897 }
Chris Mason79f95c82007-03-01 15:16:26 -05001898
1899 max_push = src_nritems / 2 + 1;
1900 /* don't try to empty the node */
Chris Masonbce4eae2008-04-24 14:42:46 -04001901 if (max_push >= src_nritems) {
Chris Mason79f95c82007-03-01 15:16:26 -05001902 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04001903 }
Yan252c38f2007-08-29 09:11:44 -04001904
Chris Mason79f95c82007-03-01 15:16:26 -05001905 if (max_push < push_items)
1906 push_items = max_push;
1907
Chris Mason5f39d392007-10-15 16:14:19 -04001908 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1909 btrfs_node_key_ptr_offset(0),
1910 (dst_nritems) *
1911 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04001912
Chris Mason5f39d392007-10-15 16:14:19 -04001913 copy_extent_buffer(dst, src,
1914 btrfs_node_key_ptr_offset(0),
1915 btrfs_node_key_ptr_offset(src_nritems - push_items),
1916 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05001917
Chris Mason5f39d392007-10-15 16:14:19 -04001918 btrfs_set_header_nritems(src, src_nritems - push_items);
1919 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001920
Chris Mason5f39d392007-10-15 16:14:19 -04001921 btrfs_mark_buffer_dirty(src);
1922 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001923
1924 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1925 BUG_ON(ret);
1926
Chris Mason79f95c82007-03-01 15:16:26 -05001927 return ret;
1928}
1929
1930/*
Chris Mason97571fd2007-02-24 13:39:08 -05001931 * helper function to insert a new root level in the tree.
1932 * A new node is allocated, and a single item is inserted to
1933 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05001934 *
1935 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05001936 */
Chris Mason98ed5172008-01-03 10:01:48 -05001937static int noinline insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001938 struct btrfs_root *root,
1939 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05001940{
Chris Mason7bb86312007-12-11 09:25:06 -05001941 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04001942 struct extent_buffer *lower;
1943 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04001944 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04001945 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04001946 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05001947
1948 BUG_ON(path->nodes[level]);
1949 BUG_ON(path->nodes[level-1] != root->node);
1950
Chris Mason7bb86312007-12-11 09:25:06 -05001951 lower = path->nodes[level-1];
1952 if (level == 1)
1953 btrfs_item_key(lower, &lower_key, 0);
1954 else
1955 btrfs_node_key(lower, &lower_key, 0);
1956
Zheng Yan31840ae2008-09-23 13:14:14 -04001957 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1958 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04001959 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001960 if (IS_ERR(c))
1961 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04001962
Chris Mason5f39d392007-10-15 16:14:19 -04001963 memset_extent_buffer(c, 0, 0, root->nodesize);
1964 btrfs_set_header_nritems(c, 1);
1965 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04001966 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001967 btrfs_set_header_generation(c, trans->transid);
1968 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04001969
Chris Mason5f39d392007-10-15 16:14:19 -04001970 write_extent_buffer(c, root->fs_info->fsid,
1971 (unsigned long)btrfs_header_fsid(c),
1972 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001973
1974 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1975 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1976 BTRFS_UUID_SIZE);
1977
Chris Mason5f39d392007-10-15 16:14:19 -04001978 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04001979 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05001980 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04001981 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001982
1983 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04001984
1985 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04001986
Chris Mason925baed2008-06-25 16:01:30 -04001987 spin_lock(&root->node_lock);
1988 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04001989 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04001990 spin_unlock(&root->node_lock);
1991
Zheng Yan31840ae2008-09-23 13:14:14 -04001992 ret = btrfs_update_extent_ref(trans, root, lower->start,
1993 lower->start, c->start,
1994 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001995 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001996 BUG_ON(ret);
1997
Chris Mason925baed2008-06-25 16:01:30 -04001998 /* the super has an extra ref to root->node */
1999 free_extent_buffer(old);
2000
Chris Mason0b86a832008-03-24 15:01:56 -04002001 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002002 extent_buffer_get(c);
2003 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002004 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002005 path->slots[level] = 0;
2006 return 0;
2007}
2008
Chris Mason74123bd2007-02-02 11:05:29 -05002009/*
2010 * worker function to insert a single pointer in a node.
2011 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002012 *
Chris Mason74123bd2007-02-02 11:05:29 -05002013 * slot and level indicate where you want the key to go, and
2014 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002015 *
2016 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002017 */
Chris Masone089f052007-03-16 16:20:31 -04002018static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2019 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002020 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002021{
Chris Mason5f39d392007-10-15 16:14:19 -04002022 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002023 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002024
2025 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002026 lower = path->nodes[level];
2027 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002028 if (slot > nritems)
2029 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002030 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002031 BUG();
2032 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002033 memmove_extent_buffer(lower,
2034 btrfs_node_key_ptr_offset(slot + 1),
2035 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002036 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002037 }
Chris Mason5f39d392007-10-15 16:14:19 -04002038 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002039 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002040 WARN_ON(trans->transid == 0);
2041 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002042 btrfs_set_header_nritems(lower, nritems + 1);
2043 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002044 return 0;
2045}
2046
Chris Mason97571fd2007-02-24 13:39:08 -05002047/*
2048 * split the node at the specified level in path in two.
2049 * The path is corrected to point to the appropriate node after the split
2050 *
2051 * Before splitting this tries to make some room in the node by pushing
2052 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002053 *
2054 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002055 */
Chris Masone02119d2008-09-05 16:13:11 -04002056static noinline int split_node(struct btrfs_trans_handle *trans,
2057 struct btrfs_root *root,
2058 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002059{
Chris Mason5f39d392007-10-15 16:14:19 -04002060 struct extent_buffer *c;
2061 struct extent_buffer *split;
2062 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002063 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002064 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002065 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002066 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002067
Chris Mason5f39d392007-10-15 16:14:19 -04002068 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002069 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002070 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002071 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002072 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002073 if (ret)
2074 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002075 } else {
2076 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002077 c = path->nodes[level];
2078 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002079 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002080 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002081 if (ret < 0)
2082 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002083 }
Chris Masone66f7092007-04-20 13:16:02 -04002084
Chris Mason5f39d392007-10-15 16:14:19 -04002085 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002086
Chris Mason925baed2008-06-25 16:01:30 -04002087 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002088 path->nodes[level + 1]->start,
2089 root->root_key.objectid,
2090 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002091 if (IS_ERR(split))
2092 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002093
Chris Mason5f39d392007-10-15 16:14:19 -04002094 btrfs_set_header_flags(split, btrfs_header_flags(c));
2095 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002096 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002097 btrfs_set_header_generation(split, trans->transid);
2098 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002099 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 write_extent_buffer(split, root->fs_info->fsid,
2101 (unsigned long)btrfs_header_fsid(split),
2102 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002103 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2104 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2105 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002106
Chris Mason7518a232007-03-12 12:01:18 -04002107 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002108
2109 copy_extent_buffer(split, c,
2110 btrfs_node_key_ptr_offset(0),
2111 btrfs_node_key_ptr_offset(mid),
2112 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2113 btrfs_set_header_nritems(split, c_nritems - mid);
2114 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002115 ret = 0;
2116
Chris Mason5f39d392007-10-15 16:14:19 -04002117 btrfs_mark_buffer_dirty(c);
2118 btrfs_mark_buffer_dirty(split);
2119
2120 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002121 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002122 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002123 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002124 if (wret)
2125 ret = wret;
2126
Zheng Yan31840ae2008-09-23 13:14:14 -04002127 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2128 BUG_ON(ret);
2129
Chris Mason5de08d72007-02-24 06:24:44 -05002130 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002131 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002132 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002133 free_extent_buffer(c);
2134 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002135 path->slots[level + 1] += 1;
2136 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002137 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002138 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002139 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002140 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002141}
2142
Chris Mason74123bd2007-02-02 11:05:29 -05002143/*
2144 * how many bytes are required to store the items in a leaf. start
2145 * and nr indicate which items in the leaf to check. This totals up the
2146 * space used both by the item structs and the item data
2147 */
Chris Mason5f39d392007-10-15 16:14:19 -04002148static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002149{
2150 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002151 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002152 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002153
2154 if (!nr)
2155 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002156 data_len = btrfs_item_end_nr(l, start);
2157 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002158 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002159 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002160 return data_len;
2161}
2162
Chris Mason74123bd2007-02-02 11:05:29 -05002163/*
Chris Masond4dbff92007-04-04 14:08:15 -04002164 * The space between the end of the leaf items and
2165 * the start of the leaf data. IOW, how much room
2166 * the leaf has left for both items and data
2167 */
Chris Masone02119d2008-09-05 16:13:11 -04002168int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2169 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002170{
Chris Mason5f39d392007-10-15 16:14:19 -04002171 int nritems = btrfs_header_nritems(leaf);
2172 int ret;
2173 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2174 if (ret < 0) {
2175 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002176 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002177 leaf_space_used(leaf, 0, nritems), nritems);
2178 }
2179 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002180}
2181
2182/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002183 * push some data in the path leaf to the right, trying to free up at
2184 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002185 *
2186 * returns 1 if the push failed because the other node didn't have enough
2187 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002188 */
Chris Masone089f052007-03-16 16:20:31 -04002189static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002190 *root, struct btrfs_path *path, int data_size,
2191 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002192{
Chris Mason5f39d392007-10-15 16:14:19 -04002193 struct extent_buffer *left = path->nodes[0];
2194 struct extent_buffer *right;
2195 struct extent_buffer *upper;
2196 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002197 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002198 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002199 int free_space;
2200 int push_space = 0;
2201 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002202 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002203 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002204 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002205 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002206 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002207 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002208 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002209
2210 slot = path->slots[1];
2211 if (!path->nodes[1]) {
2212 return 1;
2213 }
2214 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002215 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002216 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002217
Chris Masona2135012008-06-25 16:01:30 -04002218 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2219
Chris Masonca7a79a2008-05-12 12:59:19 -04002220 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002221 btrfs_tree_lock(right);
Chris Mason123abc82007-03-14 14:14:43 -04002222 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002223 if (free_space < data_size + sizeof(struct btrfs_item))
2224 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002225
Chris Mason5f39d392007-10-15 16:14:19 -04002226 /* cow and double check */
2227 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002228 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002229 if (ret)
2230 goto out_unlock;
2231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 free_space = btrfs_leaf_free_space(root, right);
Chris Mason925baed2008-06-25 16:01:30 -04002233 if (free_space < data_size + sizeof(struct btrfs_item))
2234 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002235
2236 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002237 if (left_nritems == 0)
2238 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002239
Chris Mason34a38212007-11-07 13:31:03 -05002240 if (empty)
2241 nr = 0;
2242 else
2243 nr = 1;
2244
Zheng Yan31840ae2008-09-23 13:14:14 -04002245 if (path->slots[0] >= left_nritems)
2246 push_space += data_size + sizeof(*item);
2247
Chris Mason34a38212007-11-07 13:31:03 -05002248 i = left_nritems - 1;
2249 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002250 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002251
Zheng Yan31840ae2008-09-23 13:14:14 -04002252 if (!empty && push_items > 0) {
2253 if (path->slots[0] > i)
2254 break;
2255 if (path->slots[0] == i) {
2256 int space = btrfs_leaf_free_space(root, left);
2257 if (space + push_space * 2 > free_space)
2258 break;
2259 }
2260 }
2261
Chris Mason00ec4c52007-02-24 12:47:20 -05002262 if (path->slots[0] == i)
2263 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002264
2265 if (!left->map_token) {
2266 map_extent_buffer(left, (unsigned long)item,
2267 sizeof(struct btrfs_item),
2268 &left->map_token, &left->kaddr,
2269 &left->map_start, &left->map_len,
2270 KM_USER1);
2271 }
2272
2273 this_item_size = btrfs_item_size(left, item);
2274 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002275 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002276
Chris Mason00ec4c52007-02-24 12:47:20 -05002277 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002278 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002279 if (i == 0)
2280 break;
2281 i--;
Chris Masondb945352007-10-15 16:15:53 -04002282 }
2283 if (left->map_token) {
2284 unmap_extent_buffer(left, left->map_token, KM_USER1);
2285 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002286 }
Chris Mason5f39d392007-10-15 16:14:19 -04002287
Chris Mason925baed2008-06-25 16:01:30 -04002288 if (push_items == 0)
2289 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002290
Chris Mason34a38212007-11-07 13:31:03 -05002291 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002292 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002293
Chris Mason00ec4c52007-02-24 12:47:20 -05002294 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002295 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002296
Chris Mason5f39d392007-10-15 16:14:19 -04002297 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002298 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002299
Chris Mason00ec4c52007-02-24 12:47:20 -05002300 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002301 data_end = leaf_data_end(root, right);
2302 memmove_extent_buffer(right,
2303 btrfs_leaf_data(right) + data_end - push_space,
2304 btrfs_leaf_data(right) + data_end,
2305 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2306
Chris Mason00ec4c52007-02-24 12:47:20 -05002307 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002308 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002309 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2310 btrfs_leaf_data(left) + leaf_data_end(root, left),
2311 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002312
2313 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2314 btrfs_item_nr_offset(0),
2315 right_nritems * sizeof(struct btrfs_item));
2316
Chris Mason00ec4c52007-02-24 12:47:20 -05002317 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002318 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2319 btrfs_item_nr_offset(left_nritems - push_items),
2320 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002321
2322 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002323 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002324 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002325 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002326 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002327 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002328 if (!right->map_token) {
2329 map_extent_buffer(right, (unsigned long)item,
2330 sizeof(struct btrfs_item),
2331 &right->map_token, &right->kaddr,
2332 &right->map_start, &right->map_len,
2333 KM_USER1);
2334 }
2335 push_space -= btrfs_item_size(right, item);
2336 btrfs_set_item_offset(right, item, push_space);
2337 }
2338
2339 if (right->map_token) {
2340 unmap_extent_buffer(right, right->map_token, KM_USER1);
2341 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002342 }
Chris Mason7518a232007-03-12 12:01:18 -04002343 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002344 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002345
Chris Mason34a38212007-11-07 13:31:03 -05002346 if (left_nritems)
2347 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002348 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002349
Zheng Yan31840ae2008-09-23 13:14:14 -04002350 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2351 BUG_ON(ret);
2352
Chris Mason5f39d392007-10-15 16:14:19 -04002353 btrfs_item_key(right, &disk_key, 0);
2354 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002355 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002356
Chris Mason00ec4c52007-02-24 12:47:20 -05002357 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002358 if (path->slots[0] >= left_nritems) {
2359 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002360 if (btrfs_header_nritems(path->nodes[0]) == 0)
2361 clean_tree_block(trans, root, path->nodes[0]);
2362 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002363 free_extent_buffer(path->nodes[0]);
2364 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002365 path->slots[1] += 1;
2366 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002367 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002368 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002369 }
2370 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002371
2372out_unlock:
2373 btrfs_tree_unlock(right);
2374 free_extent_buffer(right);
2375 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002376}
Chris Mason925baed2008-06-25 16:01:30 -04002377
Chris Mason00ec4c52007-02-24 12:47:20 -05002378/*
Chris Mason74123bd2007-02-02 11:05:29 -05002379 * push some data in the path leaf to the left, trying to free up at
2380 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2381 */
Chris Masone089f052007-03-16 16:20:31 -04002382static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002383 *root, struct btrfs_path *path, int data_size,
2384 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002385{
Chris Mason5f39d392007-10-15 16:14:19 -04002386 struct btrfs_disk_key disk_key;
2387 struct extent_buffer *right = path->nodes[0];
2388 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002389 int slot;
2390 int i;
2391 int free_space;
2392 int push_space = 0;
2393 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002394 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002395 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002396 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002397 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002398 int ret = 0;
2399 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002400 u32 this_item_size;
2401 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002402
2403 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002404 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002405 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002406 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002407 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002408
Chris Mason3685f792007-10-19 09:23:27 -04002409 right_nritems = btrfs_header_nritems(right);
2410 if (right_nritems == 0) {
2411 return 1;
2412 }
2413
Chris Masona2135012008-06-25 16:01:30 -04002414 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2415
Chris Masonca7a79a2008-05-12 12:59:19 -04002416 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002417 btrfs_tree_lock(left);
Chris Mason123abc82007-03-14 14:14:43 -04002418 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002419 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002420 ret = 1;
2421 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002422 }
Chris Mason02217ed2007-03-02 16:08:05 -05002423
2424 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002425 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002426 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002427 if (ret) {
2428 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002429 ret = 1;
2430 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002431 }
Chris Mason3685f792007-10-19 09:23:27 -04002432
Chris Mason123abc82007-03-14 14:14:43 -04002433 free_space = btrfs_leaf_free_space(root, left);
Chris Mason0783fcf2007-03-12 20:12:07 -04002434 if (free_space < data_size + sizeof(struct btrfs_item)) {
Chris Mason925baed2008-06-25 16:01:30 -04002435 ret = 1;
2436 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002437 }
2438
Chris Mason34a38212007-11-07 13:31:03 -05002439 if (empty)
2440 nr = right_nritems;
2441 else
2442 nr = right_nritems - 1;
2443
2444 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002445 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002446 if (!right->map_token) {
2447 map_extent_buffer(right, (unsigned long)item,
2448 sizeof(struct btrfs_item),
2449 &right->map_token, &right->kaddr,
2450 &right->map_start, &right->map_len,
2451 KM_USER1);
2452 }
2453
Zheng Yan31840ae2008-09-23 13:14:14 -04002454 if (!empty && push_items > 0) {
2455 if (path->slots[0] < i)
2456 break;
2457 if (path->slots[0] == i) {
2458 int space = btrfs_leaf_free_space(root, right);
2459 if (space + push_space * 2 > free_space)
2460 break;
2461 }
2462 }
2463
Chris Masonbe0e5c02007-01-26 15:51:26 -05002464 if (path->slots[0] == i)
2465 push_space += data_size + sizeof(*item);
Chris Masondb945352007-10-15 16:15:53 -04002466
2467 this_item_size = btrfs_item_size(right, item);
2468 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002469 break;
Chris Masondb945352007-10-15 16:15:53 -04002470
Chris Masonbe0e5c02007-01-26 15:51:26 -05002471 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002472 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002473 }
Chris Masondb945352007-10-15 16:15:53 -04002474
2475 if (right->map_token) {
2476 unmap_extent_buffer(right, right->map_token, KM_USER1);
2477 right->map_token = NULL;
2478 }
2479
Chris Masonbe0e5c02007-01-26 15:51:26 -05002480 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002481 ret = 1;
2482 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002483 }
Chris Mason34a38212007-11-07 13:31:03 -05002484 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002485 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002486
Chris Masonbe0e5c02007-01-26 15:51:26 -05002487 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002488 copy_extent_buffer(left, right,
2489 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2490 btrfs_item_nr_offset(0),
2491 push_items * sizeof(struct btrfs_item));
2492
Chris Mason123abc82007-03-14 14:14:43 -04002493 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Mason5f39d392007-10-15 16:14:19 -04002494 btrfs_item_offset_nr(right, push_items -1);
2495
2496 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002497 leaf_data_end(root, left) - push_space,
2498 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002499 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002500 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002501 old_left_nritems = btrfs_header_nritems(left);
Chris Masoneb60cea2007-02-02 09:18:22 -05002502 BUG_ON(old_left_nritems < 0);
2503
Chris Masondb945352007-10-15 16:15:53 -04002504 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002505 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002506 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002507
Chris Mason5f39d392007-10-15 16:14:19 -04002508 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002509 if (!left->map_token) {
2510 map_extent_buffer(left, (unsigned long)item,
2511 sizeof(struct btrfs_item),
2512 &left->map_token, &left->kaddr,
2513 &left->map_start, &left->map_len,
2514 KM_USER1);
2515 }
2516
Chris Mason5f39d392007-10-15 16:14:19 -04002517 ioff = btrfs_item_offset(left, item);
2518 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002519 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002520 }
Chris Mason5f39d392007-10-15 16:14:19 -04002521 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002522 if (left->map_token) {
2523 unmap_extent_buffer(left, left->map_token, KM_USER1);
2524 left->map_token = NULL;
2525 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002526
2527 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002528 if (push_items > right_nritems) {
2529 printk("push items %d nr %u\n", push_items, right_nritems);
2530 WARN_ON(1);
2531 }
Chris Mason5f39d392007-10-15 16:14:19 -04002532
Chris Mason34a38212007-11-07 13:31:03 -05002533 if (push_items < right_nritems) {
2534 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2535 leaf_data_end(root, right);
2536 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2537 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2538 btrfs_leaf_data(right) +
2539 leaf_data_end(root, right), push_space);
2540
2541 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002542 btrfs_item_nr_offset(push_items),
2543 (btrfs_header_nritems(right) - push_items) *
2544 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002545 }
Yaneef1c492007-11-26 10:58:13 -05002546 right_nritems -= push_items;
2547 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002548 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002549 for (i = 0; i < right_nritems; i++) {
2550 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002551
2552 if (!right->map_token) {
2553 map_extent_buffer(right, (unsigned long)item,
2554 sizeof(struct btrfs_item),
2555 &right->map_token, &right->kaddr,
2556 &right->map_start, &right->map_len,
2557 KM_USER1);
2558 }
2559
2560 push_space = push_space - btrfs_item_size(right, item);
2561 btrfs_set_item_offset(right, item, push_space);
2562 }
2563 if (right->map_token) {
2564 unmap_extent_buffer(right, right->map_token, KM_USER1);
2565 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002566 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002567
Chris Mason5f39d392007-10-15 16:14:19 -04002568 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002569 if (right_nritems)
2570 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002571
Zheng Yan31840ae2008-09-23 13:14:14 -04002572 ret = btrfs_update_ref(trans, root, right, left,
2573 old_left_nritems, push_items);
2574 BUG_ON(ret);
2575
Chris Mason5f39d392007-10-15 16:14:19 -04002576 btrfs_item_key(right, &disk_key, 0);
2577 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002578 if (wret)
2579 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002580
2581 /* then fixup the leaf pointer in the path */
2582 if (path->slots[0] < push_items) {
2583 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002584 if (btrfs_header_nritems(path->nodes[0]) == 0)
2585 clean_tree_block(trans, root, path->nodes[0]);
2586 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002587 free_extent_buffer(path->nodes[0]);
2588 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002589 path->slots[1] -= 1;
2590 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002591 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002592 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002593 path->slots[0] -= push_items;
2594 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002595 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002596 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002597out:
2598 btrfs_tree_unlock(left);
2599 free_extent_buffer(left);
2600 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002601}
2602
Chris Mason74123bd2007-02-02 11:05:29 -05002603/*
2604 * split the path's leaf in two, making sure there is at least data_size
2605 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002606 *
2607 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002608 */
Chris Masone02119d2008-09-05 16:13:11 -04002609static noinline int split_leaf(struct btrfs_trans_handle *trans,
2610 struct btrfs_root *root,
2611 struct btrfs_key *ins_key,
2612 struct btrfs_path *path, int data_size,
2613 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002614{
Chris Mason5f39d392007-10-15 16:14:19 -04002615 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002616 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002617 int mid;
2618 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002619 struct extent_buffer *right;
Chris Mason0783fcf2007-03-12 20:12:07 -04002620 int space_needed = data_size + sizeof(struct btrfs_item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002621 int data_copy_size;
2622 int rt_data_off;
2623 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002624 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002625 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002626 int double_split;
2627 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002628 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002629
Chris Masoncc0c5532007-10-25 15:42:57 -04002630 if (extend)
2631 space_needed = data_size;
2632
Chris Mason40689472007-03-17 14:29:23 -04002633 /* first try to make some room by pushing left and right */
Chris Mason3685f792007-10-19 09:23:27 -04002634 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002635 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002636 if (wret < 0) {
Chris Masoneaee50e2007-03-13 11:17:52 -04002637 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002638 }
2639 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002640 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002641 if (wret < 0)
2642 return wret;
2643 }
2644 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002645
Chris Mason3685f792007-10-19 09:23:27 -04002646 /* did the pushes work? */
Chris Masoncc0c5532007-10-25 15:42:57 -04002647 if (btrfs_leaf_free_space(root, l) >= space_needed)
Chris Mason3685f792007-10-19 09:23:27 -04002648 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002649 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002650
Chris Mason5c680ed2007-02-22 11:39:13 -05002651 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002652 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002653 if (ret)
2654 return ret;
2655 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002656again:
2657 double_split = 0;
2658 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002659 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002660 nritems = btrfs_header_nritems(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002661 mid = (nritems + 1)/ 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002662
Chris Mason925baed2008-06-25 16:01:30 -04002663 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002664 path->nodes[1]->start,
2665 root->root_key.objectid,
2666 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002667 if (IS_ERR(right)) {
2668 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002669 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002670 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002671
Chris Mason5f39d392007-10-15 16:14:19 -04002672 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002673 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002674 btrfs_set_header_generation(right, trans->transid);
2675 btrfs_set_header_owner(right, root->root_key.objectid);
2676 btrfs_set_header_level(right, 0);
2677 write_extent_buffer(right, root->fs_info->fsid,
2678 (unsigned long)btrfs_header_fsid(right),
2679 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002680
2681 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2682 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2683 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002684 if (mid <= slot) {
2685 if (nritems == 1 ||
2686 leaf_space_used(l, mid, nritems - mid) + space_needed >
2687 BTRFS_LEAF_DATA_SIZE(root)) {
2688 if (slot >= nritems) {
2689 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002690 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002691 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002692 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002693 path->slots[1] + 1, 1);
2694 if (wret)
2695 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002696
2697 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002698 free_extent_buffer(path->nodes[0]);
2699 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002700 path->slots[0] = 0;
2701 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002702 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002703 return ret;
2704 }
2705 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002706 if (mid != nritems &&
2707 leaf_space_used(l, mid, nritems - mid) +
2708 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2709 double_split = 1;
2710 }
Chris Masond4dbff92007-04-04 14:08:15 -04002711 }
2712 } else {
2713 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2714 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002715 if (!extend && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002716 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002717 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002718 wret = insert_ptr(trans, root, path,
2719 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002720 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002721 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002722 if (wret)
2723 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002724 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002725 free_extent_buffer(path->nodes[0]);
2726 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002727 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002728 if (path->slots[1] == 0) {
2729 wret = fixup_low_keys(trans, root,
2730 path, &disk_key, 1);
2731 if (wret)
2732 ret = wret;
2733 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002734 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002735 return ret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002736 } else if (extend && slot == 0) {
2737 mid = 1;
2738 } else {
2739 mid = slot;
2740 if (mid != nritems &&
2741 leaf_space_used(l, mid, nritems - mid) +
2742 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2743 double_split = 1;
2744 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002745 }
Chris Masond4dbff92007-04-04 14:08:15 -04002746 }
2747 }
Chris Mason5f39d392007-10-15 16:14:19 -04002748 nritems = nritems - mid;
2749 btrfs_set_header_nritems(right, nritems);
2750 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2751
2752 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2753 btrfs_item_nr_offset(mid),
2754 nritems * sizeof(struct btrfs_item));
2755
2756 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002757 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2758 data_copy_size, btrfs_leaf_data(l) +
2759 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002760
Chris Mason5f39d392007-10-15 16:14:19 -04002761 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2762 btrfs_item_end_nr(l, mid);
2763
2764 for (i = 0; i < nritems; i++) {
2765 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002766 u32 ioff;
2767
2768 if (!right->map_token) {
2769 map_extent_buffer(right, (unsigned long)item,
2770 sizeof(struct btrfs_item),
2771 &right->map_token, &right->kaddr,
2772 &right->map_start, &right->map_len,
2773 KM_USER1);
2774 }
2775
2776 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002777 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002778 }
Chris Mason74123bd2007-02-02 11:05:29 -05002779
Chris Masondb945352007-10-15 16:15:53 -04002780 if (right->map_token) {
2781 unmap_extent_buffer(right, right->map_token, KM_USER1);
2782 right->map_token = NULL;
2783 }
2784
Chris Mason5f39d392007-10-15 16:14:19 -04002785 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002786 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002787 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002788 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2789 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002790 if (wret)
2791 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002792
2793 btrfs_mark_buffer_dirty(right);
2794 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002795 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002796
Zheng Yan31840ae2008-09-23 13:14:14 -04002797 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2798 BUG_ON(ret);
2799
Chris Masonbe0e5c02007-01-26 15:51:26 -05002800 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002801 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002802 free_extent_buffer(path->nodes[0]);
2803 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002804 path->slots[0] -= mid;
2805 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002806 } else {
2807 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002808 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002809 }
Chris Mason5f39d392007-10-15 16:14:19 -04002810
Chris Masoneb60cea2007-02-02 09:18:22 -05002811 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002812
Chris Masoncc0c5532007-10-25 15:42:57 -04002813 if (double_split) {
2814 BUG_ON(num_doubles != 0);
2815 num_doubles++;
2816 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002817 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002818 return ret;
2819}
2820
Chris Masond352ac62008-09-29 15:18:18 -04002821/*
2822 * make the item pointed to by the path smaller. new_size indicates
2823 * how small to make it, and from_end tells us if we just chop bytes
2824 * off the end of the item or if we shift the item to chop bytes off
2825 * the front.
2826 */
Chris Masonb18c6682007-04-17 13:26:50 -04002827int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2828 struct btrfs_root *root,
2829 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04002830 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04002831{
2832 int ret = 0;
2833 int slot;
2834 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002835 struct extent_buffer *leaf;
2836 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04002837 u32 nritems;
2838 unsigned int data_end;
2839 unsigned int old_data_start;
2840 unsigned int old_size;
2841 unsigned int size_diff;
2842 int i;
2843
2844 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002845 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04002846 slot = path->slots[0];
2847
2848 old_size = btrfs_item_size_nr(leaf, slot);
2849 if (old_size == new_size)
2850 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04002851
Chris Mason5f39d392007-10-15 16:14:19 -04002852 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002853 data_end = leaf_data_end(root, leaf);
2854
Chris Mason5f39d392007-10-15 16:14:19 -04002855 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04002856
Chris Masonb18c6682007-04-17 13:26:50 -04002857 size_diff = old_size - new_size;
2858
2859 BUG_ON(slot < 0);
2860 BUG_ON(slot >= nritems);
2861
2862 /*
2863 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2864 */
2865 /* first correct the data pointers */
2866 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002867 u32 ioff;
2868 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002869
2870 if (!leaf->map_token) {
2871 map_extent_buffer(leaf, (unsigned long)item,
2872 sizeof(struct btrfs_item),
2873 &leaf->map_token, &leaf->kaddr,
2874 &leaf->map_start, &leaf->map_len,
2875 KM_USER1);
2876 }
2877
Chris Mason5f39d392007-10-15 16:14:19 -04002878 ioff = btrfs_item_offset(leaf, item);
2879 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04002880 }
Chris Masondb945352007-10-15 16:15:53 -04002881
2882 if (leaf->map_token) {
2883 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2884 leaf->map_token = NULL;
2885 }
2886
Chris Masonb18c6682007-04-17 13:26:50 -04002887 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04002888 if (from_end) {
2889 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2890 data_end + size_diff, btrfs_leaf_data(leaf) +
2891 data_end, old_data_start + new_size - data_end);
2892 } else {
2893 struct btrfs_disk_key disk_key;
2894 u64 offset;
2895
2896 btrfs_item_key(leaf, &disk_key, slot);
2897
2898 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2899 unsigned long ptr;
2900 struct btrfs_file_extent_item *fi;
2901
2902 fi = btrfs_item_ptr(leaf, slot,
2903 struct btrfs_file_extent_item);
2904 fi = (struct btrfs_file_extent_item *)(
2905 (unsigned long)fi - size_diff);
2906
2907 if (btrfs_file_extent_type(leaf, fi) ==
2908 BTRFS_FILE_EXTENT_INLINE) {
2909 ptr = btrfs_item_ptr_offset(leaf, slot);
2910 memmove_extent_buffer(leaf, ptr,
2911 (unsigned long)fi,
2912 offsetof(struct btrfs_file_extent_item,
2913 disk_bytenr));
2914 }
2915 }
2916
2917 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2918 data_end + size_diff, btrfs_leaf_data(leaf) +
2919 data_end, old_data_start - data_end);
2920
2921 offset = btrfs_disk_key_offset(&disk_key);
2922 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2923 btrfs_set_item_key(leaf, &disk_key, slot);
2924 if (slot == 0)
2925 fixup_low_keys(trans, root, path, &disk_key, 1);
2926 }
Chris Mason5f39d392007-10-15 16:14:19 -04002927
2928 item = btrfs_item_nr(leaf, slot);
2929 btrfs_set_item_size(leaf, item, new_size);
2930 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002931
2932 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002933 if (btrfs_leaf_free_space(root, leaf) < 0) {
2934 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04002935 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002936 }
Chris Masonb18c6682007-04-17 13:26:50 -04002937 return ret;
2938}
2939
Chris Masond352ac62008-09-29 15:18:18 -04002940/*
2941 * make the item pointed to by the path bigger, data_size is the new size.
2942 */
Chris Mason5f39d392007-10-15 16:14:19 -04002943int btrfs_extend_item(struct btrfs_trans_handle *trans,
2944 struct btrfs_root *root, struct btrfs_path *path,
2945 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04002946{
2947 int ret = 0;
2948 int slot;
2949 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04002950 struct extent_buffer *leaf;
2951 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04002952 u32 nritems;
2953 unsigned int data_end;
2954 unsigned int old_data;
2955 unsigned int old_size;
2956 int i;
2957
2958 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002959 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04002960
Chris Mason5f39d392007-10-15 16:14:19 -04002961 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002962 data_end = leaf_data_end(root, leaf);
2963
Chris Mason5f39d392007-10-15 16:14:19 -04002964 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2965 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04002966 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04002967 }
Chris Mason6567e832007-04-16 09:22:45 -04002968 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002969 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04002970
2971 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04002972 if (slot >= nritems) {
2973 btrfs_print_leaf(root, leaf);
2974 printk("slot %d too large, nritems %d\n", slot, nritems);
2975 BUG_ON(1);
2976 }
Chris Mason6567e832007-04-16 09:22:45 -04002977
2978 /*
2979 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2980 */
2981 /* first correct the data pointers */
2982 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002983 u32 ioff;
2984 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04002985
2986 if (!leaf->map_token) {
2987 map_extent_buffer(leaf, (unsigned long)item,
2988 sizeof(struct btrfs_item),
2989 &leaf->map_token, &leaf->kaddr,
2990 &leaf->map_start, &leaf->map_len,
2991 KM_USER1);
2992 }
Chris Mason5f39d392007-10-15 16:14:19 -04002993 ioff = btrfs_item_offset(leaf, item);
2994 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04002995 }
Chris Mason5f39d392007-10-15 16:14:19 -04002996
Chris Masondb945352007-10-15 16:15:53 -04002997 if (leaf->map_token) {
2998 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2999 leaf->map_token = NULL;
3000 }
3001
Chris Mason6567e832007-04-16 09:22:45 -04003002 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003003 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003004 data_end - data_size, btrfs_leaf_data(leaf) +
3005 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003006
Chris Mason6567e832007-04-16 09:22:45 -04003007 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003008 old_size = btrfs_item_size_nr(leaf, slot);
3009 item = btrfs_item_nr(leaf, slot);
3010 btrfs_set_item_size(leaf, item, old_size + data_size);
3011 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003012
3013 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003014 if (btrfs_leaf_free_space(root, leaf) < 0) {
3015 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003016 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003017 }
Chris Mason6567e832007-04-16 09:22:45 -04003018 return ret;
3019}
3020
Chris Mason74123bd2007-02-02 11:05:29 -05003021/*
Chris Masond352ac62008-09-29 15:18:18 -04003022 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003023 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003024 * Returns the number of keys that were inserted.
3025 */
3026int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3027 struct btrfs_root *root,
3028 struct btrfs_path *path,
3029 struct btrfs_key *cpu_key, u32 *data_size,
3030 int nr)
3031{
3032 struct extent_buffer *leaf;
3033 struct btrfs_item *item;
3034 int ret = 0;
3035 int slot;
3036 int slot_orig;
3037 int i;
3038 u32 nritems;
3039 u32 total_data = 0;
3040 u32 total_size = 0;
3041 unsigned int data_end;
3042 struct btrfs_disk_key disk_key;
3043 struct btrfs_key found_key;
3044
3045 found_key.objectid = 0;
3046 nr = min_t(int, nr, BTRFS_NODEPTRS_PER_BLOCK(root));
3047
3048 for (i = 0; i < nr; i++)
3049 total_data += data_size[i];
3050
3051 total_data = min_t(u32, total_data, BTRFS_LEAF_DATA_SIZE(root));
3052 total_size = total_data + (nr * sizeof(struct btrfs_item));
3053 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3054 if (ret == 0)
3055 return -EEXIST;
3056 if (ret < 0)
3057 goto out;
3058
3059 slot_orig = path->slots[0];
3060 leaf = path->nodes[0];
3061
3062 nritems = btrfs_header_nritems(leaf);
3063 data_end = leaf_data_end(root, leaf);
3064
3065 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3066 for (i = nr; i >= 0; i--) {
3067 total_data -= data_size[i];
3068 total_size -= data_size[i] + sizeof(struct btrfs_item);
3069 if (total_size < btrfs_leaf_free_space(root, leaf))
3070 break;
3071 }
3072 nr = i;
3073 }
3074
3075 slot = path->slots[0];
3076 BUG_ON(slot < 0);
3077
3078 if (slot != nritems) {
3079 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3080
3081 item = btrfs_item_nr(leaf, slot);
3082 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3083
3084 /* figure out how many keys we can insert in here */
3085 total_data = data_size[0];
3086 for (i = 1; i < nr; i++) {
3087 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3088 break;
3089 total_data += data_size[i];
3090 }
3091 nr = i;
3092
3093 if (old_data < data_end) {
3094 btrfs_print_leaf(root, leaf);
3095 printk("slot %d old_data %d data_end %d\n",
3096 slot, old_data, data_end);
3097 BUG_ON(1);
3098 }
3099 /*
3100 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3101 */
3102 /* first correct the data pointers */
3103 WARN_ON(leaf->map_token);
3104 for (i = slot; i < nritems; i++) {
3105 u32 ioff;
3106
3107 item = btrfs_item_nr(leaf, i);
3108 if (!leaf->map_token) {
3109 map_extent_buffer(leaf, (unsigned long)item,
3110 sizeof(struct btrfs_item),
3111 &leaf->map_token, &leaf->kaddr,
3112 &leaf->map_start, &leaf->map_len,
3113 KM_USER1);
3114 }
3115
3116 ioff = btrfs_item_offset(leaf, item);
3117 btrfs_set_item_offset(leaf, item, ioff - total_data);
3118 }
3119 if (leaf->map_token) {
3120 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3121 leaf->map_token = NULL;
3122 }
3123
3124 /* shift the items */
3125 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3126 btrfs_item_nr_offset(slot),
3127 (nritems - slot) * sizeof(struct btrfs_item));
3128
3129 /* shift the data */
3130 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3131 data_end - total_data, btrfs_leaf_data(leaf) +
3132 data_end, old_data - data_end);
3133 data_end = old_data;
3134 } else {
3135 /*
3136 * this sucks but it has to be done, if we are inserting at
3137 * the end of the leaf only insert 1 of the items, since we
3138 * have no way of knowing whats on the next leaf and we'd have
3139 * to drop our current locks to figure it out
3140 */
3141 nr = 1;
3142 }
3143
3144 /* setup the item for the new data */
3145 for (i = 0; i < nr; i++) {
3146 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3147 btrfs_set_item_key(leaf, &disk_key, slot + i);
3148 item = btrfs_item_nr(leaf, slot + i);
3149 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3150 data_end -= data_size[i];
3151 btrfs_set_item_size(leaf, item, data_size[i]);
3152 }
3153 btrfs_set_header_nritems(leaf, nritems + nr);
3154 btrfs_mark_buffer_dirty(leaf);
3155
3156 ret = 0;
3157 if (slot == 0) {
3158 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3159 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3160 }
3161
3162 if (btrfs_leaf_free_space(root, leaf) < 0) {
3163 btrfs_print_leaf(root, leaf);
3164 BUG();
3165 }
3166out:
3167 if (!ret)
3168 ret = nr;
3169 return ret;
3170}
3171
3172/*
3173 * Given a key and some data, insert items into the tree.
3174 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003175 */
Chris Mason9c583092008-01-29 15:15:18 -05003176int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003177 struct btrfs_root *root,
3178 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003179 struct btrfs_key *cpu_key, u32 *data_size,
3180 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003181{
Chris Mason5f39d392007-10-15 16:14:19 -04003182 struct extent_buffer *leaf;
3183 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003184 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003185 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003186 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003187 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003188 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003189 u32 total_size = 0;
3190 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003191 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003192 struct btrfs_disk_key disk_key;
3193
Chris Mason9c583092008-01-29 15:15:18 -05003194 for (i = 0; i < nr; i++) {
3195 total_data += data_size[i];
3196 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003197
Josef Bacik7b128762008-07-24 12:17:14 -04003198 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003199 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003200 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003201 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003202 if (ret < 0)
3203 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003204
Chris Mason62e27492007-03-15 12:56:47 -04003205 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003206 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003207
Chris Mason5f39d392007-10-15 16:14:19 -04003208 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003209 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003210
Chris Masonf25956c2008-09-12 15:32:53 -04003211 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003212 btrfs_print_leaf(root, leaf);
3213 printk("not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003214 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003215 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003216 }
Chris Mason5f39d392007-10-15 16:14:19 -04003217
Chris Mason62e27492007-03-15 12:56:47 -04003218 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003219 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003220
Chris Masonbe0e5c02007-01-26 15:51:26 -05003221 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003222 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003223
Chris Mason5f39d392007-10-15 16:14:19 -04003224 if (old_data < data_end) {
3225 btrfs_print_leaf(root, leaf);
3226 printk("slot %d old_data %d data_end %d\n",
3227 slot, old_data, data_end);
3228 BUG_ON(1);
3229 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003230 /*
3231 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3232 */
3233 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003234 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003235 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003236 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003237
Chris Mason5f39d392007-10-15 16:14:19 -04003238 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003239 if (!leaf->map_token) {
3240 map_extent_buffer(leaf, (unsigned long)item,
3241 sizeof(struct btrfs_item),
3242 &leaf->map_token, &leaf->kaddr,
3243 &leaf->map_start, &leaf->map_len,
3244 KM_USER1);
3245 }
3246
Chris Mason5f39d392007-10-15 16:14:19 -04003247 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003248 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003249 }
Chris Masondb945352007-10-15 16:15:53 -04003250 if (leaf->map_token) {
3251 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3252 leaf->map_token = NULL;
3253 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003254
3255 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003256 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003257 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003258 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003259
3260 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003261 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003262 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003263 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003264 data_end = old_data;
3265 }
Chris Mason5f39d392007-10-15 16:14:19 -04003266
Chris Mason62e27492007-03-15 12:56:47 -04003267 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003268 for (i = 0; i < nr; i++) {
3269 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3270 btrfs_set_item_key(leaf, &disk_key, slot + i);
3271 item = btrfs_item_nr(leaf, slot + i);
3272 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3273 data_end -= data_size[i];
3274 btrfs_set_item_size(leaf, item, data_size[i]);
3275 }
3276 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003277 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003278
3279 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003280 if (slot == 0) {
3281 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003282 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003283 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003284
Chris Mason5f39d392007-10-15 16:14:19 -04003285 if (btrfs_leaf_free_space(root, leaf) < 0) {
3286 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003287 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003288 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003289out:
Chris Mason62e27492007-03-15 12:56:47 -04003290 return ret;
3291}
3292
3293/*
3294 * Given a key and some data, insert an item into the tree.
3295 * This does all the path init required, making room in the tree if needed.
3296 */
Chris Masone089f052007-03-16 16:20:31 -04003297int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3298 *root, struct btrfs_key *cpu_key, void *data, u32
3299 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003300{
3301 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003302 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003303 struct extent_buffer *leaf;
3304 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003305
Chris Mason2c90e5d2007-04-02 10:50:19 -04003306 path = btrfs_alloc_path();
3307 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003308 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003309 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003310 leaf = path->nodes[0];
3311 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3312 write_extent_buffer(leaf, data, ptr, data_size);
3313 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003314 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003315 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003316 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003317}
3318
Chris Mason74123bd2007-02-02 11:05:29 -05003319/*
Chris Mason5de08d72007-02-24 06:24:44 -05003320 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003321 *
Chris Masond352ac62008-09-29 15:18:18 -04003322 * the tree should have been previously balanced so the deletion does not
3323 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003324 */
Chris Masone089f052007-03-16 16:20:31 -04003325static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3326 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003327{
Chris Mason5f39d392007-10-15 16:14:19 -04003328 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003329 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003330 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003331 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003332
Chris Mason5f39d392007-10-15 16:14:19 -04003333 nritems = btrfs_header_nritems(parent);
Chris Masonbb803952007-03-01 12:04:21 -05003334 if (slot != nritems -1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003335 memmove_extent_buffer(parent,
3336 btrfs_node_key_ptr_offset(slot),
3337 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003338 sizeof(struct btrfs_key_ptr) *
3339 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003340 }
Chris Mason7518a232007-03-12 12:01:18 -04003341 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003342 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003343 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003344 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003345 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003346 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003347 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003348 struct btrfs_disk_key disk_key;
3349
3350 btrfs_node_key(parent, &disk_key, 0);
3351 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003352 if (wret)
3353 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003354 }
Chris Masond6025572007-03-30 14:27:56 -04003355 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003356 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003357}
3358
Chris Mason74123bd2007-02-02 11:05:29 -05003359/*
Chris Mason323ac952008-10-01 19:05:46 -04003360 * a helper function to delete the leaf pointed to by path->slots[1] and
3361 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3362 * already know it, it is faster to have them pass it down than to
3363 * read it out of the node again.
3364 *
3365 * This deletes the pointer in path->nodes[1] and frees the leaf
3366 * block extent. zero is returned if it all worked out, < 0 otherwise.
3367 *
3368 * The path must have already been setup for deleting the leaf, including
3369 * all the proper balancing. path->nodes[1] must be locked.
3370 */
3371noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3372 struct btrfs_root *root,
3373 struct btrfs_path *path, u64 bytenr)
3374{
3375 int ret;
3376 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3377
3378 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3379 if (ret)
3380 return ret;
3381
3382 ret = btrfs_free_extent(trans, root, bytenr,
3383 btrfs_level_size(root, 0),
3384 path->nodes[1]->start,
3385 btrfs_header_owner(path->nodes[1]),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003386 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003387 return ret;
3388}
3389/*
Chris Mason74123bd2007-02-02 11:05:29 -05003390 * delete the item at the leaf level in path. If that empties
3391 * the leaf, remove it from the tree
3392 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003393int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3394 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003395{
Chris Mason5f39d392007-10-15 16:14:19 -04003396 struct extent_buffer *leaf;
3397 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003398 int last_off;
3399 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003400 int ret = 0;
3401 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003402 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003403 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003404
Chris Mason5f39d392007-10-15 16:14:19 -04003405 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003406 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3407
3408 for (i = 0; i < nr; i++)
3409 dsize += btrfs_item_size_nr(leaf, slot + i);
3410
Chris Mason5f39d392007-10-15 16:14:19 -04003411 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003412
Chris Mason85e21ba2008-01-29 15:11:36 -05003413 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003414 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003415
3416 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003417 data_end + dsize,
3418 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003419 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003420
Chris Mason85e21ba2008-01-29 15:11:36 -05003421 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003422 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003423
Chris Mason5f39d392007-10-15 16:14:19 -04003424 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003425 if (!leaf->map_token) {
3426 map_extent_buffer(leaf, (unsigned long)item,
3427 sizeof(struct btrfs_item),
3428 &leaf->map_token, &leaf->kaddr,
3429 &leaf->map_start, &leaf->map_len,
3430 KM_USER1);
3431 }
Chris Mason5f39d392007-10-15 16:14:19 -04003432 ioff = btrfs_item_offset(leaf, item);
3433 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003434 }
Chris Masondb945352007-10-15 16:15:53 -04003435
3436 if (leaf->map_token) {
3437 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3438 leaf->map_token = NULL;
3439 }
3440
Chris Mason5f39d392007-10-15 16:14:19 -04003441 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003442 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003443 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003444 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003445 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003446 btrfs_set_header_nritems(leaf, nritems - nr);
3447 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003448
Chris Mason74123bd2007-02-02 11:05:29 -05003449 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003450 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003451 if (leaf == root->node) {
3452 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003453 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003454 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3455 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003456 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003457 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003458 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003459 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003460 struct btrfs_disk_key disk_key;
3461
3462 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003463 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003464 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003465 if (wret)
3466 ret = wret;
3467 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003468
Chris Mason74123bd2007-02-02 11:05:29 -05003469 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003470 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003471 /* push_leaf_left fixes the path.
3472 * make sure the path still points to our leaf
3473 * for possible call to del_ptr below
3474 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003475 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003476 extent_buffer_get(leaf);
3477
Chris Mason85e21ba2008-01-29 15:11:36 -05003478 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003479 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003480 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003481
3482 if (path->nodes[0] == leaf &&
3483 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003484 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003485 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003486 ret = wret;
3487 }
Chris Mason5f39d392007-10-15 16:14:19 -04003488
3489 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003490 path->slots[1] = slot;
3491 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3492 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003493 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003494 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003495 /* if we're still in the path, make sure
3496 * we're dirty. Otherwise, one of the
3497 * push_leaf functions must have already
3498 * dirtied this buffer
3499 */
3500 if (path->nodes[0] == leaf)
3501 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003502 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003503 }
Chris Masond5719762007-03-23 10:01:08 -04003504 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003505 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003506 }
3507 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003508 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003509}
3510
Chris Mason97571fd2007-02-24 13:39:08 -05003511/*
Chris Mason925baed2008-06-25 16:01:30 -04003512 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003513 * returns 0 if it found something or 1 if there are no lesser leaves.
3514 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003515 *
3516 * This may release the path, and so you may lose any locks held at the
3517 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003518 */
3519int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3520{
Chris Mason925baed2008-06-25 16:01:30 -04003521 struct btrfs_key key;
3522 struct btrfs_disk_key found_key;
3523 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003524
Chris Mason925baed2008-06-25 16:01:30 -04003525 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003526
Chris Mason925baed2008-06-25 16:01:30 -04003527 if (key.offset > 0)
3528 key.offset--;
3529 else if (key.type > 0)
3530 key.type--;
3531 else if (key.objectid > 0)
3532 key.objectid--;
3533 else
3534 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003535
Chris Mason925baed2008-06-25 16:01:30 -04003536 btrfs_release_path(root, path);
3537 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3538 if (ret < 0)
3539 return ret;
3540 btrfs_item_key(path->nodes[0], &found_key, 0);
3541 ret = comp_keys(&found_key, &key);
3542 if (ret < 0)
3543 return 0;
3544 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003545}
3546
Chris Mason3f157a22008-06-25 16:01:31 -04003547/*
3548 * A helper function to walk down the tree starting at min_key, and looking
3549 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003550 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003551 *
3552 * This does not cow, but it does stuff the starting key it finds back
3553 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3554 * key and get a writable path.
3555 *
3556 * This does lock as it descends, and path->keep_locks should be set
3557 * to 1 by the caller.
3558 *
3559 * This honors path->lowest_level to prevent descent past a given level
3560 * of the tree.
3561 *
Chris Masond352ac62008-09-29 15:18:18 -04003562 * min_trans indicates the oldest transaction that you are interested
3563 * in walking through. Any nodes or leaves older than min_trans are
3564 * skipped over (without reading them).
3565 *
Chris Mason3f157a22008-06-25 16:01:31 -04003566 * returns zero if something useful was found, < 0 on error and 1 if there
3567 * was nothing in the tree that matched the search criteria.
3568 */
3569int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003570 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003571 struct btrfs_path *path, int cache_only,
3572 u64 min_trans)
3573{
3574 struct extent_buffer *cur;
3575 struct btrfs_key found_key;
3576 int slot;
Yan96524802008-07-24 12:19:49 -04003577 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003578 u32 nritems;
3579 int level;
3580 int ret = 1;
3581
3582again:
3583 cur = btrfs_lock_root_node(root);
3584 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003585 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003586 path->nodes[level] = cur;
3587 path->locks[level] = 1;
3588
3589 if (btrfs_header_generation(cur) < min_trans) {
3590 ret = 1;
3591 goto out;
3592 }
3593 while(1) {
3594 nritems = btrfs_header_nritems(cur);
3595 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003596 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003597
Chris Mason323ac952008-10-01 19:05:46 -04003598 /* at the lowest level, we're done, setup the path and exit */
3599 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003600 if (slot >= nritems)
3601 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003602 ret = 0;
3603 path->slots[level] = slot;
3604 btrfs_item_key_to_cpu(cur, &found_key, slot);
3605 goto out;
3606 }
Yan96524802008-07-24 12:19:49 -04003607 if (sret && slot > 0)
3608 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003609 /*
3610 * check this node pointer against the cache_only and
3611 * min_trans parameters. If it isn't in cache or is too
3612 * old, skip to the next one.
3613 */
3614 while(slot < nritems) {
3615 u64 blockptr;
3616 u64 gen;
3617 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003618 struct btrfs_disk_key disk_key;
3619
Chris Mason3f157a22008-06-25 16:01:31 -04003620 blockptr = btrfs_node_blockptr(cur, slot);
3621 gen = btrfs_node_ptr_generation(cur, slot);
3622 if (gen < min_trans) {
3623 slot++;
3624 continue;
3625 }
3626 if (!cache_only)
3627 break;
3628
Chris Masone02119d2008-09-05 16:13:11 -04003629 if (max_key) {
3630 btrfs_node_key(cur, &disk_key, slot);
3631 if (comp_keys(&disk_key, max_key) >= 0) {
3632 ret = 1;
3633 goto out;
3634 }
3635 }
3636
Chris Mason3f157a22008-06-25 16:01:31 -04003637 tmp = btrfs_find_tree_block(root, blockptr,
3638 btrfs_level_size(root, level - 1));
3639
3640 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3641 free_extent_buffer(tmp);
3642 break;
3643 }
3644 if (tmp)
3645 free_extent_buffer(tmp);
3646 slot++;
3647 }
Chris Masone02119d2008-09-05 16:13:11 -04003648find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003649 /*
3650 * we didn't find a candidate key in this node, walk forward
3651 * and find another one
3652 */
3653 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003654 path->slots[level] = slot;
3655 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003656 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003657 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003658 btrfs_release_path(root, path);
3659 goto again;
3660 } else {
3661 goto out;
3662 }
3663 }
3664 /* save our key for returning back */
3665 btrfs_node_key_to_cpu(cur, &found_key, slot);
3666 path->slots[level] = slot;
3667 if (level == path->lowest_level) {
3668 ret = 0;
3669 unlock_up(path, level, 1);
3670 goto out;
3671 }
3672 cur = read_node_slot(root, cur, slot);
3673
3674 btrfs_tree_lock(cur);
3675 path->locks[level - 1] = 1;
3676 path->nodes[level - 1] = cur;
3677 unlock_up(path, level, 1);
3678 }
3679out:
3680 if (ret == 0)
3681 memcpy(min_key, &found_key, sizeof(found_key));
3682 return ret;
3683}
3684
3685/*
3686 * this is similar to btrfs_next_leaf, but does not try to preserve
3687 * and fixup the path. It looks for and returns the next key in the
3688 * tree based on the current path and the cache_only and min_trans
3689 * parameters.
3690 *
3691 * 0 is returned if another key is found, < 0 if there are any errors
3692 * and 1 is returned if there are no higher keys in the tree
3693 *
3694 * path->keep_locks should be set to 1 on the search made before
3695 * calling this function.
3696 */
Chris Masone7a84562008-06-25 16:01:31 -04003697int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003698 struct btrfs_key *key, int lowest_level,
3699 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003700{
3701 int level = lowest_level;
3702 int slot;
3703 struct extent_buffer *c;
3704
3705 while(level < BTRFS_MAX_LEVEL) {
3706 if (!path->nodes[level])
3707 return 1;
3708
3709 slot = path->slots[level] + 1;
3710 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003711next:
Chris Masone7a84562008-06-25 16:01:31 -04003712 if (slot >= btrfs_header_nritems(c)) {
3713 level++;
3714 if (level == BTRFS_MAX_LEVEL) {
3715 return 1;
3716 }
3717 continue;
3718 }
3719 if (level == 0)
3720 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003721 else {
3722 u64 blockptr = btrfs_node_blockptr(c, slot);
3723 u64 gen = btrfs_node_ptr_generation(c, slot);
3724
3725 if (cache_only) {
3726 struct extent_buffer *cur;
3727 cur = btrfs_find_tree_block(root, blockptr,
3728 btrfs_level_size(root, level - 1));
3729 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3730 slot++;
3731 if (cur)
3732 free_extent_buffer(cur);
3733 goto next;
3734 }
3735 free_extent_buffer(cur);
3736 }
3737 if (gen < min_trans) {
3738 slot++;
3739 goto next;
3740 }
Chris Masone7a84562008-06-25 16:01:31 -04003741 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003742 }
Chris Masone7a84562008-06-25 16:01:31 -04003743 return 0;
3744 }
3745 return 1;
3746}
3747
Chris Mason7bb86312007-12-11 09:25:06 -05003748/*
Chris Mason925baed2008-06-25 16:01:30 -04003749 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05003750 * returns 0 if it found something or 1 if there are no greater leaves.
3751 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05003752 */
Chris Mason234b63a2007-03-13 10:46:10 -04003753int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05003754{
3755 int slot;
3756 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003757 struct extent_buffer *c;
3758 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04003759 struct btrfs_key key;
3760 u32 nritems;
3761 int ret;
3762
3763 nritems = btrfs_header_nritems(path->nodes[0]);
3764 if (nritems == 0) {
3765 return 1;
3766 }
3767
3768 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3769
Chris Mason925baed2008-06-25 16:01:30 -04003770 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04003771 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04003772 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3773 path->keep_locks = 0;
3774
3775 if (ret < 0)
3776 return ret;
3777
Chris Masona2135012008-06-25 16:01:30 -04003778 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04003779 /*
3780 * by releasing the path above we dropped all our locks. A balance
3781 * could have added more items next to the key that used to be
3782 * at the very end of the block. So, check again here and
3783 * advance the path if there are now more items available.
3784 */
Chris Masona2135012008-06-25 16:01:30 -04003785 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04003786 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04003787 goto done;
3788 }
Chris Masond97e63b2007-02-20 16:40:44 -05003789
Chris Mason234b63a2007-03-13 10:46:10 -04003790 while(level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05003791 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05003792 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003793
Chris Masond97e63b2007-02-20 16:40:44 -05003794 slot = path->slots[level] + 1;
3795 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04003796 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05003797 level++;
Chris Mason925baed2008-06-25 16:01:30 -04003798 if (level == BTRFS_MAX_LEVEL) {
Chris Mason7bb86312007-12-11 09:25:06 -05003799 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04003800 }
Chris Masond97e63b2007-02-20 16:40:44 -05003801 continue;
3802 }
Chris Mason5f39d392007-10-15 16:14:19 -04003803
Chris Mason925baed2008-06-25 16:01:30 -04003804 if (next) {
3805 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04003806 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04003807 }
Chris Mason5f39d392007-10-15 16:14:19 -04003808
Chris Mason0bd40a72008-07-17 12:54:43 -04003809 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3810 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05003811 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003812
Chris Masonca7a79a2008-05-12 12:59:19 -04003813 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04003814 if (!path->skip_locking) {
3815 WARN_ON(!btrfs_tree_locked(c));
3816 btrfs_tree_lock(next);
3817 }
Chris Masond97e63b2007-02-20 16:40:44 -05003818 break;
3819 }
3820 path->slots[level] = slot;
3821 while(1) {
3822 level--;
3823 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04003824 if (path->locks[level])
3825 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003826 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05003827 path->nodes[level] = next;
3828 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04003829 if (!path->skip_locking)
3830 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05003831 if (!level)
3832 break;
Chris Mason925baed2008-06-25 16:01:30 -04003833 if (level == 1 && path->locks[1] && path->reada)
3834 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04003835 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04003836 if (!path->skip_locking) {
3837 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3838 btrfs_tree_lock(next);
3839 }
Chris Masond97e63b2007-02-20 16:40:44 -05003840 }
Chris Mason925baed2008-06-25 16:01:30 -04003841done:
3842 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05003843 return 0;
3844}
Chris Mason0b86a832008-03-24 15:01:56 -04003845
Chris Mason3f157a22008-06-25 16:01:31 -04003846/*
3847 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3848 * searching until it gets past min_objectid or finds an item of 'type'
3849 *
3850 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3851 */
Chris Mason0b86a832008-03-24 15:01:56 -04003852int btrfs_previous_item(struct btrfs_root *root,
3853 struct btrfs_path *path, u64 min_objectid,
3854 int type)
3855{
3856 struct btrfs_key found_key;
3857 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04003858 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04003859 int ret;
3860
3861 while(1) {
3862 if (path->slots[0] == 0) {
3863 ret = btrfs_prev_leaf(root, path);
3864 if (ret != 0)
3865 return ret;
3866 } else {
3867 path->slots[0]--;
3868 }
3869 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04003870 nritems = btrfs_header_nritems(leaf);
3871 if (nritems == 0)
3872 return 1;
3873 if (path->slots[0] == nritems)
3874 path->slots[0]--;
3875
Chris Mason0b86a832008-03-24 15:01:56 -04003876 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3877 if (found_key.type == type)
3878 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04003879 if (found_key.objectid < min_objectid)
3880 break;
3881 if (found_key.objectid == min_objectid &&
3882 found_key.type < type)
3883 break;
Chris Mason0b86a832008-03-24 15:01:56 -04003884 }
3885 return 1;
3886}