blob: c8f4c540cc2c4b47f19fe5da0713b233177bfc4a [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 Mason2c90e5d2007-04-02 10:50:19 -040041struct btrfs_path *btrfs_alloc_path(void)
42{
Chris Masondf24a2b2007-04-04 09:36:31 -040043 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050044 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
45 if (path)
Chris Mason2cc58cf2007-08-27 16:49:44 -040046 path->reada = 1;
Chris Masondf24a2b2007-04-04 09:36:31 -040047 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040048}
49
Chris Masonb4ce94d2009-02-04 09:25:08 -050050/*
51 * set all locked nodes in the path to blocking locks. This should
52 * be done before scheduling
53 */
54noinline void btrfs_set_path_blocking(struct btrfs_path *p)
55{
56 int i;
57 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
58 if (p->nodes[i] && p->locks[i])
59 btrfs_set_lock_blocking(p->nodes[i]);
60 }
61}
62
63/*
64 * reset all the locked nodes in the patch to spinning locks.
65 */
66noinline void btrfs_clear_path_blocking(struct btrfs_path *p)
67{
68 int i;
69 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
70 if (p->nodes[i] && p->locks[i])
71 btrfs_clear_lock_blocking(p->nodes[i]);
72 }
73}
74
Chris Masond352ac62008-09-29 15:18:18 -040075/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040076void btrfs_free_path(struct btrfs_path *p)
77{
Chris Masondf24a2b2007-04-04 09:36:31 -040078 btrfs_release_path(NULL, p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040079 kmem_cache_free(btrfs_path_cachep, p);
80}
81
Chris Masond352ac62008-09-29 15:18:18 -040082/*
83 * path release drops references on the extent buffers in the path
84 * and it drops any locks held by this path
85 *
86 * It is safe to call this on paths that no locks or extent buffers held.
87 */
Chris Masond3977122009-01-05 21:25:51 -050088noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050089{
90 int i;
Chris Masona2135012008-06-25 16:01:30 -040091
Chris Mason234b63a2007-03-13 10:46:10 -040092 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -040093 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -050094 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -040095 continue;
96 if (p->locks[i]) {
97 btrfs_tree_unlock(p->nodes[i]);
98 p->locks[i] = 0;
99 }
Chris Mason5f39d392007-10-15 16:14:19 -0400100 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400101 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500102 }
103}
104
Chris Masond352ac62008-09-29 15:18:18 -0400105/*
106 * safely gets a reference on the root node of a tree. A lock
107 * is not taken, so a concurrent writer may put a different node
108 * at the root of the tree. See btrfs_lock_root_node for the
109 * looping required.
110 *
111 * The extent buffer returned by this has a reference taken, so
112 * it won't disappear. It may stop being the root of the tree
113 * at any time because there are no locks held.
114 */
Chris Mason925baed2008-06-25 16:01:30 -0400115struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
116{
117 struct extent_buffer *eb;
118 spin_lock(&root->node_lock);
119 eb = root->node;
120 extent_buffer_get(eb);
121 spin_unlock(&root->node_lock);
122 return eb;
123}
124
Chris Masond352ac62008-09-29 15:18:18 -0400125/* loop around taking references on and locking the root node of the
126 * tree until you end up with a lock on the root. A locked buffer
127 * is returned, with a reference held.
128 */
Chris Mason925baed2008-06-25 16:01:30 -0400129struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
130{
131 struct extent_buffer *eb;
132
Chris Masond3977122009-01-05 21:25:51 -0500133 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400134 eb = btrfs_root_node(root);
135 btrfs_tree_lock(eb);
136
137 spin_lock(&root->node_lock);
138 if (eb == root->node) {
139 spin_unlock(&root->node_lock);
140 break;
141 }
142 spin_unlock(&root->node_lock);
143
144 btrfs_tree_unlock(eb);
145 free_extent_buffer(eb);
146 }
147 return eb;
148}
149
Chris Masond352ac62008-09-29 15:18:18 -0400150/* cowonly root (everything not a reference counted cow subvolume), just get
151 * put onto a simple dirty list. transaction.c walks this to make sure they
152 * get properly updated on disk.
153 */
Chris Mason0b86a832008-03-24 15:01:56 -0400154static void add_root_to_dirty_list(struct btrfs_root *root)
155{
156 if (root->track_dirty && list_empty(&root->dirty_list)) {
157 list_add(&root->dirty_list,
158 &root->fs_info->dirty_cowonly_roots);
159 }
160}
161
Chris Masond352ac62008-09-29 15:18:18 -0400162/*
163 * used by snapshot creation to make a copy of a root for a tree with
164 * a given objectid. The buffer with the new root node is returned in
165 * cow_ret, and this func returns zero on success or a negative error code.
166 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500167int btrfs_copy_root(struct btrfs_trans_handle *trans,
168 struct btrfs_root *root,
169 struct extent_buffer *buf,
170 struct extent_buffer **cow_ret, u64 new_root_objectid)
171{
172 struct extent_buffer *cow;
173 u32 nritems;
174 int ret = 0;
175 int level;
Chris Mason4aec2b52007-12-18 16:25:45 -0500176 struct btrfs_root *new_root;
Chris Masonbe20aa92007-12-17 20:14:01 -0500177
Chris Mason4aec2b52007-12-18 16:25:45 -0500178 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
179 if (!new_root)
180 return -ENOMEM;
181
182 memcpy(new_root, root, sizeof(*new_root));
183 new_root->root_key.objectid = new_root_objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500184
185 WARN_ON(root->ref_cows && trans->transid !=
186 root->fs_info->running_transaction->transid);
187 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
188
189 level = btrfs_header_level(buf);
190 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400191
192 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
193 new_root_objectid, trans->transid,
194 level, buf->start, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500195 if (IS_ERR(cow)) {
196 kfree(new_root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500197 return PTR_ERR(cow);
Chris Mason4aec2b52007-12-18 16:25:45 -0500198 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500199
200 copy_extent_buffer(cow, buf, 0, 0, cow->len);
201 btrfs_set_header_bytenr(cow, cow->start);
202 btrfs_set_header_generation(cow, trans->transid);
203 btrfs_set_header_owner(cow, new_root_objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400204 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Masonbe20aa92007-12-17 20:14:01 -0500205
Yan Zheng2b820322008-11-17 21:11:30 -0500206 write_extent_buffer(cow, root->fs_info->fsid,
207 (unsigned long)btrfs_header_fsid(cow),
208 BTRFS_FSID_SIZE);
209
Chris Masonbe20aa92007-12-17 20:14:01 -0500210 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400211 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
Chris Mason4aec2b52007-12-18 16:25:45 -0500212 kfree(new_root);
213
Chris Masonbe20aa92007-12-17 20:14:01 -0500214 if (ret)
215 return ret;
216
217 btrfs_mark_buffer_dirty(cow);
218 *cow_ret = cow;
219 return 0;
220}
221
Chris Masond352ac62008-09-29 15:18:18 -0400222/*
Chris Masond3977122009-01-05 21:25:51 -0500223 * does the dirty work in cow of a single block. The parent block (if
224 * supplied) is updated to point to the new cow copy. The new buffer is marked
225 * dirty and returned locked. If you modify the block it needs to be marked
226 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400227 *
228 * search_start -- an allocation hint for the new block
229 *
Chris Masond3977122009-01-05 21:25:51 -0500230 * empty_size -- a hint that you plan on doing more cow. This is the size in
231 * bytes the allocator should try to find free next to the block it returns.
232 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400233 *
234 * prealloc_dest -- if you have already reserved a destination for the cow,
Chris Masond3977122009-01-05 21:25:51 -0500235 * this uses that block instead of allocating a new one.
236 * btrfs_alloc_reserved_extent is used to finish the allocation.
Chris Masond352ac62008-09-29 15:18:18 -0400237 */
Chris Masond3977122009-01-05 21:25:51 -0500238static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400239 struct btrfs_root *root,
240 struct extent_buffer *buf,
241 struct extent_buffer *parent, int parent_slot,
242 struct extent_buffer **cow_ret,
Chris Mason65b51a02008-08-01 15:11:20 -0400243 u64 search_start, u64 empty_size,
244 u64 prealloc_dest)
Chris Mason6702ed42007-08-07 16:15:09 -0400245{
Zheng Yan31840ae2008-09-23 13:14:14 -0400246 u64 parent_start;
Chris Mason5f39d392007-10-15 16:14:19 -0400247 struct extent_buffer *cow;
Chris Mason7bb86312007-12-11 09:25:06 -0500248 u32 nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400249 int ret = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500250 int level;
Chris Mason925baed2008-06-25 16:01:30 -0400251 int unlock_orig = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400252
Chris Mason925baed2008-06-25 16:01:30 -0400253 if (*cow_ret == buf)
254 unlock_orig = 1;
255
256 WARN_ON(!btrfs_tree_locked(buf));
257
Zheng Yan31840ae2008-09-23 13:14:14 -0400258 if (parent)
259 parent_start = parent->start;
260 else
261 parent_start = 0;
262
Chris Mason7bb86312007-12-11 09:25:06 -0500263 WARN_ON(root->ref_cows && trans->transid !=
264 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400265 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400266
Chris Mason7bb86312007-12-11 09:25:06 -0500267 level = btrfs_header_level(buf);
268 nritems = btrfs_header_nritems(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400269
Chris Mason65b51a02008-08-01 15:11:20 -0400270 if (prealloc_dest) {
271 struct btrfs_key ins;
272
273 ins.objectid = prealloc_dest;
274 ins.offset = buf->len;
275 ins.type = BTRFS_EXTENT_ITEM_KEY;
276
Zheng Yan31840ae2008-09-23 13:14:14 -0400277 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400278 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400279 trans->transid, level, &ins);
Chris Mason65b51a02008-08-01 15:11:20 -0400280 BUG_ON(ret);
281 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
282 buf->len);
283 } else {
284 cow = btrfs_alloc_free_block(trans, root, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400285 parent_start,
Chris Mason65b51a02008-08-01 15:11:20 -0400286 root->root_key.objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400287 trans->transid, level,
288 search_start, empty_size);
Chris Mason65b51a02008-08-01 15:11:20 -0400289 }
Chris Mason6702ed42007-08-07 16:15:09 -0400290 if (IS_ERR(cow))
291 return PTR_ERR(cow);
292
Chris Masonb4ce94d2009-02-04 09:25:08 -0500293 /* cow is set to blocking by btrfs_init_new_buffer */
294
Chris Mason5f39d392007-10-15 16:14:19 -0400295 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400296 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400297 btrfs_set_header_generation(cow, trans->transid);
298 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -0400299 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
Chris Mason6702ed42007-08-07 16:15:09 -0400300
Yan Zheng2b820322008-11-17 21:11:30 -0500301 write_extent_buffer(cow, root->fs_info->fsid,
302 (unsigned long)btrfs_header_fsid(cow),
303 BTRFS_FSID_SIZE);
304
Chris Mason5f39d392007-10-15 16:14:19 -0400305 WARN_ON(btrfs_header_generation(buf) > trans->transid);
306 if (btrfs_header_generation(buf) != trans->transid) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400307 u32 nr_extents;
Zheng Yan31840ae2008-09-23 13:14:14 -0400308 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
Chris Mason6702ed42007-08-07 16:15:09 -0400309 if (ret)
310 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400311
312 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
313 WARN_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -0400314 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
315 /*
316 * There are only two places that can drop reference to
317 * tree blocks owned by living reloc trees, one is here,
Yan Zhengf82d02d2008-10-29 14:49:05 -0400318 * the other place is btrfs_drop_subtree. In both places,
Zheng Yan1a40e232008-09-26 10:09:34 -0400319 * we check reference count while tree block is locked.
320 * Furthermore, if reference count is one, it won't get
321 * increased by someone else.
322 */
323 u32 refs;
324 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
325 buf->len, &refs);
326 BUG_ON(ret);
327 if (refs == 1) {
328 ret = btrfs_update_ref(trans, root, buf, cow,
329 0, nritems);
330 clean_tree_block(trans, root, buf);
331 } else {
332 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
333 }
334 BUG_ON(ret);
Chris Mason6702ed42007-08-07 16:15:09 -0400335 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -0400336 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
337 if (ret)
338 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400339 clean_tree_block(trans, root, buf);
340 }
341
Zheng Yan1a40e232008-09-26 10:09:34 -0400342 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
Zheng Yan1a40e232008-09-26 10:09:34 -0400343 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
344 WARN_ON(ret);
345 }
346
Chris Mason6702ed42007-08-07 16:15:09 -0400347 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400348 WARN_ON(parent && parent != buf);
Chris Mason925baed2008-06-25 16:01:30 -0400349
350 spin_lock(&root->node_lock);
Chris Mason6702ed42007-08-07 16:15:09 -0400351 root->node = cow;
Chris Mason5f39d392007-10-15 16:14:19 -0400352 extent_buffer_get(cow);
Chris Mason925baed2008-06-25 16:01:30 -0400353 spin_unlock(&root->node_lock);
354
Chris Mason6702ed42007-08-07 16:15:09 -0400355 if (buf != root->commit_root) {
Chris Masondb945352007-10-15 16:15:53 -0400356 btrfs_free_extent(trans, root, buf->start,
Zheng Yan31840ae2008-09-23 13:14:14 -0400357 buf->len, buf->start,
358 root->root_key.objectid,
359 btrfs_header_generation(buf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400360 level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400361 }
Chris Mason5f39d392007-10-15 16:14:19 -0400362 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400363 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400364 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400365 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400366 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500367 WARN_ON(trans->transid == 0);
368 btrfs_set_node_ptr_generation(parent, parent_slot,
369 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400370 btrfs_mark_buffer_dirty(parent);
Chris Mason5f39d392007-10-15 16:14:19 -0400371 WARN_ON(btrfs_header_generation(parent) != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -0500372 btrfs_free_extent(trans, root, buf->start, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400373 parent_start, btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400374 btrfs_header_generation(parent), level, 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400375 }
Chris Mason925baed2008-06-25 16:01:30 -0400376 if (unlock_orig)
377 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400378 free_extent_buffer(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400379 btrfs_mark_buffer_dirty(cow);
380 *cow_ret = cow;
381 return 0;
382}
383
Chris Masond352ac62008-09-29 15:18:18 -0400384/*
385 * cows a single block, see __btrfs_cow_block for the real work.
386 * This version of it has extra checks so that a block isn't cow'd more than
387 * once per transaction, as long as it hasn't been written yet
388 */
Chris Masond3977122009-01-05 21:25:51 -0500389noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400390 struct btrfs_root *root, struct extent_buffer *buf,
391 struct extent_buffer *parent, int parent_slot,
Chris Mason65b51a02008-08-01 15:11:20 -0400392 struct extent_buffer **cow_ret, u64 prealloc_dest)
Chris Mason02217ed2007-03-02 16:08:05 -0500393{
Chris Mason6702ed42007-08-07 16:15:09 -0400394 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400395 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500396
Chris Masonccd467d2007-06-28 15:57:36 -0400397 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500398 printk(KERN_CRIT "trans %llu running %llu\n",
399 (unsigned long long)trans->transid,
400 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400401 root->fs_info->running_transaction->transid);
402 WARN_ON(1);
403 }
404 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500405 printk(KERN_CRIT "trans %llu running %llu\n",
406 (unsigned long long)trans->transid,
407 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400408 WARN_ON(1);
409 }
Chris Masondc17ff82008-01-08 15:46:30 -0500410
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400411 if (btrfs_header_generation(buf) == trans->transid &&
412 btrfs_header_owner(buf) == root->root_key.objectid &&
Chris Mason63b10fc2008-04-01 11:21:32 -0400413 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason02217ed2007-03-02 16:08:05 -0500414 *cow_ret = buf;
Chris Mason65b51a02008-08-01 15:11:20 -0400415 WARN_ON(prealloc_dest);
Chris Mason02217ed2007-03-02 16:08:05 -0500416 return 0;
417 }
Chris Masonc4876852009-02-04 09:24:25 -0500418
Chris Mason0b86a832008-03-24 15:01:56 -0400419 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500420
421 if (parent)
422 btrfs_set_lock_blocking(parent);
423 btrfs_set_lock_blocking(buf);
424
Chris Masonf510cfe2007-10-15 16:14:48 -0400425 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason65b51a02008-08-01 15:11:20 -0400426 parent_slot, cow_ret, search_start, 0,
427 prealloc_dest);
Chris Masonf510cfe2007-10-15 16:14:48 -0400428 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400429}
430
Chris Masond352ac62008-09-29 15:18:18 -0400431/*
432 * helper function for defrag to decide if two blocks pointed to by a
433 * node are actually close by
434 */
Chris Mason6b800532007-10-15 16:17:34 -0400435static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400436{
Chris Mason6b800532007-10-15 16:17:34 -0400437 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400438 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400439 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400440 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500441 return 0;
442}
443
Chris Mason081e9572007-11-06 10:26:24 -0500444/*
445 * compare two keys in a memcmp fashion
446 */
447static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
448{
449 struct btrfs_key k1;
450
451 btrfs_disk_key_to_cpu(&k1, disk);
452
453 if (k1.objectid > k2->objectid)
454 return 1;
455 if (k1.objectid < k2->objectid)
456 return -1;
457 if (k1.type > k2->type)
458 return 1;
459 if (k1.type < k2->type)
460 return -1;
461 if (k1.offset > k2->offset)
462 return 1;
463 if (k1.offset < k2->offset)
464 return -1;
465 return 0;
466}
467
Josef Bacikf3465ca2008-11-12 14:19:50 -0500468/*
469 * same as comp_keys only with two btrfs_key's
470 */
471static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
472{
473 if (k1->objectid > k2->objectid)
474 return 1;
475 if (k1->objectid < k2->objectid)
476 return -1;
477 if (k1->type > k2->type)
478 return 1;
479 if (k1->type < k2->type)
480 return -1;
481 if (k1->offset > k2->offset)
482 return 1;
483 if (k1->offset < k2->offset)
484 return -1;
485 return 0;
486}
Chris Mason081e9572007-11-06 10:26:24 -0500487
Chris Masond352ac62008-09-29 15:18:18 -0400488/*
489 * this is used by the defrag code to go through all the
490 * leaves pointed to by a node and reallocate them so that
491 * disk order is close to key order
492 */
Chris Mason6702ed42007-08-07 16:15:09 -0400493int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400494 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -0400495 int start_slot, int cache_only, u64 *last_ret,
496 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400497{
Chris Mason6b800532007-10-15 16:17:34 -0400498 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400499 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -0400500 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -0400501 u64 search_start = *last_ret;
502 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400503 u64 other;
504 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400505 int end_slot;
506 int i;
507 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -0400508 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -0400509 int uptodate;
510 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500511 int progress_passed = 0;
512 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400513
Chris Mason5708b952007-10-25 15:43:18 -0400514 parent_level = btrfs_header_level(parent);
515 if (cache_only && parent_level != 1)
516 return 0;
517
Chris Masond3977122009-01-05 21:25:51 -0500518 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -0400519 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -0500520 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -0400521 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -0400522
Chris Mason6b800532007-10-15 16:17:34 -0400523 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -0400524 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -0400525 end_slot = parent_nritems;
526
527 if (parent_nritems == 1)
528 return 0;
529
Chris Masonb4ce94d2009-02-04 09:25:08 -0500530 btrfs_set_lock_blocking(parent);
531
Chris Mason6702ed42007-08-07 16:15:09 -0400532 for (i = start_slot; i < end_slot; i++) {
533 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400534
Chris Mason5708b952007-10-25 15:43:18 -0400535 if (!parent->map_token) {
536 map_extent_buffer(parent,
537 btrfs_node_key_ptr_offset(i),
538 sizeof(struct btrfs_key_ptr),
539 &parent->map_token, &parent->kaddr,
540 &parent->map_start, &parent->map_len,
541 KM_USER1);
542 }
Chris Mason081e9572007-11-06 10:26:24 -0500543 btrfs_node_key(parent, &disk_key, i);
544 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
545 continue;
546
547 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400548 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -0400549 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400550 if (last_block == 0)
551 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400552
Chris Mason6702ed42007-08-07 16:15:09 -0400553 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400554 other = btrfs_node_blockptr(parent, i - 1);
555 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400556 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400557 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -0400558 other = btrfs_node_blockptr(parent, i + 1);
559 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400560 }
Chris Masone9d0b132007-08-10 14:06:19 -0400561 if (close) {
562 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400563 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400564 }
Chris Mason5708b952007-10-25 15:43:18 -0400565 if (parent->map_token) {
566 unmap_extent_buffer(parent, parent->map_token,
567 KM_USER1);
568 parent->map_token = NULL;
569 }
Chris Mason6702ed42007-08-07 16:15:09 -0400570
Chris Mason6b800532007-10-15 16:17:34 -0400571 cur = btrfs_find_tree_block(root, blocknr, blocksize);
572 if (cur)
Chris Mason1259ab72008-05-12 13:39:03 -0400573 uptodate = btrfs_buffer_uptodate(cur, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400574 else
575 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -0400576 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -0400577 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -0400578 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400579 continue;
580 }
Chris Mason6b800532007-10-15 16:17:34 -0400581 if (!cur) {
582 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -0400583 blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -0400584 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -0400585 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -0400586 }
Chris Mason6702ed42007-08-07 16:15:09 -0400587 }
Chris Masone9d0b132007-08-10 14:06:19 -0400588 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400589 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400590
Chris Masone7a84562008-06-25 16:01:31 -0400591 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500592 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400593 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400594 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400595 min(16 * blocksize,
Chris Mason65b51a02008-08-01 15:11:20 -0400596 (end_slot - i) * blocksize), 0);
Yan252c38f2007-08-29 09:11:44 -0400597 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400598 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400599 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400600 break;
Yan252c38f2007-08-29 09:11:44 -0400601 }
Chris Masone7a84562008-06-25 16:01:31 -0400602 search_start = cur->start;
603 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400604 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400605 btrfs_tree_unlock(cur);
606 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400607 }
Chris Mason5708b952007-10-25 15:43:18 -0400608 if (parent->map_token) {
609 unmap_extent_buffer(parent, parent->map_token,
610 KM_USER1);
611 parent->map_token = NULL;
612 }
Chris Mason6702ed42007-08-07 16:15:09 -0400613 return err;
614}
615
Chris Mason74123bd2007-02-02 11:05:29 -0500616/*
617 * The leaf data grows from end-to-front in the node.
618 * this returns the address of the start of the last item,
619 * which is the stop of the leaf data stack
620 */
Chris Mason123abc82007-03-14 14:14:43 -0400621static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400622 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500623{
Chris Mason5f39d392007-10-15 16:14:19 -0400624 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500625 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400626 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -0400627 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500628}
629
Chris Masond352ac62008-09-29 15:18:18 -0400630/*
631 * extra debugging checks to make sure all the items in a key are
632 * well formed and in the proper order
633 */
Chris Mason123abc82007-03-14 14:14:43 -0400634static int check_node(struct btrfs_root *root, struct btrfs_path *path,
635 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500636{
Chris Mason5f39d392007-10-15 16:14:19 -0400637 struct extent_buffer *parent = NULL;
638 struct extent_buffer *node = path->nodes[level];
639 struct btrfs_disk_key parent_key;
640 struct btrfs_disk_key node_key;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500641 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400642 int slot;
643 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400644 u32 nritems = btrfs_header_nritems(node);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500645
646 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400647 parent = path->nodes[level + 1];
Aneesha1f39632007-07-11 10:03:27 -0400648
Chris Mason8d7be552007-05-10 11:24:42 -0400649 slot = path->slots[level];
Chris Mason7518a232007-03-12 12:01:18 -0400650 BUG_ON(nritems == 0);
651 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400652 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400653 btrfs_node_key(parent, &parent_key, parent_slot);
654 btrfs_node_key(node, &node_key, 0);
655 BUG_ON(memcmp(&parent_key, &node_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400656 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400657 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400658 btrfs_header_bytenr(node));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500659 }
Chris Mason123abc82007-03-14 14:14:43 -0400660 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason8d7be552007-05-10 11:24:42 -0400661 if (slot != 0) {
Chris Mason5f39d392007-10-15 16:14:19 -0400662 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
663 btrfs_node_key(node, &node_key, slot);
664 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
Chris Mason8d7be552007-05-10 11:24:42 -0400665 }
666 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400667 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
668 btrfs_node_key(node, &node_key, slot);
669 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500670 }
671 return 0;
672}
673
Chris Masond352ac62008-09-29 15:18:18 -0400674/*
675 * extra checking to make sure all the items in a leaf are
676 * well formed and in the proper order
677 */
Chris Mason123abc82007-03-14 14:14:43 -0400678static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
679 int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500680{
Chris Mason5f39d392007-10-15 16:14:19 -0400681 struct extent_buffer *leaf = path->nodes[level];
682 struct extent_buffer *parent = NULL;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500683 int parent_slot;
Chris Mason8d7be552007-05-10 11:24:42 -0400684 struct btrfs_key cpukey;
Chris Mason5f39d392007-10-15 16:14:19 -0400685 struct btrfs_disk_key parent_key;
686 struct btrfs_disk_key leaf_key;
687 int slot = path->slots[0];
Chris Mason8d7be552007-05-10 11:24:42 -0400688
Chris Mason5f39d392007-10-15 16:14:19 -0400689 u32 nritems = btrfs_header_nritems(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500690
691 if (path->nodes[level + 1])
Chris Mason5f39d392007-10-15 16:14:19 -0400692 parent = path->nodes[level + 1];
Chris Mason7518a232007-03-12 12:01:18 -0400693
694 if (nritems == 0)
695 return 0;
696
697 if (parent) {
Aneesha1f39632007-07-11 10:03:27 -0400698 parent_slot = path->slots[level + 1];
Chris Mason5f39d392007-10-15 16:14:19 -0400699 btrfs_node_key(parent, &parent_key, parent_slot);
700 btrfs_item_key(leaf, &leaf_key, 0);
Chris Mason6702ed42007-08-07 16:15:09 -0400701
Chris Mason5f39d392007-10-15 16:14:19 -0400702 BUG_ON(memcmp(&parent_key, &leaf_key,
Chris Masone2fa7222007-03-12 16:22:34 -0400703 sizeof(struct btrfs_disk_key)));
Chris Mason1d4f8a02007-03-13 09:28:32 -0400704 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
Chris Masondb945352007-10-15 16:15:53 -0400705 btrfs_header_bytenr(leaf));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500706 }
Chris Mason5f39d392007-10-15 16:14:19 -0400707 if (slot != 0 && slot < nritems - 1) {
708 btrfs_item_key(leaf, &leaf_key, slot);
709 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
710 if (comp_keys(&leaf_key, &cpukey) <= 0) {
711 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500712 printk(KERN_CRIT "slot %d offset bad key\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400713 BUG_ON(1);
714 }
715 if (btrfs_item_offset_nr(leaf, slot - 1) !=
716 btrfs_item_end_nr(leaf, slot)) {
717 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500718 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400719 BUG_ON(1);
720 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500721 }
Chris Mason8d7be552007-05-10 11:24:42 -0400722 if (slot < nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400723 btrfs_item_key(leaf, &leaf_key, slot);
724 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
725 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
726 if (btrfs_item_offset_nr(leaf, slot) !=
727 btrfs_item_end_nr(leaf, slot + 1)) {
728 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -0500729 printk(KERN_CRIT "slot %d offset bad\n", slot);
Chris Mason5f39d392007-10-15 16:14:19 -0400730 BUG_ON(1);
731 }
Chris Mason8d7be552007-05-10 11:24:42 -0400732 }
Chris Mason5f39d392007-10-15 16:14:19 -0400733 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
734 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500735 return 0;
736}
737
Chris Masond3977122009-01-05 21:25:51 -0500738static noinline int check_block(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500739 struct btrfs_path *path, int level)
Chris Masonaa5d6be2007-02-28 16:35:06 -0500740{
Chris Mason85d824c2008-04-10 10:23:19 -0400741 return 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500742 if (level == 0)
Chris Mason123abc82007-03-14 14:14:43 -0400743 return check_leaf(root, path, level);
744 return check_node(root, path, level);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500745}
746
Chris Mason74123bd2007-02-02 11:05:29 -0500747/*
Chris Mason5f39d392007-10-15 16:14:19 -0400748 * search for key in the extent_buffer. The items start at offset p,
749 * and they are item_size apart. There are 'max' items in p.
750 *
Chris Mason74123bd2007-02-02 11:05:29 -0500751 * the slot in the array is returned via slot, and it points to
752 * the place where you would insert key if it is not found in
753 * the array.
754 *
755 * slot may point to max if the key is bigger than all of the keys
756 */
Chris Masone02119d2008-09-05 16:13:11 -0400757static noinline int generic_bin_search(struct extent_buffer *eb,
758 unsigned long p,
759 int item_size, struct btrfs_key *key,
760 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500761{
762 int low = 0;
763 int high = max;
764 int mid;
765 int ret;
Chris Mason479965d2007-10-15 16:14:27 -0400766 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400767 struct btrfs_disk_key unaligned;
768 unsigned long offset;
769 char *map_token = NULL;
770 char *kaddr = NULL;
771 unsigned long map_start = 0;
772 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -0400773 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500774
Chris Masond3977122009-01-05 21:25:51 -0500775 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500776 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400777 offset = p + mid * item_size;
778
779 if (!map_token || offset < map_start ||
780 (offset + sizeof(struct btrfs_disk_key)) >
781 map_start + map_len) {
Chris Mason479965d2007-10-15 16:14:27 -0400782 if (map_token) {
Chris Mason5f39d392007-10-15 16:14:19 -0400783 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Mason479965d2007-10-15 16:14:27 -0400784 map_token = NULL;
785 }
Chris Mason934d3752008-12-08 16:43:10 -0500786
787 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -0400788 sizeof(struct btrfs_disk_key),
789 &map_token, &kaddr,
790 &map_start, &map_len, KM_USER0);
Chris Mason5f39d392007-10-15 16:14:19 -0400791
Chris Mason479965d2007-10-15 16:14:27 -0400792 if (!err) {
793 tmp = (struct btrfs_disk_key *)(kaddr + offset -
794 map_start);
795 } else {
796 read_extent_buffer(eb, &unaligned,
797 offset, sizeof(unaligned));
798 tmp = &unaligned;
799 }
800
Chris Mason5f39d392007-10-15 16:14:19 -0400801 } else {
802 tmp = (struct btrfs_disk_key *)(kaddr + offset -
803 map_start);
804 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500805 ret = comp_keys(tmp, key);
806
807 if (ret < 0)
808 low = mid + 1;
809 else if (ret > 0)
810 high = mid;
811 else {
812 *slot = mid;
Chris Mason479965d2007-10-15 16:14:27 -0400813 if (map_token)
814 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500815 return 0;
816 }
817 }
818 *slot = low;
Chris Mason5f39d392007-10-15 16:14:19 -0400819 if (map_token)
820 unmap_extent_buffer(eb, map_token, KM_USER0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500821 return 1;
822}
823
Chris Mason97571fd2007-02-24 13:39:08 -0500824/*
825 * simple bin_search frontend that does the right thing for
826 * leaves vs nodes
827 */
Chris Mason5f39d392007-10-15 16:14:19 -0400828static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
829 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500830{
Chris Mason5f39d392007-10-15 16:14:19 -0400831 if (level == 0) {
832 return generic_bin_search(eb,
833 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400834 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400835 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400836 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500837 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400838 return generic_bin_search(eb,
839 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400840 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400841 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400842 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500843 }
844 return -1;
845}
846
Chris Masond352ac62008-09-29 15:18:18 -0400847/* given a node and slot number, this reads the blocks it points to. The
848 * extent buffer is returned with a reference taken (but unlocked).
849 * NULL is returned on error.
850 */
Chris Masone02119d2008-09-05 16:13:11 -0400851static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400852 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500853{
Chris Masonca7a79a2008-05-12 12:59:19 -0400854 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -0500855 if (slot < 0)
856 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400857 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -0500858 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -0400859
860 BUG_ON(level == 0);
861
Chris Masondb945352007-10-15 16:15:53 -0400862 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -0400863 btrfs_level_size(root, level - 1),
864 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -0500865}
866
Chris Masond352ac62008-09-29 15:18:18 -0400867/*
868 * node level balancing, used to make sure nodes are in proper order for
869 * item deletion. We balance from the top down, so we have to make sure
870 * that a deletion won't leave an node completely empty later on.
871 */
Chris Masone02119d2008-09-05 16:13:11 -0400872static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500873 struct btrfs_root *root,
874 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500875{
Chris Mason5f39d392007-10-15 16:14:19 -0400876 struct extent_buffer *right = NULL;
877 struct extent_buffer *mid;
878 struct extent_buffer *left = NULL;
879 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500880 int ret = 0;
881 int wret;
882 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500883 int orig_slot = path->slots[level];
Chris Mason54aa1f42007-06-22 14:16:25 -0400884 int err_on_enospc = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500885 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500886
887 if (level == 0)
888 return 0;
889
Chris Mason5f39d392007-10-15 16:14:19 -0400890 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500891
Chris Mason925baed2008-06-25 16:01:30 -0400892 WARN_ON(!path->locks[level]);
Chris Mason7bb86312007-12-11 09:25:06 -0500893 WARN_ON(btrfs_header_generation(mid) != trans->transid);
894
Chris Mason1d4f8a02007-03-13 09:28:32 -0400895 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500896
Chris Mason234b63a2007-03-13 10:46:10 -0400897 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -0400898 parent = path->nodes[level + 1];
Chris Masonbb803952007-03-01 12:04:21 -0500899 pslot = path->slots[level + 1];
900
Chris Mason40689472007-03-17 14:29:23 -0400901 /*
902 * deal with the case where there is only one pointer in the root
903 * by promoting the node below to a root
904 */
Chris Mason5f39d392007-10-15 16:14:19 -0400905 if (!parent) {
906 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500907
Chris Mason5f39d392007-10-15 16:14:19 -0400908 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500909 return 0;
910
911 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -0400912 child = read_node_slot(root, mid, 0);
Jeff Mahoney7951f3c2009-02-12 10:06:15 -0500913 BUG_ON(!child);
Chris Mason925baed2008-06-25 16:01:30 -0400914 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500915 btrfs_set_lock_blocking(child);
Chris Mason65b51a02008-08-01 15:11:20 -0400916 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
Yan2f375ab2008-02-01 14:58:07 -0500917 BUG_ON(ret);
918
Chris Mason925baed2008-06-25 16:01:30 -0400919 spin_lock(&root->node_lock);
Chris Masonbb803952007-03-01 12:04:21 -0500920 root->node = child;
Chris Mason925baed2008-06-25 16:01:30 -0400921 spin_unlock(&root->node_lock);
922
Zheng Yan31840ae2008-09-23 13:14:14 -0400923 ret = btrfs_update_extent_ref(trans, root, child->start,
924 mid->start, child->start,
925 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400926 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400927 BUG_ON(ret);
928
Chris Mason0b86a832008-03-24 15:01:56 -0400929 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400930 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500931
Chris Mason925baed2008-06-25 16:01:30 -0400932 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500933 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400934 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -0400935 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500936 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400937 free_extent_buffer(mid);
Chris Mason7bb86312007-12-11 09:25:06 -0500938 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
Zheng Yan31840ae2008-09-23 13:14:14 -0400939 mid->start, root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400940 btrfs_header_generation(mid),
941 level, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500942 /* once for the root ptr */
Chris Mason5f39d392007-10-15 16:14:19 -0400943 free_extent_buffer(mid);
Chris Masondb945352007-10-15 16:15:53 -0400944 return ret;
Chris Masonbb803952007-03-01 12:04:21 -0500945 }
Chris Mason5f39d392007-10-15 16:14:19 -0400946 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -0400947 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500948 return 0;
949
Chris Mason5f39d392007-10-15 16:14:19 -0400950 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400951 err_on_enospc = 1;
952
Chris Mason5f39d392007-10-15 16:14:19 -0400953 left = read_node_slot(root, parent, pslot - 1);
954 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -0400955 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500956 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -0400957 wret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -0400958 parent, pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400959 if (wret) {
960 ret = wret;
961 goto enospc;
962 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400963 }
Chris Mason5f39d392007-10-15 16:14:19 -0400964 right = read_node_slot(root, parent, pslot + 1);
965 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -0400966 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500967 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -0400968 wret = btrfs_cow_block(trans, root, right,
Chris Mason65b51a02008-08-01 15:11:20 -0400969 parent, pslot + 1, &right, 0);
Chris Mason2cc58cf2007-08-27 16:49:44 -0400970 if (wret) {
971 ret = wret;
972 goto enospc;
973 }
974 }
975
976 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -0400977 if (left) {
978 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -0400979 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500980 if (wret < 0)
981 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400982 if (btrfs_header_nritems(mid) < 2)
Chris Mason54aa1f42007-06-22 14:16:25 -0400983 err_on_enospc = 1;
Chris Masonbb803952007-03-01 12:04:21 -0500984 }
Chris Mason79f95c82007-03-01 15:16:26 -0500985
986 /*
987 * then try to empty the right most buffer into the middle
988 */
Chris Mason5f39d392007-10-15 16:14:19 -0400989 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -0400990 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400991 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -0500992 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -0400993 if (btrfs_header_nritems(right) == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400994 u64 bytenr = right->start;
Chris Mason7bb86312007-12-11 09:25:06 -0500995 u64 generation = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -0400996 u32 blocksize = right->len;
997
Chris Mason5f39d392007-10-15 16:14:19 -0400998 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -0400999 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001000 free_extent_buffer(right);
Chris Masonbb803952007-03-01 12:04:21 -05001001 right = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001002 wret = del_ptr(trans, root, path, level + 1, pslot +
1003 1);
Chris Masonbb803952007-03-01 12:04:21 -05001004 if (wret)
1005 ret = wret;
Chris Masondb945352007-10-15 16:15:53 -04001006 wret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04001007 blocksize, parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001008 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001009 generation, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001010 if (wret)
1011 ret = wret;
1012 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001013 struct btrfs_disk_key right_key;
1014 btrfs_node_key(right, &right_key, 0);
1015 btrfs_set_node_key(parent, &right_key, pslot + 1);
1016 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001017 }
1018 }
Chris Mason5f39d392007-10-15 16:14:19 -04001019 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001020 /*
1021 * we're not allowed to leave a node with one item in the
1022 * tree during a delete. A deletion from lower in the tree
1023 * could try to delete the only pointer in this node.
1024 * So, pull some keys from the left.
1025 * There has to be a left pointer at this point because
1026 * otherwise we would have pulled some pointers from the
1027 * right
1028 */
Chris Mason5f39d392007-10-15 16:14:19 -04001029 BUG_ON(!left);
1030 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001031 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001032 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001033 goto enospc;
1034 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001035 if (wret == 1) {
1036 wret = push_node_left(trans, root, left, mid, 1);
1037 if (wret < 0)
1038 ret = wret;
1039 }
Chris Mason79f95c82007-03-01 15:16:26 -05001040 BUG_ON(wret == 1);
1041 }
Chris Mason5f39d392007-10-15 16:14:19 -04001042 if (btrfs_header_nritems(mid) == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001043 /* we've managed to empty the middle node, drop it */
Chris Mason7bb86312007-12-11 09:25:06 -05001044 u64 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04001045 u64 bytenr = mid->start;
1046 u32 blocksize = mid->len;
Chris Mason925baed2008-06-25 16:01:30 -04001047
Chris Mason5f39d392007-10-15 16:14:19 -04001048 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001049 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001050 free_extent_buffer(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001051 mid = NULL;
Chris Masone089f052007-03-16 16:20:31 -04001052 wret = del_ptr(trans, root, path, level + 1, pslot);
Chris Masonbb803952007-03-01 12:04:21 -05001053 if (wret)
1054 ret = wret;
Chris Mason7bb86312007-12-11 09:25:06 -05001055 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04001056 parent->start,
Chris Mason7bb86312007-12-11 09:25:06 -05001057 btrfs_header_owner(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001058 root_gen, level, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001059 if (wret)
1060 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -05001061 } else {
1062 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001063 struct btrfs_disk_key mid_key;
1064 btrfs_node_key(mid, &mid_key, 0);
1065 btrfs_set_node_key(parent, &mid_key, pslot);
1066 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001067 }
Chris Masonbb803952007-03-01 12:04:21 -05001068
Chris Mason79f95c82007-03-01 15:16:26 -05001069 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001070 if (left) {
1071 if (btrfs_header_nritems(left) > orig_slot) {
1072 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001073 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001074 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001075 path->slots[level + 1] -= 1;
1076 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001077 if (mid) {
1078 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001079 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001080 }
Chris Masonbb803952007-03-01 12:04:21 -05001081 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001082 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001083 path->slots[level] = orig_slot;
1084 }
1085 }
Chris Mason79f95c82007-03-01 15:16:26 -05001086 /* double check we haven't messed things up */
Chris Mason123abc82007-03-14 14:14:43 -04001087 check_block(root, path, level);
Chris Masone20d96d2007-03-22 12:13:20 -04001088 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001089 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001090 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001091enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001092 if (right) {
1093 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001094 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001095 }
1096 if (left) {
1097 if (path->nodes[level] != left)
1098 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001099 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001100 }
Chris Masonbb803952007-03-01 12:04:21 -05001101 return ret;
1102}
1103
Chris Masond352ac62008-09-29 15:18:18 -04001104/* Node balancing for insertion. Here we only split or push nodes around
1105 * when they are completely full. This is also done top down, so we
1106 * have to be pessimistic.
1107 */
Chris Masond3977122009-01-05 21:25:51 -05001108static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001109 struct btrfs_root *root,
1110 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001111{
Chris Mason5f39d392007-10-15 16:14:19 -04001112 struct extent_buffer *right = NULL;
1113 struct extent_buffer *mid;
1114 struct extent_buffer *left = NULL;
1115 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001116 int ret = 0;
1117 int wret;
1118 int pslot;
1119 int orig_slot = path->slots[level];
1120 u64 orig_ptr;
1121
1122 if (level == 0)
1123 return 1;
1124
Chris Mason5f39d392007-10-15 16:14:19 -04001125 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001126 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001127 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1128
1129 if (level < BTRFS_MAX_LEVEL - 1)
Chris Mason5f39d392007-10-15 16:14:19 -04001130 parent = path->nodes[level + 1];
Chris Masone66f7092007-04-20 13:16:02 -04001131 pslot = path->slots[level + 1];
1132
Chris Mason5f39d392007-10-15 16:14:19 -04001133 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001134 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001135
Chris Mason5f39d392007-10-15 16:14:19 -04001136 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001137
1138 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001139 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001140 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001141
1142 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001143 btrfs_set_lock_blocking(left);
1144
Chris Mason5f39d392007-10-15 16:14:19 -04001145 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001146 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1147 wret = 1;
1148 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001149 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason65b51a02008-08-01 15:11:20 -04001150 pslot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001151 if (ret)
1152 wret = 1;
1153 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001154 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001155 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001156 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001157 }
Chris Masone66f7092007-04-20 13:16:02 -04001158 if (wret < 0)
1159 ret = wret;
1160 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001161 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001162 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001163 btrfs_node_key(mid, &disk_key, 0);
1164 btrfs_set_node_key(parent, &disk_key, pslot);
1165 btrfs_mark_buffer_dirty(parent);
1166 if (btrfs_header_nritems(left) > orig_slot) {
1167 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001168 path->slots[level + 1] -= 1;
1169 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001170 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001171 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001172 } else {
1173 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001174 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001175 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001176 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001177 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001178 }
Chris Masone66f7092007-04-20 13:16:02 -04001179 return 0;
1180 }
Chris Mason925baed2008-06-25 16:01:30 -04001181 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001182 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001183 }
Chris Mason925baed2008-06-25 16:01:30 -04001184 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001185
1186 /*
1187 * then try to empty the right most buffer into the middle
1188 */
Chris Mason5f39d392007-10-15 16:14:19 -04001189 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001190 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001191
Chris Mason925baed2008-06-25 16:01:30 -04001192 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001193 btrfs_set_lock_blocking(right);
1194
Chris Mason5f39d392007-10-15 16:14:19 -04001195 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001196 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1197 wret = 1;
1198 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001199 ret = btrfs_cow_block(trans, root, right,
1200 parent, pslot + 1,
Chris Mason65b51a02008-08-01 15:11:20 -04001201 &right, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001202 if (ret)
1203 wret = 1;
1204 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001205 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001206 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001207 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001208 }
Chris Masone66f7092007-04-20 13:16:02 -04001209 if (wret < 0)
1210 ret = wret;
1211 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001212 struct btrfs_disk_key disk_key;
1213
1214 btrfs_node_key(right, &disk_key, 0);
1215 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1216 btrfs_mark_buffer_dirty(parent);
1217
1218 if (btrfs_header_nritems(mid) <= orig_slot) {
1219 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001220 path->slots[level + 1] += 1;
1221 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001222 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001223 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001224 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001225 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001226 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001227 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001228 }
Chris Masone66f7092007-04-20 13:16:02 -04001229 return 0;
1230 }
Chris Mason925baed2008-06-25 16:01:30 -04001231 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001232 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001233 }
Chris Masone66f7092007-04-20 13:16:02 -04001234 return 1;
1235}
1236
Chris Mason74123bd2007-02-02 11:05:29 -05001237/*
Chris Masond352ac62008-09-29 15:18:18 -04001238 * readahead one full node of leaves, finding things that are close
1239 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001240 */
Chris Masone02119d2008-09-05 16:13:11 -04001241static noinline void reada_for_search(struct btrfs_root *root,
1242 struct btrfs_path *path,
1243 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001244{
Chris Mason5f39d392007-10-15 16:14:19 -04001245 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001246 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001247 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001248 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001249 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001250 u64 nread = 0;
Chris Mason3c69fae2007-08-07 15:52:22 -04001251 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001252 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001253 u32 nr;
1254 u32 blocksize;
1255 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001256
Chris Masona6b6e752007-10-15 16:22:39 -04001257 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001258 return;
1259
Chris Mason6702ed42007-08-07 16:15:09 -04001260 if (!path->nodes[level])
1261 return;
1262
Chris Mason5f39d392007-10-15 16:14:19 -04001263 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001264
Chris Mason3c69fae2007-08-07 15:52:22 -04001265 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001266 blocksize = btrfs_level_size(root, level - 1);
1267 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001268 if (eb) {
1269 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001270 return;
1271 }
1272
Chris Masona7175312009-01-22 09:23:10 -05001273 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001274
Chris Mason5f39d392007-10-15 16:14:19 -04001275 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001276 nr = slot;
Chris Masond3977122009-01-05 21:25:51 -05001277 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001278 if (direction < 0) {
1279 if (nr == 0)
1280 break;
1281 nr--;
1282 } else if (direction > 0) {
1283 nr++;
1284 if (nr >= nritems)
1285 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001286 }
Chris Mason01f46652007-12-21 16:24:26 -05001287 if (path->reada < 0 && objectid) {
1288 btrfs_node_key(node, &disk_key, nr);
1289 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1290 break;
1291 }
Chris Mason6b800532007-10-15 16:17:34 -04001292 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001293 if ((search <= target && target - search <= 65536) ||
1294 (search > target && search - target <= 65536)) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001295 readahead_tree_block(root, search, blocksize,
1296 btrfs_node_ptr_generation(node, nr));
Chris Mason6b800532007-10-15 16:17:34 -04001297 nread += blocksize;
1298 }
1299 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001300 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001301 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001302 }
1303}
Chris Mason925baed2008-06-25 16:01:30 -04001304
Chris Masond352ac62008-09-29 15:18:18 -04001305/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001306 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1307 * cache
1308 */
1309static noinline int reada_for_balance(struct btrfs_root *root,
1310 struct btrfs_path *path, int level)
1311{
1312 int slot;
1313 int nritems;
1314 struct extent_buffer *parent;
1315 struct extent_buffer *eb;
1316 u64 gen;
1317 u64 block1 = 0;
1318 u64 block2 = 0;
1319 int ret = 0;
1320 int blocksize;
1321
1322 parent = path->nodes[level - 1];
1323 if (!parent)
1324 return 0;
1325
1326 nritems = btrfs_header_nritems(parent);
1327 slot = path->slots[level];
1328 blocksize = btrfs_level_size(root, level);
1329
1330 if (slot > 0) {
1331 block1 = btrfs_node_blockptr(parent, slot - 1);
1332 gen = btrfs_node_ptr_generation(parent, slot - 1);
1333 eb = btrfs_find_tree_block(root, block1, blocksize);
1334 if (eb && btrfs_buffer_uptodate(eb, gen))
1335 block1 = 0;
1336 free_extent_buffer(eb);
1337 }
1338 if (slot < nritems) {
1339 block2 = btrfs_node_blockptr(parent, slot + 1);
1340 gen = btrfs_node_ptr_generation(parent, slot + 1);
1341 eb = btrfs_find_tree_block(root, block2, blocksize);
1342 if (eb && btrfs_buffer_uptodate(eb, gen))
1343 block2 = 0;
1344 free_extent_buffer(eb);
1345 }
1346 if (block1 || block2) {
1347 ret = -EAGAIN;
1348 btrfs_release_path(root, path);
1349 if (block1)
1350 readahead_tree_block(root, block1, blocksize, 0);
1351 if (block2)
1352 readahead_tree_block(root, block2, blocksize, 0);
1353
1354 if (block1) {
1355 eb = read_tree_block(root, block1, blocksize, 0);
1356 free_extent_buffer(eb);
1357 }
1358 if (block1) {
1359 eb = read_tree_block(root, block2, blocksize, 0);
1360 free_extent_buffer(eb);
1361 }
1362 }
1363 return ret;
1364}
1365
1366
1367/*
Chris Masond3977122009-01-05 21:25:51 -05001368 * when we walk down the tree, it is usually safe to unlock the higher layers
1369 * in the tree. The exceptions are when our path goes through slot 0, because
1370 * operations on the tree might require changing key pointers higher up in the
1371 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001372 *
Chris Masond3977122009-01-05 21:25:51 -05001373 * callers might also have set path->keep_locks, which tells this code to keep
1374 * the lock if the path points to the last slot in the block. This is part of
1375 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001376 *
Chris Masond3977122009-01-05 21:25:51 -05001377 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1378 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001379 */
Chris Masone02119d2008-09-05 16:13:11 -04001380static noinline void unlock_up(struct btrfs_path *path, int level,
1381 int lowest_unlock)
Chris Mason925baed2008-06-25 16:01:30 -04001382{
1383 int i;
1384 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001385 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001386 struct extent_buffer *t;
1387
1388 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1389 if (!path->nodes[i])
1390 break;
1391 if (!path->locks[i])
1392 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001393 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001394 skip_level = i + 1;
1395 continue;
1396 }
Chris Mason051e1b92008-06-25 16:01:30 -04001397 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001398 u32 nritems;
1399 t = path->nodes[i];
1400 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001401 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001402 skip_level = i + 1;
1403 continue;
1404 }
1405 }
Chris Mason051e1b92008-06-25 16:01:30 -04001406 if (skip_level < i && i >= lowest_unlock)
1407 no_skips = 1;
1408
Chris Mason925baed2008-06-25 16:01:30 -04001409 t = path->nodes[i];
1410 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1411 btrfs_tree_unlock(t);
1412 path->locks[i] = 0;
1413 }
1414 }
1415}
1416
Chris Mason3c69fae2007-08-07 15:52:22 -04001417/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001418 * This releases any locks held in the path starting at level and
1419 * going all the way up to the root.
1420 *
1421 * btrfs_search_slot will keep the lock held on higher nodes in a few
1422 * corner cases, such as COW of the block at slot zero in the node. This
1423 * ignores those rules, and it should only be called when there are no
1424 * more updates to be done higher up in the tree.
1425 */
1426noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1427{
1428 int i;
1429
1430 if (path->keep_locks || path->lowest_level)
1431 return;
1432
1433 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1434 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001435 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001436 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001437 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001438 btrfs_tree_unlock(path->nodes[i]);
1439 path->locks[i] = 0;
1440 }
1441}
1442
1443/*
Chris Mason74123bd2007-02-02 11:05:29 -05001444 * look for key in the tree. path is filled in with nodes along the way
1445 * if key is found, we return zero and you can find the item in the leaf
1446 * level of the path (level 0)
1447 *
1448 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05001449 * be inserted, and 1 is returned. If there are other errors during the
1450 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05001451 *
1452 * if ins_len > 0, nodes and leaves will be split as we walk down the
1453 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1454 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05001455 */
Chris Masone089f052007-03-16 16:20:31 -04001456int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1457 *root, struct btrfs_key *key, struct btrfs_path *p, int
1458 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001459{
Chris Mason5f39d392007-10-15 16:14:19 -04001460 struct extent_buffer *b;
Chris Mason051e1b92008-06-25 16:01:30 -04001461 struct extent_buffer *tmp;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001462 int slot;
1463 int ret;
1464 int level;
Chris Mason3c69fae2007-08-07 15:52:22 -04001465 int should_reada = p->reada;
Chris Mason925baed2008-06-25 16:01:30 -04001466 int lowest_unlock = 1;
Chris Mason594a24e2008-06-25 16:01:30 -04001467 int blocksize;
Chris Mason9f3a7422007-08-07 15:52:19 -04001468 u8 lowest_level = 0;
Chris Mason594a24e2008-06-25 16:01:30 -04001469 u64 blocknr;
1470 u64 gen;
Chris Mason65b51a02008-08-01 15:11:20 -04001471 struct btrfs_key prealloc_block;
Chris Mason9f3a7422007-08-07 15:52:19 -04001472
Chris Mason6702ed42007-08-07 16:15:09 -04001473 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001474 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001475 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04001476
Chris Mason925baed2008-06-25 16:01:30 -04001477 if (ins_len < 0)
1478 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001479
1480 prealloc_block.objectid = 0;
1481
Chris Masonbb803952007-03-01 12:04:21 -05001482again:
Chris Mason5cd57b22008-06-25 16:01:30 -04001483 if (p->skip_locking)
1484 b = btrfs_root_node(root);
1485 else
1486 b = btrfs_lock_root_node(root);
Chris Mason925baed2008-06-25 16:01:30 -04001487
Chris Masoneb60cea2007-02-02 09:18:22 -05001488 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04001489 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001490
1491 /*
1492 * setup the path here so we can release it under lock
1493 * contention with the cow code
1494 */
1495 p->nodes[level] = b;
1496 if (!p->skip_locking)
1497 p->locks[level] = 1;
1498
Chris Mason02217ed2007-03-02 16:08:05 -05001499 if (cow) {
1500 int wret;
Chris Mason65b51a02008-08-01 15:11:20 -04001501
1502 /* is a cow on this block not required */
Chris Mason65b51a02008-08-01 15:11:20 -04001503 if (btrfs_header_generation(b) == trans->transid &&
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001504 btrfs_header_owner(b) == root->root_key.objectid &&
Chris Mason65b51a02008-08-01 15:11:20 -04001505 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason65b51a02008-08-01 15:11:20 -04001506 goto cow_done;
1507 }
Chris Mason65b51a02008-08-01 15:11:20 -04001508
1509 /* ok, we have to cow, is our old prealloc the right
1510 * size?
1511 */
1512 if (prealloc_block.objectid &&
1513 prealloc_block.offset != b->len) {
Chris Mason7b78c172009-02-04 09:12:46 -05001514 btrfs_release_path(root, p);
Chris Mason65b51a02008-08-01 15:11:20 -04001515 btrfs_free_reserved_extent(root,
1516 prealloc_block.objectid,
1517 prealloc_block.offset);
1518 prealloc_block.objectid = 0;
Chris Mason7b78c172009-02-04 09:12:46 -05001519 goto again;
Chris Mason65b51a02008-08-01 15:11:20 -04001520 }
1521
1522 /*
1523 * for higher level blocks, try not to allocate blocks
1524 * with the block and the parent locks held.
1525 */
Chris Mason284b0662009-02-09 16:22:03 -05001526 if (level > 0 && !prealloc_block.objectid) {
Chris Mason65b51a02008-08-01 15:11:20 -04001527 u32 size = b->len;
1528 u64 hint = b->start;
1529
1530 btrfs_release_path(root, p);
1531 ret = btrfs_reserve_extent(trans, root,
1532 size, size, 0,
1533 hint, (u64)-1,
1534 &prealloc_block, 0);
1535 BUG_ON(ret);
1536 goto again;
1537 }
1538
Chris Masonb4ce94d2009-02-04 09:25:08 -05001539 btrfs_set_path_blocking(p);
1540
Chris Masone20d96d2007-03-22 12:13:20 -04001541 wret = btrfs_cow_block(trans, root, b,
1542 p->nodes[level + 1],
1543 p->slots[level + 1],
Chris Mason65b51a02008-08-01 15:11:20 -04001544 &b, prealloc_block.objectid);
1545 prealloc_block.objectid = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001546 if (wret) {
Chris Mason5f39d392007-10-15 16:14:19 -04001547 free_extent_buffer(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001548 ret = wret;
1549 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001550 }
Chris Mason02217ed2007-03-02 16:08:05 -05001551 }
Chris Mason65b51a02008-08-01 15:11:20 -04001552cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05001553 BUG_ON(!cow && ins_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001554 if (level != btrfs_header_level(b))
Chris Mason2c90e5d2007-04-02 10:50:19 -04001555 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04001556 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001557
Chris Masoneb60cea2007-02-02 09:18:22 -05001558 p->nodes[level] = b;
Chris Mason5cd57b22008-06-25 16:01:30 -04001559 if (!p->skip_locking)
1560 p->locks[level] = 1;
Chris Mason65b51a02008-08-01 15:11:20 -04001561
Chris Masonb4ce94d2009-02-04 09:25:08 -05001562 btrfs_clear_path_blocking(p);
1563
1564 /*
1565 * we have a lock on b and as long as we aren't changing
1566 * the tree, there is no way to for the items in b to change.
1567 * It is safe to drop the lock on our parent before we
1568 * go through the expensive btree search on b.
1569 *
1570 * If cow is true, then we might be changing slot zero,
1571 * which may require changing the parent. So, we can't
1572 * drop the lock until after we know which slot we're
1573 * operating on.
1574 */
1575 if (!cow)
1576 btrfs_unlock_up_safe(p, level + 1);
1577
Chris Mason123abc82007-03-14 14:14:43 -04001578 ret = check_block(root, p, level);
Chris Mason65b51a02008-08-01 15:11:20 -04001579 if (ret) {
1580 ret = -1;
1581 goto done;
1582 }
Chris Mason925baed2008-06-25 16:01:30 -04001583
Chris Mason5f39d392007-10-15 16:14:19 -04001584 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001585
Chris Mason5f39d392007-10-15 16:14:19 -04001586 if (level != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001587 if (ret && slot > 0)
1588 slot -= 1;
1589 p->slots[level] = slot;
Chris Mason459931e2008-12-10 09:10:46 -05001590 if ((p->search_for_split || ins_len > 0) &&
1591 btrfs_header_nritems(b) >=
Chris Mason15147942008-04-24 09:22:51 -04001592 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001593 int sret;
1594
1595 sret = reada_for_balance(root, p, level);
1596 if (sret)
1597 goto again;
1598
1599 btrfs_set_path_blocking(p);
1600 sret = split_node(trans, root, p, level);
1601 btrfs_clear_path_blocking(p);
1602
Chris Mason5c680ed2007-02-22 11:39:13 -05001603 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001604 if (sret) {
1605 ret = sret;
1606 goto done;
1607 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001608 b = p->nodes[level];
Chris Mason5c680ed2007-02-22 11:39:13 -05001609 slot = p->slots[level];
Chris Mason7b78c172009-02-04 09:12:46 -05001610 } else if (ins_len < 0 &&
1611 btrfs_header_nritems(b) <
1612 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001613 int sret;
1614
1615 sret = reada_for_balance(root, p, level);
1616 if (sret)
1617 goto again;
1618
1619 btrfs_set_path_blocking(p);
1620 sret = balance_level(trans, root, p, level);
1621 btrfs_clear_path_blocking(p);
1622
Chris Mason65b51a02008-08-01 15:11:20 -04001623 if (sret) {
1624 ret = sret;
1625 goto done;
1626 }
Chris Masonbb803952007-03-01 12:04:21 -05001627 b = p->nodes[level];
Chris Masonf510cfe2007-10-15 16:14:48 -04001628 if (!b) {
1629 btrfs_release_path(NULL, p);
Chris Masonbb803952007-03-01 12:04:21 -05001630 goto again;
Chris Masonf510cfe2007-10-15 16:14:48 -04001631 }
Chris Masonbb803952007-03-01 12:04:21 -05001632 slot = p->slots[level];
Chris Mason5f39d392007-10-15 16:14:19 -04001633 BUG_ON(btrfs_header_nritems(b) == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05001634 }
Chris Masonf9efa9c2008-06-25 16:14:04 -04001635 unlock_up(p, level, lowest_unlock);
1636
Chris Mason9f3a7422007-08-07 15:52:19 -04001637 /* this is only true while dropping a snapshot */
Chris Mason925baed2008-06-25 16:01:30 -04001638 if (level == lowest_level) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001639 ret = 0;
1640 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04001641 }
Chris Masonca7a79a2008-05-12 12:59:19 -04001642
Chris Mason594a24e2008-06-25 16:01:30 -04001643 blocknr = btrfs_node_blockptr(b, slot);
1644 gen = btrfs_node_ptr_generation(b, slot);
1645 blocksize = btrfs_level_size(root, level - 1);
1646
1647 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1648 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
Chris Mason051e1b92008-06-25 16:01:30 -04001649 b = tmp;
1650 } else {
1651 /*
1652 * reduce lock contention at high levels
1653 * of the btree by dropping locks before
1654 * we read.
1655 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001656 if (level > 0) {
Chris Mason051e1b92008-06-25 16:01:30 -04001657 btrfs_release_path(NULL, p);
1658 if (tmp)
1659 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001660 if (should_reada)
1661 reada_for_search(root, p,
1662 level, slot,
1663 key->objectid);
1664
Chris Mason594a24e2008-06-25 16:01:30 -04001665 tmp = read_tree_block(root, blocknr,
1666 blocksize, gen);
1667 if (tmp)
1668 free_extent_buffer(tmp);
Chris Mason051e1b92008-06-25 16:01:30 -04001669 goto again;
1670 } else {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001671 btrfs_set_path_blocking(p);
Chris Masona74a4b92008-06-25 16:01:31 -04001672 if (tmp)
1673 free_extent_buffer(tmp);
Chris Masonf9efa9c2008-06-25 16:14:04 -04001674 if (should_reada)
1675 reada_for_search(root, p,
1676 level, slot,
1677 key->objectid);
Chris Mason051e1b92008-06-25 16:01:30 -04001678 b = read_node_slot(root, b, slot);
1679 }
1680 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001681 if (!p->skip_locking) {
1682 int lret;
1683
1684 btrfs_clear_path_blocking(p);
1685 lret = btrfs_try_spin_lock(b);
1686
1687 if (!lret) {
1688 btrfs_set_path_blocking(p);
1689 btrfs_tree_lock(b);
1690 btrfs_clear_path_blocking(p);
1691 }
1692 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001693 } else {
1694 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05001695 if (ins_len > 0 &&
1696 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001697 int sret;
1698
1699 btrfs_set_path_blocking(p);
1700 sret = split_leaf(trans, root, key,
Chris Masoncc0c5532007-10-25 15:42:57 -04001701 p, ins_len, ret == 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001702 btrfs_clear_path_blocking(p);
1703
Chris Mason5c680ed2007-02-22 11:39:13 -05001704 BUG_ON(sret > 0);
Chris Mason65b51a02008-08-01 15:11:20 -04001705 if (sret) {
1706 ret = sret;
1707 goto done;
1708 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001709 }
Chris Mason459931e2008-12-10 09:10:46 -05001710 if (!p->search_for_split)
1711 unlock_up(p, level, lowest_unlock);
Chris Mason65b51a02008-08-01 15:11:20 -04001712 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001713 }
1714 }
Chris Mason65b51a02008-08-01 15:11:20 -04001715 ret = 1;
1716done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05001717 /*
1718 * we don't really know what they plan on doing with the path
1719 * from here on, so for now just mark it as blocking
1720 */
1721 btrfs_set_path_blocking(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001722 if (prealloc_block.objectid) {
1723 btrfs_free_reserved_extent(root,
1724 prealloc_block.objectid,
1725 prealloc_block.offset);
1726 }
Chris Mason65b51a02008-08-01 15:11:20 -04001727 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001728}
1729
Zheng Yan1a40e232008-09-26 10:09:34 -04001730int btrfs_merge_path(struct btrfs_trans_handle *trans,
1731 struct btrfs_root *root,
1732 struct btrfs_key *node_keys,
1733 u64 *nodes, int lowest_level)
1734{
1735 struct extent_buffer *eb;
1736 struct extent_buffer *parent;
1737 struct btrfs_key key;
1738 u64 bytenr;
1739 u64 generation;
1740 u32 blocksize;
1741 int level;
1742 int slot;
1743 int key_match;
1744 int ret;
1745
1746 eb = btrfs_lock_root_node(root);
1747 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1748 BUG_ON(ret);
1749
Chris Masonb4ce94d2009-02-04 09:25:08 -05001750 btrfs_set_lock_blocking(eb);
1751
Zheng Yan1a40e232008-09-26 10:09:34 -04001752 parent = eb;
1753 while (1) {
1754 level = btrfs_header_level(parent);
1755 if (level == 0 || level <= lowest_level)
1756 break;
1757
1758 ret = bin_search(parent, &node_keys[lowest_level], level,
1759 &slot);
1760 if (ret && slot > 0)
1761 slot--;
1762
1763 bytenr = btrfs_node_blockptr(parent, slot);
1764 if (nodes[level - 1] == bytenr)
1765 break;
1766
1767 blocksize = btrfs_level_size(root, level - 1);
1768 generation = btrfs_node_ptr_generation(parent, slot);
1769 btrfs_node_key_to_cpu(eb, &key, slot);
1770 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1771
Yan Zhengf82d02d2008-10-29 14:49:05 -04001772 if (generation == trans->transid) {
Zheng Yan1a40e232008-09-26 10:09:34 -04001773 eb = read_tree_block(root, bytenr, blocksize,
1774 generation);
1775 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001776 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001777 }
1778
1779 /*
1780 * if node keys match and node pointer hasn't been modified
1781 * in the running transaction, we can merge the path. for
1782 * blocks owened by reloc trees, the node pointer check is
1783 * skipped, this is because these blocks are fully controlled
1784 * by the space balance code, no one else can modify them.
1785 */
1786 if (!nodes[level - 1] || !key_match ||
1787 (generation == trans->transid &&
1788 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1789 if (level == 1 || level == lowest_level + 1) {
1790 if (generation == trans->transid) {
1791 btrfs_tree_unlock(eb);
1792 free_extent_buffer(eb);
1793 }
1794 break;
1795 }
1796
1797 if (generation != trans->transid) {
1798 eb = read_tree_block(root, bytenr, blocksize,
1799 generation);
1800 btrfs_tree_lock(eb);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001801 btrfs_set_lock_blocking(eb);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001802 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001803
1804 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1805 &eb, 0);
1806 BUG_ON(ret);
1807
Yan Zhengf82d02d2008-10-29 14:49:05 -04001808 if (root->root_key.objectid ==
1809 BTRFS_TREE_RELOC_OBJECTID) {
1810 if (!nodes[level - 1]) {
1811 nodes[level - 1] = eb->start;
1812 memcpy(&node_keys[level - 1], &key,
1813 sizeof(node_keys[0]));
1814 } else {
1815 WARN_ON(1);
1816 }
1817 }
1818
Zheng Yan1a40e232008-09-26 10:09:34 -04001819 btrfs_tree_unlock(parent);
1820 free_extent_buffer(parent);
1821 parent = eb;
1822 continue;
1823 }
1824
Zheng Yan1a40e232008-09-26 10:09:34 -04001825 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1826 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1827 btrfs_mark_buffer_dirty(parent);
1828
1829 ret = btrfs_inc_extent_ref(trans, root,
1830 nodes[level - 1],
1831 blocksize, parent->start,
1832 btrfs_header_owner(parent),
1833 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001834 level - 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04001835 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001836
1837 /*
1838 * If the block was created in the running transaction,
1839 * it's possible this is the last reference to it, so we
1840 * should drop the subtree.
1841 */
1842 if (generation == trans->transid) {
1843 ret = btrfs_drop_subtree(trans, root, eb, parent);
1844 BUG_ON(ret);
1845 btrfs_tree_unlock(eb);
1846 free_extent_buffer(eb);
1847 } else {
1848 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan1a40e232008-09-26 10:09:34 -04001849 blocksize, parent->start,
1850 btrfs_header_owner(parent),
1851 btrfs_header_generation(parent),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001852 level - 1, 1);
Yan Zhengf82d02d2008-10-29 14:49:05 -04001853 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04001854 }
1855 break;
1856 }
1857 btrfs_tree_unlock(parent);
1858 free_extent_buffer(parent);
1859 return 0;
1860}
1861
Chris Mason74123bd2007-02-02 11:05:29 -05001862/*
1863 * adjust the pointers going up the tree, starting at level
1864 * making sure the right key of each node is points to 'key'.
1865 * This is used after shifting pointers to the left, so it stops
1866 * fixing up pointers when a given leaf/node is not in slot 0 of the
1867 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05001868 *
1869 * If this fails to write a tree block, it returns -1, but continues
1870 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -05001871 */
Chris Mason5f39d392007-10-15 16:14:19 -04001872static int fixup_low_keys(struct btrfs_trans_handle *trans,
1873 struct btrfs_root *root, struct btrfs_path *path,
1874 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001875{
1876 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001877 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001878 struct extent_buffer *t;
1879
Chris Mason234b63a2007-03-13 10:46:10 -04001880 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001881 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05001882 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05001883 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001884 t = path->nodes[i];
1885 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04001886 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001887 if (tslot != 0)
1888 break;
1889 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001890 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001891}
1892
Chris Mason74123bd2007-02-02 11:05:29 -05001893/*
Zheng Yan31840ae2008-09-23 13:14:14 -04001894 * update item key.
1895 *
1896 * This function isn't completely safe. It's the caller's responsibility
1897 * that the new key won't break the order
1898 */
1899int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1900 struct btrfs_root *root, struct btrfs_path *path,
1901 struct btrfs_key *new_key)
1902{
1903 struct btrfs_disk_key disk_key;
1904 struct extent_buffer *eb;
1905 int slot;
1906
1907 eb = path->nodes[0];
1908 slot = path->slots[0];
1909 if (slot > 0) {
1910 btrfs_item_key(eb, &disk_key, slot - 1);
1911 if (comp_keys(&disk_key, new_key) >= 0)
1912 return -1;
1913 }
1914 if (slot < btrfs_header_nritems(eb) - 1) {
1915 btrfs_item_key(eb, &disk_key, slot + 1);
1916 if (comp_keys(&disk_key, new_key) <= 0)
1917 return -1;
1918 }
1919
1920 btrfs_cpu_key_to_disk(&disk_key, new_key);
1921 btrfs_set_item_key(eb, &disk_key, slot);
1922 btrfs_mark_buffer_dirty(eb);
1923 if (slot == 0)
1924 fixup_low_keys(trans, root, path, &disk_key, 1);
1925 return 0;
1926}
1927
1928/*
Chris Mason74123bd2007-02-02 11:05:29 -05001929 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05001930 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05001931 *
1932 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1933 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05001934 */
Chris Mason98ed5172008-01-03 10:01:48 -05001935static int push_node_left(struct btrfs_trans_handle *trans,
1936 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04001937 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001938{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001939 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001940 int src_nritems;
1941 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001942 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001943
Chris Mason5f39d392007-10-15 16:14:19 -04001944 src_nritems = btrfs_header_nritems(src);
1945 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04001946 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05001947 WARN_ON(btrfs_header_generation(src) != trans->transid);
1948 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001949
Chris Masonbce4eae2008-04-24 14:42:46 -04001950 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04001951 return 1;
1952
Chris Masond3977122009-01-05 21:25:51 -05001953 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001954 return 1;
1955
Chris Masonbce4eae2008-04-24 14:42:46 -04001956 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04001957 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04001958 if (push_items < src_nritems) {
1959 /* leave at least 8 pointers in the node if
1960 * we aren't going to empty it
1961 */
1962 if (src_nritems - push_items < 8) {
1963 if (push_items <= 8)
1964 return 1;
1965 push_items -= 8;
1966 }
1967 }
1968 } else
1969 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05001970
Chris Mason5f39d392007-10-15 16:14:19 -04001971 copy_extent_buffer(dst, src,
1972 btrfs_node_key_ptr_offset(dst_nritems),
1973 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05001974 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04001975
Chris Masonbb803952007-03-01 12:04:21 -05001976 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04001977 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1978 btrfs_node_key_ptr_offset(push_items),
1979 (src_nritems - push_items) *
1980 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05001981 }
Chris Mason5f39d392007-10-15 16:14:19 -04001982 btrfs_set_header_nritems(src, src_nritems - push_items);
1983 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1984 btrfs_mark_buffer_dirty(src);
1985 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04001986
1987 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1988 BUG_ON(ret);
1989
Chris Masonbb803952007-03-01 12:04:21 -05001990 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001991}
1992
Chris Mason97571fd2007-02-24 13:39:08 -05001993/*
Chris Mason79f95c82007-03-01 15:16:26 -05001994 * try to push data from one node into the next node right in the
1995 * tree.
1996 *
1997 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1998 * error, and > 0 if there was no room in the right hand block.
1999 *
2000 * this will only push up to 1/2 the contents of the left node over
2001 */
Chris Mason5f39d392007-10-15 16:14:19 -04002002static int balance_node_right(struct btrfs_trans_handle *trans,
2003 struct btrfs_root *root,
2004 struct extent_buffer *dst,
2005 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002006{
Chris Mason79f95c82007-03-01 15:16:26 -05002007 int push_items = 0;
2008 int max_push;
2009 int src_nritems;
2010 int dst_nritems;
2011 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002012
Chris Mason7bb86312007-12-11 09:25:06 -05002013 WARN_ON(btrfs_header_generation(src) != trans->transid);
2014 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2015
Chris Mason5f39d392007-10-15 16:14:19 -04002016 src_nritems = btrfs_header_nritems(src);
2017 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002018 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002019 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002020 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002021
Chris Masond3977122009-01-05 21:25:51 -05002022 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002023 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002024
2025 max_push = src_nritems / 2 + 1;
2026 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002027 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002028 return 1;
Yan252c38f2007-08-29 09:11:44 -04002029
Chris Mason79f95c82007-03-01 15:16:26 -05002030 if (max_push < push_items)
2031 push_items = max_push;
2032
Chris Mason5f39d392007-10-15 16:14:19 -04002033 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2034 btrfs_node_key_ptr_offset(0),
2035 (dst_nritems) *
2036 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002037
Chris Mason5f39d392007-10-15 16:14:19 -04002038 copy_extent_buffer(dst, src,
2039 btrfs_node_key_ptr_offset(0),
2040 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002041 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002042
Chris Mason5f39d392007-10-15 16:14:19 -04002043 btrfs_set_header_nritems(src, src_nritems - push_items);
2044 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002045
Chris Mason5f39d392007-10-15 16:14:19 -04002046 btrfs_mark_buffer_dirty(src);
2047 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002048
2049 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
2050 BUG_ON(ret);
2051
Chris Mason79f95c82007-03-01 15:16:26 -05002052 return ret;
2053}
2054
2055/*
Chris Mason97571fd2007-02-24 13:39:08 -05002056 * helper function to insert a new root level in the tree.
2057 * A new node is allocated, and a single item is inserted to
2058 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002059 *
2060 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002061 */
Chris Masond3977122009-01-05 21:25:51 -05002062static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002063 struct btrfs_root *root,
2064 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002065{
Chris Mason7bb86312007-12-11 09:25:06 -05002066 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002067 struct extent_buffer *lower;
2068 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002069 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002070 struct btrfs_disk_key lower_key;
Zheng Yan31840ae2008-09-23 13:14:14 -04002071 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002072
2073 BUG_ON(path->nodes[level]);
2074 BUG_ON(path->nodes[level-1] != root->node);
2075
Chris Mason7bb86312007-12-11 09:25:06 -05002076 lower = path->nodes[level-1];
2077 if (level == 1)
2078 btrfs_item_key(lower, &lower_key, 0);
2079 else
2080 btrfs_node_key(lower, &lower_key, 0);
2081
Zheng Yan31840ae2008-09-23 13:14:14 -04002082 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2083 root->root_key.objectid, trans->transid,
Christoph Hellwigad3d81b2008-09-05 16:43:28 -04002084 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002085 if (IS_ERR(c))
2086 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002087
Chris Mason5f39d392007-10-15 16:14:19 -04002088 memset_extent_buffer(c, 0, 0, root->nodesize);
2089 btrfs_set_header_nritems(c, 1);
2090 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002091 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002092 btrfs_set_header_generation(c, trans->transid);
2093 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002094
Chris Mason5f39d392007-10-15 16:14:19 -04002095 write_extent_buffer(c, root->fs_info->fsid,
2096 (unsigned long)btrfs_header_fsid(c),
2097 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002098
2099 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2100 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2101 BTRFS_UUID_SIZE);
2102
Chris Mason5f39d392007-10-15 16:14:19 -04002103 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002104 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002105 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002106 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002107
2108 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002109
2110 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002111
Chris Mason925baed2008-06-25 16:01:30 -04002112 spin_lock(&root->node_lock);
2113 old = root->node;
Chris Mason5f39d392007-10-15 16:14:19 -04002114 root->node = c;
Chris Mason925baed2008-06-25 16:01:30 -04002115 spin_unlock(&root->node_lock);
2116
Zheng Yan31840ae2008-09-23 13:14:14 -04002117 ret = btrfs_update_extent_ref(trans, root, lower->start,
2118 lower->start, c->start,
2119 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002120 trans->transid, level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002121 BUG_ON(ret);
2122
Chris Mason925baed2008-06-25 16:01:30 -04002123 /* the super has an extra ref to root->node */
2124 free_extent_buffer(old);
2125
Chris Mason0b86a832008-03-24 15:01:56 -04002126 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002127 extent_buffer_get(c);
2128 path->nodes[level] = c;
Chris Mason925baed2008-06-25 16:01:30 -04002129 path->locks[level] = 1;
Chris Mason5c680ed2007-02-22 11:39:13 -05002130 path->slots[level] = 0;
2131 return 0;
2132}
2133
Chris Mason74123bd2007-02-02 11:05:29 -05002134/*
2135 * worker function to insert a single pointer in a node.
2136 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002137 *
Chris Mason74123bd2007-02-02 11:05:29 -05002138 * slot and level indicate where you want the key to go, and
2139 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002140 *
2141 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -05002142 */
Chris Masone089f052007-03-16 16:20:31 -04002143static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2144 *root, struct btrfs_path *path, struct btrfs_disk_key
Chris Masondb945352007-10-15 16:15:53 -04002145 *key, u64 bytenr, int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002146{
Chris Mason5f39d392007-10-15 16:14:19 -04002147 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002148 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002149
2150 BUG_ON(!path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002151 lower = path->nodes[level];
2152 nritems = btrfs_header_nritems(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002153 if (slot > nritems)
2154 BUG();
Chris Mason123abc82007-03-14 14:14:43 -04002155 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
Chris Mason74123bd2007-02-02 11:05:29 -05002156 BUG();
2157 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002158 memmove_extent_buffer(lower,
2159 btrfs_node_key_ptr_offset(slot + 1),
2160 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002161 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002162 }
Chris Mason5f39d392007-10-15 16:14:19 -04002163 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002164 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002165 WARN_ON(trans->transid == 0);
2166 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002167 btrfs_set_header_nritems(lower, nritems + 1);
2168 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002169 return 0;
2170}
2171
Chris Mason97571fd2007-02-24 13:39:08 -05002172/*
2173 * split the node at the specified level in path in two.
2174 * The path is corrected to point to the appropriate node after the split
2175 *
2176 * Before splitting this tries to make some room in the node by pushing
2177 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002178 *
2179 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002180 */
Chris Masone02119d2008-09-05 16:13:11 -04002181static noinline int split_node(struct btrfs_trans_handle *trans,
2182 struct btrfs_root *root,
2183 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002184{
Chris Mason5f39d392007-10-15 16:14:19 -04002185 struct extent_buffer *c;
2186 struct extent_buffer *split;
2187 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002188 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002189 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002190 int wret;
Chris Mason7518a232007-03-12 12:01:18 -04002191 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002192
Chris Mason5f39d392007-10-15 16:14:19 -04002193 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002194 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002195 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002196 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002197 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002198 if (ret)
2199 return ret;
Chris Masone66f7092007-04-20 13:16:02 -04002200 } else {
2201 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002202 c = path->nodes[level];
2203 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002204 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002205 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002206 if (ret < 0)
2207 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002208 }
Chris Masone66f7092007-04-20 13:16:02 -04002209
Chris Mason5f39d392007-10-15 16:14:19 -04002210 c_nritems = btrfs_header_nritems(c);
Chris Mason7bb86312007-12-11 09:25:06 -05002211
Chris Mason925baed2008-06-25 16:01:30 -04002212 split = btrfs_alloc_free_block(trans, root, root->nodesize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002213 path->nodes[level + 1]->start,
2214 root->root_key.objectid,
2215 trans->transid, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002216 if (IS_ERR(split))
2217 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002218
Chris Mason5f39d392007-10-15 16:14:19 -04002219 btrfs_set_header_flags(split, btrfs_header_flags(c));
2220 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002221 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002222 btrfs_set_header_generation(split, trans->transid);
2223 btrfs_set_header_owner(split, root->root_key.objectid);
Chris Mason63b10fc2008-04-01 11:21:32 -04002224 btrfs_set_header_flags(split, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002225 write_extent_buffer(split, root->fs_info->fsid,
2226 (unsigned long)btrfs_header_fsid(split),
2227 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002228 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2229 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2230 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002231
Chris Mason7518a232007-03-12 12:01:18 -04002232 mid = (c_nritems + 1) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04002233
2234 copy_extent_buffer(split, c,
2235 btrfs_node_key_ptr_offset(0),
2236 btrfs_node_key_ptr_offset(mid),
2237 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2238 btrfs_set_header_nritems(split, c_nritems - mid);
2239 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002240 ret = 0;
2241
Chris Mason5f39d392007-10-15 16:14:19 -04002242 btrfs_mark_buffer_dirty(c);
2243 btrfs_mark_buffer_dirty(split);
2244
2245 btrfs_node_key(split, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002246 wret = insert_ptr(trans, root, path, &disk_key, split->start,
Chris Mason5f39d392007-10-15 16:14:19 -04002247 path->slots[level + 1] + 1,
Chris Mason123abc82007-03-14 14:14:43 -04002248 level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002249 if (wret)
2250 ret = wret;
2251
Zheng Yan31840ae2008-09-23 13:14:14 -04002252 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2253 BUG_ON(ret);
2254
Chris Mason5de08d72007-02-24 06:24:44 -05002255 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002256 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002257 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002258 free_extent_buffer(c);
2259 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002260 path->slots[level + 1] += 1;
2261 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002262 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002263 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002264 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002265 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002266}
2267
Chris Mason74123bd2007-02-02 11:05:29 -05002268/*
2269 * how many bytes are required to store the items in a leaf. start
2270 * and nr indicate which items in the leaf to check. This totals up the
2271 * space used both by the item structs and the item data
2272 */
Chris Mason5f39d392007-10-15 16:14:19 -04002273static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002274{
2275 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002276 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002277 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002278
2279 if (!nr)
2280 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002281 data_len = btrfs_item_end_nr(l, start);
2282 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002283 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002284 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002285 return data_len;
2286}
2287
Chris Mason74123bd2007-02-02 11:05:29 -05002288/*
Chris Masond4dbff92007-04-04 14:08:15 -04002289 * The space between the end of the leaf items and
2290 * the start of the leaf data. IOW, how much room
2291 * the leaf has left for both items and data
2292 */
Chris Masond3977122009-01-05 21:25:51 -05002293noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002294 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002295{
Chris Mason5f39d392007-10-15 16:14:19 -04002296 int nritems = btrfs_header_nritems(leaf);
2297 int ret;
2298 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2299 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002300 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2301 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002302 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002303 leaf_space_used(leaf, 0, nritems), nritems);
2304 }
2305 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002306}
2307
2308/*
Chris Mason00ec4c52007-02-24 12:47:20 -05002309 * push some data in the path leaf to the right, trying to free up at
2310 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -05002311 *
2312 * returns 1 if the push failed because the other node didn't have enough
2313 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -05002314 */
Chris Masone089f052007-03-16 16:20:31 -04002315static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002316 *root, struct btrfs_path *path, int data_size,
2317 int empty)
Chris Mason00ec4c52007-02-24 12:47:20 -05002318{
Chris Mason5f39d392007-10-15 16:14:19 -04002319 struct extent_buffer *left = path->nodes[0];
2320 struct extent_buffer *right;
2321 struct extent_buffer *upper;
2322 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002323 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002324 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002325 int free_space;
2326 int push_space = 0;
2327 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002328 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002329 u32 left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002330 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002331 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002332 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002333 u32 this_item_size;
Chris Mason54aa1f42007-06-22 14:16:25 -04002334 int ret;
Chris Mason00ec4c52007-02-24 12:47:20 -05002335
2336 slot = path->slots[1];
Chris Masond3977122009-01-05 21:25:51 -05002337 if (!path->nodes[1])
Chris Mason00ec4c52007-02-24 12:47:20 -05002338 return 1;
Chris Masond3977122009-01-05 21:25:51 -05002339
Chris Mason00ec4c52007-02-24 12:47:20 -05002340 upper = path->nodes[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002341 if (slot >= btrfs_header_nritems(upper) - 1)
Chris Mason00ec4c52007-02-24 12:47:20 -05002342 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002343
Chris Masona2135012008-06-25 16:01:30 -04002344 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2345
Chris Masonca7a79a2008-05-12 12:59:19 -04002346 right = read_node_slot(root, upper, slot + 1);
Chris Mason925baed2008-06-25 16:01:30 -04002347 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002348 btrfs_set_lock_blocking(right);
2349
Chris Mason123abc82007-03-14 14:14:43 -04002350 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002351 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002352 goto out_unlock;
Chris Mason02217ed2007-03-02 16:08:05 -05002353
Chris Mason5f39d392007-10-15 16:14:19 -04002354 /* cow and double check */
2355 ret = btrfs_cow_block(trans, root, right, upper,
Chris Mason65b51a02008-08-01 15:11:20 -04002356 slot + 1, &right, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002357 if (ret)
2358 goto out_unlock;
2359
Chris Mason5f39d392007-10-15 16:14:19 -04002360 free_space = btrfs_leaf_free_space(root, right);
Yan Zheng87b29b22008-12-17 10:21:48 -05002361 if (free_space < data_size)
Chris Mason925baed2008-06-25 16:01:30 -04002362 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002363
2364 left_nritems = btrfs_header_nritems(left);
Chris Mason925baed2008-06-25 16:01:30 -04002365 if (left_nritems == 0)
2366 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002367
Chris Mason34a38212007-11-07 13:31:03 -05002368 if (empty)
2369 nr = 0;
2370 else
2371 nr = 1;
2372
Zheng Yan31840ae2008-09-23 13:14:14 -04002373 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002374 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002375
Chris Mason34a38212007-11-07 13:31:03 -05002376 i = left_nritems - 1;
2377 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002378 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002379
Zheng Yan31840ae2008-09-23 13:14:14 -04002380 if (!empty && push_items > 0) {
2381 if (path->slots[0] > i)
2382 break;
2383 if (path->slots[0] == i) {
2384 int space = btrfs_leaf_free_space(root, left);
2385 if (space + push_space * 2 > free_space)
2386 break;
2387 }
2388 }
2389
Chris Mason00ec4c52007-02-24 12:47:20 -05002390 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002391 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002392
2393 if (!left->map_token) {
2394 map_extent_buffer(left, (unsigned long)item,
2395 sizeof(struct btrfs_item),
2396 &left->map_token, &left->kaddr,
2397 &left->map_start, &left->map_len,
2398 KM_USER1);
2399 }
2400
2401 this_item_size = btrfs_item_size(left, item);
2402 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002403 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002404
Chris Mason00ec4c52007-02-24 12:47:20 -05002405 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002406 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002407 if (i == 0)
2408 break;
2409 i--;
Chris Masondb945352007-10-15 16:15:53 -04002410 }
2411 if (left->map_token) {
2412 unmap_extent_buffer(left, left->map_token, KM_USER1);
2413 left->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002414 }
Chris Mason5f39d392007-10-15 16:14:19 -04002415
Chris Mason925baed2008-06-25 16:01:30 -04002416 if (push_items == 0)
2417 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002418
Chris Mason34a38212007-11-07 13:31:03 -05002419 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002420 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002421
Chris Mason00ec4c52007-02-24 12:47:20 -05002422 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002423 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002424
Chris Mason5f39d392007-10-15 16:14:19 -04002425 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002426 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002427
Chris Mason00ec4c52007-02-24 12:47:20 -05002428 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002429 data_end = leaf_data_end(root, right);
2430 memmove_extent_buffer(right,
2431 btrfs_leaf_data(right) + data_end - push_space,
2432 btrfs_leaf_data(right) + data_end,
2433 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2434
Chris Mason00ec4c52007-02-24 12:47:20 -05002435 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002436 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002437 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2438 btrfs_leaf_data(left) + leaf_data_end(root, left),
2439 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002440
2441 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2442 btrfs_item_nr_offset(0),
2443 right_nritems * sizeof(struct btrfs_item));
2444
Chris Mason00ec4c52007-02-24 12:47:20 -05002445 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002446 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2447 btrfs_item_nr_offset(left_nritems - push_items),
2448 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002449
2450 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002451 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002452 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002453 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002454 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002455 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002456 if (!right->map_token) {
2457 map_extent_buffer(right, (unsigned long)item,
2458 sizeof(struct btrfs_item),
2459 &right->map_token, &right->kaddr,
2460 &right->map_start, &right->map_len,
2461 KM_USER1);
2462 }
2463 push_space -= btrfs_item_size(right, item);
2464 btrfs_set_item_offset(right, item, push_space);
2465 }
2466
2467 if (right->map_token) {
2468 unmap_extent_buffer(right, right->map_token, KM_USER1);
2469 right->map_token = NULL;
Chris Mason00ec4c52007-02-24 12:47:20 -05002470 }
Chris Mason7518a232007-03-12 12:01:18 -04002471 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002472 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002473
Chris Mason34a38212007-11-07 13:31:03 -05002474 if (left_nritems)
2475 btrfs_mark_buffer_dirty(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002476 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002477
Zheng Yan31840ae2008-09-23 13:14:14 -04002478 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2479 BUG_ON(ret);
2480
Chris Mason5f39d392007-10-15 16:14:19 -04002481 btrfs_item_key(right, &disk_key, 0);
2482 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002483 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002484
Chris Mason00ec4c52007-02-24 12:47:20 -05002485 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002486 if (path->slots[0] >= left_nritems) {
2487 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002488 if (btrfs_header_nritems(path->nodes[0]) == 0)
2489 clean_tree_block(trans, root, path->nodes[0]);
2490 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002491 free_extent_buffer(path->nodes[0]);
2492 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002493 path->slots[1] += 1;
2494 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002495 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002496 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002497 }
2498 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002499
2500out_unlock:
2501 btrfs_tree_unlock(right);
2502 free_extent_buffer(right);
2503 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002504}
Chris Mason925baed2008-06-25 16:01:30 -04002505
Chris Mason00ec4c52007-02-24 12:47:20 -05002506/*
Chris Mason74123bd2007-02-02 11:05:29 -05002507 * push some data in the path leaf to the left, trying to free up at
2508 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2509 */
Chris Masone089f052007-03-16 16:20:31 -04002510static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason34a38212007-11-07 13:31:03 -05002511 *root, struct btrfs_path *path, int data_size,
2512 int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002513{
Chris Mason5f39d392007-10-15 16:14:19 -04002514 struct btrfs_disk_key disk_key;
2515 struct extent_buffer *right = path->nodes[0];
2516 struct extent_buffer *left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002517 int slot;
2518 int i;
2519 int free_space;
2520 int push_space = 0;
2521 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002522 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002523 u32 old_left_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002524 u32 right_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002525 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002526 int ret = 0;
2527 int wret;
Chris Masondb945352007-10-15 16:15:53 -04002528 u32 this_item_size;
2529 u32 old_left_item_size;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002530
2531 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04002532 if (slot == 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002533 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002534 if (!path->nodes[1])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002535 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002536
Chris Mason3685f792007-10-19 09:23:27 -04002537 right_nritems = btrfs_header_nritems(right);
Chris Masond3977122009-01-05 21:25:51 -05002538 if (right_nritems == 0)
Chris Mason3685f792007-10-19 09:23:27 -04002539 return 1;
Chris Mason3685f792007-10-19 09:23:27 -04002540
Chris Masona2135012008-06-25 16:01:30 -04002541 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2542
Chris Masonca7a79a2008-05-12 12:59:19 -04002543 left = read_node_slot(root, path->nodes[1], slot - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002544 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002545 btrfs_set_lock_blocking(left);
2546
Chris Mason123abc82007-03-14 14:14:43 -04002547 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002548 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002549 ret = 1;
2550 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002551 }
Chris Mason02217ed2007-03-02 16:08:05 -05002552
2553 /* cow and double check */
Chris Mason5f39d392007-10-15 16:14:19 -04002554 ret = btrfs_cow_block(trans, root, left,
Chris Mason65b51a02008-08-01 15:11:20 -04002555 path->nodes[1], slot - 1, &left, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002556 if (ret) {
2557 /* we hit -ENOSPC, but it isn't fatal here */
Chris Mason925baed2008-06-25 16:01:30 -04002558 ret = 1;
2559 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04002560 }
Chris Mason3685f792007-10-19 09:23:27 -04002561
Chris Mason123abc82007-03-14 14:14:43 -04002562 free_space = btrfs_leaf_free_space(root, left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002563 if (free_space < data_size) {
Chris Mason925baed2008-06-25 16:01:30 -04002564 ret = 1;
2565 goto out;
Chris Mason02217ed2007-03-02 16:08:05 -05002566 }
2567
Chris Mason34a38212007-11-07 13:31:03 -05002568 if (empty)
2569 nr = right_nritems;
2570 else
2571 nr = right_nritems - 1;
2572
2573 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002574 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002575 if (!right->map_token) {
2576 map_extent_buffer(right, (unsigned long)item,
2577 sizeof(struct btrfs_item),
2578 &right->map_token, &right->kaddr,
2579 &right->map_start, &right->map_len,
2580 KM_USER1);
2581 }
2582
Zheng Yan31840ae2008-09-23 13:14:14 -04002583 if (!empty && push_items > 0) {
2584 if (path->slots[0] < i)
2585 break;
2586 if (path->slots[0] == i) {
2587 int space = btrfs_leaf_free_space(root, right);
2588 if (space + push_space * 2 > free_space)
2589 break;
2590 }
2591 }
2592
Chris Masonbe0e5c02007-01-26 15:51:26 -05002593 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002594 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002595
2596 this_item_size = btrfs_item_size(right, item);
2597 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002598 break;
Chris Masondb945352007-10-15 16:15:53 -04002599
Chris Masonbe0e5c02007-01-26 15:51:26 -05002600 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002601 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602 }
Chris Masondb945352007-10-15 16:15:53 -04002603
2604 if (right->map_token) {
2605 unmap_extent_buffer(right, right->map_token, KM_USER1);
2606 right->map_token = NULL;
2607 }
2608
Chris Masonbe0e5c02007-01-26 15:51:26 -05002609 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002610 ret = 1;
2611 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002612 }
Chris Mason34a38212007-11-07 13:31:03 -05002613 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04002614 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002615
Chris Masonbe0e5c02007-01-26 15:51:26 -05002616 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002617 copy_extent_buffer(left, right,
2618 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2619 btrfs_item_nr_offset(0),
2620 push_items * sizeof(struct btrfs_item));
2621
Chris Mason123abc82007-03-14 14:14:43 -04002622 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05002623 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002624
2625 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04002626 leaf_data_end(root, left) - push_space,
2627 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04002628 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002629 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002630 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002631 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002632
Chris Masondb945352007-10-15 16:15:53 -04002633 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002634 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002635 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002636
Chris Mason5f39d392007-10-15 16:14:19 -04002637 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002638 if (!left->map_token) {
2639 map_extent_buffer(left, (unsigned long)item,
2640 sizeof(struct btrfs_item),
2641 &left->map_token, &left->kaddr,
2642 &left->map_start, &left->map_len,
2643 KM_USER1);
2644 }
2645
Chris Mason5f39d392007-10-15 16:14:19 -04002646 ioff = btrfs_item_offset(left, item);
2647 btrfs_set_item_offset(left, item,
Chris Masondb945352007-10-15 16:15:53 -04002648 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05002649 }
Chris Mason5f39d392007-10-15 16:14:19 -04002650 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masondb945352007-10-15 16:15:53 -04002651 if (left->map_token) {
2652 unmap_extent_buffer(left, left->map_token, KM_USER1);
2653 left->map_token = NULL;
2654 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002655
2656 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05002657 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05002658 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2659 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05002660 WARN_ON(1);
2661 }
Chris Mason5f39d392007-10-15 16:14:19 -04002662
Chris Mason34a38212007-11-07 13:31:03 -05002663 if (push_items < right_nritems) {
2664 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2665 leaf_data_end(root, right);
2666 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2667 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2668 btrfs_leaf_data(right) +
2669 leaf_data_end(root, right), push_space);
2670
2671 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04002672 btrfs_item_nr_offset(push_items),
2673 (btrfs_header_nritems(right) - push_items) *
2674 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05002675 }
Yaneef1c492007-11-26 10:58:13 -05002676 right_nritems -= push_items;
2677 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002678 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002679 for (i = 0; i < right_nritems; i++) {
2680 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002681
2682 if (!right->map_token) {
2683 map_extent_buffer(right, (unsigned long)item,
2684 sizeof(struct btrfs_item),
2685 &right->map_token, &right->kaddr,
2686 &right->map_start, &right->map_len,
2687 KM_USER1);
2688 }
2689
2690 push_space = push_space - btrfs_item_size(right, item);
2691 btrfs_set_item_offset(right, item, push_space);
2692 }
2693 if (right->map_token) {
2694 unmap_extent_buffer(right, right->map_token, KM_USER1);
2695 right->map_token = NULL;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002696 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002697
Chris Mason5f39d392007-10-15 16:14:19 -04002698 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05002699 if (right_nritems)
2700 btrfs_mark_buffer_dirty(right);
Chris Mason098f59c2007-05-11 11:33:21 -04002701
Zheng Yan31840ae2008-09-23 13:14:14 -04002702 ret = btrfs_update_ref(trans, root, right, left,
2703 old_left_nritems, push_items);
2704 BUG_ON(ret);
2705
Chris Mason5f39d392007-10-15 16:14:19 -04002706 btrfs_item_key(right, &disk_key, 0);
2707 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002708 if (wret)
2709 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002710
2711 /* then fixup the leaf pointer in the path */
2712 if (path->slots[0] < push_items) {
2713 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002714 if (btrfs_header_nritems(path->nodes[0]) == 0)
2715 clean_tree_block(trans, root, path->nodes[0]);
2716 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002717 free_extent_buffer(path->nodes[0]);
2718 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002719 path->slots[1] -= 1;
2720 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002721 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002722 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002723 path->slots[0] -= push_items;
2724 }
Chris Masoneb60cea2007-02-02 09:18:22 -05002725 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002726 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04002727out:
2728 btrfs_tree_unlock(left);
2729 free_extent_buffer(left);
2730 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002731}
2732
Chris Mason74123bd2007-02-02 11:05:29 -05002733/*
2734 * split the path's leaf in two, making sure there is at least data_size
2735 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002736 *
2737 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -05002738 */
Chris Masone02119d2008-09-05 16:13:11 -04002739static noinline int split_leaf(struct btrfs_trans_handle *trans,
2740 struct btrfs_root *root,
2741 struct btrfs_key *ins_key,
2742 struct btrfs_path *path, int data_size,
2743 int extend)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002744{
Chris Mason5f39d392007-10-15 16:14:19 -04002745 struct extent_buffer *l;
Chris Mason7518a232007-03-12 12:01:18 -04002746 u32 nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -05002747 int mid;
2748 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04002749 struct extent_buffer *right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002750 int data_copy_size;
2751 int rt_data_off;
2752 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04002753 int ret = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002754 int wret;
Chris Masoncc0c5532007-10-25 15:42:57 -04002755 int double_split;
2756 int num_doubles = 0;
Chris Masond4dbff92007-04-04 14:08:15 -04002757 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002758
Chris Mason40689472007-03-17 14:29:23 -04002759 /* first try to make some room by pushing left and right */
Chris Mason459931e2008-12-10 09:10:46 -05002760 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
Chris Mason34a38212007-11-07 13:31:03 -05002761 wret = push_leaf_right(trans, root, path, data_size, 0);
Chris Masond3977122009-01-05 21:25:51 -05002762 if (wret < 0)
Chris Masoneaee50e2007-03-13 11:17:52 -04002763 return wret;
Chris Mason3685f792007-10-19 09:23:27 -04002764 if (wret) {
Chris Mason34a38212007-11-07 13:31:03 -05002765 wret = push_leaf_left(trans, root, path, data_size, 0);
Chris Mason3685f792007-10-19 09:23:27 -04002766 if (wret < 0)
2767 return wret;
2768 }
2769 l = path->nodes[0];
Chris Masonaa5d6be2007-02-28 16:35:06 -05002770
Chris Mason3685f792007-10-19 09:23:27 -04002771 /* did the pushes work? */
Yan Zheng87b29b22008-12-17 10:21:48 -05002772 if (btrfs_leaf_free_space(root, l) >= data_size)
Chris Mason3685f792007-10-19 09:23:27 -04002773 return 0;
Chris Mason3326d1b2007-10-15 16:18:25 -04002774 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002775
Chris Mason5c680ed2007-02-22 11:39:13 -05002776 if (!path->nodes[1]) {
Chris Masone089f052007-03-16 16:20:31 -04002777 ret = insert_new_root(trans, root, path, 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002778 if (ret)
2779 return ret;
2780 }
Chris Masoncc0c5532007-10-25 15:42:57 -04002781again:
2782 double_split = 0;
2783 l = path->nodes[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05002784 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04002785 nritems = btrfs_header_nritems(l);
Chris Masond3977122009-01-05 21:25:51 -05002786 mid = (nritems + 1) / 2;
Chris Mason54aa1f42007-06-22 14:16:25 -04002787
Chris Mason925baed2008-06-25 16:01:30 -04002788 right = btrfs_alloc_free_block(trans, root, root->leafsize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002789 path->nodes[1]->start,
2790 root->root_key.objectid,
2791 trans->transid, 0, l->start, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04002792 if (IS_ERR(right)) {
2793 BUG_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002794 return PTR_ERR(right);
Chris Masoncea9e442008-04-09 16:28:12 -04002795 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002796
Chris Mason5f39d392007-10-15 16:14:19 -04002797 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
Chris Masondb945352007-10-15 16:15:53 -04002798 btrfs_set_header_bytenr(right, right->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002799 btrfs_set_header_generation(right, trans->transid);
2800 btrfs_set_header_owner(right, root->root_key.objectid);
2801 btrfs_set_header_level(right, 0);
2802 write_extent_buffer(right, root->fs_info->fsid,
2803 (unsigned long)btrfs_header_fsid(right),
2804 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002805
2806 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2807 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2808 BTRFS_UUID_SIZE);
Chris Masond4dbff92007-04-04 14:08:15 -04002809 if (mid <= slot) {
2810 if (nritems == 1 ||
Yan Zheng87b29b22008-12-17 10:21:48 -05002811 leaf_space_used(l, mid, nritems - mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002812 BTRFS_LEAF_DATA_SIZE(root)) {
2813 if (slot >= nritems) {
2814 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002815 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002816 wret = insert_ptr(trans, root, path,
Chris Masondb945352007-10-15 16:15:53 -04002817 &disk_key, right->start,
Chris Masond4dbff92007-04-04 14:08:15 -04002818 path->slots[1] + 1, 1);
2819 if (wret)
2820 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002821
2822 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002823 free_extent_buffer(path->nodes[0]);
2824 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002825 path->slots[0] = 0;
2826 path->slots[1] += 1;
Chris Mason0ef8b242008-04-03 16:29:02 -04002827 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002828 return ret;
2829 }
2830 mid = slot;
Chris Mason3326d1b2007-10-15 16:18:25 -04002831 if (mid != nritems &&
2832 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002833 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason3326d1b2007-10-15 16:18:25 -04002834 double_split = 1;
2835 }
Chris Masond4dbff92007-04-04 14:08:15 -04002836 }
2837 } else {
Yan Zheng87b29b22008-12-17 10:21:48 -05002838 if (leaf_space_used(l, 0, mid) + data_size >
Chris Masond4dbff92007-04-04 14:08:15 -04002839 BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason459931e2008-12-10 09:10:46 -05002840 if (!extend && data_size && slot == 0) {
Chris Masond4dbff92007-04-04 14:08:15 -04002841 btrfs_cpu_key_to_disk(&disk_key, ins_key);
Chris Mason5f39d392007-10-15 16:14:19 -04002842 btrfs_set_header_nritems(right, 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002843 wret = insert_ptr(trans, root, path,
2844 &disk_key,
Chris Masondb945352007-10-15 16:15:53 -04002845 right->start,
Chris Mason098f59c2007-05-11 11:33:21 -04002846 path->slots[1], 1);
Chris Masond4dbff92007-04-04 14:08:15 -04002847 if (wret)
2848 ret = wret;
Chris Mason925baed2008-06-25 16:01:30 -04002849 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002850 free_extent_buffer(path->nodes[0]);
2851 path->nodes[0] = right;
Chris Masond4dbff92007-04-04 14:08:15 -04002852 path->slots[0] = 0;
Chris Masona429e512007-04-18 16:15:28 -04002853 if (path->slots[1] == 0) {
2854 wret = fixup_low_keys(trans, root,
Chris Masond3977122009-01-05 21:25:51 -05002855 path, &disk_key, 1);
Chris Masona429e512007-04-18 16:15:28 -04002856 if (wret)
2857 ret = wret;
2858 }
Chris Mason0ef8b242008-04-03 16:29:02 -04002859 btrfs_mark_buffer_dirty(right);
Chris Masond4dbff92007-04-04 14:08:15 -04002860 return ret;
Chris Mason459931e2008-12-10 09:10:46 -05002861 } else if ((extend || !data_size) && slot == 0) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002862 mid = 1;
2863 } else {
2864 mid = slot;
2865 if (mid != nritems &&
2866 leaf_space_used(l, mid, nritems - mid) +
Yan Zheng87b29b22008-12-17 10:21:48 -05002867 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Masoncc0c5532007-10-25 15:42:57 -04002868 double_split = 1;
2869 }
Chris Mason5ee78ac2007-10-19 14:01:21 -04002870 }
Chris Masond4dbff92007-04-04 14:08:15 -04002871 }
2872 }
Chris Mason5f39d392007-10-15 16:14:19 -04002873 nritems = nritems - mid;
2874 btrfs_set_header_nritems(right, nritems);
2875 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2876
2877 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2878 btrfs_item_nr_offset(mid),
2879 nritems * sizeof(struct btrfs_item));
2880
2881 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04002882 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2883 data_copy_size, btrfs_leaf_data(l) +
2884 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05002885
Chris Mason5f39d392007-10-15 16:14:19 -04002886 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2887 btrfs_item_end_nr(l, mid);
2888
2889 for (i = 0; i < nritems; i++) {
2890 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002891 u32 ioff;
2892
2893 if (!right->map_token) {
2894 map_extent_buffer(right, (unsigned long)item,
2895 sizeof(struct btrfs_item),
2896 &right->map_token, &right->kaddr,
2897 &right->map_start, &right->map_len,
2898 KM_USER1);
2899 }
2900
2901 ioff = btrfs_item_offset(right, item);
Chris Mason5f39d392007-10-15 16:14:19 -04002902 btrfs_set_item_offset(right, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04002903 }
Chris Mason74123bd2007-02-02 11:05:29 -05002904
Chris Masondb945352007-10-15 16:15:53 -04002905 if (right->map_token) {
2906 unmap_extent_buffer(right, right->map_token, KM_USER1);
2907 right->map_token = NULL;
2908 }
2909
Chris Mason5f39d392007-10-15 16:14:19 -04002910 btrfs_set_header_nritems(l, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002911 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002912 btrfs_item_key(right, &disk_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002913 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2914 path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002915 if (wret)
2916 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04002917
2918 btrfs_mark_buffer_dirty(right);
2919 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05002920 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002921
Zheng Yan31840ae2008-09-23 13:14:14 -04002922 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2923 BUG_ON(ret);
2924
Chris Masonbe0e5c02007-01-26 15:51:26 -05002925 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04002926 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002927 free_extent_buffer(path->nodes[0]);
2928 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002929 path->slots[0] -= mid;
2930 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04002931 } else {
2932 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002933 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002934 }
Chris Mason5f39d392007-10-15 16:14:19 -04002935
Chris Masoneb60cea2007-02-02 09:18:22 -05002936 BUG_ON(path->slots[0] < 0);
Chris Masond4dbff92007-04-04 14:08:15 -04002937
Chris Masoncc0c5532007-10-25 15:42:57 -04002938 if (double_split) {
2939 BUG_ON(num_doubles != 0);
2940 num_doubles++;
2941 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04002942 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002943 return ret;
2944}
2945
Chris Masond352ac62008-09-29 15:18:18 -04002946/*
Chris Mason459931e2008-12-10 09:10:46 -05002947 * This function splits a single item into two items,
2948 * giving 'new_key' to the new item and splitting the
2949 * old one at split_offset (from the start of the item).
2950 *
2951 * The path may be released by this operation. After
2952 * the split, the path is pointing to the old item. The
2953 * new item is going to be in the same node as the old one.
2954 *
2955 * Note, the item being split must be smaller enough to live alone on
2956 * a tree block with room for one extra struct btrfs_item
2957 *
2958 * This allows us to split the item in place, keeping a lock on the
2959 * leaf the entire time.
2960 */
2961int btrfs_split_item(struct btrfs_trans_handle *trans,
2962 struct btrfs_root *root,
2963 struct btrfs_path *path,
2964 struct btrfs_key *new_key,
2965 unsigned long split_offset)
2966{
2967 u32 item_size;
2968 struct extent_buffer *leaf;
2969 struct btrfs_key orig_key;
2970 struct btrfs_item *item;
2971 struct btrfs_item *new_item;
2972 int ret = 0;
2973 int slot;
2974 u32 nritems;
2975 u32 orig_offset;
2976 struct btrfs_disk_key disk_key;
2977 char *buf;
2978
2979 leaf = path->nodes[0];
2980 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2981 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2982 goto split;
2983
2984 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2985 btrfs_release_path(root, path);
2986
2987 path->search_for_split = 1;
2988 path->keep_locks = 1;
2989
2990 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2991 path->search_for_split = 0;
2992
2993 /* if our item isn't there or got smaller, return now */
2994 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2995 path->slots[0])) {
2996 path->keep_locks = 0;
2997 return -EAGAIN;
2998 }
2999
Yan Zheng87b29b22008-12-17 10:21:48 -05003000 ret = split_leaf(trans, root, &orig_key, path,
3001 sizeof(struct btrfs_item), 1);
Chris Mason459931e2008-12-10 09:10:46 -05003002 path->keep_locks = 0;
3003 BUG_ON(ret);
3004
Chris Masonb4ce94d2009-02-04 09:25:08 -05003005 /*
3006 * make sure any changes to the path from split_leaf leave it
3007 * in a blocking state
3008 */
3009 btrfs_set_path_blocking(path);
3010
Chris Mason459931e2008-12-10 09:10:46 -05003011 leaf = path->nodes[0];
Chris Mason42dc7ba2008-12-15 11:44:56 -05003012 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003013
3014split:
3015 item = btrfs_item_nr(leaf, path->slots[0]);
3016 orig_offset = btrfs_item_offset(leaf, item);
3017 item_size = btrfs_item_size(leaf, item);
3018
3019
3020 buf = kmalloc(item_size, GFP_NOFS);
3021 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3022 path->slots[0]), item_size);
3023 slot = path->slots[0] + 1;
3024 leaf = path->nodes[0];
3025
3026 nritems = btrfs_header_nritems(leaf);
3027
3028 if (slot != nritems) {
3029 /* shift the items */
3030 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3031 btrfs_item_nr_offset(slot),
3032 (nritems - slot) * sizeof(struct btrfs_item));
3033
3034 }
3035
3036 btrfs_cpu_key_to_disk(&disk_key, new_key);
3037 btrfs_set_item_key(leaf, &disk_key, slot);
3038
3039 new_item = btrfs_item_nr(leaf, slot);
3040
3041 btrfs_set_item_offset(leaf, new_item, orig_offset);
3042 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3043
3044 btrfs_set_item_offset(leaf, item,
3045 orig_offset + item_size - split_offset);
3046 btrfs_set_item_size(leaf, item, split_offset);
3047
3048 btrfs_set_header_nritems(leaf, nritems + 1);
3049
3050 /* write the data for the start of the original item */
3051 write_extent_buffer(leaf, buf,
3052 btrfs_item_ptr_offset(leaf, path->slots[0]),
3053 split_offset);
3054
3055 /* write the data for the new item */
3056 write_extent_buffer(leaf, buf + split_offset,
3057 btrfs_item_ptr_offset(leaf, slot),
3058 item_size - split_offset);
3059 btrfs_mark_buffer_dirty(leaf);
3060
3061 ret = 0;
3062 if (btrfs_leaf_free_space(root, leaf) < 0) {
3063 btrfs_print_leaf(root, leaf);
3064 BUG();
3065 }
3066 kfree(buf);
3067 return ret;
3068}
3069
3070/*
Chris Masond352ac62008-09-29 15:18:18 -04003071 * make the item pointed to by the path smaller. new_size indicates
3072 * how small to make it, and from_end tells us if we just chop bytes
3073 * off the end of the item or if we shift the item to chop bytes off
3074 * the front.
3075 */
Chris Masonb18c6682007-04-17 13:26:50 -04003076int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3077 struct btrfs_root *root,
3078 struct btrfs_path *path,
Chris Mason179e29e2007-11-01 11:28:41 -04003079 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003080{
3081 int ret = 0;
3082 int slot;
3083 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003084 struct extent_buffer *leaf;
3085 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003086 u32 nritems;
3087 unsigned int data_end;
3088 unsigned int old_data_start;
3089 unsigned int old_size;
3090 unsigned int size_diff;
3091 int i;
3092
3093 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003094 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003095 slot = path->slots[0];
3096
3097 old_size = btrfs_item_size_nr(leaf, slot);
3098 if (old_size == new_size)
3099 return 0;
Chris Masonb18c6682007-04-17 13:26:50 -04003100
Chris Mason5f39d392007-10-15 16:14:19 -04003101 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003102 data_end = leaf_data_end(root, leaf);
3103
Chris Mason5f39d392007-10-15 16:14:19 -04003104 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003105
Chris Masonb18c6682007-04-17 13:26:50 -04003106 size_diff = old_size - new_size;
3107
3108 BUG_ON(slot < 0);
3109 BUG_ON(slot >= nritems);
3110
3111 /*
3112 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3113 */
3114 /* first correct the data pointers */
3115 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003116 u32 ioff;
3117 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003118
3119 if (!leaf->map_token) {
3120 map_extent_buffer(leaf, (unsigned long)item,
3121 sizeof(struct btrfs_item),
3122 &leaf->map_token, &leaf->kaddr,
3123 &leaf->map_start, &leaf->map_len,
3124 KM_USER1);
3125 }
3126
Chris Mason5f39d392007-10-15 16:14:19 -04003127 ioff = btrfs_item_offset(leaf, item);
3128 btrfs_set_item_offset(leaf, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003129 }
Chris Masondb945352007-10-15 16:15:53 -04003130
3131 if (leaf->map_token) {
3132 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3133 leaf->map_token = NULL;
3134 }
3135
Chris Masonb18c6682007-04-17 13:26:50 -04003136 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003137 if (from_end) {
3138 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3139 data_end + size_diff, btrfs_leaf_data(leaf) +
3140 data_end, old_data_start + new_size - data_end);
3141 } else {
3142 struct btrfs_disk_key disk_key;
3143 u64 offset;
3144
3145 btrfs_item_key(leaf, &disk_key, slot);
3146
3147 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3148 unsigned long ptr;
3149 struct btrfs_file_extent_item *fi;
3150
3151 fi = btrfs_item_ptr(leaf, slot,
3152 struct btrfs_file_extent_item);
3153 fi = (struct btrfs_file_extent_item *)(
3154 (unsigned long)fi - size_diff);
3155
3156 if (btrfs_file_extent_type(leaf, fi) ==
3157 BTRFS_FILE_EXTENT_INLINE) {
3158 ptr = btrfs_item_ptr_offset(leaf, slot);
3159 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003160 (unsigned long)fi,
3161 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003162 disk_bytenr));
3163 }
3164 }
3165
3166 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3167 data_end + size_diff, btrfs_leaf_data(leaf) +
3168 data_end, old_data_start - data_end);
3169
3170 offset = btrfs_disk_key_offset(&disk_key);
3171 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3172 btrfs_set_item_key(leaf, &disk_key, slot);
3173 if (slot == 0)
3174 fixup_low_keys(trans, root, path, &disk_key, 1);
3175 }
Chris Mason5f39d392007-10-15 16:14:19 -04003176
3177 item = btrfs_item_nr(leaf, slot);
3178 btrfs_set_item_size(leaf, item, new_size);
3179 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003180
3181 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003182 if (btrfs_leaf_free_space(root, leaf) < 0) {
3183 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003184 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003185 }
Chris Masonb18c6682007-04-17 13:26:50 -04003186 return ret;
3187}
3188
Chris Masond352ac62008-09-29 15:18:18 -04003189/*
3190 * make the item pointed to by the path bigger, data_size is the new size.
3191 */
Chris Mason5f39d392007-10-15 16:14:19 -04003192int btrfs_extend_item(struct btrfs_trans_handle *trans,
3193 struct btrfs_root *root, struct btrfs_path *path,
3194 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003195{
3196 int ret = 0;
3197 int slot;
3198 int slot_orig;
Chris Mason5f39d392007-10-15 16:14:19 -04003199 struct extent_buffer *leaf;
3200 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003201 u32 nritems;
3202 unsigned int data_end;
3203 unsigned int old_data;
3204 unsigned int old_size;
3205 int i;
3206
3207 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003208 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003209
Chris Mason5f39d392007-10-15 16:14:19 -04003210 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003211 data_end = leaf_data_end(root, leaf);
3212
Chris Mason5f39d392007-10-15 16:14:19 -04003213 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3214 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003215 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003216 }
Chris Mason6567e832007-04-16 09:22:45 -04003217 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003218 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003219
3220 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003221 if (slot >= nritems) {
3222 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003223 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3224 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003225 BUG_ON(1);
3226 }
Chris Mason6567e832007-04-16 09:22:45 -04003227
3228 /*
3229 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3230 */
3231 /* first correct the data pointers */
3232 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003233 u32 ioff;
3234 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003235
3236 if (!leaf->map_token) {
3237 map_extent_buffer(leaf, (unsigned long)item,
3238 sizeof(struct btrfs_item),
3239 &leaf->map_token, &leaf->kaddr,
3240 &leaf->map_start, &leaf->map_len,
3241 KM_USER1);
3242 }
Chris Mason5f39d392007-10-15 16:14:19 -04003243 ioff = btrfs_item_offset(leaf, item);
3244 btrfs_set_item_offset(leaf, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003245 }
Chris Mason5f39d392007-10-15 16:14:19 -04003246
Chris Masondb945352007-10-15 16:15:53 -04003247 if (leaf->map_token) {
3248 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3249 leaf->map_token = NULL;
3250 }
3251
Chris Mason6567e832007-04-16 09:22:45 -04003252 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003253 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003254 data_end - data_size, btrfs_leaf_data(leaf) +
3255 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003256
Chris Mason6567e832007-04-16 09:22:45 -04003257 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003258 old_size = btrfs_item_size_nr(leaf, slot);
3259 item = btrfs_item_nr(leaf, slot);
3260 btrfs_set_item_size(leaf, item, old_size + data_size);
3261 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003262
3263 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003264 if (btrfs_leaf_free_space(root, leaf) < 0) {
3265 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003266 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003267 }
Chris Mason6567e832007-04-16 09:22:45 -04003268 return ret;
3269}
3270
Chris Mason74123bd2007-02-02 11:05:29 -05003271/*
Chris Masond352ac62008-09-29 15:18:18 -04003272 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003273 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003274 * Returns the number of keys that were inserted.
3275 */
3276int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3277 struct btrfs_root *root,
3278 struct btrfs_path *path,
3279 struct btrfs_key *cpu_key, u32 *data_size,
3280 int nr)
3281{
3282 struct extent_buffer *leaf;
3283 struct btrfs_item *item;
3284 int ret = 0;
3285 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003286 int i;
3287 u32 nritems;
3288 u32 total_data = 0;
3289 u32 total_size = 0;
3290 unsigned int data_end;
3291 struct btrfs_disk_key disk_key;
3292 struct btrfs_key found_key;
3293
Yan Zheng87b29b22008-12-17 10:21:48 -05003294 for (i = 0; i < nr; i++) {
3295 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3296 BTRFS_LEAF_DATA_SIZE(root)) {
3297 break;
3298 nr = i;
3299 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003300 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003301 total_size += data_size[i] + sizeof(struct btrfs_item);
3302 }
3303 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003304
Josef Bacikf3465ca2008-11-12 14:19:50 -05003305 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3306 if (ret == 0)
3307 return -EEXIST;
3308 if (ret < 0)
3309 goto out;
3310
Josef Bacikf3465ca2008-11-12 14:19:50 -05003311 leaf = path->nodes[0];
3312
3313 nritems = btrfs_header_nritems(leaf);
3314 data_end = leaf_data_end(root, leaf);
3315
3316 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3317 for (i = nr; i >= 0; i--) {
3318 total_data -= data_size[i];
3319 total_size -= data_size[i] + sizeof(struct btrfs_item);
3320 if (total_size < btrfs_leaf_free_space(root, leaf))
3321 break;
3322 }
3323 nr = i;
3324 }
3325
3326 slot = path->slots[0];
3327 BUG_ON(slot < 0);
3328
3329 if (slot != nritems) {
3330 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3331
3332 item = btrfs_item_nr(leaf, slot);
3333 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3334
3335 /* figure out how many keys we can insert in here */
3336 total_data = data_size[0];
3337 for (i = 1; i < nr; i++) {
3338 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3339 break;
3340 total_data += data_size[i];
3341 }
3342 nr = i;
3343
3344 if (old_data < data_end) {
3345 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003346 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003347 slot, old_data, data_end);
3348 BUG_ON(1);
3349 }
3350 /*
3351 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3352 */
3353 /* first correct the data pointers */
3354 WARN_ON(leaf->map_token);
3355 for (i = slot; i < nritems; i++) {
3356 u32 ioff;
3357
3358 item = btrfs_item_nr(leaf, i);
3359 if (!leaf->map_token) {
3360 map_extent_buffer(leaf, (unsigned long)item,
3361 sizeof(struct btrfs_item),
3362 &leaf->map_token, &leaf->kaddr,
3363 &leaf->map_start, &leaf->map_len,
3364 KM_USER1);
3365 }
3366
3367 ioff = btrfs_item_offset(leaf, item);
3368 btrfs_set_item_offset(leaf, item, ioff - total_data);
3369 }
3370 if (leaf->map_token) {
3371 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3372 leaf->map_token = NULL;
3373 }
3374
3375 /* shift the items */
3376 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3377 btrfs_item_nr_offset(slot),
3378 (nritems - slot) * sizeof(struct btrfs_item));
3379
3380 /* shift the data */
3381 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3382 data_end - total_data, btrfs_leaf_data(leaf) +
3383 data_end, old_data - data_end);
3384 data_end = old_data;
3385 } else {
3386 /*
3387 * this sucks but it has to be done, if we are inserting at
3388 * the end of the leaf only insert 1 of the items, since we
3389 * have no way of knowing whats on the next leaf and we'd have
3390 * to drop our current locks to figure it out
3391 */
3392 nr = 1;
3393 }
3394
3395 /* setup the item for the new data */
3396 for (i = 0; i < nr; i++) {
3397 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3398 btrfs_set_item_key(leaf, &disk_key, slot + i);
3399 item = btrfs_item_nr(leaf, slot + i);
3400 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3401 data_end -= data_size[i];
3402 btrfs_set_item_size(leaf, item, data_size[i]);
3403 }
3404 btrfs_set_header_nritems(leaf, nritems + nr);
3405 btrfs_mark_buffer_dirty(leaf);
3406
3407 ret = 0;
3408 if (slot == 0) {
3409 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3410 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3411 }
3412
3413 if (btrfs_leaf_free_space(root, leaf) < 0) {
3414 btrfs_print_leaf(root, leaf);
3415 BUG();
3416 }
3417out:
3418 if (!ret)
3419 ret = nr;
3420 return ret;
3421}
3422
3423/*
3424 * Given a key and some data, insert items into the tree.
3425 * This does all the path init required, making room in the tree if needed.
Chris Mason74123bd2007-02-02 11:05:29 -05003426 */
Chris Mason9c583092008-01-29 15:15:18 -05003427int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003428 struct btrfs_root *root,
3429 struct btrfs_path *path,
Chris Mason9c583092008-01-29 15:15:18 -05003430 struct btrfs_key *cpu_key, u32 *data_size,
3431 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003432{
Chris Mason5f39d392007-10-15 16:14:19 -04003433 struct extent_buffer *leaf;
3434 struct btrfs_item *item;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003435 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003436 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05003437 int slot_orig;
Chris Mason9c583092008-01-29 15:15:18 -05003438 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003439 u32 nritems;
Chris Mason9c583092008-01-29 15:15:18 -05003440 u32 total_size = 0;
3441 u32 total_data = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003442 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003443 struct btrfs_disk_key disk_key;
3444
Chris Masond3977122009-01-05 21:25:51 -05003445 for (i = 0; i < nr; i++)
Chris Mason9c583092008-01-29 15:15:18 -05003446 total_data += data_size[i];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003447
Josef Bacik7b128762008-07-24 12:17:14 -04003448 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Mason9c583092008-01-29 15:15:18 -05003449 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003450 if (ret == 0)
Chris Masonf0930a32007-03-02 09:47:58 -05003451 return -EEXIST;
Chris Masoned2ff2c2007-03-01 18:59:40 -05003452 if (ret < 0)
3453 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003454
Chris Mason62e27492007-03-15 12:56:47 -04003455 slot_orig = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003456 leaf = path->nodes[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003457
Chris Mason5f39d392007-10-15 16:14:19 -04003458 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04003459 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003460
Chris Masonf25956c2008-09-12 15:32:53 -04003461 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04003462 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003463 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05003464 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003465 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003466 }
Chris Mason5f39d392007-10-15 16:14:19 -04003467
Chris Mason62e27492007-03-15 12:56:47 -04003468 slot = path->slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05003469 BUG_ON(slot < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003470
Chris Masonbe0e5c02007-01-26 15:51:26 -05003471 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003472 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003473
Chris Mason5f39d392007-10-15 16:14:19 -04003474 if (old_data < data_end) {
3475 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003476 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04003477 slot, old_data, data_end);
3478 BUG_ON(1);
3479 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003480 /*
3481 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3482 */
3483 /* first correct the data pointers */
Chris Masondb945352007-10-15 16:15:53 -04003484 WARN_ON(leaf->map_token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003485 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003486 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003487
Chris Mason5f39d392007-10-15 16:14:19 -04003488 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003489 if (!leaf->map_token) {
3490 map_extent_buffer(leaf, (unsigned long)item,
3491 sizeof(struct btrfs_item),
3492 &leaf->map_token, &leaf->kaddr,
3493 &leaf->map_start, &leaf->map_len,
3494 KM_USER1);
3495 }
3496
Chris Mason5f39d392007-10-15 16:14:19 -04003497 ioff = btrfs_item_offset(leaf, item);
Chris Mason9c583092008-01-29 15:15:18 -05003498 btrfs_set_item_offset(leaf, item, ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003499 }
Chris Masondb945352007-10-15 16:15:53 -04003500 if (leaf->map_token) {
3501 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3502 leaf->map_token = NULL;
3503 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003504
3505 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003506 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003507 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003508 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003509
3510 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003511 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05003512 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003513 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003514 data_end = old_data;
3515 }
Chris Mason5f39d392007-10-15 16:14:19 -04003516
Chris Mason62e27492007-03-15 12:56:47 -04003517 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003518 for (i = 0; i < nr; i++) {
3519 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3520 btrfs_set_item_key(leaf, &disk_key, slot + i);
3521 item = btrfs_item_nr(leaf, slot + i);
3522 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3523 data_end -= data_size[i];
3524 btrfs_set_item_size(leaf, item, data_size[i]);
3525 }
3526 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Mason5f39d392007-10-15 16:14:19 -04003527 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003528
3529 ret = 0;
Chris Mason5a01a2e2008-01-30 11:43:54 -05003530 if (slot == 0) {
3531 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Chris Masone089f052007-03-16 16:20:31 -04003532 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05003533 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003534
Chris Mason5f39d392007-10-15 16:14:19 -04003535 if (btrfs_leaf_free_space(root, leaf) < 0) {
3536 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003537 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003538 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05003539out:
Chris Masonb4ce94d2009-02-04 09:25:08 -05003540 btrfs_unlock_up_safe(path, 1);
Chris Mason62e27492007-03-15 12:56:47 -04003541 return ret;
3542}
3543
3544/*
3545 * Given a key and some data, insert an item into the tree.
3546 * This does all the path init required, making room in the tree if needed.
3547 */
Chris Masone089f052007-03-16 16:20:31 -04003548int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3549 *root, struct btrfs_key *cpu_key, void *data, u32
3550 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003551{
3552 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003553 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003554 struct extent_buffer *leaf;
3555 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003556
Chris Mason2c90e5d2007-04-02 10:50:19 -04003557 path = btrfs_alloc_path();
3558 BUG_ON(!path);
Chris Mason2c90e5d2007-04-02 10:50:19 -04003559 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003560 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003561 leaf = path->nodes[0];
3562 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3563 write_extent_buffer(leaf, data, ptr, data_size);
3564 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003565 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003566 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003567 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003568}
3569
Chris Mason74123bd2007-02-02 11:05:29 -05003570/*
Chris Mason5de08d72007-02-24 06:24:44 -05003571 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003572 *
Chris Masond352ac62008-09-29 15:18:18 -04003573 * the tree should have been previously balanced so the deletion does not
3574 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003575 */
Chris Masone089f052007-03-16 16:20:31 -04003576static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3577 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003578{
Chris Mason5f39d392007-10-15 16:14:19 -04003579 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003580 u32 nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003581 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003582 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003583
Chris Mason5f39d392007-10-15 16:14:19 -04003584 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003585 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003586 memmove_extent_buffer(parent,
3587 btrfs_node_key_ptr_offset(slot),
3588 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003589 sizeof(struct btrfs_key_ptr) *
3590 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05003591 }
Chris Mason7518a232007-03-12 12:01:18 -04003592 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04003593 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04003594 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04003595 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05003596 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04003597 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05003598 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003599 struct btrfs_disk_key disk_key;
3600
3601 btrfs_node_key(parent, &disk_key, 0);
3602 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05003603 if (wret)
3604 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003605 }
Chris Masond6025572007-03-30 14:27:56 -04003606 btrfs_mark_buffer_dirty(parent);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003607 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003608}
3609
Chris Mason74123bd2007-02-02 11:05:29 -05003610/*
Chris Mason323ac952008-10-01 19:05:46 -04003611 * a helper function to delete the leaf pointed to by path->slots[1] and
3612 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3613 * already know it, it is faster to have them pass it down than to
3614 * read it out of the node again.
3615 *
3616 * This deletes the pointer in path->nodes[1] and frees the leaf
3617 * block extent. zero is returned if it all worked out, < 0 otherwise.
3618 *
3619 * The path must have already been setup for deleting the leaf, including
3620 * all the proper balancing. path->nodes[1] must be locked.
3621 */
3622noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3623 struct btrfs_root *root,
3624 struct btrfs_path *path, u64 bytenr)
3625{
3626 int ret;
3627 u64 root_gen = btrfs_header_generation(path->nodes[1]);
Chris Mason4d081c42009-02-04 09:31:28 -05003628 u64 parent_start = path->nodes[1]->start;
3629 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003630
3631 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3632 if (ret)
3633 return ret;
3634
Chris Mason4d081c42009-02-04 09:31:28 -05003635 /*
3636 * btrfs_free_extent is expensive, we want to make sure we
3637 * aren't holding any locks when we call it
3638 */
3639 btrfs_unlock_up_safe(path, 0);
3640
Chris Mason323ac952008-10-01 19:05:46 -04003641 ret = btrfs_free_extent(trans, root, bytenr,
3642 btrfs_level_size(root, 0),
Chris Mason4d081c42009-02-04 09:31:28 -05003643 parent_start, parent_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003644 root_gen, 0, 1);
Chris Mason323ac952008-10-01 19:05:46 -04003645 return ret;
3646}
3647/*
Chris Mason74123bd2007-02-02 11:05:29 -05003648 * delete the item at the leaf level in path. If that empties
3649 * the leaf, remove it from the tree
3650 */
Chris Mason85e21ba2008-01-29 15:11:36 -05003651int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3652 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003653{
Chris Mason5f39d392007-10-15 16:14:19 -04003654 struct extent_buffer *leaf;
3655 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003656 int last_off;
3657 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003658 int ret = 0;
3659 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05003660 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003661 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003662
Chris Mason5f39d392007-10-15 16:14:19 -04003663 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05003664 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3665
3666 for (i = 0; i < nr; i++)
3667 dsize += btrfs_item_size_nr(leaf, slot + i);
3668
Chris Mason5f39d392007-10-15 16:14:19 -04003669 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003670
Chris Mason85e21ba2008-01-29 15:11:36 -05003671 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04003672 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003673
3674 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04003675 data_end + dsize,
3676 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05003677 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003678
Chris Mason85e21ba2008-01-29 15:11:36 -05003679 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003680 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003681
Chris Mason5f39d392007-10-15 16:14:19 -04003682 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003683 if (!leaf->map_token) {
3684 map_extent_buffer(leaf, (unsigned long)item,
3685 sizeof(struct btrfs_item),
3686 &leaf->map_token, &leaf->kaddr,
3687 &leaf->map_start, &leaf->map_len,
3688 KM_USER1);
3689 }
Chris Mason5f39d392007-10-15 16:14:19 -04003690 ioff = btrfs_item_offset(leaf, item);
3691 btrfs_set_item_offset(leaf, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04003692 }
Chris Masondb945352007-10-15 16:15:53 -04003693
3694 if (leaf->map_token) {
3695 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3696 leaf->map_token = NULL;
3697 }
3698
Chris Mason5f39d392007-10-15 16:14:19 -04003699 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05003700 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04003701 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05003702 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003703 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003704 btrfs_set_header_nritems(leaf, nritems - nr);
3705 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04003706
Chris Mason74123bd2007-02-02 11:05:29 -05003707 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04003708 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003709 if (leaf == root->node) {
3710 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05003711 } else {
Chris Mason323ac952008-10-01 19:05:46 -04003712 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3713 BUG_ON(ret);
Chris Mason9a8dd152007-02-23 08:38:36 -05003714 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003715 } else {
Chris Mason7518a232007-03-12 12:01:18 -04003716 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003717 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003718 struct btrfs_disk_key disk_key;
3719
3720 btrfs_item_key(leaf, &disk_key, 0);
Chris Masone089f052007-03-16 16:20:31 -04003721 wret = fixup_low_keys(trans, root, path,
Chris Mason5f39d392007-10-15 16:14:19 -04003722 &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003723 if (wret)
3724 ret = wret;
3725 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003726
Chris Mason74123bd2007-02-02 11:05:29 -05003727 /* delete the leaf if it is mostly empty */
Chris Mason85e21ba2008-01-29 15:11:36 -05003728 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003729 /* push_leaf_left fixes the path.
3730 * make sure the path still points to our leaf
3731 * for possible call to del_ptr below
3732 */
Chris Mason4920c9a2007-01-26 16:38:42 -05003733 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04003734 extent_buffer_get(leaf);
3735
Chris Mason85e21ba2008-01-29 15:11:36 -05003736 wret = push_leaf_left(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003737 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003738 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04003739
3740 if (path->nodes[0] == leaf &&
3741 btrfs_header_nritems(leaf)) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003742 wret = push_leaf_right(trans, root, path, 1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003743 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05003744 ret = wret;
3745 }
Chris Mason5f39d392007-10-15 16:14:19 -04003746
3747 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04003748 path->slots[1] = slot;
Chris Masond3977122009-01-05 21:25:51 -05003749 ret = btrfs_del_leaf(trans, root, path,
3750 leaf->start);
Chris Mason323ac952008-10-01 19:05:46 -04003751 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003752 free_extent_buffer(leaf);
Chris Mason5de08d72007-02-24 06:24:44 -05003753 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003754 /* if we're still in the path, make sure
3755 * we're dirty. Otherwise, one of the
3756 * push_leaf functions must have already
3757 * dirtied this buffer
3758 */
3759 if (path->nodes[0] == leaf)
3760 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003761 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003762 }
Chris Masond5719762007-03-23 10:01:08 -04003763 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04003764 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003765 }
3766 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003767 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003768}
3769
Chris Mason97571fd2007-02-24 13:39:08 -05003770/*
Chris Mason925baed2008-06-25 16:01:30 -04003771 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05003772 * returns 0 if it found something or 1 if there are no lesser leaves.
3773 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04003774 *
3775 * This may release the path, and so you may lose any locks held at the
3776 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05003777 */
3778int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3779{
Chris Mason925baed2008-06-25 16:01:30 -04003780 struct btrfs_key key;
3781 struct btrfs_disk_key found_key;
3782 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05003783
Chris Mason925baed2008-06-25 16:01:30 -04003784 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05003785
Chris Mason925baed2008-06-25 16:01:30 -04003786 if (key.offset > 0)
3787 key.offset--;
3788 else if (key.type > 0)
3789 key.type--;
3790 else if (key.objectid > 0)
3791 key.objectid--;
3792 else
3793 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003794
Chris Mason925baed2008-06-25 16:01:30 -04003795 btrfs_release_path(root, path);
3796 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3797 if (ret < 0)
3798 return ret;
3799 btrfs_item_key(path->nodes[0], &found_key, 0);
3800 ret = comp_keys(&found_key, &key);
3801 if (ret < 0)
3802 return 0;
3803 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05003804}
3805
Chris Mason3f157a22008-06-25 16:01:31 -04003806/*
3807 * A helper function to walk down the tree starting at min_key, and looking
3808 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04003809 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04003810 *
3811 * This does not cow, but it does stuff the starting key it finds back
3812 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3813 * key and get a writable path.
3814 *
3815 * This does lock as it descends, and path->keep_locks should be set
3816 * to 1 by the caller.
3817 *
3818 * This honors path->lowest_level to prevent descent past a given level
3819 * of the tree.
3820 *
Chris Masond352ac62008-09-29 15:18:18 -04003821 * min_trans indicates the oldest transaction that you are interested
3822 * in walking through. Any nodes or leaves older than min_trans are
3823 * skipped over (without reading them).
3824 *
Chris Mason3f157a22008-06-25 16:01:31 -04003825 * returns zero if something useful was found, < 0 on error and 1 if there
3826 * was nothing in the tree that matched the search criteria.
3827 */
3828int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04003829 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04003830 struct btrfs_path *path, int cache_only,
3831 u64 min_trans)
3832{
3833 struct extent_buffer *cur;
3834 struct btrfs_key found_key;
3835 int slot;
Yan96524802008-07-24 12:19:49 -04003836 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04003837 u32 nritems;
3838 int level;
3839 int ret = 1;
3840
Chris Mason934d3752008-12-08 16:43:10 -05003841 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04003842again:
3843 cur = btrfs_lock_root_node(root);
3844 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04003845 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04003846 path->nodes[level] = cur;
3847 path->locks[level] = 1;
3848
3849 if (btrfs_header_generation(cur) < min_trans) {
3850 ret = 1;
3851 goto out;
3852 }
Chris Masond3977122009-01-05 21:25:51 -05003853 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04003854 nritems = btrfs_header_nritems(cur);
3855 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04003856 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003857
Chris Mason323ac952008-10-01 19:05:46 -04003858 /* at the lowest level, we're done, setup the path and exit */
3859 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04003860 if (slot >= nritems)
3861 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04003862 ret = 0;
3863 path->slots[level] = slot;
3864 btrfs_item_key_to_cpu(cur, &found_key, slot);
3865 goto out;
3866 }
Yan96524802008-07-24 12:19:49 -04003867 if (sret && slot > 0)
3868 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04003869 /*
3870 * check this node pointer against the cache_only and
3871 * min_trans parameters. If it isn't in cache or is too
3872 * old, skip to the next one.
3873 */
Chris Masond3977122009-01-05 21:25:51 -05003874 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04003875 u64 blockptr;
3876 u64 gen;
3877 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04003878 struct btrfs_disk_key disk_key;
3879
Chris Mason3f157a22008-06-25 16:01:31 -04003880 blockptr = btrfs_node_blockptr(cur, slot);
3881 gen = btrfs_node_ptr_generation(cur, slot);
3882 if (gen < min_trans) {
3883 slot++;
3884 continue;
3885 }
3886 if (!cache_only)
3887 break;
3888
Chris Masone02119d2008-09-05 16:13:11 -04003889 if (max_key) {
3890 btrfs_node_key(cur, &disk_key, slot);
3891 if (comp_keys(&disk_key, max_key) >= 0) {
3892 ret = 1;
3893 goto out;
3894 }
3895 }
3896
Chris Mason3f157a22008-06-25 16:01:31 -04003897 tmp = btrfs_find_tree_block(root, blockptr,
3898 btrfs_level_size(root, level - 1));
3899
3900 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3901 free_extent_buffer(tmp);
3902 break;
3903 }
3904 if (tmp)
3905 free_extent_buffer(tmp);
3906 slot++;
3907 }
Chris Masone02119d2008-09-05 16:13:11 -04003908find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04003909 /*
3910 * we didn't find a candidate key in this node, walk forward
3911 * and find another one
3912 */
3913 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04003914 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003915 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04003916 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04003917 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04003918 if (sret == 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04003919 btrfs_release_path(root, path);
3920 goto again;
3921 } else {
Chris Masonb4ce94d2009-02-04 09:25:08 -05003922 btrfs_clear_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003923 goto out;
3924 }
3925 }
3926 /* save our key for returning back */
3927 btrfs_node_key_to_cpu(cur, &found_key, slot);
3928 path->slots[level] = slot;
3929 if (level == path->lowest_level) {
3930 ret = 0;
3931 unlock_up(path, level, 1);
3932 goto out;
3933 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05003934 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003935 cur = read_node_slot(root, cur, slot);
3936
3937 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003938
Chris Mason3f157a22008-06-25 16:01:31 -04003939 path->locks[level - 1] = 1;
3940 path->nodes[level - 1] = cur;
3941 unlock_up(path, level, 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003942 btrfs_clear_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003943 }
3944out:
3945 if (ret == 0)
3946 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05003947 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04003948 return ret;
3949}
3950
3951/*
3952 * this is similar to btrfs_next_leaf, but does not try to preserve
3953 * and fixup the path. It looks for and returns the next key in the
3954 * tree based on the current path and the cache_only and min_trans
3955 * parameters.
3956 *
3957 * 0 is returned if another key is found, < 0 if there are any errors
3958 * and 1 is returned if there are no higher keys in the tree
3959 *
3960 * path->keep_locks should be set to 1 on the search made before
3961 * calling this function.
3962 */
Chris Masone7a84562008-06-25 16:01:31 -04003963int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04003964 struct btrfs_key *key, int lowest_level,
3965 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04003966{
3967 int level = lowest_level;
3968 int slot;
3969 struct extent_buffer *c;
3970
Chris Mason934d3752008-12-08 16:43:10 -05003971 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05003972 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04003973 if (!path->nodes[level])
3974 return 1;
3975
3976 slot = path->slots[level] + 1;
3977 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04003978next:
Chris Masone7a84562008-06-25 16:01:31 -04003979 if (slot >= btrfs_header_nritems(c)) {
3980 level++;
Chris Masond3977122009-01-05 21:25:51 -05003981 if (level == BTRFS_MAX_LEVEL)
Chris Masone7a84562008-06-25 16:01:31 -04003982 return 1;
Chris Masone7a84562008-06-25 16:01:31 -04003983 continue;
3984 }
3985 if (level == 0)
3986 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04003987 else {
3988 u64 blockptr = btrfs_node_blockptr(c, slot);
3989 u64 gen = btrfs_node_ptr_generation(c, slot);
3990
3991 if (cache_only) {
3992 struct extent_buffer *cur;
3993 cur = btrfs_find_tree_block(root, blockptr,
3994 btrfs_level_size(root, level - 1));
3995 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3996 slot++;
3997 if (cur)
3998 free_extent_buffer(cur);
3999 goto next;
4000 }
4001 free_extent_buffer(cur);
4002 }
4003 if (gen < min_trans) {
4004 slot++;
4005 goto next;
4006 }
Chris Masone7a84562008-06-25 16:01:31 -04004007 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004008 }
Chris Masone7a84562008-06-25 16:01:31 -04004009 return 0;
4010 }
4011 return 1;
4012}
4013
Chris Mason7bb86312007-12-11 09:25:06 -05004014/*
Chris Mason925baed2008-06-25 16:01:30 -04004015 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004016 * returns 0 if it found something or 1 if there are no greater leaves.
4017 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004018 */
Chris Mason234b63a2007-03-13 10:46:10 -04004019int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004020{
4021 int slot;
4022 int level = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004023 struct extent_buffer *c;
4024 struct extent_buffer *next = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004025 struct btrfs_key key;
4026 u32 nritems;
4027 int ret;
4028
4029 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004030 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004031 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004032
4033 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4034
Chris Mason925baed2008-06-25 16:01:30 -04004035 btrfs_release_path(root, path);
Chris Masona2135012008-06-25 16:01:30 -04004036 path->keep_locks = 1;
Chris Mason925baed2008-06-25 16:01:30 -04004037 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4038 path->keep_locks = 0;
4039
4040 if (ret < 0)
4041 return ret;
4042
Chris Masonb4ce94d2009-02-04 09:25:08 -05004043 btrfs_set_path_blocking(path);
Chris Masona2135012008-06-25 16:01:30 -04004044 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004045 /*
4046 * by releasing the path above we dropped all our locks. A balance
4047 * could have added more items next to the key that used to be
4048 * at the very end of the block. So, check again here and
4049 * advance the path if there are now more items available.
4050 */
Chris Masona2135012008-06-25 16:01:30 -04004051 if (nritems > 0 && path->slots[0] < nritems - 1) {
Chris Mason168fd7d2008-06-25 16:01:30 -04004052 path->slots[0]++;
Chris Mason925baed2008-06-25 16:01:30 -04004053 goto done;
4054 }
Chris Masond97e63b2007-02-20 16:40:44 -05004055
Chris Masond3977122009-01-05 21:25:51 -05004056 while (level < BTRFS_MAX_LEVEL) {
Chris Masond97e63b2007-02-20 16:40:44 -05004057 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05004058 return 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004059
Chris Masond97e63b2007-02-20 16:40:44 -05004060 slot = path->slots[level] + 1;
4061 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004062 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004063 level++;
Chris Masond3977122009-01-05 21:25:51 -05004064 if (level == BTRFS_MAX_LEVEL)
Chris Mason7bb86312007-12-11 09:25:06 -05004065 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004066 continue;
4067 }
Chris Mason5f39d392007-10-15 16:14:19 -04004068
Chris Mason925baed2008-06-25 16:01:30 -04004069 if (next) {
4070 btrfs_tree_unlock(next);
Chris Mason5f39d392007-10-15 16:14:19 -04004071 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004072 }
Chris Mason5f39d392007-10-15 16:14:19 -04004073
Chris Masonb4ce94d2009-02-04 09:25:08 -05004074 /* the path was set to blocking above */
Chris Mason0bd40a72008-07-17 12:54:43 -04004075 if (level == 1 && (path->locks[1] || path->skip_locking) &&
4076 path->reada)
Chris Mason01f46652007-12-21 16:24:26 -05004077 reada_for_search(root, path, level, slot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004078
Chris Masonca7a79a2008-05-12 12:59:19 -04004079 next = read_node_slot(root, c, slot);
Chris Mason5cd57b22008-06-25 16:01:30 -04004080 if (!path->skip_locking) {
4081 WARN_ON(!btrfs_tree_locked(c));
4082 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004083 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004084 }
Chris Masond97e63b2007-02-20 16:40:44 -05004085 break;
4086 }
4087 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004088 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004089 level--;
4090 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004091 if (path->locks[level])
4092 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04004093 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004094 path->nodes[level] = next;
4095 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004096 if (!path->skip_locking)
4097 path->locks[level] = 1;
Chris Masond97e63b2007-02-20 16:40:44 -05004098 if (!level)
4099 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004100
4101 btrfs_set_path_blocking(path);
Chris Mason925baed2008-06-25 16:01:30 -04004102 if (level == 1 && path->locks[1] && path->reada)
4103 reada_for_search(root, path, level, slot, 0);
Chris Masonca7a79a2008-05-12 12:59:19 -04004104 next = read_node_slot(root, next, 0);
Chris Mason5cd57b22008-06-25 16:01:30 -04004105 if (!path->skip_locking) {
4106 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
4107 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004108 btrfs_set_lock_blocking(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004109 }
Chris Masond97e63b2007-02-20 16:40:44 -05004110 }
Chris Mason925baed2008-06-25 16:01:30 -04004111done:
4112 unlock_up(path, 0, 1);
Chris Masond97e63b2007-02-20 16:40:44 -05004113 return 0;
4114}
Chris Mason0b86a832008-03-24 15:01:56 -04004115
Chris Mason3f157a22008-06-25 16:01:31 -04004116/*
4117 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4118 * searching until it gets past min_objectid or finds an item of 'type'
4119 *
4120 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4121 */
Chris Mason0b86a832008-03-24 15:01:56 -04004122int btrfs_previous_item(struct btrfs_root *root,
4123 struct btrfs_path *path, u64 min_objectid,
4124 int type)
4125{
4126 struct btrfs_key found_key;
4127 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004128 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004129 int ret;
4130
Chris Masond3977122009-01-05 21:25:51 -05004131 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004132 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004133 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004134 ret = btrfs_prev_leaf(root, path);
4135 if (ret != 0)
4136 return ret;
4137 } else {
4138 path->slots[0]--;
4139 }
4140 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004141 nritems = btrfs_header_nritems(leaf);
4142 if (nritems == 0)
4143 return 1;
4144 if (path->slots[0] == nritems)
4145 path->slots[0]--;
4146
Chris Mason0b86a832008-03-24 15:01:56 -04004147 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4148 if (found_key.type == type)
4149 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004150 if (found_key.objectid < min_objectid)
4151 break;
4152 if (found_key.objectid == min_objectid &&
4153 found_key.type < type)
4154 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004155 }
4156 return 1;
4157}